By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,881 Online
Bytes IT Community
+ Ask a Question
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
  1. 13  void f1(int a[]) {
  2. 14       const int m = 100;
  3. 15       int b[m];
  4. 16       for (int j = 0; j < m; ++j) b[j] = 0;
  5. 17       for (int i = 0; i < 10; ++i) ++b[a[i]];
  6. 18       for (int i = 0, j = 0; j < m; ++j)
  7. 19               for (int k = b[j]; k > 0; --k) a[i++] = j;
  8. 20       return;
  9. 21  }
  10. 22  int main (int argc, char **argv)
  11. 23  {
  12. 24      int a[10];
  13. 25      char reply;
  14. 26      for (int i = 0; i < 10; i++) a[i] = 100 - i;
  15. 27      f1(a);
  16. 28      for (int i = 0; i < 10; i++) cout << i << ". " << a[i] << endl;
  17. 29      cout << "Press q (or any other key) followed by 'Enter' to quit: ";
  18. 30      cin >> reply;
  19. 31          return 0;
  20. 32  }
  21.  
  22. OUTPUT:
  23.  
  24. 0. 91
  25. 1. 92
  26. 2. 93
  27. 3. 94
  28. 4. 95
  29. 5. 96
  30. 6. 97
  31. 7. 98
  32. 8. 99
  33. 9. 91
  34. Press q (or any other key) followed by 'Enter' to quit:
  35.  
  36.  
Mar 16 '07 #1
Share this Question
Share on Google+
10 Replies


RedSon
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

RedSon
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

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

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

arne
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

arne
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

arne
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

Post your reply

Sign in to post your reply or Sign up for a free account.