Help.. Can't figure out Min & Max
May 1, 2016 at 10:56am UTC
Hello,
I'm writing a code for a program that has different departures and land for an airport. I am trying to get the greatest and lowest. I wrote a function that supposedly is to store the greatest after cycling through the array. For example, I wrote a function to loop through the array and compare it with each indexes. For some reason, as it cycles through each index it does not store the max in my high variable. It appears that it is replacing one after the other and it is not comparing to see which index contain a high number than the other.
Please see my code below. Any help will be appreciated.
Thanks in advance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
#include <iostream>
#include <fstream>
using namespace std;
struct planeInfo
{
int planesLanded;
int planesDeparted;
int greatestNumOfPlanesLanded;
int leastNumOfPlanesLanded;
};
void highestLanded(planeInfo array[], int size);
void leastlanded (planeInfo array[],int size);
int main()
{
const int SIZE = 12;
planeInfo month[SIZE];
ifstream inFile;
inFile.open("flights.txt" );
for (int i = 0; i < SIZE; i++)
{
inFile >> month[i].planesLanded;
inFile >> month[i].planesDeparted;
inFile >> month[i].greatestNumOfPlanesLanded;
inFile >> month[i].leastNumOfPlanesLanded;
}
highestLanded(month,SIZE);
leastlanded(month, SIZE);
inFile.close();
system("pause" );
return 0;
}
void highestLanded(planeInfo arr[], int size)
{
int high;
int monthIndex = 0;
high = arr[0].greatestNumOfPlanesLanded;
for (int count = 1; count < 6; count++)
{
if (arr[count].greatestNumOfPlanesLanded < high);
high = arr[count].greatestNumOfPlanesLanded;
monthIndex = count + 1;
}
cout << high;
}
void leastlanded(planeInfo arr[], int size)
{
int least;
int monthIndex = 0;
least = arr[0].leastNumOfPlanesLanded;
for (int count = 1; count < size; count++)
{
if (arr[count].leastNumOfPlanesLanded < least);
{
least = arr[count].leastNumOfPlanesLanded;
monthIndex = count + 1;
}
}
}
May 1, 2016 at 11:04am UTC
1 2 3 4
if (arr[count].greatestNumOfPlanesLanded < high);
high = arr[count].greatestNumOfPlanesLanded;
monthIndex = count + 1;
See that semi-colon at the end of the first line? That makes the END of the if block. So this code is the same as this:
1 2 3 4 5 6 7
if (arr[count].greatestNumOfPlanesLanded < high)
{
}
high = arr[count].greatestNumOfPlanesLanded;
monthIndex = count + 1;
You can see that the if block is empty. Similarly on line 78.
Is line 60 meant to be part of the if block? In C++, indenting is meaningless. It's just a way for you to lay out the code to make it easy to read.
Put all your if block code inside braces
{ ... }
Line 57; you seem to be testing that the value is LESS than the current highest value. Shouldn't be be looking for values that are HIGHER?
May 1, 2016 at 7:11pm UTC
Hi Moschops,
I see where my mistake is at. Really appreciate your help! Thank you!
Topic archived. No new replies allowed.