# Array function average

• (2 Pages)
• 1
• 2

## 16 Replies - 30821 Views - Last Post: 25 April 2010 - 01:21 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=169797&amp;s=6b6b069e6fe35edee1c708b5364d6525&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 nikc121

Reputation: 2
• Posts: 133
• Joined: 12-April 10

# Array function average

Posted 23 April 2010 - 10:07 AM

The overall purpose of the program is to allow users to input a list of positive numbers into an array, find the average of the numbers in the array and output the result.

Specific requirements:

The application shall:

1. Allow users to enter 10 numbers in double format

2. Store the numbers in an array

3. Create and use a function to calculate the average of the numbers in the array

4. Output the average

Quote

I have been slowly trying to figure this code out. I have listed below what I have so far, but I cant seem to figure out how to use the function code to calculate the average, prompt the user to enter only 10 numbers (in double format) and then print average to screen. Any help would be appreciated. I know I am missing alot, just cant seem to make it all fall together.
```#include <iostream>
using namespace std;
main ()
{
int size = 10                          //Array size
int array [size];                 //Declare array
int sum = 0
for (int i=0; i < 10; i++)       //Loop which calcultes sum
}

```

Is This A Good Question/Topic? 0

## Replies To: Array function average

### #2 Crunch

• D.I.C Lover

Reputation: 139
• Posts: 1,222
• Joined: 28-July 09

## Re: Array function average

Posted 23 April 2010 - 10:11 AM

You have to put a scanf inside a loop and set it to iterate 10 times

int sum;

inside another loop put

sum+=array[index];

loop through until you reach the end of array

next use a prinf to print the result.

Simple

### #3 nikc121

Reputation: 2
• Posts: 133
• Joined: 12-April 10

## Re: Array function average

Posted 23 April 2010 - 10:20 AM

Like this?
```int size = 10;                          //Array size
int array [size];                 //Declare array
int sum = 0;
for (int i=0; i < 10; i++);       //Loop which calcultes sum
}              sum+=array[i];
average = sum/10
cout <<"Average is:"<<average;
```

### #4 sarmanu

• D.I.C Lover

Reputation: 967
• Posts: 2,362
• Joined: 04-December 09

## Re: Array function average

Posted 23 April 2010 - 10:37 AM

``` for (int i=0; i < 10; i++); // >> Remove me

```

You may want to remove the semicolon at the end of that for loop. If that semicolon remains there, then the instructor after that for, is ignored.
Secondly, you seem to have a floating '}'. Remove it...
And this doesn't compile on MSVC:
```int size = 10;                          //Array size
int array [size];

```

because "size" is not a constant. Declare it as:
```const int size = 10;

```

### #5 nikc121

Reputation: 2
• Posts: 133
• Joined: 12-April 10

## Re: Array function average

Posted 23 April 2010 - 10:41 AM

UGGGGG,, still not working. I removed the ; but not sure what {} to remove. Its all very confusing. It seems like a simple program but for someone with no experience this is very difficult. Any more guidance would be greatly appreciated. I will be on this for several more hours,,,Trial and error...

### #6 sarmanu

• D.I.C Lover

Reputation: 967
• Posts: 2,362
• Joined: 04-December 09

## Re: Array function average

Posted 23 April 2010 - 10:42 AM

Please post the entire code.

### #7 nikc121

Reputation: 2
• Posts: 133
• Joined: 12-April 10

## Re: Array function average

Posted 23 April 2010 - 10:47 AM

sarmanu, on 23 April 2010 - 09:42 AM, said:

Please post the entire code.

Quote

I know its not much there. Im still learning. I dont even think I have my declarations correct. I still need to prompt the user to enter 10 numbers, and print the average of the ten numbers. Trying to get as much done as possible inbetween a baby running around vomiting everywhere. Yea fun...Again thank you for the help and guidance, its keeping me from pulling most of my hair out. Project due before midnight..Probably not going to happen.
```include <iostream>
using namespace std;
main ()

int sum
int average
int number

{
int size = 10;                          //Array size
int array [10];                 //Declare array
int sum = 0;
for (int i=0; i < 10; i++)       //Loop which calcultes sum
}              sum+=array[i];
average = sum/10
cout <<"Average is:"<<average;
```

### #8 Oler1s

• D.I.C Lover

Reputation: 1397
• Posts: 3,884
• Joined: 04-June 09

## Re: Array function average

Posted 23 April 2010 - 11:04 AM

```main ()

int sum
int average
int number

```

What is this? Does this make sense? Check your C++ books? Is this syntactically consistent? Braces randomly thrown about. Code guessing. This is horrible.

Stop vomiting code. First, figure out precisely step by step what must happen. This is a pure logic exercise. Pen and paper. You can detail step by step what happens on pen and paper.

Then, you figure out how to implement what you have on paper in code, step by step. Right now, all I see is random placement of random code, hoping it works.

### #9 nikc121

Reputation: 2
• Posts: 133
• Joined: 12-April 10

## Re: Array function average

Posted 23 April 2010 - 01:45 PM

Oler1s, on 23 April 2010 - 10:04 AM, said:

```main ()

int sum
int average
int number

```

What is this? Does this make sense? Check your C++ books? Is this syntactically consistent? Braces randomly thrown about. Code guessing. This is horrible.

Stop vomiting code. First, figure out precisely step by step what must happen. This is a pure logic exercise. Pen and paper. You can detail step by step what happens on pen and paper.

Then, you figure out how to implement what you have on paper in code, step by step. Right now, all I see is random placement of random code, hoping it works.

Quote

Ok,, I tried a new code. I am still getting over 33 errors. Not sure what else I can try. Any help?
```#include <iostream>
using namespace std;
main ()

int sum;
int average;
int score [10];

int k;
int sum = 0;
int average = 0;
for (k = 0;k<10;k++)
{
cout <<"Enter score for 10 test:" << (k+1) <<endl;
cin>> score[k]
sum = sum + score [k];
}
average = sum/10;
for (k=0; k<10; k++)
cout<<"score for test:"<<(k+1)<< "is"<<score [k]<<endl;
}
cout<<"The average for all 10 test is"<<average<<endl;
return 0;
}
```

### #10 jjl

• Engineer

Reputation: 1270
• Posts: 4,998
• Joined: 09-June 09

## Re: Array function average

Posted 23 April 2010 - 02:16 PM

these are some of the most noticable errors ive ever seen. How are you not picking up on them? oh probably cause you didnt pay attention in class
```#include <iostream>
using namespace std;

int main () //main should always be of type int - no exceptions
{ //<----------NO BRACKETS???

int sum;
int average;
int score [10];

int k;
//int sum = 0; //sum is defined twice
//int average = 0; //average is defined twice
for (k = 0;k<10;k++)
{
cout <<"Enter score for 10 test:" << (k+1) <<endl;
cin>> score[k]; //<--- no semi colon
sum = sum + score [k];
}
average = sum/10;
for (k=0; k<10; k++)
cout<<"score for test:"<<(k+1)<< "is"<<score [k]<<endl;

cout<<"The average for all 10 test is"<<average<<endl;
return 0;
}

```

### #11 Oler1s

• D.I.C Lover

Reputation: 1397
• Posts: 3,884
• Joined: 04-June 09

## Re: Array function average

Posted 23 April 2010 - 02:17 PM

Some rather critical syntactical errors. Here's an exercise:

Write a programs that puts hello world on screen.

EDIT: Nevermind, pre-empted.

This post has been edited by Oler1s: 23 April 2010 - 02:18 PM

### #12 nikc121

Reputation: 2
• Posts: 133
• Joined: 12-April 10

## Re: Array function average

Posted 23 April 2010 - 05:42 PM

Quote

ImaSexy, on 23 April 2010 - 01:16 PM, said:

these are some of the most noticable errors ive ever seen. How are you not picking up on them? oh probably cause you didnt pay attention in class
```#include <iostream>
using namespace std;

int main () //main should always be of type int - no exceptions
{ //<----------NO BRACKETS???

int sum;
int average;
int score [10];

int k;
//int sum = 0; //sum is defined twice
//int average = 0; //average is defined twice
for (k = 0;k<10;k++)
{
cout <<"Enter score for 10 test:" << (k+1) <<endl;
cin>> score[k]; //<--- no semi colon
sum = sum + score [k];
}
average = sum/10;
for (k=0; k<10; k++)
cout<<"score for test:"<<(k+1)<< "is"<<score [k]<<endl;

cout<<"The average for all 10 test is"<<average<<endl;
return 0;
}

```

Thank you for your help...Considering I have never done this before, I think I did pretty well. I found a couple of those errors after staring at it for a couple of hours. My class is online and consist of the instructor telling us to read and then figure a code out. I am trying my best. Sorry if I am not up to most of the peoples standards that are on here. I came to this forum because you guys are suppose to be the experts.I know some people on here try to take advantage and get you to do there homework. Im not that type. Im giving it my best, and after reading the prelude to programming book several times, it all seems rather confusing. Again thank you for your help (not your comment).

This post has been edited by JackOfAllTrades: 24 April 2010 - 07:04 AM
Reason for edit:: Removed self-quoting. Don't put your response in quotes, dude.

### #13 jjl

• Engineer

Reputation: 1270
• Posts: 4,998
• Joined: 09-June 09

## Re: Array function average

Posted 24 April 2010 - 01:03 AM

a lot of people on this form blatenly post there code with zero effort to try and understand theres along with no effort to try and fix them, they expect us to take there code and make it work for them. now that i see that you are beginner and trying to understand the comcepts that makes me feel better programming in the beginning is the hards think to grasp, but once you hit that level of understanding the amount in which you cam learn is endless. happy to hear you worked out your problems

### #14 nikc121

Reputation: 2
• Posts: 133
• Joined: 12-April 10

## Re: Array function average

Posted 24 April 2010 - 06:46 AM

ImaSexy, on 24 April 2010 - 12:03 AM, said:

a lot of people on this form blatenly post there code with zero effort to try and understand theres along with no effort to try and fix them, they expect us to take there code and make it work for them. now that i see that you are beginner and trying to understand the comcepts that makes me feel better programming in the beginning is the hards think to grasp, but once you hit that level of understanding the amount in which you cam learn is endless. happy to hear you worked out your problems

Thank you, I was up most of the night redoing my code. I finally got it to compile and ex..I changed the errors you found and made some other minor changes, however my instructor say "to fully respect the specifications you have to implement the computation of the average as a function that should be called from the main function." Ok now Im confused again. Is she asking me to input another line of code, I thought I already called it from main. Guess I have to do some more reading today. Any guidance would be appreciated. Thanks again for your understanding.
```]include <iostream>
using namespace std;

int main ()
{
int sum=0;
int average=0;
int score [10];

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

cout<<"The average for all 10 test is"<<average<<endl;
return 0;
}
```

This post has been edited by JackOfAllTrades: 24 April 2010 - 07:04 AM
Reason for edit:: Removed self-quoting. Don't put your response in quotes, dude.

### #15 CTphpnwb

• D.I.C Lover

Reputation: 3810
• Posts: 13,833
• Joined: 08-August 08

## Re: Array function average

Posted 24 April 2010 - 07:33 AM

She means that the average shouldn't be calculated in main, but instead done in a separate function:
```int main()
{
.
.
.
average = theavg( score, 10);
.
.
.
}

double theavg( double sc[],  int sc_count)
{
int k;
double sum = 0;
for (k = 0;k < sc_count;k++)
{
sum = sum + score [k];
}
return sum/10;
}
```

This post has been edited by CTphpnwb: 24 April 2010 - 07:35 AM