average call function

need to define my average funtion then call it from main

  • (8 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

109 Replies - 10160 Views - Last Post: 03 May 2010 - 03:47 PM Rate Topic: -----

#1 nikc121  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 123
  • Joined: 12-April 10

average call function

Posted 29 April 2010 - 09:47 AM

I had my code completed then my instructor wanted me to "1. Define the average function, 2. Inside the avg function you should put the code that calculates the average, 3. Call the avg function from the main function. I think I have it but would like you guys and gals expert opinion (suck up)...Plus I am unable to test it at the moment due to errors with my Visual studio program, Maybe you can help with that too. Getting error "application failed to start because msvcp90d was not found. I have tried to down load the .dll file but still getting the same error, Should I just remove the VS and re install? Thanks again for helping.
******************************************************/
/* File: arrayproject                                 */ 
/*                                                    */ 
/* Created by: me                                     */ 
/* Date: May 1, 2010                                  */ 
/*                                                    */ 
/* Program to allow user to input a list of           */ 
/* positive numbers into an array, find the average   */
/* of the numbers in the array and output the result  */
/*                                                    */
/*  Inputs: (keyboard)                                */
/*  Input 10 positive numbers                         */ 
/*                                                    */ 
/* Output:                                            */ 
/* Print average on screen                            */ 
/*                                                    */ 
/* Algorithm: Allow user to enter 10 numbers in       */
/* double format. Store the numbers in an array.      */
/* Create and use a function to calculate the         */
/* average. Out put the average                       */ 
/******************************************************/ 

#include <iostream>
using namespace std;

int main ()
{
	int sum = 0;
	int average = 0;
	int score [10];
	int k;
	int avg(int a[])// this is to define 

//int sum = 0;
// int average = 0;
	for (k = 0; k<10; k++)
		average = avg(score)//this is to call the func

	{
		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;
}


Is This A Good Question/Topic? 0
  • +

Replies To: average call function

#2 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: average call function

Posted 29 April 2010 - 11:54 AM

The code you have submitted does not have the average function routine. Can you post the code with that in so we can take a look at it.
Was This Post Helpful? 0
  • +
  • -

#3 masoug  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 181
  • Joined: 30-December 09

Re: average call function

Posted 29 April 2010 - 12:21 PM

Quote

Getting error "application failed to start because msvcp90d was not found. I have tried to down load the .dll file but still getting the same error, Should I just remove the VS and re install?

Yes, you might be better off reinstalling. You have to put the DLL inside a directory (I don't remember) but it is used when you execute programs built from a "Debug" configuration.

Like Martyn.Rae said, you just declared the function, but didn't define it. I would change it to:
float avg(int numberOf, ...); //Have you used ellipsis before?
{
// Your code (Sorry I don't have time to explain, look up variable argument list on google)
}



-Masoug
Was This Post Helpful? 0
  • +
  • -

#4 nikc121  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 123
  • Joined: 12-April 10

Re: average call function

Posted 30 April 2010 - 08:04 AM

View PostMartyn.Rae, on 29 April 2010 - 10:54 AM, said:

The code you have submitted does not have the average function routine. Can you post the code with that in so we can take a look at it.

My problem now: Cannot call the avg funtion before you have the value of the array,,call the avg funtion after the values of array are read...So do I move average = avg(score) to below cin.. score [k] or am I completly off. Second Need avg funtion definition. I think the code for that is int avg (int a[10]) and where do I put that? Please help.. due tonight.
#include <iostream>
using namespace std;

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

//int sum = 0;
// int average = 0;
	for (k = 0; k<10; k++)
		average = avg(score)

	{
		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;
}

Was This Post Helpful? 0
  • +
  • -

#5 nikc121  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 123
  • Joined: 12-April 10

Re: average call function

Posted 30 April 2010 - 08:24 AM

Ok here is my latest code. The instructor wanted me to call the avg function after the values of array are read and to include a avg function definition. Can anyone look at this and tell me if its correct. It did compile and ran properly but the intstructor doesnt much care if it runs properly more that she wants all her codes in like she ask. Thanks for all the help.
#include <iostream>
using namespace std;

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

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

Was This Post Helpful? 0
  • +
  • -

#6 nikc121  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 123
  • Joined: 12-April 10

Re: average call function

Posted 30 April 2010 - 09:19 AM

Thought I had my code correct. Instructor says not....wants me to call the avgerage function after the values of the array are read and to add a average function definition. I think I did that below. Could you guys please review before I submit. It compiles and runs,but it did before I changed it too. My instructor want more code that is needed just to see if we have it...
#include <iostream>
using namespace std;

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

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

Was This Post Helpful? 0
  • +
  • -

#7 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: average call function

Posted 30 April 2010 - 09:28 AM

I am not sure, but I believe that is not what your instructor meant.
I believe The instructor wants you to create an avg function, that will get an array of int values, and will calculate inside it the average. then the returned value of the function will be the average of thenumbers in that array.
in other words, instead of calculating the avg in the main, do it inside a function.

as for your code, you have compile errors, but you says the code is working. are these problems while copy\paste the code to the browser?

you miss ";" after declaring the avg
average does not exist in the avg function scope
average = avg(score) //<<missing ";"


also, you calculate the average for every input you get. is that also in purpose?
Was This Post Helpful? 1
  • +
  • -

#8 diego_pmc  Icon User is offline

  • D.I.C Addict

Reputation: 81
  • View blog
  • Posts: 565
  • Joined: 13-May 09

Re: average call function

Posted 30 April 2010 - 09:29 AM

View Postnikc121, on 30 April 2010 - 08:19 AM, said:

It compiles and runs


No, it does not. Here's a list of compile errors I get:

Quote

In function `int main()':
14|error: expected initializer before "for"
14|error: expected `;' before ')' token
26|error: `size' was not declared in this scope

In function `int avg()':
28|error: `average' was not declared in this scope
=== Build finished: 4 errors, 0 warnings ==
=


And I don't think your teacher asks you to add a function definition (you already did that). I suppose he's telling you that you need a declaration before the main function, because right now your 'avg' function is not visible in main. There are two ways you can fix this: either move 'avg' above 'main', or add a function prototype of 'avg' (use Google) above 'main' (your choice, I'd say you should use the prototype).

This post has been edited by diego_pmc: 30 April 2010 - 09:47 AM

Was This Post Helpful? 1
  • +
  • -

#9 joesyuh  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 174
  • Joined: 30-September 08

Re: average call function

Posted 30 April 2010 - 09:30 AM

From what I can tell he is asking you to call the average function and actually calculate the average in it. Right now you are simply printing the average in the function. Why would you pass the array to the function if you are not going to use it?

Also

average = avg(score)


You have your function returning 0. So everytime that line runs you are simply over writing the variable average with 0.

Basically you need to move your average calculation to the avg function.
Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6031
  • View blog
  • Posts: 23,414
  • Joined: 23-August 08

Re: average call function

Posted 30 April 2010 - 09:39 AM

Why did you open a new topic when you already had one???

Merged.
Was This Post Helpful? 1
  • +
  • -

#11 nikc121  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 123
  • Joined: 12-April 10

Re: average call function

Posted 30 April 2010 - 09:45 AM

View PostJackOfAllTrades, on 30 April 2010 - 08:39 AM, said:

Why did you open a new topic when you already had one???

Merged.

sorry, new to this. THought since I had different problems I should open new topic.

View Postjapanir, on 30 April 2010 - 08:28 AM, said:

I am not sure, but I believe that is not what your instructor meant.
I believe The instructor wants you to create an avg function, that will get an array of int values, and will calculate inside it the average. then the returned value of the function will be the average of thenumbers in that array.
in other words, instead of calculating the avg in the main, do it inside a function.

as for your code, you have compile errors, but you says the code is working. are these problems while copy\paste the code to the browser?

you miss ";" after declaring the avg
average does not exist in the avg function scope
average = avg(score) //<<missing ";"


also, you calculate the average for every input you get. is that also in purpose?
Maybe there is something wrong with my VS im using, ill retry it. I added the ; behind the average = avg(score) and yes I need the average in a function. I thought that was what int avg (int a[size]) did. Guess Im completly off. I dont understand most of this. Im going to keep trying. Should I add sum+ score[k]; and average = sum/10; after the cin<< score [k], would this fix it, or am I lost. Thanks again for your help.
Was This Post Helpful? 0
  • +
  • -

#12 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,531
  • Joined: 09-June 09

Re: average call function

Posted 30 April 2010 - 09:58 AM

tell me, does this avg function calculate the average of anything?

int avg (int a[size]) 
{ 
        cout<<"The average for all 10 test is"<<average<<endl; 
        return 0; 
}



infact the function does not even compile.

heres an example of what your prof. wants

int avg (int a[], int size) //a[] is the array passed int, size is the number of elements in array
{ 
        int sum = 0;
        for(int i=0; i<size; i++)
            //sum the array here
        
        // return the average, remember that the average is the sum divided by the number of elements
}


This post has been edited by ImaSexy: 30 April 2010 - 02:29 PM

Was This Post Helpful? 1
  • +
  • -

#13 nikc121  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 123
  • Joined: 12-April 10

Re: average call function

Posted 30 April 2010 - 10:17 AM

View PostImaSexy, on 30 April 2010 - 08:58 AM, said:

tell me, does this avg function calculate the average of anything?

int avg (int a[size]) 
{ 
        cout<<"The average for all 10 test is"<<average<<endl; 
        return 0; 
}



infact the function does not even compile.

heres an example of what your prof. wants

int avg (int a[], int size) //a[] is the array passed int, size is the number of elements in array
{ 
        int sum = 0;
        for(int i=0; i<size; i++)
            //sum the array here
        
        // return the average, remember that the average is the num divided by the number of elements
}


I have to confess I am completly lost. I dont know what I am doing. This has been the hardest thing I ever did, I did undercover narcotics....Anyway back on track. Im going ot have to delet my VS and re install, because my program is working, but now it want even open. So I cant even change my above code. Is there anyway that you could maybe highlight what I have right in my code and red mark where I need to make changes. At least that might show me where I am goig wrong. I dont understand when you guys talk about in the main function and passing the array. This was suppose to be a simple program and I actually used one of my instructor examples per her request, and I still cant get it right. Please help anyway you can.
Was This Post Helpful? 0
  • +
  • -

#14 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,531
  • Joined: 09-June 09

Re: average call function

Posted 30 April 2010 - 12:19 PM

what is it that u are confused about?
Was This Post Helpful? 0
  • +
  • -

#15 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: average call function

Posted 30 April 2010 - 12:35 PM

Quote

I have to confess I am completly lost.
That happens to all people starting out. Don't sweat it. The first step is to get back on track, onto familiar ground.

Quote

This has been the hardest thing I ever did,
Can you ride a bike? Or can you draw? Once you get it, you get it. You might be out of practice, but never confused. That's a bit like programming. Once you learn how to think like a programmer, programming is easy. You wonder how you had trouble before. And even if stop for 10 years, going back is simple. Right now, you don't get it. That's OK. At a certain point, you will. Just keep trying. Like someone trying to ride a bike, once you figure it out, it's all fun from that point on.

Quote

Im going ot have to delet my VS and re install,
No, no, this is a very extreme thing to do. You don't need to reinstall.

Quote

Is there anyway that you could maybe highlight what I have right in my code and red mark where I need to make changes.
Maybe. I don't know what your entire code looks like right now. You're not getting anything relating to the average function correct. You are lost right now, so to get back on track, let's go back to the program before the average function is involved.

Can you change your program so that you remove any aspect of the average function? Instead, post your program that takes 10 numbers from the user. And then prints out 0 as the average. 0 isn't the average, but do it because it's really easy. Sort of like building a house, we need to make sure the foundation is correct. If it is, then you can build the average function.

EDIT: To emphasize, post that program here, so we can tell you if it's right or wrong!

This post has been edited by Oler1s: 30 April 2010 - 12:36 PM

Was This Post Helpful? 0
  • +
  • -

  • (8 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »