السلام عليكم جميعا
اخواني اريد من شرح لهذا الكود بلغة سي بلس بلس
#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
-------------------------------