GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Oct-2008, 00:47
8100 Power 8100 Power is offline
New Member
 
Join Date: Sep 2008
Posts: 24
8100 Power is on a distinguished road

Implementing a function into into my program?


Hi, I've wrote my grade averaging program. But, my teacher is asking us to use functions and have the function return the average instead of the cout. This is what I have so far:

CPP / C++ / C Code:
#include <iostream>
using namespace std;


int main(){
    
    int total=0;
    int gradeCounter=1;
    int grade, maxgrades;
    int average;


    cout << "How many Grades do you want to enter?" << endl;
    cin >> maxgrades;

    cout << "Enter " << maxgrades << " grades" << endl;

    while (gradeCounter <= maxgrades){
          cout << "Enter Grade: ";
          cin >> grade;
          total = total + grade;
          gradeCounter = gradeCounter + 1;
          average = total / maxgrades;
    }
    
    
    cout << "Average Grades: " << average << endl;
    
    
    system("PAUSE");
    return 0;
    
}

Functions is new to me, as well as C++.
  #2  
Old 11-Oct-2008, 01:19
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Implementing a function into into my program? Help!


Quote:
Originally Posted by 8100 Power
Hi, I've wrote my grade averaging program. But, my teacher is asking us to use functions and have the function return the average instead of the cout.
You haven't asked a question, but I assume that you are wanting assistance in creating a function. If this is correct, study the following example:
CPP / C++ / C Code:
#include <iostream>

using namespace std;

int f(int);

int main() {
    int i = 0, j = 1;

    cout << "i (main()) =  " << i << endl;
    cout << "j (main()) =  " << j << endl;

    i = f(j);

    cout << "i (main()) =  " << i << endl;
    cout << "j (main()) =  " << j << endl;

    return 0;
}

int f(int k) {
    int v = 0;

    cout << "\tk (f()) =  " << k << endl;
    cout << "\tv (f()) =  " << v << endl;

    v = ++k;

    cout << "\tk (f()) =  " << k << endl;
    cout << "\tv (f()) =  " << v << endl;

    return v;
}
Various comments:
  • Compile & execute this example. Study the output of execution & compare it to the source code to understand then intended behaviour.
  • Study the syntax. A function prototype is provided as well.
  • Recognize that main() is a function as well.
If you still have questions, ask specifically about what is still unclear.
  #3  
Old 11-Oct-2008, 11:53
8100 Power 8100 Power is offline
New Member
 
Join Date: Sep 2008
Posts: 24
8100 Power is on a distinguished road

Re: Implementing a function into into my program?


I guess what i'm saying is. I'm trying to implement all of my "math" into the function, and then call it in my main function. I've correctly called functions, but, I don't understand this one. I don't know what to call since I'm averaging all the grades and outputting the average.
  #4  
Old 11-Oct-2008, 13:27
8100 Power 8100 Power is offline
New Member
 
Join Date: Sep 2008
Posts: 24
8100 Power is on a distinguished road

Re: Implementing a function into into my program?


This is what I have. But, I have a continuous loop, it isn't stopping..

CPP / C++ / C Code:
#include <iostream>
using namespace std;

double gradeAvg(double);

int main(){
    
    int total=0;
    int gradeCounter=1;
    double grade; 
    int maxgrades;
    int average;


    cout << "How many Grades do you want to enter?" << endl;
    cin >> maxgrades;

    cout << "Enter " << maxgrades << " grades" << endl;

    while (gradeCounter <= maxgrades){
          cout << "Enter Grade: ";
          cin >> grade;
          double gradeAvg( average);
    }
    
    
    cout << "Average Grades: " << average << endl;
    
    
    system("PAUSE");
    return 0;
    
}

double gradeAvg ( double average){
  int total, gradeCounter, maxgrades;
  double grade;


    total = total + grade;
    gradeCounter = gradeCounter + 1;
    average = total / maxgrades;

    cout << "Average Grades: " << average << endl;

return average;
}

  #5  
Old 11-Oct-2008, 17:03
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 802
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Implementing a function into into my program?


CPP / C++ / C Code:
while (gradeCounter <= maxgrades)
  {
    /* Put a printf() and a getchar() inside of this loop to print the values 
     of all variables concerned.  Maybe a if(gradeCounter  > 50) break; too */
  }
  #6  
Old 11-Oct-2008, 17:24
dlp dlp is offline
Member
 
Join Date: May 2006
Posts: 157
dlp has a spectacular aura about

Re: Implementing a function into into my program?


1. Why should gradeCounter ever become more than maxgrades? You never change the value.

2. I don't think your instructor is being clear about your requirements for the function. If you never call cout/cin in the function gradeAvg, you will only average one value unless you:
a) do fun math by making an average, then undoing the average and recalculating with another value.
b) Pass a bunch of numbers into gradeAvg function, which means you have to know how to use std::list or std::vector, which seems a bit above the scope of this program.

Either we or you need more details about your assignment to help you out.
  #7  
Old 11-Oct-2008, 18:49
8100 Power 8100 Power is offline
New Member
 
Join Date: Sep 2008
Posts: 24
8100 Power is on a distinguished road

Re: Implementing a function into into my program?


Quote:
Originally Posted by dlp
1. Why should gradeCounter ever become more than maxgrades? You never change the value.

2. I don't think your instructor is being clear about your requirements for the function. If you never call cout/cin in the function gradeAvg, you will only average one value unless you:
a) do fun math by making an average, then undoing the average and recalculating with another value.
b) Pass a bunch of numbers into gradeAvg function, which means you have to know how to use std::list or std::vector, which seems a bit above the scope of this program.

Either we or you need more details about your assignment to help you out.

1. That's my loop control variable.

2. I totally agree on the instructors assignment. She hasn't be clear since day one!

a) ?? Don't really understand.
b) We haven't learned that yet..

Instructions as I see it:
" Modify your get average program so that it returns the function rather than cout."
  #8  
Old 11-Oct-2008, 19:48
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 802
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Implementing a function into into my program?


Did you fix the loop yet? You need to have that working correctly first.
CPP / C++ / C Code:
    while (gradeCounter++ < maxgrades)
    {
        cout <<"gradeCounter= "<< gradeCounter <<"  maxgrades= "<< maxgrades << endl;
You may want to consider the versatility of a 'for' loop there.

Quote:
Modify your get average program so that it returns the function rather than cout.
You mean 'returns the average rather than printing it from the function' don't you?
Do you need to store the grades which the users inputs?
If so you'll need to assign them to an array or something as they are entered within the loop.

Regardless, you wouldn't call the average() function until you have:
- the total of all the values
- total number of input values
So take the function call out of the loop, and instead add up the values as they are input within the loop ,
and then call a slightly different function than you presently have (which is close)
CPP / C++ / C Code:
double average(double total, int num)
Don't forget you will need to catch that returned value in main() in order to print it from there...
Last edited by Howard_L : 11-Oct-2008 at 20:35.
  #9  
Old 11-Oct-2008, 22:39
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Implementing a function into into my program?


Quote:
Originally Posted by 8100 Power
CPP / C++ / C Code:
double gradeAvg( average);
You are calling the function, but you are not retaining what its return value. Do so through something similar to the following:
CPP / C++ / C Code:
double value = grade(average);
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
run script command on ns2.26 newbie06 Computer Software Forum - Linux 65 19-Aug-2009 08:50
Equation solver RazoR C Programming Language 3 18-May-2008 10:24
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
Need Help with input files. Efferus C++ Forum 2 24-Nov-2007 17:19
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 14:12

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:24.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.