average call function

need to define my average funtion then call it from main

  • (8 Pages)
  • +
  • « First
  • 5
  • 6
  • 7
  • 8

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

#91 nikc121  Icon User is offline

  • D.I.C Head

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

Re: average call function

Posted 01 May 2010 - 07:53 PM

View PostImaSexy, on 01 May 2010 - 06:45 PM, said:

click here :
http://www.learncpp....-and-arguments/

i want you to read each section, and i mean really read it. After you will have a full understanding and you will probably be kicking yourself. I myself did not even take a c++ class but this learncpp.com taught me everything (somewhat) that i know today

I promise If I can get thru tonight, I will print the entire thing and read it in and out....Ive only got one hour left tonight to finish my program. Did you get my email?
Was This Post Helpful? 0
  • +
  • -

#92 nikc121  Icon User is offline

  • D.I.C Head

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

Re: average call function

Posted 02 May 2010 - 07:08 PM

Yes Im back.. Got an extension...I have my code up to par, I think. Did alot of reading on functions and arrays last night. This latest code is working ok, but only prints the average of the last number I enter. ANd I think I am missing average = sum/10 cout<<Average:"<<average;/ Im just not sure where it goes exactly. Again thanks to all the patients...You guys are way better than my instructor 3 days and no replies from her.
#include <iostream> 
using namespace std; 
 
const int NumScores = 10; 
 
int main () 
{   
    int score[NumScores];  

    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; 
		//system ("pause");
    } 
	int sum = 0;
	for(int i = 0; i<NumScores; i++)
	sum = sum + score[i];
	return sum/10;
}

Was This Post Helpful? 0
  • +
  • -

#93 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: average call function

Posted 02 May 2010 - 07:44 PM

int main() is the entry point of your program, when main hits its return value the program ends and the return value is sent to the Operating System. dont return the value out of main, just output the value

#include <iostream> 
using namespace std; 

const int NumScores = 10; 

int main () 
{ 
	int score[NumScores]; 

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

	} 
	int sum = 0;
	for(int i = 0; i<NumScores; i++)
		sum = sum + score[i];
	cout<<"Average : "<<sum/NumScores<<endl;

	//pause the window before closing
	cin.ignore();
	cin.get();
	return 0; //always return 0 from main
}


This post has been edited by ImaSexy: 02 May 2010 - 07:45 PM

Was This Post Helpful? 1
  • +
  • -

#94 nikc121  Icon User is offline

  • D.I.C Head

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

Re: average call function

Posted 02 May 2010 - 08:10 PM

HI, Ive never used cin.ignor or cin.get. I changed the code but now have 5 errors. Error 5 error C2001: newline in constant c:\documents and settings\my documents\visual studio 2008\projects\arraycall3\arraycall3\arraycall3.cpp 24
Error 4 error C2143: syntax error : missing ';' before ':' c:\documents and settings\my documents\visual studio 2008\projects\arraycall3\arraycall3\arraycall3.cpp 24
Error 2 error C2065: 'Average' : undeclared identifier c:\documents and settings\my documents\visual studio 2008\projects\arraycall3\arraycall3\arraycall3.cpp 24
Error 1 error C2065: 'i' : undeclared identifier c:\documents and settings\my documents\visual studio 2008\projects\arraycall3\arraycall3\arraycall3.cpp 23

I cant win for losing.
Was This Post Helpful? 0
  • +
  • -

#95 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: average call function

Posted 02 May 2010 - 08:17 PM

you need to learn how to recognize compiler errors.

I havent even see the code but i will tell you what they are

error 1: your missing a semi colon somewhere
error 2: your using a variable that has not been declared yet
error 3: you have not declared the variable i
Was This Post Helpful? 1
  • +
  • -

#96 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,671
  • Joined: 23-August 08

Re: average call function

Posted 02 May 2010 - 08:17 PM

You STILL don't have a function!!! Hell-oooooo!?!?!
Was This Post Helpful? 0
  • +
  • -

#97 nikc121  Icon User is offline

  • D.I.C Head

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

Re: average call function

Posted 02 May 2010 - 08:18 PM

View PostJackOfAllTrades, on 02 May 2010 - 07:17 PM, said:

You STILL don't have a function!!! Hell-oooooo!?!?!

Dont worry, This is my first and last C++ class, after tonight youll never here from me. I just have to get this thing to run before midnight and then youll be done with me.
Was This Post Helpful? 0
  • +
  • -

#98 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: average call function

Posted 02 May 2010 - 08:38 PM

hes not saying to never return to DIC, were here to help you. Hes just pointing out that your assignment ask for your to use a function :)
Was This Post Helpful? 1
  • +
  • -

#99 nikc121  Icon User is offline

  • D.I.C Head

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

Re: average call function

Posted 02 May 2010 - 08:41 PM

View PostImaSexy, on 02 May 2010 - 07:38 PM, said:

hes not saying to never return to DIC, were here to help you. Hes just pointing out that your assignment ask for your to use a function :)

Yea I know, Im not taking offense. By the way I found the errors, It did build but Im getting the error message when I debug that MSVCP90d.dll was not found. I ran into this before and ended up having to reinstall VS. So not sure what the output looks like, Im assuming its good..Since you did it....Im turning it in now..Wish me luck....
Was This Post Helpful? 0
  • +
  • -

#100 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: average call function

Posted 02 May 2010 - 08:54 PM

i thought you need to use a function? and dont just assume its good just because i did it, you need to understand how basic arrays work
Was This Post Helpful? 0
  • +
  • -

#101 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 79
  • View blog
  • Posts: 1,366
  • Joined: 28-March 09

Re: average call function

Posted 02 May 2010 - 11:15 PM

If you come back...
#include <iostream>

//declare a function prototype
double avg(int a[])

int main()
{
//your codes to fill the array
total = avg(a[arrName]);
cout << "The avg of all the numbers is: " << total << endl;
return 0;
}

double avg(int a[arrName])
{
  //calculat the average
return (/*value of all numbers added than divided by total elements*/)
}


Was This Post Helpful? 0
  • +
  • -

#102 nikc121  Icon User is offline

  • D.I.C Head

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

Re: average call function

Posted 03 May 2010 - 08:11 AM

View PostImaSexy, on 02 May 2010 - 07:54 PM, said:

i thought you need to use a function? and dont just assume its good just because i did it, you need to understand how basic arrays work

Your right. Instructor sent exam back says I need to use a funciton. I know the funtion is double avg (int a[]). If you look as some of my previous post I was using it, just in the wrong places. Now i just need to figure out after I put that above int main () what else I have to add.
Was This Post Helpful? 0
  • +
  • -

#103 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 841
  • View blog
  • Posts: 2,472
  • Joined: 20-March 10

Re: average call function

Posted 03 May 2010 - 09:43 AM

I think your instructor is looking for something like this


#include <iostream>
using namespace std;

const int NumScores = 10;
int AverageScore (const int NumScores, int score[]);


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];

        }
        Average = AverageScore(NumScores, score);
        cout<<"Average Score : "<<Average<<endl;

        //pause the window before closing
        cin.ignore();
        cin.get();
        return 0; //always return 0 from main
}


//***********************
//*function AverageScore*
//***********************
int AverageScore (const int NumScores, int score[])
{
int Average;
int sum = 0;
        for(int i = 0; i<NumScores; i++)
                sum = sum + score[i];
Average = sum/NumScores;

return Average;
}









but you need to read up on functions and how to call them.
Was This Post Helpful? -2
  • +
  • -

#104 Kohana  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 56
  • Joined: 09-January 10

Re: average call function

Posted 03 May 2010 - 10:17 AM

Snoopy you have taken my code and filled in the function for him. The objective is not to provide code to these people but help them understand. Never mind, there goes another computer scientist who will probably end up with a degree he doesn't understand.
Was This Post Helpful? 1
  • +
  • -

#105 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: average call function

Posted 03 May 2010 - 10:29 AM

Seriously its not a hard concept if you just read the material that i post for you. And i know you did not read the last material i posted becuase theres not even a funciton in your code.

start here and read each section
http://www.learncpp....k-at-functions/

Then read the part on arrays, if you dont understand any of this then i feel there is no chance that you will learn object oriented programming, which is key in c++

This post has been edited by ImaSexy: 03 May 2010 - 10:30 AM

Was This Post Helpful? 0
  • +
  • -

  • (8 Pages)
  • +
  • « First
  • 5
  • 6
  • 7
  • 8