943,929 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1808
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
May 22nd, 2009
0

Histogram of int array

Expand Post »
write a program that read in an array of integers-- ideally from a text file. and then displays a histogram of those numbers, divided into the ranges 0-9, 10-19 ,20-29, and so forth, up to the range of containing only the valye 100.

Please help me to write a efficient and better code..
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <iomanip>
  3. #include "genlib.h"
  4. #include "strutils.h"
  5. #include <fstream>
  6.  
  7. void Histogram(int*); //Function prototype
  8. void print(string*);
  9.  
  10. int main(){
  11. int arr[10] = {2,4,5,23,26,29,6,100,100,80}; //integer array inialization
  12. ofstream out("arr.txt"); //open file for write
  13. for (int i =0;i<10;i++){
  14. out << arr[i] << ' '; // store data in file
  15. }
  16.  
  17. for(int i=0;i<10;i++){
  18. arr[i]=0; //initialization array with zero, so can get values from file
  19. }
  20. out.close(); //close is must.
  21.  
  22. ifstream in("arr.txt"); //open file for read
  23. for(int i=0;;i++){
  24. in >> arr[i]; // reading data from file into memory
  25. if(in.fail()) break;
  26. }
  27.  
  28. Histogram(arr); //call Histogram Function
  29.  
  30. return 0;
  31. }
  32.  
  33. void Histogram(int *arr){
  34. string str[11]; // for storing number of "*"
  35. for(int i =0;i<10;i++){
  36. if(arr[i]>=0 && arr[i] <10){
  37. str[0]+="*";
  38. }else
  39. if(arr[i]>=10 && arr[i] <20){
  40. str[1]+="*";
  41. }else
  42. if(arr[i]>=20 && arr[i] <30){
  43. str[2]+="*";
  44. }else
  45. if(arr[i]>=30 && arr[i] <40){
  46. str[3]+="*";
  47. }else
  48. if(arr[i]>=40 && arr[i] <50){
  49. str[4]+="*";
  50. }else
  51. if(arr[i]>=50 && arr[i] <60){
  52. str[5]+="*";
  53. }else
  54. if(arr[i]>=60 && arr[i] <70){
  55. str[6]+="*";
  56. }else
  57. if(arr[i]>=70 && arr[i] <80){
  58. str[7]+="*";
  59. }else
  60. if(arr[i]>=80 && arr[i] <90){
  61. str[8]+="*";
  62. }else
  63. if(arr[i]>=90 && arr[i] <100){
  64. str[9]+="*";
  65. }else
  66. if(arr[i]==100){
  67. str[10]+="*";
  68. }
  69. }
  70.  
  71. print(str); // call print function for display formated data
  72. }
  73.  
  74. void print(string *str){
  75. int i,j =0; // Display data
  76. for (i=0;i<=100,j<11;i+=10,j++){
  77. cout << setw(8)<< IntegerToString(i) + " :" << str[j] << endl;
  78. }
  79. }
Output remain the same as in the image file...
Attached Thumbnails
Click image for larger version

Name:	output.jpg
Views:	140
Size:	3.5 KB
ID:	10205  
Attached Files
File Type: cpp Histogram.cpp (1.8 KB, 27 views)
Last edited by Narue; May 22nd, 2009 at 10:40 am. Reason: added code tags
Similar Threads
yun
Reputation Points: 10
Solved Threads: 2
Light Poster
yun is offline Offline
42 posts
since May 2009
May 22nd, 2009
0

Re: Histogram of int array

Are you sure that this program compiles? You seem to have used string in your code, but you haven't included <string.h> at all...!

Apart from that, could you tell us what IDE/compiler you are using... or does the code assume all objects to belong to the namespace std , because I don't see using namespace std; anywhere...??

Apart from that, you have two custom header files, which we do not have, so how are we going to compile your program, seeing as some functions are defined in those header files...??
Last edited by amrith92; May 22nd, 2009 at 10:54 am.
Reputation Points: 130
Solved Threads: 22
Junior Poster
amrith92 is offline Offline
187 posts
since Jul 2008
May 22nd, 2009
0

Re: Histogram of int array

Click to Expand / Collapse  Quote originally posted by amrith92 ...
Are you sure that this program compiles? You seem to have used string in your code, but you haven't included <string.h> at all...!
That's because std::string is not declared in <string.h> -- its in <string> header file. And some compiler include <string> with <fstream>
Last edited by Ancient Dragon; May 22nd, 2009 at 11:04 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
May 22nd, 2009
0

Re: Histogram of int array

That's because std::string is not declared in <string.h> -- its in <string> header file. And some compiler include <string> with <fstream>
I hadn't realized that... thanks! ... But it does work as std::string in my IDE (Dev C++), and it (I mean, including <string.h> ) does not produce any backward warnings at all...
Last edited by amrith92; May 22nd, 2009 at 11:13 am.
Reputation Points: 130
Solved Threads: 22
Junior Poster
amrith92 is offline Offline
187 posts
since Jul 2008
May 22nd, 2009
0

Re: Histogram of int array

Ye program Run well as u can see the output of the program. i m using microsoft visual studio 2005 and the header files u r talking about related to university meterial which include
IntegerToString(int) lot of other helpfull functionse. and yes ofcourse using namespace std; is very important but genlib.h have info about it..... thanks for the reply..
yun
Reputation Points: 10
Solved Threads: 2
Light Poster
yun is offline Offline
42 posts
since May 2009
May 22nd, 2009
1

Re: Histogram of int array

Another approach might be to display as you go. Here's some pseudocode to rough out how that might work:

1) declare variables needed---stream to read file and two int variables
2) use a while loop to read in numbers from file

body of the while loop will:
3) divide number read in from file by 10 using integer math
4) increase the number obtained in 3) by one
5) use another loop to display the number of *s indicated by the number obtained in 4)
6) start a new line
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
May 22nd, 2009
0

Re: Histogram of int array

Click to Expand / Collapse  Quote originally posted by Lerner ...
Another approach might be to display as you go. Here's some pseudocode to rough out how that might work:

1) declare variables needed---stream to read file and two int variables
2) use a while loop to read in numbers from file

body of the while loop will:
3) divide number read in from file by 10 using integer math
4) increase the number obtained in 3) by one
5) use another loop to display the number of *s indicated by the number obtained in 4)
6) start a new line
Yeah, this is a good algorithm to use for the same...
Reputation Points: 130
Solved Threads: 22
Junior Poster
amrith92 is offline Offline
187 posts
since Jul 2008
May 22nd, 2009
0

Re: Histogram of int array

Click to Expand / Collapse  Quote originally posted by amrith92 ...
and it (I mean, including <string.h> ) does not produce any backward warnings at all...
why should the compiler produce warnings? string.h defines functions such as strcmp(), strcat(), etc. which are related to character arrays, not std::string. Those functions and that header file were inherited from C language.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
May 22nd, 2009
1

Re: Histogram of int array

Instead of the big if...else if block, how about

C++ Syntax (Toggle Plain Text)
  1. for( int i = 0; i < 10; i++ )
  2. {
  3. index = arr[i] / 10;
  4. str[index] += "*";
  5. }
Last edited by vmanes; May 22nd, 2009 at 4:08 pm.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
May 22nd, 2009
1

Re: Histogram of int array

And for your future reference, when you have an if...else if block that is separating values into range groupings, you don't need to test both upper and lower bounds in the succeeding conditions.
C++ Syntax (Toggle Plain Text)
  1. if( arr[i] >= 0 && arr[i] < 10 )
  2. {
  3. str[0] += "*";
  4. }
  5. else if( arr[i] < 20 )
  6. {
  7. str[1] += "*";
  8. }
  9. else if( arr[i] < 30 )
  10. {
  11. //and so on
The fact that you arrive at the test for < 20 means the value must be >= 10 to have not been caught by the first test, and so on.

And do be afraid to put a space between operators and operands - it makes the code a bit more readable. Spaces are cheap.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Which stl container...
Next Thread in C++ Forum Timeline: error C2064: term does not evaluate to a function taking 1 arguments line 33





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC