I am new to programming and have no idea how I have made it this far in the class, but this is our last assignment and I am stuck. This is what the assignment says:

Create program that calculates the average of three test scores:

The program should contain 3 value-returning functions: main (), getTestScore (), and calcAverage(). The main () function should call the getTestScore () function to get and return each of 3 test scores. The test scores may contain a decimal place. (Hint: the main () function will need to call the getTestScore function three times.) The main() function then should call the calcAverage () function to calculate and return the average of the 3 test scores. When the calcAverage() function has completed its task, the main() function should display the average on the screen. Display the average with one decimal place.

Can anyone help? It would be greatly appreciated. This is what I have now (yikes!):

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

//function prototype
double calcAverage (int, int, int);

int main()
{
//declare variables
int score1 = 0;
int score2 = 0;
int score3 = 0;
double average = 0.0;

//get input items
cout << "First test score: ";
cin >> score1;
cout << "Second test score: ";
cin >> score2;
cout << "Third test score: ";
cin >> score3;

//call function to calculate payments
average = calcAverage (int score1, int score2, int score3);
cout << "Average: " << average << endl;


return 0;
} //end of main function

//function definitions
double calcAverage (int num1, int num2, int num3)
{
double avg = 0.0;
avg = static_cast<double>(num1 + num2 + num3) / 3.0;
return avg;
} //end of calcAverage function

Recommended Answers

All 7 Replies

You have not implemented getTestScore. This should not be that difficult.

//Array to store marks for three scores
int scores[3];

getTestScore (scores);
average = calcAverage (scores);
-
-
-
-
-

double calcAverage (int s[]) {
    double a;
    int t=0;
    for(int i=0; i<3; i++) {
          t = t + s[i];
     a=t/3;
     return a;
}

void getTestScore(int *s) {
     cout<<"\nEnter the scores for 3 test:";
     for(int i=0; i<3; i++) {
           cout<<"\nScore "<<(i+1)<<"?";
           cin>>s[i]; 
     }        
 }

Here's a sample code..

class CAvg
{
   private:
      int s;
      double data[3];
      double avg(void)
      {
         double result=0.0;
         for(size_t i=0;i<3;i++)
            result+=data[i];
         return result/3.0;
      }
   public:
      CAvg():s(0){}
      double getTestScore(const double _data)
      {
         if(s<=2)
         {
            data[s]=_data;
            if(s==2)
               return avg();
            s++;    
         }
         return 0.0;
      }
};

int main()
{
   CAvg av;
   double average=0.0;
   double inp=0.0;
   for(size_t i=0;i<3;i++)
   {
      std::cout<<"score "<<i+1<<" : ";
      std::cin>>inp;
      average=av.getTestScore(inp);
   }
   std::cout<<"Average = "<<average;
}

Thanks everyone for your help. I apologize for my programming ignorance, but I have been trying to read over past material and figure out what some of the things in the codes above mean (I do not think we have gotten that far yet!). If there is any way anyone could make it more simple, or put more comments (sorry! i know it is frustrating prob. to people that already know this stuff!) it would be greatly appreciated. I feel like I am reading a foreign language when I look at programs!:/

Don't get frustrated, besides what you have written in the first place wasn't that bad!(You could have used code tags to make it more readable)

commented: Nice.. +1

Thanks! Okay I was trying to understand that earlier so I read about code tags. Would I just take the following steps?
(1) Type [
(2)Type code
(3) Type ]

[code=cplusplus]//your code here [/code]

Chris

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.