Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part 1: Let each of the code fragments below be inside the main function exactly

ID: 3863707 • Letter: P

Question


Part 1: Let each of the code fragments below be inside the main function exactly as given with no surrounding code. Each fragment contains one or more errors. Comments above the code explain what the code should do.
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).

1)
// store some values into variables
    value = 3;
    name = Alice;
   int age = "18";
   int PI = 3.14;
   char firstLetter = "A";
   double price = '1.99';
   string zipCode = 90245;

2)
// compute the result of 2 + 3 * 4
   char val1 = 2;
   char val2 = 3, val3 = 4;
   char result = (val1 + val2) * val3;

3)
// compute half the area of a square with side length 5
   int side = 5;
   int area = side + side
   int halfArea = area / 2

4)
// get a number from the user and print whether it is even or odd
value = 0;
cout >> 'Enter a number: ';
cin << value;
if (value = 2)
      cout >> "You entered value ";
      cout >> "That value is even";
else
      cout >> "You entered value ";
      cout >> "That value is odd";
  
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! ";
      cout<<"You've made the dean's list! ";
else if (completedUnits > 54)
      cout<<"You have completed the required number of units. ";
      cout<<"You can graduate! ";
else if (gpa > 3.5 || completedUnits > 54)
      cout<<"Great job! ";
      cout<<"You have completed the required number of units with a high GPA. ";
      cout<<"You are graduating with honors! ";

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;

Part 2:
1) Write a simple program which asks the user for 2 whole numbers a and b and computes the sum of all the numbers in the range from the smaller number to the larger number.
e.g: a=2, b=4 -> 2 + 3 + 4 = 9
     a=4, b=2 -> same result

2) Write a simple program which asks the user to enter exam scores until the user enters -1 and prints how many A/B/C/D/F grades were given and the average score.
e.g.: 100, 75, 100, -1 ->
                            2 A, 0 B, 1 C, 0 D, 0 F
                            Average score: 91.7

3) Write a function named isDivisibleBy which takes two ints a and b and returns true if a is divisible by b, false otherwise. Call your function in the main a few times and print the results.
e.g. isDivisbleBy(6, 3) -> true
     isDivisbleBy(6, 5) -> false

4) Write a function that takes two positive int parameters base and exponent and returns base to the power exponent.
Call your function in the main a few times and print the results.
e.g: powerFn(2, 3) -> 8

a) name the function powerLib and use the math library to compute the result
b) name the function powerLoop and use a loop to compute the result

Explanation / Answer

Dear Student,

Here are the answers...

NOTE: I have commented out each section of the code......Which has errors and corrected those errors..

--------------------------------------------------------------------------------------------------------------------------------------

Code 1:

//varibale type is not declared..it should be int value = 3;

    value = 3;

//name is a variable which assigned a string value so the correct initialization would be

//string name = "Alice";

    name = Alice;
// age is type int, so it must assigned a int value, while it has been assigned a string value the correct statement would be int age = 18;

int age = "18";

//PI varibale is assigned a float value while the variable data type is int. So the correct statement would be float PI = 3.14;
   int PI = 3.14;

//The single character is represnted by single quote. So the correct statement would be...

//char firstletter = 'A'
   char firstLetter = "A";

//Price has assigned a char value. The correct statement would be...double price = 1.99
   double price = '1.99';

//Zip code is a integer type value so the varible data type would be int. So the correct statement is int zipcode = 90245
   string zipCode = 90245;

----------------------------------------------------------------------------------------------------------------------------------------

Code:2

// compute the result of 2 + 3 * 4

//varible is assigned a integer value while the type is a char value. It must be a integer type so correct statement would be.
   char val1 = 2; // int val1 = 2;
   char val2 = 3, val3 = 4; //int val2 = 3, valu3 = 4;
   char result = (val1 + val2) * val3; //int result = (val1 + val2) * val3;

----------------------------------------------------------------------------------------------------------------------------------------

Code:3


// compute half the area of a square with side length 5
   int side = 5;

//the area of squre = side * side. the correct statement would be... int area = side*side; Also the semicolon is missing in each statement.
   int area = side + side //int area = side * side;
   int halfArea = area / 2 //int halfArea = area/2;

-----------------------------------------------------------------------------------------------------------------------------------------

Code:4..

4)
// get a number from the user and print whether it is even or odd

//We are taking input value from the user so we must declare the value not initialize.So the correct staement would be.. int value;
value = 0;

//Display message should be inside double quote..and cout is written as cout <<.So the correct statement would be..
cout >> 'Enter a number: '; //cout<<"Enter a number";
cin << value; // cin>>value;

//Here we are comparing value not assigning so we must use == sign. and the statement inside the if and else block must be in a curly braces.So..


if (value = 2) //if(value ==2)

{

//Write cout as cout<<.
      cout >> "You entered value "; //cout<< "You entered value ";
      cout >> "That value is even"; //cout << "That value is even";
}

else

{
      cout >> "You entered value "; // cout << "You entered value ";
      cout >> "That value is odd";       //cout << "That value is odd";

}

------------------------------------------------------------------------------------------------------------------------------------------

Kindly Check and Verify Thanks...!!!