473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How do I change this?

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
10 1415
RedSon
5,000 Expert 4TB
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
wazzup
25
so, which line in function f1 ?
Mar 16 '07 #3
RedSon
5,000 Expert 4TB
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
315 Expert 100+
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
5,000 Expert 4TB
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
315 Expert 100+
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
wazzup
25
it works. Can you explain me how it works? Pleaseee!
Thanks much!
Mar 16 '07 #8
arne
315 Expert 100+
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
wazzup
25
yup. It's the test that how much understanding the code
Mar 16 '07 #10
arne
315 Expert 100+
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

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

Similar topics

1
by: Scott | last post by:
In the result of a SELECT statement, how can you change cell values from NULL to BLANK? The following does NOT do it: SET fieldname = ' ' WHERE fieldname IS NULL Also, for colums with a DATE...
10
by: TheKeith | last post by:
I don't know much about javascript, so take it easy on me. Is there a way to dynamically change a CSS layers dimensions on the fly. Here is what I'm doing. I have a bunch of thumbnails that when...
18
by: Jan Tuxen | last post by:
Jakob Nielsen in his most recent Alertbox (http://www.useit.com/alertbox/20040503.html) tells web authors to change the color of visited links. I agree to his purpose: Help users understand...
2
by: MLH | last post by:
What's the simplest way to allow a user of an A97 app to change password?
3
by: roger beniot | last post by:
I would like to figure out how to detect an IP address change for an XP/Win2K3 machine that is leasing an IP via DHCP (and do it in C#)... Is there any event that indicates an IP address...
4
by: active | last post by:
It appears to me that if I change the Control's client size the controls size does not change to agree with the new size. Does it work like the VB6 ScaleWidth and ScaleHeight? That is after I...
10
by: Hugh | last post by:
Hi there, An existing solution is used to create a new solution. It is easy to change the names of individual forms and modules. However, how to change the name of project folder? Thanks in...
2
by: Bob Alston | last post by:
I have an application with several forms. Most forms use radio boxes for Yes/No choices. On a few of these, once one option is selected, the user cannot change the selection without moving to...
1
by: eighthman11 | last post by:
Hi everyone: I created a custom toolbar called Password, so users can change their password. I used the Command "User and Group Accounts". I know the user can not change anything in the Tab...
1
by: monkey1001 | last post by:
my program is suppose to show my due change and i got it working but my change and coins are wrong how can i improve it thank you..(its supposed to be in java)
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.