Link to home
Start Free TrialLog in
Avatar of Derrel
Derrel

asked on

learning to use C++

I need to know how to pass an array testScores to a function called testaverage that calculates the test average
Avatar of tdubroff
tdubroff

Homework?
Avatar of Jan Louwerens
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...
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?
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.
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);
}
ASKER CERTIFIED SOLUTION
Avatar of hongjun
hongjun
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hongjun, did you read the question history?  This is not the first time you've done this!
He obviously doesn't care.
Sorry about that.
There will be no next time.
Good.
Avatar of Derrel

ASKER

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
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!
What he said.
So replete, "nietod", ... so replete.