446,389 Members | 1,881 Online
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

# How do I change this?

 P: 25 How do I change something in function f1 to displays the values stored in the array a in ascending order Expand|Select|Wrap|Line Numbers 13  void f1(int a[]) { 14       const int m = 100; 15       int b[m]; 16       for (int j = 0; j < m; ++j) b[j] = 0; 17       for (int i = 0; i < 10; ++i) ++b[a[i]]; 18       for (int i = 0, j = 0; j < m; ++j) 19               for (int k = b[j]; k > 0; --k) a[i++] = j; 20       return; 21  } 22  int main (int argc, char **argv) 23  { 24      int a[10]; 25      char reply; 26      for (int i = 0; i < 10; i++) a[i] = 100 - i; 27      f1(a); 28      for (int i = 0; i < 10; i++) cout << i << ". " << a[i] << endl; 29      cout << "Press q (or any other key) followed by 'Enter' to quit: "; 30      cin >> reply; 31          return 0; 32  }   OUTPUT:   0. 91 1. 92 2. 93 3. 94 4. 95 5. 96 6. 97 7. 98 8. 99 9. 91 Press q (or any other key) followed by 'Enter' to quit:     Mar 16 '07 #1
10 Replies

 Expert 5K+ P: 5,000 Easy, you can sort the array, and display it from 0 to n-1 where n is the size of the array. Mar 16 '07 #2

 P: 25 so, which line in function f1 ? Mar 16 '07 #3

 Expert 5K+ P: 5,000 I don't know, it doesn't look like your f1 sorts anything. I can't even tell what your function does. By the way naming your function f1 is probably the worst possible thing you could do. Mar 16 '07 #4

 Expert 100+ P: 315 so, which line in function f1 ? Change line 20 to: Expand|Select|Wrap|Line Numbers     sort(a, a+10); return;   Mar 16 '07 #5

 Expert 5K+ P: 5,000 Change line 20 to: Expand|Select|Wrap|Line Numbers     sort(a, a+10); return;   You could do that but you better make sure that sort actually does something. Or you could write sort yourself. Mar 16 '07 #6

 Expert 100+ P: 315 You could do that but you better make sure that sort actually does something. Or you could write sort yourself. sort will almost certainly do something, unless STL functions are empty nowadays :-) Mar 16 '07 #7

 P: 25 it works. Can you explain me how it works? Pleaseee! Thanks much! Mar 16 '07 #8

 Expert 100+ P: 315 it works. Can you explain me how it works? Pleaseee! Thanks much! Sure. 'sort' is part of the algorithms contained in the STL (Standard Template Library). The STL provides standard containers like vectors, sets, lists and the like as well as the most frequent algorithms for them, such as find, max, merge ... or sort. I thought that the original post was more meant as a brain teaser than the attempt to implement some kind of sorting, no? Mar 16 '07 #9

 P: 25 yup. It's the test that how much understanding the code Mar 16 '07 #10

 Expert 100+ P: 315 yup. It's the test that how much understanding the code Well, I did not understand the code. I just tried to get the array sorted by changing only one line in the code, so ... the changed code will of course also work if you delete the lines 14-19 (including). Mar 16 '07 #11