Question

In: Computer Science

Please identify each error present, explain the type of error (syntax/logic/run-time), and provide a fix which...

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;

Solutions

Expert Solution

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.


Related Solutions

C++ : Find the syntax errors in the following program. For each syntax error, fix it...
C++ : Find the syntax errors in the following program. For each syntax error, fix it and add a comment at the end of the line explaining what the error was. #include <iostream> #include <cmath> using namespace std; int main(){ Double a, b, c; 2=b; cout<<"Enter length of hypotenuse"<<endl; cin>>c>>endl; cout>>"Enter length of a side"<<endl; cin>>a; double intermediate = pow(c, 2)-pow(a, 2); b = sqrt(intermediate); cout<<"Length of other side is:" b<<endline; return 0; }
Please Fix Syntax Error import java.util.Scanner; public class SalaryCalc {    double Rpay = 0, Opay...
Please Fix Syntax Error import java.util.Scanner; public class SalaryCalc {    double Rpay = 0, Opay = 0;    void calPay(double hours, double rate) {        if (hours <= 40) {            Rpay = hours * rate;            Opay = 0;        } else {            double Rhr, Ohr;            Rhr = 40;            Ohr = hours - Rhr;            Rpay = Rhr * rate;   ...
To earn full credit, program must be free of syntax, run-time, and logic errors; include program...
To earn full credit, program must be free of syntax, run-time, and logic errors; include program comments; use reasonable readable variable names and prompts. To include variables in the input prompt, you must use concatenation character (+). For example: assume you already asked for input of employee's first name and last name (they are stored into variables FirstName and LastName, then use this prompt to ask for employee's weekly hours which includes the employee's full name in the input statement....
For each of the following Visual Basic code snippets, identify the syntax error.   If intX >...
For each of the following Visual Basic code snippets, identify the syntax error.   If intX > 100   lblResult.Text = "Invalid Data" End If   Dim str As String = "Hello" Dim intLength As Integer intLength = Length(str) If intZ < 10 Then   lblResult.Text = "Invalid Data"   Dim str As String = "123" If str.IsNumeric Then   lblResult.Text = "It is a number." End If   Select Case intX   Case < 0     lblResult.Text = "Value too low."   Case > 100     lblResult.Text = "Value too high."   Case Else     lblResult.Text = "Value...
Please provide an example of both a Type I Error and Type II Error. Why is...
Please provide an example of both a Type I Error and Type II Error. Why is it that increasing the sample size reduces the probability of a Type II error to an acceptable level. Please discuss.
please correct the error and fix this code: (i need this work and present 3 graphs):...
please correct the error and fix this code: (i need this work and present 3 graphs): Sampling_Rate = 0.00004; % which means one data point every 0.001 sec Total_Time = 0:Sampling_Rate:1; % An array for time from 0 to 1 sec with 0.01 sec increment Omega = 49.11; % in [rad/s] zeta=0.0542; %unitless Omega_d=49.03; % in [rad/s] Displacement_Amplitude = 6.009; % in [mm] Phase_Angle = 1.52; % in [rad] Total_No_of_Points = length(Total_Time); % equal to the number of points in...
Please provide assistance to fix the segmentation fault error I am receiving and solve the following...
Please provide assistance to fix the segmentation fault error I am receiving and solve the following problem I am working on: My goal is to build a Trie data structure in C++ that can do the following: - Capable to insert any given dictionary .txt file filled with a single word per line (i.e. file includes ant, bat, car, desk, etc.) into the Trie. A sample dictionary file that I'm working with can be found at http://txt.do/1pht5 - Prompts the...
Provide one example each of both a Type I and Type II error that could occur...
Provide one example each of both a Type I and Type II error that could occur when running predictive engines relevant to sports betting. Which is likely to be more costly and why?
This week, we are learning about categorical logic. Provide an example of each type of categorical...
This week, we are learning about categorical logic. Provide an example of each type of categorical proposition. Why is it important to understand categorical logic? Provide some examples of how you could apply these lessons to your personal and professional life.
Identify the given scenario as either a potential Type I or Type II error. Explain your...
Identify the given scenario as either a potential Type I or Type II error. Explain your decision by referring to the definitions of Type I and Type II errors. Silicon breast implants have been popular for many years for purposes of breast reconstruction and breast enlargement. Since no evidence had been collected by the drug manufacturing company nor the public, it was incorrectly assumed that they presented no harm to public health (i.e., failure to reject the null hypothesis). Currently,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT