# learning to use C++

I need to know how to pass an array testScores to a function called testaverage that calculates the test average
###### Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x

Commented:
#include <stdio.h>

int testaverage(int testScores[]);

void main()
{
int testScores[3]={50,60,36}; // initializing array
// I assume that there are only 3 scores

int average;

average = testaverage(testScores); // pass the array to function

printf("The average score is %d\n",average);
}

int testaverage(int testScores[])
{
int i, sum=0;

for ( i=0; i<=2; i++ ) // to accumulate scores
sum += testScores[i];

return sum / 3;
}
0

Commented:
Homework?
0

Software EngineerCommented:
for best results, use an STL vector.
if you MUST use an array, you'll have to pass in the length of the array also...
0

Commented:
We cannot provide answers to school assignments.  That is grounds for removal from this site.  (for both you and the experts involved.)  We can provide only limitied help in accademic assignments.    We can answer specific (direct) questions, like you might ask your teacher.  We can review your work and post suggestions, again, like your teacher might do.

Do you have specific questions?
Do you have any work on this (incomplete even) that we can review?

The algorithm you will need to use is pretty simple.  You will declare a variable that is to store the total of all the scores.  You will initialize this variable to 0.  Then you will go through the array with a loop and add each score into the total.  Then when you are done, you will have the total of all scores.  You divide this by the number of scores to get an average.  Can you attempt that?
0

Commented:
You can pass the pointer to an array.
e.g. int arr[100];
and your function will take 'arr' as an argument. This is translated as arr[0] & the first element of an array is deployed into pointer to that array.It is like passing array to the function.

If you need algorithm to calculate the average nietod has already given you the guideline.

But instead of using static array try using STL as suggested by jlouwere. It is simpler to use & manipulate with.
U can use vector or list.

Hope this helps.
0

Commented:
It seems rather strange to suggest using STL to someone who doesn't know how to pass a array as a parameter...
It's like suggesting you're child to take the car before it even car ride a bike...

The syntax to pass a array as parameter:

void f(int *a)
{
a[1] = 0;
}

int main()
{
int array[10];
f(array);
}
0

Commented:
hongjun, did you read the question history?  This is not the first time you've done this!
0

Commented:
He obviously doesn't care.
0

Commented:
There will be no next time.
0

Commented:
Good.
0

Author Commented:
I am a student and it was not a test question it was a question that i am doing to learn how to program at home and my instructor is helping me with it.
he is the one that told me where to find you . I just got c++ for my home pc and am trying to learn how to use it so I would appreciate you not being so hard on honqjun thanks
0

Commented:
I think if you were to read "nietod's" comment of January 11, carefully, he was NOT discouraging anybody from offering you help.  He clearly pointed out that we are here to help you in a limited capacity, somewhat as teachers.  We are NOT here TO DO your homework or school assignments for you, but TO ASSIST with answers TO SPECIFIC QUESTIONS you might have or in need of clarifying (pretty much as if you were in a classroom with your hand raised to ask your teacher a question).

If your teacher were to give you an assignment to complete at home, you would not go up to him/her later and ask him/her to do the same assignment for you that he/she has just delegated you to do at home.  On the other hand, if you had a specific question for your teacher relating to the assignment, wherein you were seeking further information or clarification to it, I cannot see where he/she would refuse to answer such questions.

Part of learning is DOING, and it is particularly needful for you to try solving your own problems.  It is when you feel like you are at a dead end, and things are just not coming together, that's when and where we come in, to point you a way out of the forest and assist in getting you back in the clear; NOT for you to say, "I have such and such a problem and need to do so and so, can you tell me how to do it?"  We need to see effort and deligence on your side, FIRST!
0

Commented:
What he said.
0

Commented:
So replete, "nietod", ... so replete.
0