April 15, 2021, 04:29:07 AM

Author Topic: C++ HW Help  (Read 18125 times)

0 Members and 1 Guest are viewing this topic.

akanana1203

  • Guest
C++ HW Help
« on: April 05, 2005, 10:14:43 PM »
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 ???

Offline Metgod

  • the deranged hacker
  • Administrator
  • Forum Hero
  • *****
  • Posts: 1116
  • Country:
  • Gender: Male
Re:C++ HW Help
« Reply #1 on: April 06, 2005, 11:34:37 AM »
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"

akanana1203

  • Guest
Re:C++ HW Help
« Reply #2 on: April 06, 2005, 12:17:54 PM »
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.

Offline Metgod

  • the deranged hacker
  • Administrator
  • Forum Hero
  • *****
  • Posts: 1116
  • Country:
  • Gender: Male
Re:C++ HW Help
« Reply #3 on: April 06, 2005, 03:53:13 PM »
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"

akanana1203

  • Guest
Re:C++ HW Help
« Reply #4 on: April 06, 2005, 04:58:46 PM »
Code: C++
  1. int main()
  2. {
  3.    int readGrade();
  4.    int getHours(int num);
  5.    int num;
  6.    num = readGrade();
  7.    cout<<"The GPA of a student is "<<num<<getHours(num)   <<"."<<endl;
  8.    return 0;
  9. }
  10.  
  11. int readGrade()
  12. {
  13.    int num;
  14.    cout<<"This program calculates the GPA of a student"<<endl;
  15.    do
  16. {  cout<<"enter a grade thats bewtween 0 and 4 times the hours: ";
  17.    cin>>num;
  18. }
  19.    while(num>=0) && (num<=4);
  20.    return num;
  21. }
  22.  
  23. int getHours(int num)
  24. {
  25.    int hours
  26.  
  27.  
That is all I have but can't understand how to finish it, if this part is even right


Offline Metgod

  • the deranged hacker
  • Administrator
  • Forum Hero
  • *****
  • Posts: 1116
  • Country:
  • Gender: Male
Re:C++ HW Help
« Reply #5 on: April 06, 2005, 06:14:28 PM »
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"

akanana1203

  • Guest
Re:C++ HW Help
« Reply #6 on: April 06, 2005, 07:40:50 PM »
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.

Offline Metgod

  • the deranged hacker
  • Administrator
  • Forum Hero
  • *****
  • Posts: 1116
  • Country:
  • Gender: Male
Re:C++ HW Help
« Reply #7 on: April 06, 2005, 10:11:38 PM »
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"

Offline Metgod

  • the deranged hacker
  • Administrator
  • Forum Hero
  • *****
  • Posts: 1116
  • Country:
  • Gender: Male
Re:C++ HW Help
« Reply #8 on: April 06, 2005, 10:20:22 PM »
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: C++
  1.  
  2. int main(void)
  3. {
  4.   int num;
  5. do {
  6.   num = func1();
  7.  } while (some condition);
  8.  
  9. }
  10.  
  11. int func1(void)
  12. {
  13.   ....
  14.   return (some result);
  15. }
  16.  
  17.  

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"

akanana1203

  • Guest
Re:C++ HW Help
« Reply #9 on: April 07, 2005, 01:38:01 AM »
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.

Offline Metgod

  • the deranged hacker
  • Administrator
  • Forum Hero
  • *****
  • Posts: 1116
  • Country:
  • Gender: Male
Re:C++ HW Help
« Reply #10 on: April 07, 2005, 11:06:15 AM »
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: C++
  1. int main()
  2. {
  3.   int readGrade();
  4.   int getHours(int num);
  5.   int num, hours;
  6.   num = readGrade();
  7.   hours = getHours(num);
  8.  
  9.   /* print the results (probably hours as that's the last calculation I think) */
  10.  
  11.   return 0;
  12. }
  13.  
  14. int readGrade()
  15. {
  16.   int num;
  17.   do
  18.   {  
  19.   /* prompt the user.. */
  20.   }
  21.   while ((num>=0) && (num<=4)); /* see point one below */
  22.   return num;
  23. }
  24.  
  25. int getHours(int num)
  26. {
  27.   /* do the calculation stuff here  in a loop */
  28.   return answer;
  29. }
  30.  

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"

akanana1203

  • Guest
Re:C++ HW Help
« Reply #11 on: April 07, 2005, 11:46:22 AM »
Ok, I think Im understanding you and from what I see you are saying is what I have here:
Code: C++
  1. int main()
  2. {
  3.   int readGrade();
  4.   int getHours(int num);
  5.   int num, hours;
  6.   num = readGrade();
  7.   hours = getHours(num);
  8.   cout<<"The GPA of a student is"<<num<<getHours(num)<<"."<<endl;
  9.   return 0;
  10. }
  11.  
  12. int readGrade()
  13. {
  14.   int num;
  15.   do
  16.   {  
  17.   cout<<.....<<endl;
  18.   cin>>num;
  19.   }
  20.   while ((num>=0) && (num<=4));
  21.   return num;
  22. }
  23.  
  24. int getHours(int num)
  25. {
  26.   int num;
  27.   do
  28.  {
  29.     cout<<.....<<endl;
  30.     cin>>num;
  31.  }
  32.   while ((num>=0) && (num<=200));
  33.   return num;
  34. }
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;)

Offline Metgod

  • the deranged hacker
  • Administrator
  • Forum Hero
  • *****
  • Posts: 1116
  • Country:
  • Gender: Male
Re:C++ HW Help
« Reply #12 on: April 07, 2005, 12:20:43 PM »
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"

akanana1203

  • Guest
Re:C++ HW Help
« Reply #13 on: April 07, 2005, 04:07:55 PM »
Ok I think I understand completely now, but I could still be wrong. Let me know what you think.
Code: C++
  1. int main()
  2. {
  3.   int readGrade();
  4.   int getHours(int num);
  5.   float calGPA(int, int);
  6.   int num, hours;
  7.   grade = readGrade();
  8.   hours = getHours(num);
  9.   GPA = calGPA(grade, hours);
  10.   cout<<"The GPA of a student is"<<GPA<<"."<<endl;
  11.   return 0;
  12. }
  13.  
  14. int readGrade()
  15. {
  16.   int grade;
  17.   cout<<"This program calculates......."<<endl;
  18.   do
  19.   {  
  20.   cout<<"Enter a grade....."<<endl;
  21.   cin>>grade;
  22.   }
  23.   while ((grade>=0 * hours) && (num<=4 * hours));
  24.   return grade;
  25. }
  26.  
  27. int getHours(int num)
  28. {
  29.   int hours;
  30.   do
  31. {
  32.     cout<<"Enter total hours. Between 0 & 200"<<endl;
  33.     cin>>hours;
  34. }
  35.   while ((hours>=0) && (hours<=200));
  36.   return hours;
  37. }
  38.  
  39. float calGPA(int grade, int hours)
  40. {
  41.    return grade/hours;
  42. }

Offline Metgod

  • the deranged hacker
  • Administrator
  • Forum Hero
  • *****
  • Posts: 1116
  • Country:
  • Gender: Male
Re:C++ HW Help
« Reply #14 on: April 07, 2005, 05:53:22 PM »
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"