need help with an assignment

Write a program to compute the grade earned by a (one) student.
The program will initially request 6 assignment, 2 test, and 1 final examination scores. All scores are out of 100. Your program should input all scores first, then determine whether all scores are within legal range. If any input score isout of range (negative or above 100), output an error statement. One error statement is enough irrespective of how many illegal scores. Use an assignment statement to determine whether or not a score is within range,and use bool (in C++) to record those information.If all scores are within legal range, calculate the course’s score and grade of the student, andoutput both the computed score and the grade earned by the student.
The score for the course is based on the following weights:
Assignments
20%
Tests
40%
Exam
40%
Based on the weighted score, assign the grade for the student as follows:
85<= score <= 100 A
73 <= score < 85 B
61<= score < 73 C
49 <= score < 61D
0 <= score < 49F
Requirement for each and every assignment.
1. Comments the program (your name, Purpose, major method).
2. use meaningful variable names
3. nice/consistent indentation
4. good input prompt
5. echoed each input (output what is being typed by the user).
6. good output layout: answer clearly labeled and separated.
7. No credits will be given if the program contains any compile-time or run-time
error.
8. Never use C/C++ statements and features not taught in class.

This is only the second assignment for the class and i am at a loss on where to start. All the resources ive looked up involve using cout and other commands that we havent been taught in class.

How would i go about weighing and calculating the score for the student?




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<stdlib.h>
#include<stdio.h>

int main()
{
	int Assign1;
	int Assign2;
	int Assign3;
	int Assign4;
	int Assign5;
	int Assign6;
	int Test1;
	int Test2;
	int Exam;
	double weight1;
	double weight2;
	weight1 = 0.2;
	weight2 = 0.4;
	printf("Score of first assignment");
	scanf("%d", Assign1);
	printf("Score of second assignment");
	scanf("%d", Assign2);
	


}
  
Last edited on
Hello Cerdoken,

Welcome to the forum.

I find if you break up the instructions it helps:

Write a program to compute the grade earned by a (one) student.

The program will initially request 6 assignment, 2 test, and 1 final examination scores. All scores are out of 100.

A. Your program should input all scores first, then determine whether all scores are within legal range.

B. If any input score isout of range (negative or above 100), output an error statement. One error statement is enough
irrespective of how many illegal scores.

C. Use an assignment statement to determine whether or not a score is within range,and use bool (in C++) to record
those information.

D. If all scores are within legal range, calculate the course’s score and grade of the student, and output both the
computed score and the grade earned by the student.

E. The score for the course is based on the following weights:
Assignments
20%
Tests
40%
Exam
40%

F. Based on the weighted score, assign the grade for the student as follows:
85<= score <= 100 A
73 <= score < 85 B
61<= score < 73 C
49 <= score < 61D
0 <= score < 49F

Requirement for each and every assignment.
1. Comments the program (your name, Purpose, major method).

2. use meaningful variable names

3. nice/consistent indentation

4. good input prompt

5. echoed each input (output what is being typed by the user).

6. good output layout: answer clearly labeled and separated.

7. No credits will be given if the program contains any compile-time or run-time
error.

8. Never use C/C++ statements and features not taught in class.


Parts A - F give you an idea of each small part. It is best to work on one small part at a time and build from each preceding part.

You have a good start so far, but I would consider this. Lines 6 - 14 are defined as "int"s and the next two line are "double"s. Chances are that you will do some calculation with the "int"s and store that result in a "double", but you will get a different result than what you think especially if here is a divide involved.

An easy way to start with is to make all numeric variables "double"s. Otherwise you will have to do something that will promote the "int"s to "double"s in a calculation later.

For now I would finish what you have started and get all the user input so that you have something to work with.

I would also decide if this is a C or C++program.

for me for now I will have to work on th program in the morning when everything is fresher.

Hope that helps,

Andy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<stdlib.h>
#include<stdio.h>

int main()
{
	double Assign1;
	double Assign2;
	double Assign3;
	double Assign4;
	double Assign5;
	double Assign6;
	double Test1;
	double Test2;
	double Exam;
	double weight1;
	double weight2;
	double result;
	double weightedResult;
	weight1 = 0.2;
	weight2 = 0.4;
	printf("Score of first assignment = ");
	scanf("%d", &Assign1);
	printf("Score of second assignment = ");
	scanf("%d", &Assign2);
	printf("Score of third assignment = ");
	scanf("%d", &Assign3);
	printf("Score of fourth assignment = ");
	scanf("%d", &Assign4);
	printf("Score of fifth assignment = ");
	scanf("%d", &Assign5);
	printf("Score of sixth assignment = ");
	scanf("%d", &Assign6);
	printf("Score of first test = ");
	scanf("%d", &Test1);
	printf("Score of second test = ");
	scanf("%d", &Test2);
	printf("Score of exam = ");
	scanf("%d", &Exam);
	system("pause");
	return 0;

	
}

Ive gotten to the point where i can get the input from the user. Now i need to figure out how to set the parameters for the inputs and set the error code. Then i need to figure out how to calculate the scores for the student which is the part that is giving me the most trouble atm.
Just a small tip Cerdoken.

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
        //instead of declaring each variable one at a time like so:

	double Assign1;
	double Assign2;
	double Assign3; // and so on
	
        //To save lines you can declare the variables together.
        
        double Assign1, Assign2, Assign3;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<stdlib.h>
#include<stdio.h>

int main()
{
	double Assign1, Assign2, Assign3, Assign4, Assign5, Assign6;
	double Test1, Test2;
	double Exam;
	double weight1;
	double weight2;
	double result;
	double weightedResult;
	char grade;
	weight1 = 0.2;
	weight2 = 0.4;
	printf("Score of first assignment = ");
	scanf("%d", &Assign1);
	printf("Score of second assignment = ");
	scanf("%d", &Assign2);
	printf("Score of third assignment = ");
	scanf("%d", &Assign3);
	printf("Score of fourth assignment = ");
	scanf("%d", &Assign4);
	printf("Score of fifth assignment = ");
	scanf("%d", &Assign5);
	printf("Score of sixth assignment = ");
	scanf("%d", &Assign6);
	printf("Score of first test = ");
	scanf("%d", &Test1);
	printf("Score of second test = ");
	scanf("%d", &Test2);
	printf("Score of exam = ");
	scanf("%d", &Exam);
	weightedResult = (Assign1+Assign2+Assign3+Assign4+Assign5+Assign6) * weight1 + (Test1+Test2+Exam) * weight2;
	result = weightedResult/100;
	printf("Score = %d ", result);
	if(result>=90)
	{
		grade == 'A';
	}
	else if(result>=80)
	{
		grade == 'B';
	}
	else if(result>=70)
	{
		grade == 'C';
	}
	else if (result>=60)
	{
		grade == 'D';
	}
	else if(result<=50)
	{
		grade == 'F';
	}printf
	("grade = %c\n", grade);
	system("pause");
	return 0;

	
	


}


for some reason the grade isnt printing in the program
Hello Cerdoken,

Some observations and suggestions:

You are off to a good start. I see that you took OKcomputer's suggestion to shorten the variable definition. You could go a bit further and shorten all the "double"s to three or four lines.

This is not always necessary, but I like to initialize my variables when they are defined. Understand that when the compiler compiles the program it sets aside memory on the stack for these variables, but only the address of where the variable is. The 0s and 1s at that location are interpreted based on the variable type. Therefore this is known as "garbage" because it is not something that you can use. In the case of a numeric variable this could be a vary large negative number. Not something that you can use.

Another advantage to initializing your variables is when the input is working properly you can change the value of the initialized variable and comment out the input for quicker testing.

I do not know what you are using to write and compile your programs with, but in C++11 on there is the "uniform initializer" of {}s. I am not sure if this will work for you or not because I am not familiar with the C standards.. You can always use "variableName = 0;"

Line 34 looks like the right formula at first glance, but you will likely need some more ()s to make it work correctly. Here is a page on Operator Precedence that you should keep handy https://en.cppreference.com/w/cpp/language/operator_precedence and here at http://www.cplusplus.com/doc/tutorial/operators/ The page here has more information and what you want is at the bottom of the page.

For line 35 I would write the "100" as "100.0" or define a constant variable for this. The advantage of making a constant variable is that yo would put this at the beginning of the function or above main where it is easy to find and change if needed. in this case this would be a variable that would never change. I might do something like this: const double DIVISOR = 100.0;. The capital letters remind you that it is defined as a constant. And you can use any name that you want.

Lines 37 to 57 I would highlight and cut then save them in a different file for now because you are using these lines at the wrong time and in the wrong place. Refer to step "A"
A. Your program should input all scores first, then determine whether all scores are within legal range.
Checking for a letter grade is not legal range. The range is 0 <= n <= 100.

At this point when you are finishing step "A" you also have to be thinking about step "B". To do this I would define with the other variables: bool errorCondition = false; then following the first "scanf" add the code:
1
2
3
4
5
6
if ((assign1 < 0 || assign1 > 100) && !errorCondition)
{
    printf("error message");

    errorCondition = true;
}

Notice the ()s around assign1 < 0 || assign1 > 100. This tells the compiler to evaluate this first before the && is done. Setting the "errorCondition to true will make the next time you use this a false statement, so even if the lhs of && evaluates to true the rhs will be false so the if statement is considered as false and will be passed.

It is a bit redundant, but put this if statement after every "scanf" that you have. Personally I would put this in a function that returns a bool, but if you have not learned about functions do not worry about it now.

Now I will load up what you have and give it some testing to see if I find anything else. If I think of something else I will let you know.

Hope that helps,

Andy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include<stdlib.h>
#include<stdio.h>

int main()
{
	double Assign1, Assign2, Assign3, Assign4, Assign5, Assign6;
	double Test1, Test2;
	double Exam;
	double weight1;
	double weight2;
	double result;
	double weightedResult;
	char grade;
	bool errorCondition = false;
	weight1 = 0.2;
	weight2 = 0.4;
	printf("Score of first assignment = ");
	scanf("%d", &Assign1);
	if ((Assign1 < 0.0 || Assign1 > 100.0) && !errorCondition)
	{
    printf("error message");

    errorCondition = true;
	}
	printf("Score of second assignment = ");
	scanf("%d", &Assign2);
	if ((Assign2 < 0 || Assign2 > 100) && !errorCondition)
	{
    printf("error message");

    errorCondition = true;
	}
	printf("Score of third assignment = ");
	scanf("%d", &Assign3);
	if ((Assign3 < 0 || Assign3 > 100) && !errorCondition)
	{
    printf("error message");

    errorCondition = true;
	}
	printf("Score of fourth assignment = ");
	scanf("%d", &Assign4);
	if ((Assign4 < 0 || Assign4 > 100) && !errorCondition)
	{
    printf("error message");

    errorCondition = true;
	}
	printf("Score of fifth assignment = ");
	scanf("%d", &Assign5);
	if ((Assign5 < 0 || Assign5 > 100) && !errorCondition)
	{
    printf("error message");

    errorCondition = true;
	}
	printf("Score of sixth assignment = ");
	scanf("%d", &Assign6);
	if ((Assign6 < 0 || Assign6 > 100) && !errorCondition)
	{
    printf("error message");

    errorCondition = true;
	}
	printf("Score of first test = ");
	scanf("%d", &Test1);
	if ((Test1 < 0 || Test1 > 100) && !errorCondition)
	{
    printf("error message");

    errorCondition = true;
	}
	printf("Score of second test = ");
	scanf("%d", &Test2);
	if ((Test2 < 0 || Test2 > 100) && !errorCondition)
	{
    printf("error message");

    errorCondition = true;
	}
	printf("Score of exam = ");
	scanf("%d", &Exam);
	if ((Exam < 0 || Exam > 100) && !errorCondition)
	{
    printf("error message");

    errorCondition = true;
	}
	weightedResult = (Assign1+Assign2+Assign3+Assign4+Assign5+Assign6) * weight1 + (Test1+Test2+Exam) * weight2;
	result = weightedResult/100.0;
	printf("Score = %d ", result);
	if(result>=90)
	{
		grade == 'A';
	}
	else if(result>=80)
	{
		grade == 'B';
	}
	else if(result>=70)
	{
		grade == 'C';
	}
	else if (result>=60)
	{
		grade == 'D';
	}
	else if(result<=50)
	{
		grade == 'F';
	}printf
	("grade = %c\n", grade);
	system("pause");
	return 0;
}


for some reason all the ifs are being skipped completely by the program
Hello Cerdoken,

To answer your question the if statements are not being skipped it is the grade == 'A'; and the others that is the problem. The "==" is comparing "grade to 'A'. What you want is assignment which is "=", so the lines should be grade = 'A';.

Do not get to excited because the "if/else if" statements are ahead of where you need to be.

I will work up the more detailed response after dinner.

Hope that helps,

Andy
Hello Cerdoken,

As I was scrolling back through your post of "Sep 19, 2018 at 3:07pm" i noticed some things I did not see earlier. I will need to look it over better as I was more concerned with you question than the whole program. Look at my following program and I think you will find answers and the differences between the two.

Yoy could follow the "scanf" with the if statement or wait until after all the input to do the if statements. My understanding of the instructions is why I put all the if statement for range checking after all the input. Mostly because of the error message, but it will work either way, so do not think that you have to change the program, but do watch your indenting.

In my IDE I had to setup the code this way for it to work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<stdio.h>
#include <conio.h>  // <--- Used for the "getch()" just before "return".
#include<stdlib.h>

#include <stdbool.h>  // <--- For using the "bool" variable type that I did and to define "false" and "true".

//#pragma warning(disable : 4996)  // <--- Needed for my compiler.

int main()
{
	const double WEIGHT1 = 0.2;
	const double WEIGHT2 = 0.4;

	float temp = 0.0;

	double Assign1 = 90.0, Assign2 = 95.0, Assign3 = 81.0, Assign4 = 96.0, Assign5 = 80.0, Assign6 = 99.0;
	double Test1 = 96.0, Test2 = 99.0;
	double Exam = 100.0;
	double result = 0.0;
	double weightedResult = 0.0;

	char grade = '\0';

	_Bool errorCondition = false;
	
	printf("Score of first assignment = ");
	scanf("%f", &temp);
	Assign1 = (double)temp;

	//printf("Score of second assignment = ");
	//scanf("%f", &temp);
	//Assign2 = (double)temp;

	//printf("Score of third assignment = ");
	//scanf("%f", &temp);
	//Assign3 = (double)temp;

	//printf("Score of fourth assignment = ");
	//scanf("%f", &temp);
	//Assign4 = (double)temp;

	//printf("Score of fifth assignment = ");
	//scanf("%f", &temp);
	//Assign5 = (double)temp;

	//printf("Score of sixth assignment = ");
	//scanf("%f", &temp);
	//Assign6 = (double)temp;

	//printf("Score of first test = ");
	//scanf("%f", &temp);
	//Test1 = (double)temp;

	//printf("Score of second test = ");
	//scanf("%f", &Test2);
	//Test2 = (double)temp;

	//printf("Score of exam = ");
	//scanf("%f", &temp);
	//Exam = (double)temp;

	if ((Assign1 <= 0 || Assign1 >= 100) && !errorCondition)
	{
		printf("\n A score of %0.0f is out of range.\n", Assign1);  // <--- Put your error message here.
		errorCondition = true;
	}

	// <--- Continue for the others.

	weightedResult = ((Assign1 + Assign2 + Assign3 + Assign4 + Assign5 + Assign6) * WEIGHT1) + ((Test1 + Test2 + Exam) * WEIGHT2);
	result = weightedResult / 100.0;

	printf("\n Weighted Result is: %0.2f\n", weightedResult);
	printf("\n Score = %0.2f ", result);
	

	printf("\n\n Press any key to continue. ");
	getch();
	
	return 0;
}


The if statements you have in the program now you can highlight and cut them from the program, but first have another file ready to paste them into for later use because they will work and there is no need to retype them later. Just fix them.

Starting at the top lines 1 - 4. Order of the include files is generally not a problem, but my quirk is that I like to an order to the includes. It helps me to remember what is needed. The last header file , or the file(s) that come after the blank line is what is extra for this given program.

As you can see by the comment for "conio.h" what it is fore. Look at the two lines before the "return" statement. This is better than using "system("pause");" which is a potential security problem. It does not matter what language you use, C or C++, it is bes not to use system.

In C++ "bool" is built into the IDE and you just use it, but for this program I had to include the "stdbool.h" header file and use "_Bool" because that is the way it is defined in the header file.

Line 7 the chances are that you will not have to use this line and that is why it is commented out. I have the feeling that your compiler is much older than mine and that is why there are some thing I can not use unless I use that line in the program.

Now inside m
"main" the first two lines are defined as constants. For what they ar they should not be changed anywhere in the program, just used. The capital letters help to remind you that they are constants.

While I am thinking about it normal variables sar better started with a lower case letter and if a variable name should contain two or more words use camel case as you did with "weightedResult".

Notice that all the variables from "Assign1" down to "Exam" all have different numbers. Once I had tested all the inputs I did this to speed things up and that is why all the inputs except the first one are commented out.

Hint: You could use a for loop to enter the assignments and along with an array You could not only shorten the amount of variables you use, but shorten the code you have written.

Line 24 the bool variable is initialized as false to make the if statements that follow the input work. This way even if all six "assign?" are out of range only one error message will print.

Lines 62 - 66 will need to be duplicated for the other five "Assign?" variables. This is where an array would be handy. If you have learned about arrays.

Line 70 I added some ()s. Not really needed, but it better conveys the intent of the formula.

Line 71 I change "100" to "100.0" . Again not necessary, but it better conveys the intent. Although this should be a constant variable. I am not sure what to call it and that is why I have not changed it yet. The "100.0" is known as a magic number and should be avoided if at all possible and replaced by a constant variable.

Lines 73 and 74 and look and compare the "%f" with line 64. The way I set that up is that line 64 will only print the whole number and no decimal point or numbers to the right of the decimal point. Where lines 73 and 74 will print a decimal point and two numbers to the right.

Lines 77 and 78 are used to replace "system("pause");". They work much the same as "system("pause");" with the "getch()" allowing any key to be pressed to continue.

This will finish parts "A" and "B" which actually combines the second part of "A" and "B" in one if statement.

Hope that helps,

Andy
Topic archived. No new replies allowed.