简体   繁体   English

在我的数组循环中找不到正确的最大值

[英]Can't find the right max Value in my Array loop

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. 但是由于某种原因,当我编写for循环以查找最大值时,它甚至在文本文件中都返回一个数字,而这……我不知道我做错了什么。 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 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 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... 最大值是: 51 <---我不知道这个数字是从哪里来的...

The 51 is coming from the line: 51来自该行:

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. 但是,这会将maxVal分配给您maxValmyArray[i] First of all you need to be assigning myArray[j] , not myArray[i] , and secondly you need to assign maxVal to the bigger value. 首先,您需要分配myArray[j] ,而不是myArray[i] ,其次,您需要将maxVal分配给更大的值。 As it is maxVal=myArray[0]; 因为它是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). 是您唯一一次为maxVal分配任何内容的maxVal ,这就是为什么它是51 (字符3的ASCII值,这是您读取的第一个字符)。 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[] . 我相信您希望myArray成为int[] Also a better way of doing this is instead of having two for loops and looping until EOF, loop while myFile >> myArray[i] : 还有一种更好的方法是不要有两个for循环并循环直到EOF,而在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. 因为myArray[j]是一个char(肯定不保存您感兴趣的整数),而maxVal是一个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). 这也是您看到51的原因-尝试将整数存储到char中时,您实际上仅从流中读取8位(这导致0到254之间的某个值,基本上是从8位块开始的值)。您的输入流)。

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. 以便能够将流中的完整整数读取到这210个char *插槽之一中。 Then, when comparing (and assigning to maxValue), you need to convert the textual int value to a numeric value, eg, using atoi(). 然后,在比较(并分配给maxValue)时,您需要将文本int值转换为数字值,例如,使用atoi()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM