الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

رجاء شرح الكود و تصحيح كود اخر

بدأه طموحي كبير في 15 مايو 2010 · 9 رد · 790 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم جميعا

اخواني اريد من شرح لهذا الكود بلغة سي بلس بلس

#include <iostream>
using std::cout;
using std::endl;

#include <iomanip>
using std::setw;

int main()
{
   const int arraySize = 10; // size of array a
   int data[ arraySize ] = { 34, 56, 4, 10, 77, 51, 93, 30, 5, 52 };
   int insert; // temporary variable to hold element to insert

   cout << "Unsorted array:\n";

   // output original array
   for ( int i = 0; i < arraySize; i++ )
      cout << setw( 4 ) << data[ i ];

   // insertion sort
   // loop over the elements of the array
   for ( int next = 1; next < arraySize; next++ )
   {
      insert = data[ next ]; // store the value in the current element

      int moveItem = next; // initialize location to place element

	  // search for the location in which to put the current element     
      while ( ( moveItem > 0 ) && ( data[ moveItem - 1 ] > insert ) )
      {
         // shift element one slot to the right
         data[ moveItem ] = data[ moveItem - 1 ];
         moveItem--;
      } // end while

      data[ moveItem ] = insert; // place inserted element into the array
   } // end for

   cout << "\nSorted array:\n";

   // output sorted array
   for ( int i = 0; i < arraySize; i++ )
      cout << setw( 4 ) << data[ i ];

   cout << endl;
   return 0; // indicates successful termination
} // end main

أيضا اريد منكم أن تصححوا لي هذا الكود

#include <iostream>
using namespace std ;
void rand(int x[])
{
  cout<<"Array which generated randomly :"<<endl;
  for(int i=0 ; i<10 ; ++i)
  {
  x= rand() % 10 ;
   cout<<x<<" ";
  }
  cout<<endl;
}void sort(int x[])
{
  int temp;
  for (int i=0 ; i<10 ; ++i)
  {
   for (int j=1 ; j<10 ; ++j)
    {
      if (x[j-1]>x[j])
      {
       temp = x[j-1];
       x[j-1]=x[j];
       x[j]=temp;
      }
    }
  }
  cout<<"Array's elements after sorted :"<<endl;
  for(int i=0 ; i<10 ; ++i)
  cout<<x<<" ";
}
int main()
{
int x[10];
 rand(x);
 sort(x);
return 0;
}

وهذا هو سؤاله ....

Exercise # 2 :

Write program that deal with array of integers that contains random values,

that's mean you should use rand() function that's generate array's elements. In

your program, you should sort the elements of randomly array. Then print your

array before and after sorting.

Note: sort the array using insertion sort algorithm that is explained in

chapter 7- section 8.

Output sample:-

Array which generated randomly :

-------------------------------

0 3 3 2 9 0 8 2 6 6

-------------------------------

Array's elements after sorted :

-------------------------------

0 0 2 2 3 3 6 6 8 9

-------------------------------

http://groups.google.com.sa/group/Cplusplusimamgroup

تفضلوا بالزيارة لقروبي :)

#2

وعليكم السلام ..

وش مشكلة الكود ؟ أشوفه صحيح :ph34r: .. ,

يمكن ناقصه أشياء بسيطة ,

#include <iostream>
#include<ctime>
using namespace std ;
void rand(int x[])
{
  srand(time(NULL));
  cout<<"Array which generated randomly :"<<endl;
  for(int i=0 ; i<10 ; ++i)
  {
  x= rand() % 10 ;
   cout<<x<<" ";
  }
  cout<<endl;
}void sort(int x[])
{
  int temp;
  for (int i=0 ; i<10 ; ++i)
  {
   for (int j=1 ; j<10 ; ++j)
    {
      if (x[j-1]>x[j])
      {
       temp = x[j-1];
       x[j-1]=x[j];
       x[j]=temp;
      }
    }
  }
  cout<<"Array's elements after sorted :"<<endl;
  for(int i=0 ; i<10 ; ++i)
  cout<<x<<" ";
}
int main()
{
int x[10];
 rand(x);
 sort(x);
system("pause");
}

بالنسبة للكود مال الـ insertion sort

راجعي هالموضوع إن شاء الله يفيدك .. ,

هذا القسم مخصص للأسئلة المجاب عليها :sleep: ,

ضعي سؤالك هنا في المرة القادمة :happy: ,

تم تعديل هذه المشاركة بواسطة إشراقــه فجــر في 15 مايو 2010 في 22:20

سبحآن الله وبحمده .. سبحآن الله العظيم .. ,

#3

الله يجزاك خير

ايه هذا السطور كيف اطلعها بس اسوي cout لا تحتاج loop ؟

وان شاء الله احط المره القادمه احط الموضوع بالمكان المناسب المعذره

ما لقيت الي ابيه بالموضوع

انا فاهمه اوله و اخره لكن ما فهمت هذي الجزئيه لو يكون الشرح بtrace لا هنتم و لكم دعوه من القلب

 for ( int next = 1; next < arraySize; next++ )
   {
      insert = data[ next ]; // store the value in the current element

      int moveItem = next; // initialize location to place element

          // search for the location in which to put the current element     
      while ( ( moveItem > 0 ) && ( data[ moveItem - 1 ] > insert ) )
      {
         // shift element one slot to the right
         data[ moveItem ] = data[ moveItem - 1 ];
         moveItem--;
      } // end while

      data[ moveItem ] = insert; // place inserted element into the array
   } // end for

تم تعديل هذه المشاركة بواسطة طموحي كبير في 15 مايو 2010 في 22:33

http://groups.google.com.sa/group/Cplusplusimamgroup

تفضلوا بالزيارة لقروبي :)

#5
for ( int next = 1; next < arraySize; next++ )

بالبداية , نسوي Loop من 1 إلى طول المصفوفة , نبدأ من العنصر الثاني في المصفوفة .. و نشوف اللي قبله ..

و في الـ loop الثانية , راح يكون الـ next =2 , يعني نبدأ من العنصر الثالث و نقارن مع العنصر الثاني و بعده العنصر الأول ..

      insert = data[ next ]; // store the value in the current element

نخزن ثاني عنصر في متغير , لأجل المقارنة . ,

int moveItem = next; // initialize location to place element

نحفظ الـ index في متغير , و لأجل المقارنة أيضًا ,

while ( ( moveItem > 0 ) && ( data[ moveItem - 1 ] > insert ) )

نقول .. طالما أن الـ moveItem أكبر من الصفر .. لأننا بنبدأ من العنصر الثاني و في الـ while بتنقص قيمته في كل مرة , نحتاج نشيك أن الـ index ما صار -1

و الشرط الثاني .. أن العنصر الأول أكبر من العنصر الثاني ..

إذا تحقق الشرطين .. ينتقل للي بعده ,

 {
         // shift element one slot to the right
         data[ moveItem ] = data[ moveItem - 1 ];
         moveItem--;
      } // end while

ضع العنصر الأول مكان العنصر الثاني ..

و بينقص الـ index بيصير 0 .. ما راح يدخل في الـ while بيطلع منها , وبينفذ الجملة اللي بعدها ..

      data[ moveItem ] = insert; // place inserted element into the array
   }

صحيح غيرنا قيمه العنصر الثاني و وضعنا فيه قيمة العنصر الأول ,

الحين عندنا العنصر الأول ماغيرنا قيمته ؟

عشان كذا حفظنا قيمة العنصر الثاني .. في بداية الـ loop , نضع في العنصر الأول قيمه الـ insert (اللي هو العنصر الثاني ) .. ,

إن شاء الله يكون واضح ,

إذا تبين تفهمين الكود , أرسمي المصفوفة , وقيمها في كل Loop و بيتضح لك ,

اقتباس
اشراقة الفجر خلاص مشكوره لقيته

:ph34r: , ما أنتبهت لردك .. فصل النت :mad: .. ,

بالتوفيق :happy:

تم تعديل هذه المشاركة بواسطة إشراقــه فجــر في 15 مايو 2010 في 23:33

سبحآن الله وبحمده .. سبحآن الله العظيم .. ,

#6

ياه الله يسعدك يالله كنت محتاجه شرح اكثرر الله يوفقك يارب

طيب والسؤال الأول ؟

http://groups.google.com.sa/group/Cplusplusimamgroup

تفضلوا بالزيارة لقروبي :)

#7
طموحي كبير كتب:

طيب والسؤال الأول ؟

اي سؤال تقصدين ؟!

سبحآن الله وبحمده .. سبحآن الله العظيم .. ,

#8

سوري اقصد السؤال الثاني

Output sample:-

Array which generated randomly :

-------------------------------

0 3 3 2 9 0 8 2 6 6

-------------------------------

Array's elements after sorted :

-------------------------------

0 0 2 2 3 3 6 6 8 9

-------------------------------

السطور يصير اكتب فقط cout ولا loop

http://groups.google.com.sa/group/Cplusplusimamgroup

تفضلوا بالزيارة لقروبي :)

#9

سويها بـ cout .. ,

هذا الكود بعد التعديل :

#include <iostream>
#include<ctime>
using namespace std ;
void rand(int x[])
{
  srand(time(NULL));
  cout<<"\nArray which generated randomly :\n\n-------------------------------\n\n";
  for(int i=0 ; i<10 ; ++i)
  {
  x= rand() % 10 ;
   cout<<x<<" ";
  }
  cout<<"\n\n-------------------------------\n\n";
}void sort(int x[])
{
  int temp;
  for (int i=0 ; i<10 ; ++i)
  {
   for (int j=1 ; j<10 ; ++j)
    {
      if (x[j-1]>x[j])
      {
       temp = x[j-1];
       x[j-1]=x[j];
       x[j]=temp;
      }
    }
  }
  cout<<"Array's elements after sorted :\n\n-------------------------------\n\n";
  for(int i=0 ; i<10 ; ++i)
  cout<<x<<" ";
  cout<<"\n\n-------------------------------\n\n";
}
int main()
{
int x[10];
 rand(x);
 sort(x);
system("pause");
}

بالتوفيق :happy: ,

1

سبحآن الله وبحمده .. سبحآن الله العظيم .. ,

مواضيع مشابهة