Function to find average and drop lowest and highest

Had this up and then thought I had it, but I definently do not. This is my first array ever so please do not be vague with answers. The error I am getting is line 19. Invalid types 'double[int]' for array subscript.


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
#include <iostream>
#include <iomanip>
#include <cmath>



using namespace std;


//decare function
void calcAvgScore(int i, double score, double total, double avg, double minScore, double maxScore, double tempScore, string name)
{
    cout << "Name: "<< name << endl;
    cout << "Total: " << total << endl;
    cout << "Average:" << avg << endl;

     for (int i= 0; i <= 5; i++)
     {
         tempScore = score[0];
         if (tempScore < minScore)
         minScore = tempScore;
         if (tempScore > maxScore)
         maxScore = tempScore;
     }

}




int main()
{
    //intializize local variables
    int i;
    int quit;
    string name;
    double score[5];
    double total = 0.0;
    double maxScore = 0.0;
    double minScore = 0.0;
    double avg = 0.0;
    double tempScore = 0.0;

//while loop
while (quit != 2)
{
    cout << "Enter first name: " << endl;
    cin >> name;

    for (int i = 0; i < 5; i++)
    {
    cout << "Enter judge" << i + 1 << " score: " << endl;
    cin >> score[i];
    if (score[i] < 1 || score[i] > 10)
    {
        cout << "Please, re-enter score (1 - 10):" << endl;
        cin >> score[i];
    }
    total += score[i];
    }

     avg = total / 3;

     maxScore = score[0];
     minScore = score[0];

     //function called
     calcAvgScore(int i, double score, double total, double avg, double minScore, double maxScore, double tempScore, string name );

     cout << "Do you wish to continue? " << endl;
     cout << "Enter 1 to continue or 2 to end: " << endl;
     cin >> quit;
     if (quit == 1)
     {
         cout << "Thank You" << endl;
     }
}//end while loop

}

1. Missing #include<string>
2. void calcAvgScore(int i, double score, double total, double avg, double minScore, double maxScore, double tempScore, string name)

should be:

void calcAvgScore(int i, double score[], double total, double avg, double minScore, double maxScore, double tempScore, string name)

3. On line 46, you are attempting to use quit , but it is uninitialized.
- Right before while (quit !=2) , you could ask the user to make a choice to enter the name or quit. Like
1
2
 cout << "Enter 1 to entry the name or 2 to end: " << endl;
          cin >> quit;


4. On line 69, you are attempting to use i , but it is uninitialized.
On the function of the calcAvgScore, What is the purpose of the first parameter ā€œiā€?

5. On line 68, when calling your function, you cannot use the variable types. It should be something like this:
calcAvgScore(i,score, total, avg, minScore, maxScore, tempScore, name);

6. It seems that you want to find the highest and lowest score. To give you an idea,
I would make separate functions, findHighest, findLowest, calcAvgScore (You got it). To give you an idea of what I mean:
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
#include <iostream>
#include <cctype>

//Function Prototypes.
void inputNumOfPancakes(int[], int);
void findHighest(int[], int, int, int);
void findLowest(int[], int, int, int);

int main() 
{

    //Declare the variables.
    const int SIZE{ 10 };
    int amount[SIZE] = { 0 };
    int pos{ 0 }, highest{ 0 }, lowest{ 0 };
    char tryAgain{ ' ' };

    do
    {
	   // Call the function for the user to input the values.
	   inputNumOfPancakes(amount, SIZE);

	   // Call the function to find the highest value.
	   highest = amount[0]; // Initialize the variable with the first array element.
	   findHighest(amount, SIZE, pos, highest);

	   // Call the function to find the lowest value.
	   lowest = amount[0]; // Initialize the variables with the first array element.
	   findLowest(amount, SIZE, pos, lowest);

	   std::cout << "Do you want to input the numbers again? >>>";
	   std::cin >> tryAgain;

    } while (toupper(tryAgain) == 'Y');

    return 0;
}

void inputNumOfPancakes(int amount[], int size)
{
    std::cout << "Please input 10 peoples pancakes consumed" << std::endl;
    for (int count = 0; count < size; count++)
    {
	   std::cout << "Person number " << count + 1 << " >>>";
	   std::cin >> amount[count];
    }
}

void findHighest(int amount[], int size, int pos, int highest)
{
    for (int count = 1; count < size; count++)
    {
	   if (amount[count] > highest)
	   {
		  highest = amount[count];
		  pos = count;
	   }
    }

    std::cout << "The person who ate the most is person number " << pos + 1 << " with " <<
	   highest << " pancakes" << std::endl;
}

void findLowest(int amount[], int size, int pos, int lowest)
{
    for (int count = 1; count < size; count++)
    {
	   if (amount[count] < lowest)
	   {
		  lowest = amount[count]; 
		  pos = count;
	   }
    }

    std::cout << "The person who ate the least is person number " << pos + 1 << " with " <<
	   lowest << " pancakes" << std::endl;
}
Last edited on
Thanks alot for all your time. I have watched several videos and looked at examples and could find nothing remotely close I'll post the result when i am complete now :)
Well, I think I am close but have some bugs can somebody give me some input on this ? Right now I have error line 20 error: invalid types 'double[int]' for array subscript. I have no idea what that means????

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
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>



using namespace std;


//decarAlcAvgScore (int i, double score, double total, double avg, double findLowest, double findHighest, double tempScore, string name)
{
    cout << endl;
    cout << "*******************************************" << endl;
    cout << "Name: "<< setprecision(2) << fixed <<  name << endl;
    cout << "Total: " << setprecision(2) << fixed << total << endl;
    cout << "Average:" << setprecision(2) << fixed << avg << endl;

    findLowest = score[0];
    findHighest = score[0];

    for (i = 1; i <= 4; i++)
    {
        tempScore = score[i];
        if (tempScore < findLowest)
            FindLowest = tempScore;
        if (tempScore > findHighest)
            findHighest - tempScore;
    }

}



int main()
{
    //intializize local variables
    int i;
    double quit;
    string name;
    int score[5];
    double total = 0.0;
    double findHighest = 0.0;
    double findLowest = 0.0;
    double tempScore = 0.0;

//while loop
while (quit != 2)
{
    cout << "Enter first name: " << endl;
    cin >> name;

    for (int i = 0; i < 5; i++)
    {
    cout << "Enter judge" << i + 1 << " score: " << endl;
    cin >> score[i];
    if (score[i] < 1 || score[i] > 10)
    {
        cout << "Please, re-enter score (1 - 10):" << endl;
        cin >> score[i];
    }
    total += score[i];
    }

    minScore = score[0];
    maxScore = score[0];

    for (i = 1; i <= 4; i++)
    {
        tempScore = score[i];
        if (tempScore < minScore)
            minScore = tempScore;
        if (tempScore > maxScore)
            maxScore - tempScore;
    }


    double avg = 0.0;


    avg = total / 5;



     //function called
     calcAvgScore (total,avg,name);

     double findLowest(int i, double score, double total, double avg, double findLowest, double findHighest, double tempScore, string name);

     cout << endl;
     cout << "Do you wish to continue? " << endl;
     cout << "Enter 1 to continue or 2 to end: " << endl;
     cin >> quit;
     if (quit == 1)
     {
         cout << "Thank You" << endl;
     }




}//end while loop


}

1) Why have you commented out line 11?

2) Look at the way you've defined score on line 11. It's a double. However, you've written the code for that function as if score is an array. So you need to change the definition of that argument to be an array.

3) What's happening at line 88? What you've written is a function prototype, for a function that you haven't defined anywhere.

4) I strongly recommend you get into the habit of using consistent indentation for your code. That will make it much easier for you (and anyone else reading your code) to see at a glance the logic and control flow of your code. In particular, your entire while loop (47 - 102) ought to be indented one more time.
Last edited on
I have never seen the formula for finding lowest and highest nor have I ever used arrays, I try to do these on my own and I can't find anything to work this is "where I am" EXAMPLE PLEASE!
Your algorithms look OK to me, except that you have typos on lines 26 and 28.

Your compiler errors should make it pretty clear what's wrong with line 26.

The one on line 28 is pretty obvious when you look at it. Can you see what it is?
Last edited on
I was getting error on 19 -20 it was type double[int] not able I have redone it several ways since then though do I need to declare score[i] another way?
MikeyBoy wrote:
2) Look at the way you've defined score on line 11. It's a double. However, you've written the code for that function as if score is an array. So you need to change the definition of that argument to be an array.
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
#include <iostream>
#include <string>

int get_score( int judge_number )
{
    std::cout << "Enter judge #" << judge_number << " score (1 - 10): " ;

    int score ;
    if( std::cin >> score && score >= 0 && score <= 10 ) return score ;

    // handle input error
    std::cout << "Please, re-enter score (1 - 10)\n" ;
    std::cin.clear() ; // clear possible failed state (non-numeric input)
    std::cin.ignore( 1000, '\n' ) ; // extract and discard the invalid input
    return get_score(judge_number) ; // try again
}

int main()
{
    const int N = 5 ;

    std::string name ;
    std::cout << "Enter first name: " ;
    std::cin >> name ;

    int score[N] ;
    for( int i = 0 ; i < N ; ++i ) score[i] = get_score(i+1) ;

    int min_score = score[0] ;
    int max_score = score[0] ;
    int total_score = score[0] ;

    for( int i = 1 ; i < N ; ++i )
    {
        total_score += score[i] ;
        if( score[i] < min_score ) min_score = score[i] ;
        if( score[i] > max_score ) max_score = score[i] ;
    }

    // we assume that if two scores are tied for the lowest score, only one of them is dropped
    // and likewise for the highest score
    const int total_sans_lowest_and_highest = total_score - min_score - max_score ;

    // avoid integer division
    const double average_after_dropping_lowest_and_highest = double(total_sans_lowest_and_highest) / (N-2) ;

    // ...
}
Ok, took me a minute to get back I see you changed i/N to a const int. and maintained i as the counter. Is that where I errered using my counter as the int? I am still not done I have to remove the highest as well but this should help me do that on my own. Thank
you
Prolly gonna seem stupid but I am looking at what you sent in the compiler and I can't make sense of the i/N or what the logic did because there is no output, I just don't see it :(
> there is no output, I just don't see it :(

If this is about the code snippet that I'd posted, there is no output because the output statements were omitted.

Here's the same code, with some output statements added:

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
#include <iostream>
#include <string>

int get_score( int judge_number )
{
    std::cout << "Enter judge #" << judge_number << " score (1 - 10): " ;

    int score ;
    if( std::cin >> score && score >= 0 && score <= 10 ) return score ;

    // handle input error
    std::cout << "Please, re-enter score (1 - 10)\n" ;
    std::cin.clear() ; // clear possible failed state (non-numeric input)
    std::cin.ignore( 1000, '\n' ) ; // extract and discard the invalid input
    return get_score(judge_number) ; // try again
}

int main()
{
    const int N = 5 ;

    std::string name ;
    std::cout << "Enter first name: " ;
    std::cin >> name ;

    int score[N] ;
    for( int i = 0 ; i < N ; ++i ) score[i] = get_score(i+1) ;

    int min_score = score[0] ;
    int max_score = score[0] ;
    int total_score = score[0] ;

    for( int i = 1 ; i < N ; ++i )
    {
        total_score += score[i] ;
        if( score[i] < min_score ) min_score = score[i] ;
        if( score[i] > max_score ) max_score = score[i] ;
    }

    // we assume that if two scores are tied for the lowest score, only one of them is dropped
    // and likewise for the highest score
    const int total_sans_lowest_and_highest = total_score - min_score - max_score ;

    // avoid integer division
    const double average_after_dropping_lowest_and_highest = double(total_sans_lowest_and_highest) / (N-2) ;

    ///////////////////// added now ////////////////////
    std::cout << "the " << N << " scores are: " ;
    for( int i = 0 ; i < N ; ++i ) std::cout << score[i] << ' ' ;
    std::cout << '\n' ;

    std::cout << "the lowest score is: " << min_score << '\n'
              << "the highest score is: " << max_score << '\n'
              << "the total after dropping the lowest and highest score is: " << total_sans_lowest_and_highest << '\n'
              << "average after dropping the lowest and highest score is: " << average_after_dropping_lowest_and_highest << '\n' ;
}
Thanks man, your way ahead of me with arrays so I was having a hard time making anything out of it I will look a lil later when I get back to my PC.
Topic archived. No new replies allowed.