learning to use C++

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

[Webinar] Streamline your web hosting managementRegister Today

x
 
hongjunConnect With a Mentor 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
 
tdubroffCommented:
Homework?
0
 
Jan LouwerensSoftware 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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
nietodCommented:
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
 
ajitmCommented:
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
 
LucHoltkampCommented:
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
 
nietodCommented:
hongjun, did you read the question history?  This is not the first time you've done this!
0
 
TryCommented:
He obviously doesn't care.
0
 
hongjunCommented:
Sorry about that.
There will be no next time.
0
 
nietodCommented:
Good.
0
 
DerrelAuthor 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
 
TryCommented:
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
 
nietodCommented:
What he said.
0
 
TryCommented:
So replete, "nietod", ... so replete.
0
All Courses

From novice to tech pro — start learning today.