Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 07-23-2007, 12:39 AM   #1 (permalink)
Rodney
Recruit
 
Rodney's Avatar
 
Join Date: Jul 2007
Posts: 8
Rodney is on a distinguished road
Smile I need help creating a Grade calculation program

Can you please help me with a problem I have, creating a grade calculation program.

The assignment reads:
Create a program and a flowchart that will compute for the grade of the student and prints its equivalent grade print.

The formula for computing the
Grade is Grade = 10% assignment + 20% seatwork + 30% Quiz + 40% exam

Note:There are 2 assignment
2 Quizzes
2 Seatworks
1 Exam

The scores given should be calculated as such:

99 - 100 = 1.0
96 - 98 = 1.25
93 - 95 = 1.50
90 - 92 = 1.75
87 - 89 = 2.0

84 - 86 = 2.25
81 - 83 = 2.50
78 - 80 = 2.75
75 - 77 = 3.00

Below 75 = 5.00

I have been given this solution in C, but I have no knowledge how to convert it into the required C++
Code:
#include <stdio.h>

double grade(double g);

int main(void)
{
  double c,i,r;
  char ch[100];
  i=r=0.0;
  while(i+=10)
    {
      printf("Please enter result for %s number 1: ", 
	     i < 11 ? "Assignement": 
	     i < 21 ? "Quiz": 
	     i < 31 ? "Seatworks" : "Exam");
      fflush(stdout);
      fgets(ch, 99, stdin);
      c=strtol(ch, (char **)NULL, 10);
      r+=grade(c)*i/100.0;
      if(i > 31)break;
      printf("Please enter result for %s number 2: ", 
	     i < 11 ? "Assignement": 
	     i < 21 ? "Quiz": 
	     i < 31 ? "Seatworks" : "Exam");
      fflush(stdout);
      fgets(ch, 99, stdin);
      c=strtol(ch, (char **)NULL, 10);
      r+=grade(c)*i/100.0;
    }
  printf("Combined result: %f\n", r);
}

double grade(double g)
{
  if(g>98)
    return 1.0;
  if(g>95)
    return 1.25;
  if(g>92)
    return 1.50;
  if(g>89)
    return 1.75;
  if(g>86)
    return 2.0;
  if(g>83)
    return 2.25;
  if(g>80)
    return 2.50;
  if(g>77)
    return 2.75;
  if(g>74)
    return 3.0;
  return 5.0;
}
If anyone could give me any pointers to, how this could be expressed as ISO/ANSI compliant C++ I would appreciate it very much.

Thank you.

Last edited by redhead; 07-23-2007 at 07:51 AM. Reason: This is how an appropriate question could be formed....
Rodney is offline   Reply With Quote
Old 07-23-2007, 07:56 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,713
redhead is on a distinguished road
According to this thread orriginaly displayed exactly as your first cry for help, I've been so kind to form it in a way that would be looked uppon with more forgiving eyes, than the usual "No we wil not do your homework.." remarks..

Now let's get a giving discussion out of this, where Rodney eventualy will learn something usefull..

By the way Rodney, welcome here at Code Newbie, as you've might noticed, the default "Please make this for me" isn't the correct way to win friends here, altho we do go a long way to help, we won't do your homework for you, without some form of commitment on your part.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 07-23-2007, 07:10 PM   #3 (permalink)
Rodney
Recruit
 
Rodney's Avatar
 
Join Date: Jul 2007
Posts: 8
Rodney is on a distinguished road
Thank very much!!!...LUv you baby!!..
Rodney is offline   Reply With Quote
Old 07-24-2007, 12:15 AM   #4 (permalink)
Rodney
Recruit
 
Rodney's Avatar
 
Join Date: Jul 2007
Posts: 8
Rodney is on a distinguished road
How did u that a kind of a program??
Rodney is offline   Reply With Quote
Old 07-24-2007, 12:16 AM   #5 (permalink)
Rodney
Recruit
 
Rodney's Avatar
 
Join Date: Jul 2007
Posts: 8
Rodney is on a distinguished road
Can u give me a flowchart that kind of a program???
Rodney is offline   Reply With Quote
Old 07-24-2007, 07:36 AM   #6 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,713
redhead is on a distinguished road
You must've mistaken me for someone else.. I aint a baby
Disclaimer:
Since you're new here and I'm on my vacation, with nothing else todo, I'll give you a very advanced walkthrough, be aware this isn't the standard reply to anyone giving the attitude you've previusly shown, an next time it will be you, who has to do all the work.
The unenlightened user asked the seasoned programmer
Quote:
Thank very much!!!...LUv you baby!!..
How did u that a kind of a program??
Can u give me a flowchart that kind of a program???
Thus spake the seasoned programmer

The flow of such a program is determined by what is expected, as your assigment describes, it is asking the user for scores given for:
  • 2 assignments
  • 2 Quizzes
  • 2 Seatworks
  • 1 Exam
Now when you look at how the final grade should be calculated ie:
Quote:
Grade = 10% assignment + 20% seatwork + 30% Quiz + 40% exam
This indicates something which can be handled through a simple loop, since the percentage of the Assignment vs. Seatworks vs. Quizzes vs. Exam has an interval of 10, you could let the counter for your loop extend at a ten interval Like I do

There are several ways of performing loops, I'm guessing you havn't gotten that far yet, so let's take this from scratch.

When interacting with a user, you need to use the Input/Output stream in order to get something from std::cin and display somethign to std::cout, which means the header I use in my example to you, shouldn't be called stdio.h but instead iostream.
Code:
/* Old C style header definition */
#include <stdio.h>

// New used C++ header definition
#include <iostream>
The program itself, is a sequence of asking a question, and recieving a computationel answer, which means the input provided by the user should be either as an integer or as a floating point value, depending on what will bennefit your usage, you should be aware of the different data types in order to decide which should fit your needs.

Now C++ is your friend, in the way that std::cin will automaticaly convert any input to the type of variable you're trying to store it into, instead of all the conversion gibberish:
Code:
fgets(ch, 99, stdin);
c=strtol(ch, (char **)NULL, 10);
I have to go through, instead you can simply store it into the variable you would like to use as your container
Code:
std::cin >> input;
With this in mind, the interaction with the user culd lead to a very simple program, remember C/C++ programming structures are top-down executed, so you wont get to anything below the current line in action, befor that line is done executing.

With this in mind, the appropriate C++ code would look something like this:
Code:
#include <iostream>

double grade(int);

int main(void)
{
  int input=0;
  double result=0.0;

  std::cout << "Enter result for Assignment 1:";
  std::cin >> input;
  result += grade(input) * 10 / 100;
  std::cout << "Enter result for Assignment 2:";
  std::cin >> input;
  result += grade(input) * 10 / 100;

  std::cout << "Enter result for Seatwork 1:";
  std::cin >> input;
  result += grade(input) * 20 / 100;
  std::cout << "Enter result for Seatwork 2:";
  std::cin >> input;
  result += grade(input) * 20 / 100;

  std::cout << "Enter result for Quiz 1:";
  std::cin >> input;
  result += grade(input) * 30 / 100;
  std::cout << "Enter result for Quiz 2:";
  std::cin >> input;
  result += grade(input) * 30 / 100;

  std::cout << "Enter result for Exam: ";
  std::cin >> input;
  result += grade(input) * 40 / 100;
  
  std::cout << "Result: " << result << std::endl;
return 1;
}
If you notice the sections with asking the user, and recieving the result, you'll see that code portion is what leads one to think some form of loop should be able to accomplish this, since all these code lines gives it a rough and disturbing look.

To simplify it, you could, like in my example, combine these interactions with the user as a simple loop.

A loop is basicaly a way of jumping back to a certain line in your code untill the condition dosn't fit the requirements any longer, with some pseudocode this could be described like
Code:
10 variabel counter initializes to value 0
20 print Here the loop starts
30 variabel counter assignes value of counter + 1
40 if value of variabel counter is less than 5, goto line 20
If you follow it closely you will see, the lines between 20 and 40 will be evaluated 5 times, since it will jump to line 20 for as long as the counter value remains less than 5.

In C/C++ the keyword for creating such a loop is either a do/while() loop Like the one I'm using or a for() loop, there are advantages and disadvantages for the two, it all depends on how you would like the condition to be avaluated, what loop type you chose, but basicaly all they do is the action the pseudo code describes.

So if you like to use a loop around the question for the user to ask, your program would begin to look something like this
Code:
#include <iostream>

double grade(int);

int main(void)
{
  int input=0;
  double result=0.0;
 
  for(int counter=1; counter < 4; ++counter)
    {
      std::cout << "Enter result 1:";
      std::cin >> input;
      result += grade(input) * (counter*10) / 100;
      std::cout << "Enter result 2:";
      std::cin >> input;
      result += grade(input) * (counter*10) / 100;
    }

  std::cout << "Enter result for Exam:";
  std::cin >> input;
  result += grade(input) * 40 / 100;
  
  std::cout << "Result: " << result << std::endl;
return 1;
}
Study this, and you see the percentage calculated from the user input ie
Code:
result += grade(input) *(counter*10)/100;
is depending on the value of your counter, this is the advantage you can have when you look at the formula for your grade calculation
Quote:
Grade = 10% assignment + 20% seatwork + 30% Quiz + 40% exam
On first passthrough you have a value of 1 stored in your counter, yealding a percentage of 10. On second it will be a value of 2 stored in your counter, yealding a percentage of 20, and so forth.
So in the end you'll end up with calculating the first three parts of your formula, with the final bit regarding the exam score remaining out side your loop scope dont worry we'll get to that eventualy

If you look closely, you'll see theres no way you tell the user, if it is an Assignment or a Seatwork you want the result from.

The question then becomes, how can you show the correct question to the user, you don't want it to ask for the result of an Assignment more than two times, same goes with the quest for a Quiz score, so you need to incorporate some way of determining what to show the user, at any given passthrough of the loop, for this a simple condition like
Code:
if(condition)
  // Do this ;
else
  // Do this ;
would be usefull, when using this you can combine the knowledge of how the loop condition will react, you still have your counter, in this case the value of the counter can help decide what to display.
Stay openminded here, you need to think abstract to follow it
You repeat the same line three times, yet you only want to display Assignment on your first evaluation, so you will need to think of the conditions embedded within eachother, with a failing first condition resulting in trying out another condition, then your code would look something like this
Code:
#include <iostream>

double grade(int);

int main(void)
{
  int input=0;
  double result=0.0;
 
  for(int counter=1; counter < 4; ++counter)
    {
      std::cout << "Enter result for ";
      if(counter < 2)
	std::cout << "Assignment ";
      else
	if(counter < 3)
	  std::cout << "Seatwork ";
	else
	  std::cout << "Quiz ";
      std::cout << "number 1: ";
      std::cin >> input;
      result += grade(input) * (counter*10) / 100;

      std::cout << "Enter result for ";
      if(counter < 2)
	std::cout << "Assignment ";
      else
	if(counter < 3)
	  std::cout << "Seatwork ";
	else
	  std::cout << "Quiz ";
      std::cout << "number 2: ";
      std::cin >> input;
      result += grade(input) * (counter*10) / 100;
    }

  std::cout << "Enter result for Exam:";
  std::cin >> input;
  result += grade(input) * 40 / 100;
  
  std::cout << "Result: " << result << std::endl;
return 1;
}
Now have another close look at it, and you'll see a portion of the code is once again repeated, you don't want excessive use of code if it can be simplified, the more lines requires more fixing time, when the anual review is due...

So you gather the internal question for the user, in a simple loop aswell, and your code will eventualy begin to make sence
Code:
#include <iostream>

double grade(int);

int main(void)
{
  int input=0;
  double result=0.0;
 
  for(int counter=1; counter < 4; ++counter)
    {
      for(int inner_counter=1; inner_counter < 3; ++inner_counter)
	{
	  std::cout << "Enter result for ";
	  if(counter < 2)
	    std::cout << "Assignment ";
	  else
	    if(counter < 3)
	      std::cout << "Seatwork ";
	    else
	      std::cout << "Quiz ";
	  std::cout << "number ";
	  std::cout << inner_counter << ": ";
	  std::cin >> input;
	  result += grade(input) * (counter*10) / 100;
	}
    }

  std::cout << "Enter result for Exam:";
  std::cin >> input;
  result += grade(input) * 40 / 100;
  
  std::cout << "Result: " << result << std::endl;
return 1;
}
The final grade isn't depending on two exam scores, but only for one single, If you wan't to keep this inside your loops, you only need to change some of the conditions in yoru loop deffinitions, and you will keep the user interaction within the same scope, and your program will look like
Code:
#include <iostream>

double grade(int);

int main(void)
{
  int input=0;
  double result=0.0;
 
  for(int counter=1; counter < 4; ++counter)
    {
      for(int inner_counter=1; inner_counter < 3; ++inner_counter)
	{
	  std::cout << "Enter result for ";
	  if(counter < 2)
	    std::cout << "Assignment ";
	  else
	    if(counter < 3)
	      std::cout << "Seatwork ";
	    else
	      if(counter < 4)
		std::cout << "Quiz ";
	      else
		std::cout << "Exam : ";
	  if(counter < 4)
	    {
	      std::cout << "number ";
	      std::cout << inner_counter << ": ";
	    }
	  else
	    ++inner_counter;
	  std::cin >> input;
	  result += grade(input) * (counter*10) / 100;
	}
    }

  std::cout << "Result: " << result << std::endl;
return 1;
}
Now you take another look, and decides the if/else is just too painful to look at, you decide to WOW the viewer with your knowledge of another condition statement the one known as tertiary operator, this operator
Code:
(condition) ? true result : false result;
will eventualy make it possible to eliminate a few calls to the std::cout, with this condition alteration, and a simplification of the embedded code involved here, your program will eventualy look like
Code:
#include <iostream>

double grade(int);

int main(void)
{
  int input=0;
  double result=0.0;
 
  for(int counter=1; counter < 4; ++counter)
    {
      for(int inner_counter=1; inner_counter < 3; ++inner_counter)
	{
	  std::cout << "Enter result for " <<
	    ((counter < 2) ? "Assignment " : 
	     (counter < 3) ? "Seatwork " :
	     (counter < 4) ? "Quiz " : "Exam : ");
	  if(counter < 4)
	    {
	      std::cout << "number ";
	      std::cout << inner_counter << ": ";
	    }
	  else
	    ++inner_counter;
	  std::cin >> input;
	  result += grade(input) * (counter*10) / 100;
	}
    }

  std::cout << "Result: " << result << std::endl;
return 1;
}
Keep in mind, the grade() function in use here, is essentialy the very same used in my first example so I didn't bother to rewrite the very same function over and over and over and over....

As a very good read, I would suggest the C++ Tutorial which is a very basic walkthrough of what C++ provides.


Now you owe me a chance to see you giving your very best, and eventualy become a lead programmer with a burning passion to use every minut of your sparetime to help influence good programming style on any newbie to the wonderful world of C/C++
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001

Last edited by redhead; 07-24-2007 at 07:49 AM.
redhead is offline   Reply With Quote
Old 07-24-2007, 03:11 PM   #7 (permalink)
Rodney
Recruit
 
Rodney's Avatar
 
Join Date: Jul 2007
Posts: 8
Rodney is on a distinguished road
ok thanks!!..for explaining...sori for the wrong messages!!!
how about the flowchart??or the pseudocode of that
Rodney is offline   Reply With Quote
Old 07-24-2007, 03:20 PM   #8 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,161
Belisarius is on a distinguished road
Rodney, Redhead has just gone much further out of his way to give you the basics of C/C++ (tailored to your assignment, noless) than 99% of the people out there would have done for you. All he (and most of the rest of us here) want in return is for you to put in the effort to learn.

If you really want to thank him, show us that you're trying to understand the material. Try writing the program in pseudocode yourself and post it here, and we'll give you pointers.
__________________
GitS
Belisarius is online now   Reply With Quote
Reply

Bookmarks

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Code Newbie Friends! (link exchange) sde Lounge 4 02-16-2006 07:07 AM
RSS Code Newbie Feeds sde Code Newbie News 0 03-17-2005 05:35 PM
Official Long Lost Code Newbie Members Page =P sde Lounge 0 10-27-2004 01:56 PM
Code Newbie Site Upgrade sde Code Newbie News 0 10-27-2004 07:24 AM
Would you mind if there were small flash elements at code newbie? sde Lounge 10 05-29-2004 09:05 PM


All times are GMT -8. The time now is 04:57 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting