Histogram of int array

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2009
Posts: 37
Reputation: yun is an unknown quantity at this point 
Solved Threads: 2
yun yun is offline Offline
Light Poster

Histogram of int array

 
0
  #1
May 22nd, 2009
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..
  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...
Last edited by Narue; May 22nd, 2009 at 10:40 am. Reason: added code tags
Attached Thumbnails
output.jpg  
Attached Files
File Type: cpp Histogram.cpp (1.8 KB, 4 views)
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 152
Reputation: amrith92 is on a distinguished road 
Solved Threads: 17
amrith92's Avatar
amrith92 amrith92 is offline Offline
Junior Poster

Re: Histogram of int array

 
0
  #2
May 22nd, 2009
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.
"C++ : Where friends have access to your private members."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,407
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1468
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Histogram of int array

 
0
  #3
May 22nd, 2009
Originally Posted by amrith92 View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 152
Reputation: amrith92 is on a distinguished road 
Solved Threads: 17
amrith92's Avatar
amrith92 amrith92 is offline Offline
Junior Poster

Re: Histogram of int array

 
0
  #4
May 22nd, 2009
Originally Posted by Ancient Dragon View Post
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.
"C++ : Where friends have access to your private members."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 37
Reputation: yun is an unknown quantity at this point 
Solved Threads: 2
yun yun is offline Offline
Light Poster

Re: Histogram of int array

 
0
  #5
May 22nd, 2009
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..
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,681
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 264
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Histogram of int array

 
1
  #6
May 22nd, 2009
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
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 152
Reputation: amrith92 is on a distinguished road 
Solved Threads: 17
amrith92's Avatar
amrith92 amrith92 is offline Offline
Junior Poster

Re: Histogram of int array

 
0
  #7
May 22nd, 2009
Originally Posted by Lerner View Post
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...
"C++ : Where friends have access to your private members."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,407
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1468
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Histogram of int array

 
0
  #8
May 22nd, 2009
Originally Posted by amrith92 View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,678
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Histogram of int array

 
1
  #9
May 22nd, 2009
Instead of the big if...else if block, how about

  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.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,678
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Histogram of int array

 
1
  #10
May 22nd, 2009
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.
  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.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC