Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 5th, 2006, 7:53 AM   #1
jack999
Newbie
 
Join Date: Apr 2006
Posts: 5
Rep Power: 0 jack999 is on a distinguished road
Help

hi guys



i'm a beginner in c++ and i have H.W to submit but i need some help

this is the question

Write a C-Program that reads five grades (Homework, Quizzes, Major1, Major2,
Final) for a number of students n (determined by the user).
o For each student
1- Calculates the total grade x: (Sum of the five grades)
2- Assign a letter grade (A+,A, B+, B, C+, C, D+, D, F) according to
the following rules :
Total Grade Letter Grade
95<= x <100 A+
90<= x < 94 A
85<= x < 89 B+
80<= x < 84 B
75<= x < 79 C+
70<= x <74 C
65<= x <69 D+
60<= x < 64 D
x<60 F
Print the total grade x and the letter grades
3- Calculate and print the number of students with each letter grade, the
format of the output must be like :
Letter Grade Number of students
------------------ --------------------------
A+ --
A -
B+ -
.
.
4- Calculate and print the max Total grade and the minimum Total grade


i solved #1 but there is a problem
i can solve 2,4 but 3 i need someone to explain it
#include<stdio.h>
#include<math.h>
main()
{
int n,i  ;
double x,hw,q,m1,m2,f,sum  ;
sum=0   ;
printf("enter number of students:");
scanf("%d",&n);
printf("enter the grade of hw,q,m1,m2 and f:");
scanf("%lf %lf %lf %lf %lf",&hw,&q,&m1,&m2,&f);

for(i=1,i<=n,i++)
{
scanf("%lf",&x);
sum+=x ;
}
x= hw+q+m1+m2+f      ;
scanf("%lf",&x);
printf("x=%lf",x);
}

Last edited by Pizentios; Apr 5th, 2006 at 9:36 AM.
jack999 is offline   Reply With Quote
Old Apr 5th, 2006, 8:02 AM   #2
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
couldn't you just do something like

for(int i = 0; i < maxstudents; i++) std::cout << x << n << std::endl;
Cleaner of course. Use your imagination to make a table or something.
__________________

Quote:
Originally Posted by Mohamed Jihad
Durka durka!
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it.

Download Code::Blocks now!
jayme is offline   Reply With Quote
Old Apr 5th, 2006, 8:10 AM   #3
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 242
Rep Power: 3 Seif is on a distinguished road
Make your Code more readable by structuring it properly, and put it in code tags, and your much more likely to get some help, also this maybe best suited for the C section rather than C++.

I can't help you with your code as I'm not too hot on printf or scanf as its more for C... that and also the format of your code is making my eyes bleed.
Seif is offline   Reply With Quote
Old Apr 5th, 2006, 8:30 AM   #4
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Hi. Your post should be in the C forum
Making your code look like this makes it much easier to read:
#include <stdio.h>
#include <math.h>
main()
{
	int n, i;
	double x, hw, q, m1, m2, f, sum;
	sum = 0;

	printf("Enter number of students: ");
	scanf("%d", &n);

	printf("Enter the grade of Homework, Quizzes, Major1, Major2 and Final: ");
	scanf("%lf %lf %lf %lf %lf", &hw, &q, &m1, &m2, &f);

	for(i = 1; i <= n; i++)
	{
		scanf("%lf",&x);
		sum+=x ;
	}
	x = hw + q + m1 + m2 + f;
	scanf("%lf",&x);
	printf("x = %lf\n",x);
}

Please read the "How to post a question" thread at the top of the C or C++ forum. And restructure your question so it looks readable.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Apr 5th, 2006, 9:24 AM   #5
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
If you want to impress your teacher (or maybe not...), you can do this for your first part:
#include <stdio.h>
#include <math.h>

int main()
{
	int number = 0, current;
	double sum;

	char *gradenames[] =
	{
		"Homework", "Quizzes", "Major1", "Major2", "Final"
	};

	double grades[] =
	{
		0, 0, 0, 0, 0
	};

	printf("Enter number of students: ");
	if(scanf("%d", &number) != 1)
	{
		printf("IDIOT!\n");
		return 1;
	}

	for(current = 0; current < number; current++)
	{
		int t;
		sum = 0;
		printf("\nStudent #%d\n", current+1);
		for(t = 0; t < 5; t++)
		{
			printf("Enter the grade of %s: ", gradenames[t]);
			if(scanf("%lf", &grades[t]) != 1)
			{
				printf("IDIOT!\n");
				return 1;
			}
			sum += grades[t];
		}
		printf("Sum of student #%d is %.2lf\n\n", current+1, sum);
	}

	return 0;
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Apr 5th, 2006, 9:37 AM   #6
Pizentios
Programming Guru
 
Pizentios's Avatar
 
Join Date: May 2004
Location: Brandon, Manitoba, Canada
Posts: 2,023
Rep Power: 7 Pizentios is on a distinguished road
Send a message via ICQ to Pizentios Send a message via MSN to Pizentios
fixed code tags
__________________
Profanity is the one language that all programmers understand.

Check out my Blog <---updated Nov 30 2007!
Pizentios is offline   Reply With Quote
Old Apr 5th, 2006, 9:37 AM   #7
Xyhm
Programmer
 
Xyhm's Avatar
 
Join Date: Mar 2006
Posts: 60
Rep Power: 3 Xyhm is on a distinguished road
Wouldn't flushing (fflush(stdout)) after the printf's that doesn't use '\n' be a good idea?
Xyhm is offline   Reply With Quote
Old Apr 5th, 2006, 9:44 AM   #8
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by Xyhm
Wouldn't flushing (fflush(stdout)) after the printf's that doesn't use '\n' be a good idea?
If it wrote to a file, I'd do that yes, but not normally. Btw '\n' doesn't flush the buffer, std::endl does. Flushing the stdin might be a good idea, but I'm not sure. DaWei or someoe else know?
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Apr 5th, 2006, 10:34 AM   #9
Xyhm
Programmer
 
Xyhm's Avatar
 
Join Date: Mar 2006
Posts: 60
Rep Power: 3 Xyhm is on a distinguished road
Quote:
Originally Posted by nnxion
If it wrote to a file, I'd do that yes, but not normally. Btw '\n' doesn't flush the buffer, std::endl does. Flushing the stdin might be a good idea, but I'm not sure. DaWei or someoe else know?
I've had problems with printf writing to the screen and not flushing afterwards. As for '\n', I've read somewhere that it makes flushing in these cases unnecessary, but I don't know why and it may very well not be true.
Xyhm is offline   Reply With Quote
Old Apr 5th, 2006, 10:56 AM   #10
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by Xyhm
I've had problems with printf writing to the screen and not flushing afterwards. As for '\n', I've read somewhere that it makes flushing in these cases unnecessary, but I don't know why and it may very well not be true.
Like I said, it's true for std::endl, not for a newline character ('\n'). Strange that you had problems with that. Must have been something else. Or like DaWei said, mixing printf with cout.

@David: Thanx for clearing that up for me.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:29 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC