Problems with arrays

I NEED HELP!!!! I have been trying to fix this code all day. I am new to coding.

There are things that are broken are the max and min. They do not pick the correct max and min or correct months. I cannot figure out how to get the average. I cannot get the array to add up and I do not know how to call that function to get average flights and delays.

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  #include <iostream>
using namespace std;

int findMax(double a[], int months);
int findMin(double a[], int months);
void findPercent(double flights[],double delays[],double percentDelay[]);
void findAverage(double a[],int months);
void getData(string & airport, string & airline, double flights[], double delays[]);


const int months = 3;

int main(){
    
    string airline, airport;
    double flights[months];
    double delays[months];
    double percentDelay[months];
    double a[months];
    double avgflights, avgdelays;
    int minindex, mexindex, leastflightsindex, leastdelaysindex, maxindex;
    
    
    string m[months] = { "January","February", "March"};
    
    getData(airport, airline, flights, delays);
    
    
    
    //functions
    findAverage(a, months);
    findPercent(flights,delays,percentDelay);
    
    minindex=findMin(percentDelay,months);
    mexindex=findMax(percentDelay,months);

    maxindex=findMax(a,months);
    leastflightsindex=findMin(a, months);
    leastdelaysindex=findMin(a, months);

    
    
    cout << "The best month to fly is " << m[minindex] << " with " << percentDelay[minindex] << "% delays" << endl;
    cout << "The worst month to fly is " << m[mexindex] << " with " << percentDelay[mexindex] << "% delays" << endl;


//The two above work all the rest do not
//Max flights
    cout << "Most busy month is " << m[maxindex] << " with " << flights[maxindex] << " flights" << endl;
    
    
//least flights and least delays
    cout << "Least busy month is " << m[leastflightsindex] << " with " << flights[leastflightsindex] << " flights" << endl;
    cout << "Month with fewest delays is " << m[leastdelaysindex] << " with " <<delays[leastdelaysindex] << " delays" << endl;


    //I do not know what to call to get the average
    cout << "The average number of flights for " << months << " Months is " <<  << " with an " << endl << "average of "  <<  << " delays" << endl;
    
}    
     
     




     
void getData(string &airport, string &airline, double flights[], double delays[])
{
    
    cout << "Enter the airline: ";
    cin >> airline;
    cout << "Enter the airport: ";
    cin >> airport;
    cout << "Enter the flights and delays:" << endl;
    
    
    for (int i = 0; i<months; i++){
    cin >> flights[i] >> delays[i];
    }
    cout << airline << " at " << airport << ":" << endl;
} 

int findMin(double a[], int months)
{
    
    int index=0;
    for (int i =1; i<months; i++){
        if(a[i]<a[index]){
            index=i;
        }
        
    }
        return index;
}

int findMax(double a[], int months)
{
    
    int index=0;
    for (int i = 1; i<months; i++){
        if(a[i]>a[index]){
            index=i;
            
        }
        
    }

        return index;
}
     
void findPercent(double flights[], double delays[], double percentDelay[])
{ 
    for(int i=0; i<months; i++){
        percentDelay[i]=(delays[i]/flights[i])*100;
    }
    
}
//this is not working
void findAverage(double a[], int months){

    double avg =0;
    for(int i = 0; i<3; i++)
    {
       avg += a[i];
       
    }
    
    
    
}   
Last edited on
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
int findMin(double a[], int months)
{
	int index = 0;
	for (int i = 1; i<months; i++) {
		if (a[i]<a[index]) 
		{
			index = i;
		}
	}
	return index;
}

int findMax(double a[], int months)
{
	int index = 0;
	for (int i = 1; i < months; i++)
	{
		if (a[i] > a[index])
		{
			index = i;
		}
	}
	return index;
}

double findAverage(double a[], int months) 
{
	double avg = 0;
	for (int i = 0; i < months; i++)
	{
		avg += a[i];
	}
	avg /= months; //Divide total sum by # of months
	return avg;
}

Variable names like 'm' and 'a' are not good. I can't tell from their names what they're supposed to be representing. Whatever array a is, you just declare it and use it. You never actually initialize any of the data inside of it.
Ok I tried this:

double flights[months];
double delays[months];
double percentDelay[];
double array[months];
int avgindex;
avgindex=findAverage(array, months);
cout << "The average number of flights for " << months << " Months is " << flights[avgindex] << " with an " << endl << "average of " << delays[avgindex] << " delays" << endl;

double findAverage(double array[], int months){

double avg = 0;
for(int i = 0; i<months; i++)
{
avg += array[i];

}
avg/=months;
return avg;
}
But it only prints out the first user input of the array


Also, I cannot figure out why the max and min functions are not working. It seems like it is not comparing correctly.
Last edited on
Topic archived. No new replies allowed.