Passing an array to function and retrun a value

I have read a file to an array successfully. However, when I attempt to send "avg" to a cout statement it returns a zero. I have walked through the function up to and including avg as a cout statement from the function(it returns correct results), however, when I attempt to "return avg;" and then write it to the output console I get zero.

Any advice?

//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.


[code]
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++)//Note 10 is the actual size of array


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




return avg;
}[/Code]
Last edited on
Hi,

i notice in your code that Size is the varisble passed to the function
yet in your code you seem to be using score_length

the other problem is the semicolon ; after your for statement
so that total+= score[index] is only run once

id write that bit like this

1
2
3
4
   for (index = 0;index<score_length;index++)
  {
      total += score[index];
  }


hope this helps
shredded
Hi,

i notice in your code that Size is the varisble passed to the function
yet in your code you seem to be using score_length

the other problem is the semicolon ; after your for statement
so that total+= score[index] is only run once

id write that bit like this

1
2
3
4
   for (index = 0;index<score_length;index++)
  {
      total += score[index];
  }


hope this helps
shredded
Thank you shredded,

It did help somewhat, however, It is still returning zero.

I did eliminate the semicolon after the for loop.

Also I replaced "score_length" with 10, that is the number of elements I am using in a test file I created.

I have tried using "score_lenght", "SIZE", and the number "10" in my function. They all return zero, however, if I return 0; to main instead of return avg; and, cout << avg; (as a part of the funcion) I get 68.8 which is the average of the 10 scores I created to be able to test my program up to this point.

Am I doing something wrong when I am trying to return avg; and then cout << avg; in main?

Any more advice?
post your main call.
avg = (total/SIZE); How much is SIZE?
Also how do I get my code to look like yours when I post. I thought it was ?

Here is my main call

#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;
}


//Function definition: calcAvg has an array passed to it and calculates the average of the test scores read into the array scores[SIZE].
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;
}
[code] Your code goes here [/code] (in your first post you've got [code][/Code])

1
2
3
calcAvg(score, 10); //you aren't storing the return value
double avg; //starts with garbage
cout << "The average score from the program is: " << avg << endl <<endl;//prints garbage 


1
2
3
for(count = 0; count < SIZE && inputFile >> score[count]; count++)
   ;
inputFile >> score[count];
Why are you trying to read one more time?
I'm very new to C++; I thought when I returned avg in my function
return avg;

I stored the calculated function, which, calculates the average in to avg at the end of my function.
Thus being able to cout << avg; and get my results. Not the case thank you for pointing that out.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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; 
        }


Thank you everyone for your time, patience, and attention

Kindest regards,

Joseph

Topic archived. No new replies allowed.