C++ HW Help

Started by akanana1203, April 05, 2005, 10:14:43 PM

Previous topic - Next topic
Hello I am in a C++ class, and I am so lost. The beginning of the semester I was ok, now I don't understand anything. The problem we have to do is:
        To calculate the GPA of a student, we divide the total quality points earned by the total number of hours attempted. Develop a program that reads such totals and computes the GPA. Repeatedly request total hours until the input is between 0 and 200 inclusively. Repeatedly request total quality points until the input is between 0 and 4 times the total hors inclusively.
If anyone could help me please, I would really appreciate it. Thanx ???

Do you need help with the code or understanding the problem ? if it's the problem itself, I'd suggest you ask your teacher/professor, because then you'd get exactly what they want. I could take a guess but I'm not the head of your class.

If after talking to your teacher and you still don't understand, feel free to ask (though I wouldn't be surprised if deciphering the question would be the hardest part).

Either way, you'd probably want to use a do while loop so you always enter it at least once (of course I could be off since I don't know the question in its full explanation).

"My Terminal is my Soul"

Hello I have spoken to my professor, still don't understand the problem. I do know that I have to use a do while loop because thats where we are at in the semester, but I think I really just don't understand the question. If you could try to shed some light on the problem I would appreciate it. I do know how to start the problem just don't know how to finish it.

Well..

What parts do you have done ?

I could help you if I understood the question. Maybe you could tell us what the professor said when trying to clarify it ?

*mutters something about people wording things more complex than they have to be for fun*

"My Terminal is my Soul"

Code  cpp Select
int main()
{
   int readGrade();
   int getHours(int num);
   int num;
   num = readGrade();
   cout<<"The GPA of a student is "<<num<<getHours(num)   <<"."<<endl;
   return 0;
}

int readGrade()
{
   int num;
   cout<<"This program calculates the GPA of a student"<<endl;
   do
{  cout<<"enter a grade thats bewtween 0 and 4 times the hours: ";
   cin>>num;
}
   while(num>=0) && (num<=4);
   return num;
}

int getHours(int num)
{
   int hours
That is all I have but can't understand how to finish it, if this part is even right


Well, from what I remember in C++ (I use C only but I remember cout and cin stuff) you seem to have the right idea. But my thing is that I don't know if I understand the question (and I don't want to lead you the wrong way).

I do see something though...

I can see (and maybe I'm not looking correctly right now)  that you are asking for a number, and not calculating until a certain number is entered. Of course I don't know if that's what the prof. wants, but I had thought (from what I did [or thought I did] understand) that you calculate each time until the person enters a certain number. But mabye I'm wrong there.

Cause I can see this happening with the loop you have there:

> enter a grade thats bewtween 0 and 4 times the hours: 1
> enter a grade thats bewtween 0 and 4 times the hours: 2
> enter a grade thats bewtween 0 and 4 times the hours: 3
> enter a grade thats bewtween 0 and 4 times the hours: 4
> enter a grade thats bewtween 0 and 4 times the hours: 4

until a user enters 15... or something.

What did the professor say about it when you asked him to explain ? Maybe that can help me help you.

Btw, probably won't be able to reply for some hours after another hour as I have a doctor appointment and then I have to come home and eat. But I'll see if you have replied again if I can before I head off for the night.
"My Terminal is my Soul"

You are right, the process that you have is absolutly correct. That was the way the teacher was trying to explain to me. I understand that part, but what Im not understanding is how I should go about getting the output to that part. Im not understanding how I finish the program that I have already to show an output that looks like the one you have and an output that ask for the total points. And then calculates it. But yes you do not calculate until a correct hour is entered and correct points are entered. Thanz for all the help you have given me so far, I do appreciate it.

Okay...

Are you saying you're having difficulty with the flow of the program ?

And do you have to do multiple calculations (as in, you get all the numbers, do the first calculation and then start over to get another one until the user quits ?) Or is it just a one time thing ?

Either way, you can pass the integers to the function and use it as the passed value. Then just create a loop (or more than one if need be).

If you have to do the several calculations, you could put a loop in the main function that loops the call to the other functions.

I hope I'm being some help.... But if I still don't have your question down, feel free to try again (I hope I have it though... if the flow of the program is the problem, then maybe it'd help to write it down -- like an outline ?) Then try to convert it to code ?

Just a thought. Well, let me know.

"My Terminal is my Soul"

Re-read your initial post.. just to clarify something if I had the right idea in my most recent post..

You can always nest functions and loops. In fact, you could even define a function inside a function (but that's probably not used very often and it could get ugly).

But let's say you have the functions func1, and func2. Then you want func2 to do a calculation and return the results.

You could do something like (some pseudo-code here)...

Code  cpp Select
int main(void)
{
  int num;
do {
  num = func1();
 } while (some condition);

}

int func1(void)
{ 
  ....
  return (some result);
}


and the some condition could include a variable in the loop itself.

But maybe that's not what you're after. Just let me know and I'll try to be of more help.
"My Terminal is my Soul"

I kind of understand what you are saying, but I think its more the flow of the problem. We only have to do one calculation and then end the program unless the user enters an invalid number, which then asks for a correct number. I've written it down, which was the part that I sent earlier, but as for the rest, thats where the trouble is.

Okay, trying again. If you only need one calculation... and I understand everything else correctly, then something like this (mind you, I'm not putting the number checks in as I'm not sure I understand them 100% and that can be your exercise -- unless that's what you're having difficulty with):


Code  cpp Select
int main()
{
  int readGrade();
  int getHours(int num);
  int num, hours;
  num = readGrade();
  hours = getHours(num);

  /* print the results (probably hours as that's the last calculation I think) */

  return 0;
}

int readGrade()
{
  int num;
  do
  {  
  /* prompt the user.. */
  }
  while ((num>=0) && (num<=4)); /* see point one below */
  return num;
}

int getHours(int num)
{
  /* do the calculation stuff here  in a loop */
  return answer;
}


Okay, the point one.. you had...

while (num >= 0) && (num <= 4);

I changed it to while ((num >= 0) && (num <= 4));

why ? I'm pretty sure that would throw up an error (you're using a while loop but then you close it off after the first ) and there is no semi colon but instead a && and so on. I would be surprised if you didn't get a compiler error actually.

Anyway, let me know if this helped, or if you need something more.


"My Terminal is my Soul"

Ok, I think Im understanding you and from what I see you are saying is what I have here:
Code  cpp Select
int main()
{
  int readGrade();
  int getHours(int num);
  int num, hours;
  num = readGrade();
  hours = getHours(num);
  cout<<"The GPA of a student is"<<num<<getHours(num)<<"."<<endl;
  return 0;
}

int readGrade()
{
  int num;
  do
  {  
  cout<<.....<<endl;
  cin>>num;
  }
  while ((num>=0) && (num<=4)); 
  return num;
}

int getHours(int num)
{
  int num;
  do
 {
    cout<<.....<<endl;
    cin>>num;
 }
  while ((num>=0) && (num<=200));
  return num;
}
So when calculating the GPA which is [grade/hours] where would that go, would that be its own function or would it go under the function int getHours? Should it be where the return is under getHours? (e.g. return num / hours;)

It could be in it's own function. I always try to go for the modular approach, but then for something simple it wouldn't really matter. Still though, good habit to get in to is making clean code.

I am guessing in C++ you can print calculations (e.g., inyour case the num / hours) but you could also just put it in a variable and print that variable.

So there are different ways (if I am understanding you right):

two examples:

declare three ints.. one for first calc, another for the second, and the last one for the third (Final answer) and then print the last one.

or

you could calculate the two with seperate functions and then just print the function result (like you had similar earlier)

and others too I'm sure.

If it were me I'd probably not do something like..

int final;

...
..
final = calcGpa(num);

and then print it out (unless I had to use that later on). I guess it just depends on the situation though (because I at times would do it the way I said I wouldn't).

Does that help ?
"My Terminal is my Soul"

Ok I think I understand completely now, but I could still be wrong. Let me know what you think.
Code  cpp Select
int main()
{
  int readGrade();
  int getHours(int num);
  float calGPA(int, int);
  int num, hours;
  grade = readGrade();
  hours = getHours(num);
  GPA = calGPA(grade, hours);
  cout<<"The GPA of a student is"<<GPA<<"."<<endl;
  return 0;
}

int readGrade()
{
  int grade;
  cout<<"This program calculates......."<<endl;
  do
  {  
  cout<<"Enter a grade....."<<endl;
  cin>>grade;
  }
  while ((grade>=0 * hours) && (num<=4 * hours)); 
  return grade;
}

int getHours(int num)
{
  int hours;
  do
{
    cout<<"Enter total hours. Between 0 & 200"<<endl;
    cin>>hours;
}
  while ((hours>=0) && (hours<=200));
  return hours;
}

float calGPA(int grade, int hours)
{
   return grade/hours;
}

Well assuming the question is answered (or the problem is solved I guess I should say), then you have the idea.

Just make sure you have GPA declared before you try to use it. (I don't see any variable GPA made before you try to use it).

since you assign it a value of a function returning float, just declare it as a type float. So I think that's it. I would of course test it out and see if it works okay, but that's the idea.

Hope I was of help.

"My Terminal is my Soul"

SMF spam blocked by CleanTalk