Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 107,670 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,041 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



averaging an array

 
Reply to this topicStart new topic

averaging an array

jencalma
post 1 Jul, 2008 - 09:39 PM
Post #1


New D.I.C Head

*
Joined: 13 Jul, 2007
Posts: 46


My Contributions


Write a program that will accept your inputs and then compute the average of all the data. Now scan through the array to find the value that is farthest (lowest and highest) from the average.
Sample Output:
Input the data for the ten (10) internet cafes:
data[ 0 ] = 600.2
data[ 1 ] = 625.0
data[ 2 ] = 512.5
data[ 3 ] = 125.7
data[ 4 ] = 216.1
data[ 5 ] = 367.4
data[ 6 ] = 855.5
data[ 7 ] = 235.5
data[ 8 ] = 1234.3
data[ 9 ] = 32586.4
average: 3735.86
Lowest: 125.7
Highest: 32586.4
User is offlineProfile CardPM

Go to the top of the page


no2pencil
post 1 Jul, 2008 - 09:41 PM
Post #2


Wet D.I.C.

Group Icon
Joined: 10 May, 2007
Posts: 4,840



Thanked 27 times

Dream Kudos: 2325

Expert In: Goofing Off

My Contributions


Are you going to post all of your homework assignments here?

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this:

Thank you for helping us helping you.
User is online!Profile CardPM

Go to the top of the page

captainhampton
post 2 Jul, 2008 - 06:00 AM
Post #3


D.I.C Addict

Group Icon
Joined: 17 Oct, 2007
Posts: 501



Thanked 2 times

Dream Kudos: 775
My Contributions


no2pencil is right, we are more than willing to help however we want some genuine effort on your front.
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 2 Jul, 2008 - 06:14 AM
Post #4


T3H R0XX0R!

Group Icon
Joined: 6 Feb, 2008
Posts: 2,300



Thanked 44 times

Dream Kudos: 1450

Expert In: C, C++

My Contributions


Create an array of 10 floats.
create two floats, hi=0; lo=999999
Create a float called total = 0
Loop 10 times to get input.
During this loop, total += the input
if input is higher than hi, hi = input
if input is lower than lo, lo = input
After the loop, float mean = total / 10

Hope this helps smile.gif
User is offlineProfile CardPM

Go to the top of the page

kapax
post 2 Jul, 2008 - 06:19 AM
Post #5


New D.I.C Head

*
Joined: 2 Jul, 2008
Posts: 33



Thanked 1 times
My Contributions


Since nobody wants to write a code, I think it would really be inappropriate, too. You should code it by yourself. Here is the algorithm:

1) Get input, increment a counter X with each input received;
2) Make a loop repeating X times and sum up all the data;
3) Divide a sum by X - here you will get an average;

To find farthest-lowest input from the average:
4) Make a simple linear search: save first element in some variable, let's say MIN;
5) Write a loop which would run X-1 times and if data[i] < MIN, then MIN = data[i];

Same with farthest-highest input. Got it?
User is offlineProfile CardPM

Go to the top of the page

jencalma
post 4 Jul, 2008 - 02:32 AM
Post #6


New D.I.C Head

*
Joined: 13 Jul, 2007
Posts: 46


My Contributions


QUOTE(kapax @ 2 Jul, 2008 - 06:19 AM) *

Since nobody wants to write a code, I think it would really be inappropriate, too. You should code it by yourself. Here is the algorithm:

1) Get input, increment a counter X with each input received;
2) Make a loop repeating X times and sum up all the data;
3) Divide a sum by X - here you will get an average;

To find farthest-lowest input from the average:
4) Make a simple linear search: save first element in some variable, let's say MIN;
5) Write a loop which would run X-1 times and if data[i] < MIN, then MIN = data[i];

Same with farthest-highest input. Got it?



here is the code that i'm working on.. but i don't know how will i show which array is the lowest and highest.

cpp
#include<iostream.h>
#include<conio.h>

void name();
int main()
{
name();
float dat[10];
cout<<"\ninput data for ten(10) integers:";
cout<<"\ndata[0]=";
cin>>dat[0];
cout<<"data[1]=";
cin>>dat[1];
cout<<"data[2]=";
cin>>dat[2];
cout<<"data[3]=";
cin>>dat[3];
cout<<"data[4]=";
cin>>dat[4];
cout<<"data[5]=";
cin>>dat[5];
cout<<"data[6]=";
cin>>dat[6];
cout<<"data[7]=";
cin>>dat[7];
cout<<"data[8]=";
cin>>dat[8];
cout<<"dats[9]=";
cin>>dat[9];

float ave;
float sum=0;
for(int a=0;a<10;a++)
{
sum=sum+dat[a];
}
ave=sum/10;
cout<<"\naverage:"<<ave;


getch();
}


Mod Edit: Please code.gif
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 4 Jul, 2008 - 03:23 AM
Post #7


T3H R0XX0R!

Group Icon
Joined: 6 Feb, 2008
Posts: 2,300



Thanked 44 times

Dream Kudos: 1450

Expert In: C, C++

My Contributions


Your average does work, it's just that your program doesn't compile because you haven't defined your name() function.

cpp
#include<iostream>
#include<conio.h>

//void name();
// there is no definition to this function...
// what do you want it to do?

using namespace std; // cout, cin

int main()
{
//name();
float dat[10];
cout<<"\ninput data for ten(10) integers:";
cout<<"\ndata[0]=";
cin>>dat[0];
cout<<"data[1]=";
cin>>dat[1];
cout<<"data[2]=";
cin>>dat[2];
cout<<"data[3]=";
cin>>dat[3];
cout<<"data[4]=";
cin>>dat[4];
cout<<"data[5]=";
cin>>dat[5];
cout<<"data[6]=";
cin>>dat[6];
cout<<"data[7]=";
cin>>dat[7];
cout<<"data[8]=";
cin>>dat[8];
cout<<"dats[9]=";
cin>>dat[9];

float ave;
float sum=0;
for(int a=0;a<10;a++)
{
sum += dat[a];
}
ave=sum/10;
cout<<"\naverage:"<<ave;


getch();
}

Until I know what name is supposed to do, I can't help you with that.

Hope this helps smile.gif
User is offlineProfile CardPM

Go to the top of the page

Mallstrop
post 4 Jul, 2008 - 03:35 AM
Post #8


New D.I.C Head

*
Joined: 19 Jun, 2008
Posts: 32



Thanked 1 times
My Contributions


Since the average is in the middle of the values, to get the furthest away from the average you're going to be after the minimum and maximum values. Once you have the min, max and the average you can compare them to see which is furthest from the average.

To get the min/Max,



Set min and max as dat[0] (It's the first one you see so you can compare against it.)

Then in your for loop:
if a value is bigger than Max, make that value your new Max
if a value is less than Min, make that value your new Min.

At the end of the for loop you have the values of Min and Max.

You can compare them afterwards to see which is furthest from average.
User is offlineProfile CardPM

Go to the top of the page

jencalma
post 4 Jul, 2008 - 05:20 AM
Post #9


New D.I.C Head

*
Joined: 13 Jul, 2007
Posts: 46


My Contributions


QUOTE(gabehabe @ 4 Jul, 2008 - 03:23 AM) *

Your average does work, it's just that your program doesn't compile because you haven't defined your name() function.

cpp
#include<iostream>
#include<conio.h>

//void name();
// there is no definition to this function...
// what do you want it to do?

using namespace std; // cout, cin

int main()
{
//name();
float dat[10];
cout<<"\ninput data for ten(10) integers:";
cout<<"\ndata[0]=";
cin>>dat[0];
cout<<"data[1]=";
cin>>dat[1];
cout<<"data[2]=";
cin>>dat[2];
cout<<"data[3]=";
cin>>dat[3];
cout<<"data[4]=";
cin>>dat[4];
cout<<"data[5]=";
cin>>dat[5];
cout<<"data[6]=";
cin>>dat[6];
cout<<"data[7]=";
cin>>dat[7];
cout<<"data[8]=";
cin>>dat[8];
cout<<"dats[9]=";
cin>>dat[9];

float ave;
float sum=0;
for(int a=0;a<10;a++)
{
sum += dat[a];
}
ave=sum/10;
cout<<"\naverage:"<<ave;


getch();
}

Until I know what name is supposed to do, I can't help you with that.

Hope this helps smile.gif



ow?! i forgot to remove the "name" the name is for my function in displaying may name but don't mind it
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 8/29/08 11:07PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month