In: Computer Science
Please identify each error present, explain the type of error (syntax/logic/run-time), and provide a fix which changes the given code as little as possible (i.e. don't rewrite the whole thing; just point out what is wrong and a fix for it).
5)
// Assign a grade based on the score
char score;
cin>>score;
if score < 0 && score > 100
cout<<"The score entered is
invalid"<<endl;
if (score <= 90)
grade = "A";
if (score <= 80)
grade = "B";
if (score <= 70)
grade = "C";
if (score <= 60)
grade = "D";
else
grade = "F";
6)
// if GPA 3.5 or better the student makes the dean's list, if
completedUnits 182 or more the student can graduate.
// if both, the student can graduate with honors
string gpa, completedUnits;
cin>>gpa>>completedUnits;
if (gpa > 3.5)
cout<<"Good job!\n";
cout<<"You've made the dean's
list!\n";
else if (completedUnits > 54)
cout<<"You have completed the
required number of units.\n";
cout<<"You can
graduate!\n";
else if (gpa > 3.5 || completedUnits > 54)
cout<<"Great job!\n";
cout<<"You have completed the
required number of units with a high GPA.\n";
cout<<"You are graduating with
honors!\n";
7)
// sum all the even numbers from 0 to 100, inclusive
int value = 0;
int sum;
while (sum < 100)
sum += value
value*=2;
8)
// get numbers from the user until the user enters 0. Output how
many numbers were entered and their sum.
int count;
while (input == 0)
int input;
cin>>input;
sum = sum + input;
count++;
cout<<"You entered "<<count<<"
values"<<endl;
cout<<"The sum is "<<sum<<endl;
9)
// sum all the numbers which are divisible by 3 or by 5 and in the
range 0 to 1000 inclusive
int sum;
for (i=0; i<1000; sum++)
if (sum % 3 || sum % 5)
sum += i;
cout<<"The sum is "<<sum<<endl;
5) There is a logic error.
if (score <= 90)......in this, it must be, if (score >= 90)
similarly for all if conditions scrore>=value.
Else all the grades ABCDEF will be printed
6) logical error.
as per the question, else if (completedUnits > 54) must be else if (completedUnits > 182)
and else if (gpa > 3.5 || completedUnits > 54) excecute if either of the statements is true,
since both the statements must be true, || should be replaced with &&, i.e,
else if (gpa > 3.5 && completedUnits > 54)
7) syntax error.
a statement must end with a semicolon in sum += value
there is also logical error in.....while (sum < 100)
it must be....... while (value <= 100) , as 100 is inclusive
8) logical error.
In the line ..........while (input == 0)
The while loop executes if the condition is true, i.e, in this case while loop executes iff user enters 0 that is vice-versa of the question. so it must be........while (input != 0)
9) logical error
since 1000 is also inclusive.......for (i=0; i<1000; sum++)
must be.....for (i=0; i<=1000; sum++)
and sum must be initiated to 0, i.e, int sum=0;
else it takes some garbage value and adds integers to it.