I have been working on this code for two weeks. I think I have it down to the last couple of lines ie funtion to call from main. But I just cant seem to get it.. Please help, due at midnight..ANd this is my first class so be gentle. :), The code needs to allow user to enter 10 numbers in double format, store the numbers in an array, Creat and use a function to calculate the average of the numbers in the array, Out put the average.

#include <iostream> 
using namespace std; 
 
const int NumScores = 10; // Give a descriptive variable name 
 
 
int main () 
{   
	int k;
	//int sum;
    int score[NumScores]; // Notice I'm using the constant? Give names to numbers. 

    for (int k=0; k<NumScores; k++) //loop that inputs arrays data
    { 
        cout <<"Enter score for 10 test:"<< (k+1) <<endl; 
        cin >> score[k]; 
		
    } 
    for (int k=0; k<NumScores; k++) 
    {  
        cout<<"score for test: "<<(k+1)<<" is "<<score[k]<<endl; 
    } 
	int sum = 0;
	for(int k=0; k<NumScores; k++);
	{
 //sum = sum + NumScores[k];
	}

For one you're using an array of int rather than an array of doubles. Other that that it looks pretty good. What isn't happening that you expect to happen?

Why is sum = sum + etc commented out.?

The basic steps done so far seem reasonable asid from those concerns.

To get the average from sum once the last loop is done just divide by the number of elements in the array.

Then change the current format so that the loop accumulating sum and the actual calculation of the average is done in a function called in main(). Pass the function the array of numbers and the number of numbers in the array. Decide whether the function doing the averaging will output the average or whether the average will be passed back to main and output there, either by storing the return value in a variable in main and then outputting the variables value or by outputted the function return value directly.

For one you're using an array of int rather than an array of doubles. Other that that it looks pretty good. What isn't happening that you expect to happen?

Why is sum = sum + etc commented out.?

The basic steps done so far seem reasonable asid from those concerns.

To get the average from sum once the last loop is done just divide by the number of elements in the array.

Then change the current format so that the loop accumulating sum and the actual calculation of the average is done in a function called in main(). Pass the function the array of numbers and the number of numbers in the array. Decide whether the function doing the averaging will output the average or whether the average will be passed back to main and output there, either by storing the return value in a variable in main and then outputting the variables value or by outputted the function return value directly.

Thats what im missing the average function. I dont know how to do it,

Thats what im missing the average function. I dont know how to do it,

can anyone show me now to do that

You have to make a decision based on the options Lerner gave you in the last paragraph. At this point there is no right or wrong answer.

At that point make a function skeleton like:

return_type myfunctionname(type param1,type param2)
{

}

See how much of it you can fill in on your own. Then post back what you have.

For one you're using an array of int rather than an array of doubles. Other that that it looks pretty good. What isn't happening that you expect to happen?

Why is sum = sum + etc commented out.?

The basic steps done so far seem reasonable asid from those concerns.

To get the average from sum once the last loop is done just divide by the number of elements in the array.

Then change the current format so that the loop accumulating sum and the actual calculation of the average is done in a function called in main(). Pass the function the array of numbers and the number of numbers in the array. Decide whether the function doing the averaging will output the average or whether the average will be passed back to main and output there, either by storing the return value in a variable in main and then outputting the variables value or by outputted the function return value directly.

Im still getting an error, but I think Im close...

#include <iostream> 
using namespace std; 
 
const int NumScores = 10; 
 
int main () 
{   
	int k;
    int score[NumScores];  

    for (int k=0; k<NumScores; k++) //loop that inputs arrays data
    { 
        cout <<"Enter score for 10 test:"<< (k+1) <<endl; 
        cin >> score[k]; 
		
    } 
    for (int k=0; k<NumScores; k++) 
    {  
        cout<<"score for test: "<<(k+1)<<" is "<<score[k]<<endl; 
    } 
	int sum = 0;
	for(int k = 0; k<NumScores; k++);
	sum = sum + score[k];
	return sum/10;
}

Your first post says that you have to use functions ... Do you know what functions are ? Post #5 contains the skeleton of a function in C++.
This link gives you another example
http://www.cplusplus.com/doc/tutorial/functions/

Now you want to move the part of code between line 22-24 in the function call. Also make the array a global variable. This will make your life easy when you are using functions

Edited 6 Years Ago by abhimanipal: n/a

Not that it has anything to do with how to write or use a function, but you need to remove the semicolon at the end of line 22 post #6, as it makes line 23 irrelevant.

And learning how to pass arrays to functions so you can minimize use of global variables is a very useful skill to have, so I would argue against declaring the array with global scope, but the choice is yours, unless your instructor has a policy you need to follow.

Edited 6 Years Ago by Lerner: n/a

You have to make a decision based on the options Lerner gave you in the last paragraph. At this point there is no right or wrong answer.

At that point make a function skeleton like:

return_type myfunctionname(type param1,type param2)
{

}

See how much of it you can fill in on your own. Then post back what you have.

Ok I figures some of it out, but I am still confused about adding a function. I thinks it double avg (int a[]) I know this goes above int main (), but I dont know how to change the rest of the code to call and calculate the verage.

#include <iostream> 
using namespace std; 
 
const int NumScores = 10; 
 
int main () 
{   
    int score[NumScores];  
	int Average;
    for (int i=0; i<NumScores; i++) //loop that inputs arrays data
    { 
        cout <<"Enter score for 10 test:"<< (i+1) <<endl; 
        cin >> score[i]; 
	
    } 
    for (int i=0; i<NumScores; i++) 
    {  
        cout<<"Average for scores:"<<(i+1)<<"is"<<score[i]<<endl; 
		cin>>score[i];
    } 
	int sum = 0;
	for(int i = 0; i<NumScores; i++)
	sum = sum + score[i];
	cout<<"Average:"<<sum/NumScores<<endl;
		

	//Pause
	cin.ignore();
	cin.get();
	return 0;

}

Ok I figures some of it out, but I am still confused about adding a function. I thinks it double avg (int a[]) I know this goes above int main (), but I dont know how to change the rest of the code to call and calculate the verage.

#include <iostream> 
using namespace std; 
 
const int NumScores = 10; 
 
int main () 
{   
    int score[NumScores];  
	int Average;
    for (int i=0; i<NumScores; i++) //loop that inputs arrays data
    { 
        cout <<"Enter score for 10 test:"<< (i+1) <<endl; 
        cin >> score[i]; 
	
    } 
    for (int i=0; i<NumScores; i++) 
    {  
        cout<<"Average for scores:"<<(i+1)<<"is"<<score[i]<<endl; 
		cin>>score[i];
    } 
	int sum = 0;
	for(int i = 0; i<NumScores; i++)
	sum = sum + score[i];
	cout<<"Average:"<<sum/NumScores<<endl;
		

	//Pause
	cin.ignore();
	cin.get();
	return 0;

}

What code actually calculates the average? That's the code that goes into the function. Then where you took the code out, add the call to the function.

Return the average, and output the return value.

This article has been dead for over six months. Start a new discussion instead.