So my professor gave us this project to read a text file and find the max value, min value, sum. But for some reason when I write the for loop to find the max value it returns a number thats not even in the text file... and I don't know what i did wrong. I'll attach my code and also the output. Thank you
int main () {
ifstream myFile;
char myArray[210];
int i;
int maxVal;
int j;
int minValue;
double myAverage;
myFile.open("Lab #5A Data File.Txt", ios::in | ios::out);
if (myFile.is_open()) {
cout << "The file is open." << endl;
myFile >> noskipws;
while (!myFile.eof()){
for (i=0; i<210; ++i) {
myFile >> myArray[i];
cout << myArray[i];
}
myFile >>myArray[i];
}
maxVal=myArray[0];
for (j=0; j< 210; j++)
if (myArray[j] > maxVal){
maxVal=myArray[j];
}
What i get when I run the code :
The file is open.
346 130 982 90 656 117 595 415 948 126 4 558 571 87 42 360 412 721 463 47 119 441 190 985 214 509 2 571 77 81 681 651 995 93 74 310 9 995 561 92 14 288 466 664 892 8 766 34 639 151 64 98 813 67 834 369
The max value is: 51 <--- I have no idea where this number came from...
The 51
is coming from the line:
maxVal=myArray[0];
In your loop to try to find the biggest element you have:
for (j=0; j< 210; j++)
if (myArray[j] > maxVal){
myArray[i]=maxVal;
}
}
However this will assign maxVal
to myArray[i]
which is not what you want. First of all you need to be assigning myArray[j]
, not myArray[i]
, and secondly you need to assign maxVal
to the bigger value. As it is maxVal=myArray[0];
is the only time you assign anything to maxVal
, which is why it is 51
(The ASCII value of the character 3
, which is the first character you read). You need to do something along the lines of:
if (myArray[j] > maxVal){
maxVal = myArray[j];
}
I believe you wanted myArray
to be an int[]
. Also a better way of doing this is instead of having two for loops and looping until EOF, loop while myFile >> myArray[i]
:
int myArray[210];
int i = 0;
//...
while (myFile >> myArray[i]) {
cout << myArray[i] << " ";
if (myArray[i] > maxVal) {
maxVal = myArray[i];
}
i++;
}
Which for the input file:
346 130 982 90 656 117 595
415 948 126 4 558 571 87 42
360 412 721 463 47 119 441
190 985 214 509 2 571 77 81
681 651 995 93 74 310 9 995
561 92 14 288 466 664 892 8
766 34 639 151 64 98 813 67 834 369
Returns:
995
To achieve what you want, you cannot do a comparison like this
if (myArray[j] > maxVal){
because myArray[j]
is a char (definitely not holding the integers you are interested in) and maxVal is an int. This is also the reason you see a 51 - when you try to store an integer into your char, you essentially only read 8 bits from the stream (which results in some value between 0 and 254 which is basically just some 8-bit block from your input stream).
You definitely want something like
char myArray[32][210];
to be able to read your full integers from the stream into one of these 210 char* slots. Then, when comparing (and assigning to maxValue), you need to convert the textual int value to a numeric value, eg, using atoi().
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.