Printing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  const int size = 3;
  int array[size];
  int n, i, j;

  /* Fill the array */
 
 cout<<"Enter "<<size<<" random numbers"<<endl;
 cout.flush();
  for ( n = 0; n < size; n++ ) {
    cin >> array[n];
    if( !cin.good() )
      break;
  }

//insertion 
  for ( i = 1; i < n; i++ ) {
    int save = array[i];

    for ( j = i - 1; j >= 0 && array[j] > save; j-- )
      array[j + 1] = array[j];

    array[j + 1] = save;
  }

  /* Print the sorted array */
  for ( i = 0; i < n; i++ ){	
   cout<<array[i];
   cout<<endl;
  }


right now you can see that i am using insertion and i am printing the array in ascending order, how to print to descending order? i tried cout<<array[size - i]l but that give me a strange error.
For i = 0, size - i evaluates to size, which is out of bounds.
what do u mean? so it's not possible?

do u mean by
for(i=0; size-i;i++)?
I mean it's the wrong expression, off by one. Here's how to do it:

1
2
3
for(i = 0; i < size; ++i) {
    cout << array[size - 1 - i] << endl;
}
Topic archived. No new replies allowed.