C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 30th, 2005, 08:59 AM
Ice Man Ice Man is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2005
Posts: 5 Ice Man User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 11 m 54 sec
Reputation Power: 0
Find largest number, average number, and numbers >= 60.

So yeah, working on a problem and can't figure out why the outputs are always zero... Any help would be apprecaited. I may just be doing something really small that I'm missing.

// ProgProb2.cpp : Defines the entry point for the console application.
//
/*
Programming Problem Number: 2
Description: Program to calculate the largest grade, average grade, and how
many grades are above 60.
File Name ProgProb2.cpp
Last Modified: 9/30/05

*/


#include "stdafx.h"
#include<iostream>
using namespace std;
int _tmain()
{
int x,y=(x/x)+1,avg=0,max=x;
cout << "Please input grade, input a negative value to calculate values:";
do
{
cin >> x;
//Find Highest Grade
if (x>max) max=x;
//Find Average of Grades
avg=(avg+x)/2;
//Find Number Of Grades Above 60
if (x>=60) y=(x/x)+1;
while (x>=0);
}
//Not sure why, but all yeilds are 0. I think it has to do with the - number.
cout << "The highest grade is: ";
cout << max;
cout << "\nThe number of grades above 60 is: ";
cout << y;
cout << "\nThe average grade is: ";
cout << avg;
cout << endl << "Press any key to end...";
cin.get();cin.get();

return 0;

Reply With Quote
  #2  
Old September 30th, 2005, 09:22 AM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 4
Send a message via AIM to Geo.Garnett
Well, Im not sure if the calculations are correct yet but I put it in my compiler and fixed a couple of small errors and it worked for me. Like I said though I haven't had time to see if the output is correct yet but here is what I got
Code:
#include <cstdlib>
#include<iostream>
using namespace std;
int main()
{
int x,y=(x/x)+1,avg=0,max=x;
cout << "Please input grade, input a negative value to calculate values:";
do
{
cin >> x;
//Find Highest Grade
if (x>max) max=x;
//Find Average of Grades
avg=(avg+x)/2;
//Find Number Of Grades Above 60
if (x>=60) y=(x/x)+1;
}
while (x>=0);

//Not sure why, but all yeilds are 0. I think it has to do with the - number.
cout << "The highest grade is: ";
cout << max;
cout << "\nThe number of grades above 60 is: ";
cout << y;
cout << "\nThe average grade is: ";
cout << avg;
cout << endl << "Press any key to end...";
cin.get();cin.get();

return 0;
}

run that through your compiler and see what it does for you. I use Dev-C++ and it works for me. Give me a lil bit and Ill try to figure out if the out put is right but so far it looks ok to me.

Reply With Quote
  #3  
Old September 30th, 2005, 09:27 AM
Ice Man Ice Man is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2005
Posts: 5 Ice Man User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 11 m 54 sec
Reputation Power: 0
Well it compiles it and what not, but the data output is always 0, no matter what numbers are put in. I am also initializing all values to 0, and whatever I initialize them to is what they are at the end of the program, it's like the calculations aren't even there.

Reply With Quote
  #4  
Old September 30th, 2005, 09:29 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 996 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 810717 Folding Title: Super Ultimate Folder - Level 2Folding Points: 810717 Folding Title: Super Ultimate Folder - Level 2Folding Points: 810717 Folding Title: Super Ultimate Folder - Level 2Folding Points: 810717 Folding Title: Super Ultimate Folder - Level 2Folding Points: 810717 Folding Title: Super Ultimate Folder - Level 2Folding Points: 810717 Folding Title: Super Ultimate Folder - Level 2Folding Points: 810717 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 14 h 5 m 52 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
Ok, here's your code, with some comments added in places where stuff is seriously wrong:

cpp Code:
Original - cpp Code
  1. #include "stdafx.h"     // Not needed in this program
  2. #include<iostream>
  3. using namespace std;
  4. int _tmain()                        // why the '_t'? proper name is main
  5. {
  6.     int x,y=(x/x)+1,avg=0,max=x;            // using an uninitialized variable to initialise is dangerous.
  7.                             // what if x = 0?
  8.                             // anyway, from the rest of your code I deduct you want y to be the
  9.                             // number of grades above 60, so why not initialise it on 0?
  10.  
  11.     cout << "Please input grade, input a negative value to calculate values:";
  12.     do
  13.     {
  14.         cin >> x;
  15.     //Find Highest Grade
  16.         if (x > max) max = x;
  17.     //Find Average of Grades
  18.         avg=(avg+x)/2;        // This is NOT the proper way to calculate an average!!
  19.                             // what you get here is the average of the current grade and the previous average.
  20.                             // to calculate the real average, you need to keep track of the number of grades and the total sum.
  21.     //Find Number Of Grades Above 60
  22.         if (x>=60) y=(x/x)+1;         // again, the strange x/x, why not just increment y?
  23.  
  24.     while (x>=0);               // also wrong, you want to break your program when someone enters a negative grade.
  25.                             // however, this way the negative grade is also added to the average (and is evaluated for all other expressions.)
  26.         }               // syntactual error here, the closing brace has to go BEFORE the while.
  27.  
  28.     //Not sure why, but all yeilds are 0. I think it has to do with the - number.
  29.         cout << "The highest grade is: ";
  30.         cout << max;
  31.         cout << "\nThe number of grades above 60 is: ";
  32.         cout << y;
  33.         cout << "\nThe average grade is: ";
  34.         cout << avg;
  35.         cout << endl << "Press any key to end...";
  36.         cin.get();cin.get();
  37.    
  38.     return 0;               // I'm also missing the closing brace for main() here, but that's probably just bad copy-pasting.
  39.  

Here a corrected version, works fine.
cpp Code:
Original - cpp Code
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     int x, y = 0 , sum = 0, max = 0, numgrade = 0;
  7.     cout << "Please input grade, input a negative value to calculate values:";
  8.     cin >> x;
  9.  
  10.     while(x >= 0)
  11.     {
  12.         //Find Highest Grade
  13.         if ( x > max)
  14.             max = x;
  15.  
  16.         //Find Average of Grades
  17.         sum += x;
  18.         numgrade++;
  19.  
  20.         //Find Number Of Grades Above 60
  21.         if (x >= 60)
  22.             y++;
  23.  
  24.         // ask new grade:
  25.         cout << "Please input another grade, or input a negative value to calculate values:";
  26.         cin >> x;
  27.  
  28.     }
  29.  
  30.     cout << "The highest grade is: ";
  31.     cout << max;
  32.     cout << "\nThe number of grades above 60 is: ";
  33.     cout << y;
  34.     cout << "\nThe average grade is: ";
  35.     cout << (sum / numgrade);
  36.     cout << endl << "Press any key to end...";
  37.     cin.get();cin.get();
  38.    
  39.     return 0;
  40. }
__________________
This is my code. Is it not nifty?

"The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools."
---Douglas Adams


Join the Itsacon fanclub!    
Zero Tolerance: Spammers banned so far: 275

Reply With Quote
  #5  
Old September 30th, 2005, 09:36 AM
Ice Man Ice Man is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2005
Posts: 5 Ice Man User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 11 m 54 sec
Reputation Power: 0
Hmm, even with that my outputs are still 0... odd.

And a new error, error C1010; unexpected end of file while looking for precompiled director head.

*Shrug* I'll work on it some more and see if I can't solve it.

Reply With Quote
  #6  
Old September 30th, 2005, 09:50 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 996 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 810717 Folding Title: Super Ultimate Folder - Level 2Folding Points: 810717 Folding Title: Super Ultimate Folder - Level 2Folding Points: 810717 Folding Title: Super Ultimate Folder - Level 2Folding Points: 810717 Folding Title: Super Ultimate Folder - Level 2Folding Points: 810717 Folding Title: Super Ultimate Folder - Level 2Folding Points: 810717 Folding Title: Super Ultimate Folder - Level 2Folding Points: 810717 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 14 h 5 m 52 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
hmm, what compiler are you using?

Attached you'll find my grades.cpp file, which compiles without errors on warnings on g++:

g++ -Wall -o grades.exe grades.cpp
Attached Files
File Type: zip grades.zip (493 Bytes, 267 views)

Reply With Quote
  #7  
Old September 30th, 2005, 10:14 AM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 4
Send a message via AIM to Geo.Garnett
Aww Itsacon I wanted this one lol, your just too quick for me.
Well, I guess its better to be late than never huh
Code:
#include <cstdlib>
#include<iostream>
using namespace std;
int main()
{
    float grade[5] = {0.0};//User input grade
    float actG = 0.0;//transfer total grade amount
    float maxG = 0.0;//max grade
    float avg = 0.0;//average
    int ovS = 0;//0ver Sixty
    int i = 1;
    cout<<"Please enter your five current scores\n";
    for (i; i <= 5; i++)
    {
        cout<<i<<" : ";
        cin>>grade[i];
        actG += grade[i];//counts how much the total grades are
        if(grade[i]>=60) ++ovS;//keeps track of how many grades are 60 and above
        if(grade[i]>=51) maxG = grade[i];
        }
    cout<<"Amount of grades over sixty : "<<ovS<<endl;
    avg = actG/5;
    cout<<"Your Average Grade Was : "<<avg<<endl;
    cout<<"Your Top Score Was : "<<maxG<<endl;
    
    cout << endl << "Press any key to end...";
    cin.get();cin.get();
    return 0;
}

Well, there you have it. Mine is a little different, because I had the user enter five grades instead of just one. Probably not what you needed but still calculates them and gives you top score, average and amount over 60. I thought it would look better this way But what do I know
Sorry this only calculates the highest grades over 51 <=NVM the max score is screwed Ill have to redo it lol. It calculates the last highest number


---Official Member Of Itsacon Fan Club---

Reply With Quote
  #8  
Old September 30th, 2005, 12:11 PM
Ice Man Ice Man is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2005
Posts: 5 Ice Man User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 11 m 54 sec
Reputation Power: 0
Microsoft Visual C++

Reply With Quote
  #9  
Old September 30th, 2005, 12:28 PM
Ice Man Ice Man is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2005
Posts: 5 Ice Man User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 11 m 54 sec
Reputation Power: 0
Here's what I've got.

// Prog2.cpp : Defines the entry point for the console application.

//

/*

Description: Program to calculate the largest grade, average grade, and how

many grades are above 60.

File Name ProgProb2.cpp

Last Modified: 9/30/05

*/

#include <stdlib>

#include<iostream>

usingnamespace std;

int main()

{
//Define Variables

int x, y = 0 , sum = 0, max = 0, numgrade = 0;

// Ask For Variables

cout << "Please input grade, input a negative value to calculate values:";

cin >> x;

//Loop To Get More Data

do

{

//Find Highest Grade

if ( x > max)

max = x;

//Find Average of Grades

sum += x;

numgrade++;

//Find Number Of Grades Above 60

if (x >= 60)

y++;

// Ask New Grade:

cin >> x;

}

while(x >= 0)

//Display Calculated Values

cout << "The highest grade is: ";

cout << max;

cout << "\nThe number of grades above 60 is: ";

cout << y;

cout << "\nThe average grade is: ";

cout << (sum / numgrade);

cout << endl << "Press any key to end...";

cin.get();cin.get();

return 0;

}


Outputs, all 0.

Error: 1010, unexpected end of file while looking for precompiled header directive.


DOH! Forgot to include #include <stdafx.h> for my particular program, lol! Thanks everyone.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Find largest number, average number, and numbers >= 60.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Linear Mode Linear Mode