| | |
Histogram of int array
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: May 2009
Posts: 37
Reputation:
Solved Threads: 2
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..
Output remain the same as in the image file...
Please help me to write a efficient and better code..
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> #include "genlib.h" #include "strutils.h" #include <fstream> void Histogram(int*); //Function prototype void print(string*); int main(){ int arr[10] = {2,4,5,23,26,29,6,100,100,80}; //integer array inialization ofstream out("arr.txt"); //open file for write for (int i =0;i<10;i++){ out << arr[i] << ' '; // store data in file } for(int i=0;i<10;i++){ arr[i]=0; //initialization array with zero, so can get values from file } out.close(); //close is must. ifstream in("arr.txt"); //open file for read for(int i=0;;i++){ in >> arr[i]; // reading data from file into memory if(in.fail()) break; } Histogram(arr); //call Histogram Function return 0; } void Histogram(int *arr){ string str[11]; // for storing number of "*" for(int i =0;i<10;i++){ if(arr[i]>=0 && arr[i] <10){ str[0]+="*"; }else if(arr[i]>=10 && arr[i] <20){ str[1]+="*"; }else if(arr[i]>=20 && arr[i] <30){ str[2]+="*"; }else if(arr[i]>=30 && arr[i] <40){ str[3]+="*"; }else if(arr[i]>=40 && arr[i] <50){ str[4]+="*"; }else if(arr[i]>=50 && arr[i] <60){ str[5]+="*"; }else if(arr[i]>=60 && arr[i] <70){ str[6]+="*"; }else if(arr[i]>=70 && arr[i] <80){ str[7]+="*"; }else if(arr[i]>=80 && arr[i] <90){ str[8]+="*"; }else if(arr[i]>=90 && arr[i] <100){ str[9]+="*"; }else if(arr[i]==100){ str[10]+="*"; } } print(str); // call print function for display formated data } void print(string *str){ int i,j =0; // Display data for (i=0;i<=100,j<11;i+=10,j++){ cout << setw(8)<< IntegerToString(i) + " :" << str[j] << endl; } }
Last edited by Narue; May 22nd, 2009 at 10:40 am. Reason: added code tags
Are you sure that this program compiles? You seem to have used
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
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...??
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."
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."
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.
•
•
•
•
That's because std::string is not declared in <string.h> -- its in <string> header file. And some compiler include <string> with <fstream>
... 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."
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."
•
•
Join Date: May 2009
Posts: 37
Reputation:
Solved Threads: 2
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..
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..
•
•
Join Date: Jul 2005
Posts: 1,681
Reputation:
Solved Threads: 264
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
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
•
•
•
•
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
"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."
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."
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.
Instead of the big if...else if block, how about
C++ Syntax (Toggle Plain Text)
for( int i = 0; i < 10; i++ ) { index = arr[i] / 10; str[index] += "*"; }
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
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.
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.
C++ Syntax (Toggle Plain Text)
if( arr[i] >= 0 && arr[i] < 10 ) { str[0] += "*"; } else if( arr[i] < 20 ) { str[1] += "*"; } else if( arr[i] < 30 ) { //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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
![]() |
Similar Threads
- Length of an int array (C++)
- int *array[10] to dynamic array equivalent? (C++)
- "invalid types `double[int]' for array subscript" - DLL problem (C++)
- Converting a String to an int array (C++)
- Problems from string to int array (C)
- fstream to char and int array (C++)
Other Threads in the C++ Forum
- Previous Thread: Which stl container...
- Next Thread: error C2064: term does not evaluate to a function taking 1 arguments line 33
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






