Okay so I have a problem with one of my homework assignments. The problem is as follows
Write a program that calculates the total grade for N classroom exercises as a percentage. The user should input the value for N followed by each of the N scores and totals. Calculate the overall percentage (sum of the total points earned divided by the total points possible) and output it as a percentage. sample input (in bold) and output (in italics) is shown below.
How many exercises to input? 3
Score received for exercise 1: 10
Total points possible for exercise 1: 10
Score received for exercise 2: 7
Total points possible for exercise 2: 12
Score received for exercise 3: 5
Total points possible for exercise 3: 8
Your total is 22 out of 30, or 73.33%.
Input Details: The input will consist of an initial integer (let's refer to it as N) followed by N pairs of integers, all responses to program prompts.
Output Details: The program uses the prompts and labels shown in the example above.
MY CODE
#include <iostream>
using namespace std;
int main()
{
int N, M, J, total, grade, possible, total_possible, total_grade;
double average;
M = 0;
total_grade = 0;
total_possible = 0;
cout << "How many exercises to input?" << endl;
cin >> N;
for (N > 0; N--;)
{
M = M++;
J = (N-N) + M;
cout << "Score received for exercise " << J << ": ";
cin >> grade;
cout << "Total points possible for exercise " << J << ": ";
cin >> possible;
total_grade = total_grade + grade;
total_possible = total_possible + possible;
}
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
average = (total_grade * 100.0)/total_possible;
cout << "Your total is " << total_grade<< " out of " << total_possible << ", or " << average << "%";
return 0;
}
I made this and it seems right but its not...please someone help me
I'm not sure how you planned on storing all of your data? I think your teacher wanted you to use an array whose length gets set when you enter how many assignments there are. Your array would have two rows, one for points earned and one for points possible, and it would have "n" columns, "n" being the number of assignments.
That may not be correct, but that's how I visualized it...
I just started learning C++ today, and I decided this would be a good learning experience to give it a shot. This is what I ended up with, it works very well, except for some reason I'm getting the "points possible" total for the score total. If someone could show me what I messed up I would appreciate it.
/*
Name: Grade Finder
Copyright: none
Author: Matt Cromer
Date: 08/11/11 19:43
Description: Write a program that calculates your percentage based on your grades/points possible.
Must be flexible as far as number of assignments goes.
*/
#include <iostream>
usingnamespace std;
int main ()
{
int n=0,n_initial; // n will be the number of assignments entered in the system.
// since n will be counting down, I want to save n_initial as well.
int grades [2][n]; // 2 rows for possible and actual, n columns because you will have a column for each item.
// # OF EXERCISES
cout << "How many exercises to input? > ";
cin >> n;
n_initial = n;
cout << "n_initial = " << n_initial;
//SCORE RECIEVED COUNTDOWN
while (n>0)
{
int x,y;
cout << endl << endl << "Score recieved for exercise " << n << ": ";
cin >> x;
grades [1][n] = x;
cout << endl << "Points possible for exercise " << n << ": ";
cin >> y;
grades [2][n] = y;
n = n-1;
}
//CALCULATION OF TOTAL AND PERCENT
int score_total=0,pointspossible_total=0,rows_1,rows_2;
//SCORE TOTAL
int xy=0;
n = n_initial;
for (int xy=0; xy < n_initial; xy++)
{
score_total += grades [1][n];
n--;
}
cout << endl << "Score Total = "<< score_total << endl;
//POINTS POSSIBLE TOTAL
xy=0;
n = n_initial;
for (int xy=0; xy < n_initial; xy++)
{
pointspossible_total += grades [2][n];
n--;
}
cout << "Possible points Total = " << pointspossible_total << endl;
cout << "Your total is " << score_total << " out of " << pointspossible_total << ", and your percentage is " << score_total/pointspossible_total << "." <<endl;
system ("pause");
return (0)
}
Not bad for my first day. I'm proud. Constructive criticism appreciated.
Oh and feel free to use this for your class (unless it's wrong of course).
The only compile-time error I see is a missing semicolon on your return statement.
You're also using indices of 1 and 2 on your array processing. C++ like C is a zero-based language, so you should be using 0 and 1.
You're also creating your array while n = 0; that's going to result in a single-dimensional array. Dynamically creating arrays is difficult; I'd recommend you begin using vectors instead. They have lots of advantages over arrays.
Finally, using ints for your scores and possible scores results in performing integer arithmetic when calculating your average. This will result in answers that aren't what you want, since C++ doesn't round or keep track of fractions during integer arithmetic.
Thanks alot man, I really appreciate the input. Could you tell me why when I run the program that I am getting the points possible for my score totals? I'm looking at it and I don't see why it would do that. Oh and as to the arrays, isn't it all relative? I know it starts at zero, but if I use one and two it shouldn't affect anything, right? I only did it for clarity of thought.
You're getting the totals, because you're overwriting your scores.
The reason you're overwriting your scores is that you don't have a valid 2-dimensional array. You're probably overwriting some data in your stack area, and it's just going unnoticed. You must properly declare your array, in order to store the data you want stored.
And, no, array indexing is not relative in C/C++. Your first element is always 0. I'm a little surprised you didn't run into a segmentation fault with your bad indices, but it may have to do with one mistake (your improper declaration) concealing another (your bad indices).
/*
Name: Grade Finder
Copyright: none
Author: Matt Cromer
Date: 08/11/11 19:43
Description: Write a program that calculates your percentage based on your grades/points possible.
Must be flexible as far as number of assignments goes.
*/
#include <iostream>
usingnamespace std;
int main ()
{
int n=0,n_initial; // n will be the number of assignments entered in the system.
// since n will be counting down, I want to save n_initial as well.
int grades [1][n]; // 2 rows for possible and actual, n columns because you will have a column for each item.
// # OF EXERCISES
cout << "How many exercises to input? > ";
cin >> n;
n_initial = n;
//SCORE RECIEVED COUNTDOWN
while (n>0)
{
int x,y;
cout << endl << endl << "Score recieved for exercise " << n << ": ";
cin >> x;
grades [0][n] = x;
cout << endl << "Points possible for exercise " << n << ": ";
cin >> y;
grades [1][n] = y;
n = n-1;
}
//CALCULATION OF TOTAL AND PERCENT
int score_total=0,pointspossible_total=0,rows_1,rows_2;
//SCORE TOTAL
int xy=0;
n = n_initial;
for (int xy=0; xy < n_initial; xy++)
{
score_total += grades [0][n];
n--;
}
cout << endl << "Score Total = "<< score_total << endl;
//POINTS POSSIBLE TOTAL
xy=0;
n = n_initial;
for (int xy=0; xy < n_initial; xy++)
{
pointspossible_total += grades [1][n];
n--;
}
cout << "Possible points Total = " << pointspossible_total << endl;
cout << "Your total is " << score_total << " out of " << pointspossible_total << ", and your percentage is " << score_total/pointspossible_total*100 << "%" <<endl;
system ("pause");
return (0);
}
I fixed my arrays, why am I still getting the wrong totals?
Is it because I am using the same variable "n" in both columns of my array?
You're still having problems properly defining your array.
First off, you use 0 as a base for indexing into an existing array, but not for defining the array. (A bit confusing, I realize.) So, your line:
int grades [1][n]; // 2 rows for possible and actual, n columns because you will have a column for each item.
should be:
int grades [2][n]; // 2 rows for possible and actual, n columns because you will have a column for each item.
Also, an array is created to the size requested, and stays that size. At the time you create grades, n is equal to 0, so you're not going to get the right size array. And, it won't automatically re-size itself just because the value you used to define it (n) changes later.
You need to either pre-allocate enough elements in your array to account for all conditions, or move the array definition to after the input of n, or use new() and delete() to dynamically allocate space for your array.
/*
Name: Grade Finder
Copyright: none
Author: Matt Cromer
Date: 08/11/11 19:43
Description: Write a program that calculates your percentage based on your grades/points possible.
Must be flexible as far as number of assignments goes.
*/
#include <iostream>
usingnamespace std;
int main ()
{
int n=0,n_initial; // n will be the number of assignments entered in the system.
// since n will be counting down, I want to save n_initial as well.
// # OF EXERCISES
cout << "How many exercises to input? > ";
cin >> n;
n_initial = n;
int grades [2][n]; // 2 rows for possible and actual, n columns because you will have a column for each item.
//SCORE RECIEVED COUNTDOWN
while (n>0)
{
int x,y;
cout << endl << endl << "Score recieved for exercise " << n << ": ";
cin >> x;
grades [0][n] = x;
cout << endl << "Points possible for exercise " << n << ": ";
cin >> y;
grades [1][n] = y;
n = n-1;
}
//CALCULATION OF TOTAL AND PERCENT
double score_total=0,pointspossible_total=0,rows_1,rows_2;
//SCORE TOTAL
int xy=0;
n = n_initial;
for (int xy=0; xy < n_initial; xy++)
{
score_total += grades [0][n];
n--;
}
cout << endl << "Score Total = "<< score_total << endl;
//POINTS POSSIBLE TOTAL
xy=0;
n = n_initial;
for (int xy=0; xy < n_initial; xy++)
{
pointspossible_total += grades [1][n];
n--;
}
cout << "Possible points Total = " << pointspossible_total << endl;
cout << "Your total is " << score_total << " out of " << pointspossible_total << ", and your percentage is " << (score_total/pointspossible_total)*100 << "%" <<endl;
system ("pause");
return (0);
}