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
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++