Sunday, August 2, 2009

C++ problem involving reading a numbers from file, putting into an array, then processing?

Alright, so the problem goes like this:


Write a program that asks the user for a file name. Assume the file contains a series of numbers, each written on a separate line. The program should read the contents of the file into an array, and then displays the following data:


-The lowest number in the array.


-The highest number in the array.


-The total numbers in the array.


-The average of the numbers.

C++ problem involving reading a numbers from file, putting into an array, then processing?
[Amanda H, I just gave you a thumb up. I hate people who don't appreciate the time and effort you are spending here to help others. Thank you!!]








Here are some of your problems:





1) average must be a double NOT an integer because it could be something like 12.75





2) Always, always, always, initialize your variables once you declare them like this:





int count = 0, highest = 0, lowest = 0, total = 0;


double average = 0.0;





3) To make your program better you can use a single for loop (the one that reads from the file) to find the total, highest, and lowest. Here is how you do this (you don't need to do it, but it will make your program faster and more efficient):





for (count = 0; count %26lt; SIZE; count++)


{


inputFile %26gt;%26gt; numbers[count];


total+= numbers[count];





if(count ==0)


highest = lowest = numbers[count];


else


{


if(numbers[count] %26gt; highest)


highest = numbers[count];


if(numbers[count] %26lt; lowest)


lowest = numbers[count];


}


}





4) Now to calculate the average do the following:


average = (double) total / (double) SIZE;





5) Don't forget to include the following:





#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;


#include %26lt;string%26gt;





6) To get the file name from the user as input do the following:





string fileName = " ";





cout %26lt;%26lt; "Enter the name of the file: ";


cin %26gt;%26gt; fileName;





// To open the file, use the following line of code:





inputFile.open(fileName.c_str());





7) You said your program won't work if you increase size to 50 but I don't see why that would happen. If you just change SIZE to 50 your program should work UNLESS the file contains LESS than 50 numbers. Make sure your data file contains at least what you set SIZE to.





// That's all I have. I hope this helps you. Let me know if you


// need anymore help. Good luck!!
Reply:The keys to this problem are:





ifstream() and array loops.





Here is some code to help you out. Note that you don't really need the array, but if this program is built on, the array will be there.





EDIT: Thanks whoever thumbs downed me. ******.So much for trying to help.





Note that there is no limit on file size since the array is dynamically allocated.





#include%26lt;iostream.h%26gt;


#include%26lt;fstream.h%26gt;





int main(void){


char filename[256];


int *numarray;


ifstream file;


int count;


int value;


int total=0;


int high=0;


int low=0;


cout%26lt;%26lt;"Enter Filename-%26gt; ";


cin%26gt;%26gt;filename;


file.open(filename,ios::in);


file.seekg (0, ios::end);


count = file.tellg();


numarray=new int[count];


file.seekg (0, ios::beg);


count=0;


while(file.good()){


file%26gt;%26gt;numarray[count];


if(count==0) {high=numarray[count]; low=numarray[count]; }


total=total + numarray[count];


if(numarray[count] %26gt; high)high = numarray[count];


if(numarray[count] %26lt; low %26amp;%26amp; numarray[count] !='\0') low=numarray[count];


count++;


}


cout%26lt;%26lt; endl %26lt;%26lt;"Low: "%26lt;%26lt; low %26lt;%26lt;endl;


cout%26lt;%26lt;"High: "%26lt;%26lt; high %26lt;%26lt;endl;


cout%26lt;%26lt;"Total Numbers: "%26lt;%26lt;count-1%26lt;%26lt;endl;


cout%26lt;%26lt;"Average: "%26lt;%26lt;total/(count-1)%26lt;%26lt;endl;


}





If you need to use the array aspart of the project, take everything in the while loop out except the file%26gt;%26gt; and the count++ lines and stick them into a for loop as such:





for (x=0;x%26lt;count;x++){


if(x==0) {high=numarray[x]; low=numarray[x]; }


total=total + numarray[x];


if(numarray[x] %26gt; high)high = numarray[x];


if(numarray[x] %26lt; low %26amp;%26amp; numarray[x] !='\0') low=numarray[x];


}
Reply:I won't do it for you, but here's some clues...





Get the filename. Open the file using fopen (and work with it using fread and fclose - look them up).





Technically you don't even need an array - you can set a variable for the lowest number (set it high, and if it's lower than the high variable, set the high variable to it), and the opposite to find the highest (set it to zero, unless you're using negatives, and if it's greater, set the lowest to it), count up the numbers (for total numbers) and add up the numbers so you can give the average at the end (total / #numbers).





If you read them in as strings you may have to convert them with atoi function (ascii to integer).





Good luck!


No comments:

Post a Comment