I have read a file to an array successfully. However, when I attempt to pass the array to the function calcAvg(calculate average) and return the average, I get a zero.

//Function prototype

double calcAvg(double [], int);

//Function call

calcAvg(score,SIZE);

I set a variable called score_length (in main) to get the size of the array. I had to declare an array to hold 500 elements, and, read a data file with an unknown number of elements.

double calcAvg(double score[], int SIZE)
{

int index; //Counter set to index array.
double total = 0; //Accumulator.
double avg = 0; //Variable to hold average score.

//Index through array to get test scores
for(index = 0; index < score_length; index++);

//Variable to hold total of scores.
total += score[index];
avg = (total / score_length);


return avg;
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

for(index = 0; index < score_length; index++); Something is amiss on that line. Check the end of it. Doing this causes the for loop to run score_length number of times with ";" as the line to be looped over.

You may want to pass the number of elements in the array into your function instead of size. Global variables are never a good idea.

Edited 6 Years Ago by jonsca: n/a

I have made some changes to the program and tested some things. Every thing is fine until I try to return "avg" to main.

In main I wrote a statement that sends "avg" to the output console, however, when I calculate total in my function it gives 68.8(correct) but in main it shows ZERO(incorrect)

If I return avg to main, can I not use avg in a cout statement and get the value of the function?

Please post the relevant portion of your main(). Without seeing it, all we can do is speculate.

And, as jonsca said:

...
You may want to pass the number of elements in the array into your function instead of size. Global variables are never a good idea.

Based on what you say, I don't think you did use a global, but take heed if you, in fact, did.

Edited 6 Years Ago by Fbody: n/a

#include<iostream>
#include<fstream>
using namespace std;


//Function prototypes
double calcAvg(double [], int); //Get average test score
double minMax(double [], int);  //Get Min & Max test scores
double stdDeviation(double[], int); //Get standard deviation of test scores.
            //Loop counter variable


int main()
{
   
    const int SIZE = 500;  //Size declaration of array
    double score[SIZE];    //Array declaration
    
    
    ifstream inputFile;    //Input file stream object
    //Open file and validate file has open. Throw exception if necessary.
    inputFile.open("myTestData.txt");                     
    if(!inputFile) cout << "Problems with file!\n";       
    
    //Read numbers from file into the array.
    int count;
    for(count = 0; count < SIZE && inputFile >> score[count]; count++);
    inputFile >> score[count];
        
    //Variable initalized to the known size of the array. 
    int score_length; //Variable declared for length of array.
    score_length = count;
    
    
    /*Index variable to step through array and display elements.  This will 
      validate that the correct numbers have been entered into the array, 
      satisfy my curiosity, and we can move forward with program. */
      
    cout << "You have " << score_length << " numbers that I will display;\n";
    
    cout << "Here are your numbers: \n";    
    for(count = 0; count < score_length; count++)
     cout << " " <<score[count]  << endl;
     cout << endl << endl;
    
    //Function to calculate average score; results returned to main.
    calcAvg(score, 10);
     
    //Closing after getting data.
    inputFile.close();    
    
    
    
    //Display the average.
    double avg;
    cout << "The average score from the program is: " << avg << endl <<endl;
    cout << "The actual average score is: 68.8\n" << endl; 
    
system("pause");
return 0;
}

double calcAvg(double score[], int SIZE)
{
     
       int index;            //Counter set to index array.
       double total = 0;     //Accumulator.
       double avg = 0;       //Variable to hold average score.
       
       //Index through array to get test scores
       for(index = 0; index < 10; index++)
       {
          //Variable to hold total of all scores.
          total += score[index];
          avg = (total/10);
       }
        
       
        
        
        return avg; 
        }

You've "hard-coded" the 10 into your program, so if you change that number, you have to change it everywhere. You never use your second parameter.

More importantly, you never assign the return value from your function to anything. "avg" in your function and "avg" in main (which is declared right above it's use so it could have garbage in it instead of 0) have no relation to each other, as they are not in the same scope (skim through your book's section on scope to see why).

Just a minor detail, if your file is bad, you report that to the user, but you continue on with the program anyway.

Edited 6 Years Ago by jonsca: n/a

This question has already been answered. Start a new discussion instead.