Question

In: Computer Science

There are logical and run time errors in the code, as well as compilation errors. **...

There are logical and run time errors in the code, as well as compilation errors.
** Your objective is get the code to run, fix any bugs in the code and
** place validation needed to prevent any memory issues. Each function has a
** description of what it does next to its prototype.

** When you find a problem, fix it in the code and note it in this header comment below.
** Please note each problem you find on a separate comment line.
** NO CREDIT for fixing a problem without adding a comment line about it in this header.
** SUBMIT THIS FIXED C++ FILE WITH YOUR ADDED COMMENTS
**
** RUBRIC:
** 1) code must compile with no errors or warnings
** 2) code runs with no bugs: 25%
** 3) fix comments in this comment header: 75%
**
** ADD YOUR COMMENTS HERE
**
** Problem 1:
** Problem 2:
** ...
** Problem n:
*/

#include <iostream>

using namespace std;

void addToList(int *); // adds integer to integer array
void displayList(int *); //displays all items in array
void dispSum(); //gives sum of all elements in array
void dispAverage(); //gives average of elements in array.

unsigned short SIZE = 0;

int main()
{
unsigned int input;
int arrayList[10];

do
{
cout << "***Main Menu***" << endl
<< "1. add number to list" << endl
<< "2. display list." << endl
<< "3. display sum." << endl
<< "4. display average." << endl
<< "0. Quit" << endl;
//clears iostream buffer
cin.clear();
cin.ignore(10, '\n');
cout << "enter a menu choice: ";
cin >> input;

switch (input)
{
case 1: addToList(arrayList);
case 2: displayList(arrayList);
case 3: dispSum();
case 4: dispAverage();
case 0:break;
default:cout << "enter a valid choice from menu:";
}

} while (input !=0);
return 0;
}

void addToList(int *arrayList)
{
int input;
cout << "enter a number int the array: ";
cin >> input;
arrayList[SIZE++] = input;
}

void displayList(int *arrayList)
{
for (int i = 0; i <= SIZE; ++i)
cout << arrayList[SIZE];
cout << endl;
}

void dispSum(int *arrayList)
{
int sum = 0;
for (int i = 0;i <= SIZE; ++i)
sum += arrayList[i];
cout << "The Sum is " << sum << "!" << endl;
}

PreviousNext

Solutions

Expert Solution

/*C++ program that fixed the logical errors and run the program without errors. Add the comments in the program with problem number and comments with necessary explanation*/
//main.cpp
#include <iostream>
using namespace std;
void addToList(int *); // adds integer to integer array
void displayList(int *); //displays all items in array

//Problem1: Missing integer pointer in function prototype
//Fixed : Pass the int *arrayList to the dispSum

void dispSum(int *arrayList);

//Problem2: Missing integer pointer in function prototype
//Fixed : Pass the int *arrayList to the dispAverage

void dispAverage(int *arrayList);
unsigned short SIZE = 0;


//start of main method
int main()
{
   unsigned int input;
   int arrayList[10];
   do
   {
       cout << "***Main Menu***" << endl
           << "1. add number to list" << endl
           << "2. display list." << endl
           << "3. display sum." << endl
           << "4. display average." << endl
           << "0. Quit" << endl;
       cout << "enter a menu choice: ";
       cin >> input;


       //Problem10: Move the below two lines of code to clear the buffer after reading user input value
       cin.clear();
       cin.ignore(10, '\n');

       switch (input)
       {


           //Problem5: Add break statement at the end of the case 1,case 2 ,case 3 ,case 4
       case 1: addToList(arrayList);break;
       case 2: displayList(arrayList);break;


           //Problem3: Missing array list to the function dispSum
           //Fixed : Pass the arrayList array name to the function calling,dispSum
       case 3: dispSum(arrayList);

                   break;


           //Problem4: Missing array list to the function dispSum
           //Fixed : Pass the arrayList array name to the function calling,dispSum
       case 4: dispAverage(arrayList);

                    break;


       case 0:break;
       default:cout << "enter a valid choice from menu:";
       }
   } while (input !=0);
   system("pause");
   return 0;
}

void addToList(int *arrayList)
{
   int input;
   cout << "enter a number int the array: ";
   cin >> input;
   arrayList[SIZE++] = input;
}

void displayList(int *arrayList)
{
   //Problem6 : display the value stored at index,i
   //Remove the SIZE in [SIZE] with [i] in cout
   //Problem7 : Run the for loop until the i-value is less than SIZE value.
   //Replace the i<=SIZE with i <size
   for (int i = 0; i < SIZE; ++i)

       cout << arrayList[i]<<" ";
   cout << endl;
}

void dispSum(int *arrayList)
{
   int sum = 0;
   //Problem8 : Run the for loop until the i-value is less than SIZE value.
   //Replace the i<=SIZE with i <size

   for (int i = 0;i < SIZE; ++i)
       sum += arrayList[i];
   cout << "The Sum is " << sum << "!" << endl;
}

//Problem9: Add the missing code for the function ,dispAverage
//gives average of elements in array.

void dispAverage(int *arrayList)
{
   int sum = 0;
   for (int i = 0;i < SIZE; ++i)
       sum += arrayList[i];
   cout << "The average is " << sum/SIZE << "!" << endl;
}

Sample Output:


Related Solutions

Every time I run this code I get two errors. It's wrote in C# and I...
Every time I run this code I get two errors. It's wrote in C# and I can't figure out what I'm missing or why it's not running properly. These are two separate classes but are for the same project. using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace RecursiveMethod { class Program { static bool ordering = true; static string str; static int quantity = 0; static string Invoic = string.Empty; static double itemPrice; static double...
Quality assurance department of software house reveals that 6.5% of programming code contain logical errors. Quality...
Quality assurance department of software house reveals that 6.5% of programming code contain logical errors. Quality check software is developed to analyze code and in a test, identifies the error in 90% of the code with errors and 5% of those without. If code is marked by the program as possibly having a logical error, what is the probability that the code DOES have an error?
There are two errors in this code. Identify the errors and give the correct code that...
There are two errors in this code. Identify the errors and give the correct code that will make the program to display the following output: Rectangle: height 2.0 width 4.0 Area of the Rectangle is 8.0 ----- public interface Shape { public double getArea(); } class Rectangle implements Shape { double height; double width; public Rectangle(double height, double width) { this.height=height; this.width=width; } public double getArea() { return height*width; } public String toString() { return "Rectangle: height "+height+" width "+width;...
Reminder, remember to code, evaluate, save, execute (run), and check the Python program for possible errors....
Reminder, remember to code, evaluate, save, execute (run), and check the Python program for possible errors. Remember to create the sales.txt file with the following data (1000, 2000, 3000, 4000, 5000). Tip, the sales.txt file must be in the same directory as the read_sales_text_file.py Python program. def main(): sales_file = open('sales.txt', 'r') for line in sales_file: amount = float(line) print('$', format(amount, '.2f')) sales_file.close() main() Output after you successfully run your code: $ 1000.00 $ 2000.00 $ 3000.00 $ 4000.00 $...
I created a shoppingcart program but I'm getting a few compilation failed errors: 1) Tests that...
I created a shoppingcart program but I'm getting a few compilation failed errors: 1) Tests that ItemToPurchase("Bottled Water", "Deer Park, 12 oz.", 1, 10) correctly initializes item compilation failed 2) Tests default constructor and accessors for ShoppingCart Compilation failed 3)Tests ShoppingCart("John Doe", "February 1, 2016") correctly initializes cart Compilation failed 4) Tests that getNumItemsInCart() returns 6 (ShoppingCart) compilation failed 5) Test that getCostOfCart() returns 9 (ShoppingCart) compilation failed Complete program: itemtopurchase.java public class ItemToPurchase { // instance variables private String...
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....
1.The below code has some errors, correct the errors and post the working code. Scanner console...
1.The below code has some errors, correct the errors and post the working code. Scanner console = new Scanner(System.in); System.out.print("Type your name: "); String name = console.nextString(); name = toUpperCase(); System.out.println(name + " has " + name.Length() + " letters"); Sample Ouptut: Type your name: John JOHN has 4 letters    2. Write a code that it reads the user's first and last name (read in the entire line as a single string), then print the last name   followed by...
Please do it in C++. Please comment on the code, and comments detail the run time...
Please do it in C++. Please comment on the code, and comments detail the run time in terms of total operations and Big O complexities. 1. Implement a class, SubstitutionCipher, with a constructor that takes a string with the 26 uppercase letters in an arbitrary order and uses that as the encoder for a cipher (that is, A is mapped to the first character of the parameter, B is mapped to the second, and so on.) Please derive the decoding...
Could someone point out the logical error in this code? The code seems to start at...
Could someone point out the logical error in this code? The code seems to start at the last input. But I want the code to output something like this: Enter name: A Enter time:5 Enter name:B Enter time: 4 Enter name: C Enter time:3 Enter name:D Enter time: 9 Enter name: E Enter time: 11 A process is started and ends in 5 sec. 1 sec passed... 2 sec passed... 3 sec passed... 4 sec passed... 5 sec passed... B...
Run the following code and explain what the code does Run this code at least twice...
Run the following code and explain what the code does Run this code at least twice and take screenshots Explain what the code does What is the running result supposed to be What caused this issue? #include <thread> #include <iostream> using namespace std; const unsigned int NTHREADS = 20; const int ITERS = 10000000; int counter; void increment() {        for (int i = 0; i < ITERS; i++)                      counter++;               } void decrement() {        for (int i...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT