#include <iostream>   
using namespace std;
    
int main()

{
   int TESTS = 0;
   int testScore[TESTS];
   int testNum = 0;
   int a;
   double total = 0;
   double average;
   testNum = 0;
   cout<<"How many scores are there? ";
   cin >> TESTS;
   cout<<"Enter a score: ";
   cin >> testScore[testNum];
   while(testNum < TESTS && testScore[testNum])
     {
                 total += testScore[testNum];
                 ++testNum;
                 if(testNum < TESTS)
                 {
                            cout<<"Enter a score: ";
                            cin>>testScore[testNum];
                 }
     }
   cout<< "The entered test scores are: ";
                 
   for (a = 0; a < testNum; ++a)
     cout<<testScore[a]<<" ";
   average = total / testNum;
   cout<<endl<<"The average test score is "<<average<<endl;
   
system("PAUSE");

}

I am trying to find the highest, lowest and average in this array. I can get the average, but I am not sure how to get it to pull the highest and lowest values that are entered.

Recommended Answers

All 6 Replies

Create a couple more variables ("minimum" and "maximum" for example) and initialize them to the first value in the array. Then during this loop:

for (a = 0; a < testNum; ++a)
     cout<<testScore[a]<<" ";

keep comparing them with testScore[a] to see if they are the lowest or highest numbers.
I hope this helps, without giving away too much information.

I am not really sure what you mean. Can you show me an example? I have just been working on this for 5 hours now and I can't see what is going wrong.

#include <iostream>   
using namespace std;
    
int main()
{
   int TESTS = 0;
   int testScore[TESTS];
   int testNum = 0;
   int a;
   double total = 0;
   double average;
   testNum = 0;
   cout<<"How many scores are there? ";
   cin >> TESTS;
   cout<<"Enter a score: ";
   cin >> testScore[testNum];
   while(testNum < TESTS && testScore[testNum])
     {
                 total += testScore[testNum];
                 ++testNum;
                 if(testNum < TESTS)
                 {
                            cout<<"Enter a score: ";
                            cin>>testScore[testNum];
                 }
     }
   cout<< "The entered test scores are: ";
                 
   for (a = 0; a < testNum; ++a)
     cout<<testScore[a]<<" ";
   average = total / testNum;
   cout<<endl<<"The average test score is "<<average<<endl;
   
system("PAUSE");

}

Create a couple more variables ("minimum" and "maximum" for example) and initialize them to the first value in the array. Then during this loop:

for (a = 0; a < testNum; ++a)
     cout<<testScore[a]<<" ";

keep comparing them with testScore[a] to see if they are the lowest or highest numbers.
I hope this helps, without giving away too much information.

I think it would be better to compare the minimum and maximum in the WHILE loop instead of the FOR loop, as suggested above, though either is doable. Assuming you do it in the While loop, here's some revised code that can get you on the right track:

#include <iostream>   
using namespace std;
    
int main()

{
   int TESTS = 0;
   int testScore[TESTS];
   int testNum = 0;
   int a;
   double total = 0;
   double average;
   testNum = 0;
   cout<<"How many scores are there? ";
   cin >> TESTS;
   cout<<"Enter a score: ";
   cin >> testScore[testNum];

   int minimum = testScore[testNum];  // initialize to first score.
   int maximum = testScore[testNum];  // initialize to first score.


   while(testNum < TESTS && testScore[testNum])
     {
                 total += testScore[testNum];
                 ++testNum;
                 if(testNum < TESTS)
                 {
                            cout<<"Enter a score: ";
                            cin>>testScore[testNum];

                            if (/* compare testScore[testNum] to minimum */)
                            {
                                 minimum = testScore[testNum];
                            }
                            if (/* compare testScore[testNum] to maximum */)
                            {
                                 maximum = testScore[testNum];
                            }
                 }
     }
   cout<< "The entered test scores are: ";
                 
   for (a = 0; a < testNum; ++a)
     cout<<testScore[a]<<" ";
   average = total / testNum;
   cout<<endl<<"The average test score is "<<average<<endl;

   // display maximum and minimum.
   
system("PAUSE");

}

See lines 19, 20, 32 - 39, and 49, which I have added. Line 49 is a simple cout statement. Look at lines 32 and 36. You need to test for the appropriate conditions and fill that in where the comment is.

I think I have it now. Take a look and let me know.

#include <iostream>   
using namespace std;
    
int main()

{
   int TESTS = 0;
   int testScore[TESTS];
   int testNum = 0;
   int a;
   double total = 0;
   double average;
   testNum = 0;
   cout<<"How many scores are there? ";
   cin >> TESTS;
   cout<<"Enter a score: ";
   cin >> testScore[testNum];

   int minimum = testScore[testNum];  // initialize to first score.
   int maximum = testScore[testNum];  // initialize to first score.


   while(testNum < TESTS && testScore[testNum])
     {
                 total += testScore[testNum];
                 ++testNum;
                 if(testNum < TESTS)
                 {
                            cout<<"Enter a score: ";
                            cin>>testScore[testNum];

                            if (testScore[testNum] < minimum)
                            {
                                 minimum = testScore[testNum];
                            }
                            if (testScore[testNum] > maximum)
                            {
                                 maximum = testScore[testNum];
                            }
                 }
     }
   cout<< "The entered test scores are: ";
                 
   for (a = 0; a < testNum; ++a)
     cout<<testScore[a]<<" ";
   average = total / testNum;
   cout<<endl<<"The average test score is "<<average<<endl;

   cout<<"The highest score is "<<maximum<<" and the lowest score is "<<minimum<<endl;
   
system("PAUSE");

}

Looks good to me. Try it out. Does it work?

It does!! Thanks for your help you have been awesome!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.