Question

*R 2B PROGRAM 1B: INSERTION SORT Find and rix errors. Ron the program once and save the outpat as a conment at the end of the source file Changed by: IDE #include <iostream> using namespace std: void insertionSort (int aryll, int size) int main double list(1001-(50.1, 30.2, 80.3, 10.5, 30.2, 40.9, 90.8, 30.2, 80.8, 30.51 int size = 10; for (int í = 0; i < size; i++) cout<< listi << cout << endl: void insertionSort (int array, int size) for (int íー0: i < size: i++) cout << list[i] << ; cout << endl: return 0:

C++ problem. Please just find and fix the errors and highlight it afterwards. Do not add any new comments or remove comments from teachers. Thank you very much

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,
Thank you for the opportunity to help you with your assignment.

The following is the code for your requirements as in the instructions.
The output and screenshot is at end of this answer.

I have made sure to include all that is necessary for you to complete this assignment.
In case, if something is missing, drop me a comment.

If you have any queries, need changes in the code or need help in following the code, leave me a comment.
I will reply within reasonable time.

If this answer helps you with your assignment, do upvote it.
Your vote will motivate me to work better.

Here is the code.
//File: program1b.cpp


#include <iostream>
using namespace std;
void insertionSort(double ary[], int size);
int main() {
double list[100] = {50.1, 30.2, 80.3, 10.5, 30.2, 40.9, 90.8, 30.2, 80.8, 30.5};
int size = 10;
for(int i = 0; i < size; i++) {
cout << list[i] << " ";
}
cout << endl;
insertionSort(list, size);
for(int i = 0; i < size; i++) {
cout << list[i] << " ";
}
cout << endl;
return 0;
}
void insertionSort(double ary[], int size) {
double temp;
int curr;
int walk;
for(curr = 1; curr < size; curr++) {
temp = ary[curr];
walk = curr - 1;
while(walk >= 0 && temp > ary[walk]) {
ary[walk+1] = ary[walk];
walk--;
}
ary[walk+1] = temp;
}
}

And here is the output when you execute the code
//Output:

List of changes

===============

1. Missing semicolon in function declaration
2. Not invoking the insertionSort function in the main function. Instead of void insertionSort(int array[], int size); should use insertionSort(list, size);
3. The array values provided in the main function is of double type, hence we need to change the first parameter in insertionSort function declaration and definition as double. That is - void insertionSort(double ary[], int size);
4. The data type of temp variable that is used to store the value of the array must be declared as double.
5. According to the comments, the insertionSort function is supposed to sort the array in descending order. But it sorts in ascending order. To fix that issue, we need to change the condition in the "while" statement as follows - while(walk >= 0 && temp > ary[walk]) {

Add a comment
Answer #2

Hi,
Thank you for the opportunity to help you with your assignment.

The following is the code for your requirements as in the instructions.
The output and screenshot is at end of this answer.

I have made sure to include all that is necessary for you to complete this assignment.
In case, if something is missing, drop me a comment.

If you have any queries, need changes in the code or need help in following the code, leave me a comment.
I will reply within reasonable time.

If this answer helps you with your assignment, do upvote it.
Your vote will motivate me to work better.

Here is the code.
//File: program1b.cpp


#include <iostream>
using namespace std;
void insertionSort(double ary[], int size);
int main() {
double list[100] = {50.1, 30.2, 80.3, 10.5, 30.2, 40.9, 90.8, 30.2, 80.8, 30.5};
int size = 10;
for(int i = 0; i < size; i++) {
cout << list[i] << " ";
}
cout << endl;
insertionSort(list, size);
for(int i = 0; i < size; i++) {
cout << list[i] << " ";
}
cout << endl;
return 0;
}
void insertionSort(double ary[], int size) {
double temp;
int curr;
int walk;
for(curr = 1; curr < size; curr++) {
temp = ary[curr];
walk = curr - 1;
while(walk >= 0 && temp > ary[walk]) {
ary[walk+1] = ary[walk];
walk--;
}
ary[walk+1] = temp;
}
}

And here is the output when you execute the code
//Output:

G program1b.cppx TERMINAL 1: cmd #include <iostream > E:ws\vscodelcpp>g++ program1b.cpp && а.exe 58.1 38.2 88.3 10.5 30.2 40.9 90.8 30.2 80.8 38.5 90.8 80.8 80.3 50.1 48.9 30.5 30.2 30.2 30.2 18.5 3 using namespace std; 4 5 void insertionSort (double ary[], int size): 6 7 int main) E: lwslvscode lcpp double list [100]50.1, 30.2, 80.3, 10.5, 30.2, 40.9, 90.8, 30.2, 80.8, 30.5; int size 1; 10 for(int í ー 0; i < size; i++) { cout << list[i] << ; 12 13 14 15 16 17 18 19 20 21 cout << endl; insertionSort (list, size); for(int í ー 0; i < size; i++) { cout <<list[i]<< cout << endl; 23 24. 25 26 void insertionSort (double ary], int size) 27 28 29 30 31 32 return e double temp; int curr: int walk; for(curr-1; currくsize; curr++) { temp ary[curr];

List of changes

===============

1. Missing semicolon in function declaration
2. Not invoking the insertionSort function in the main function. Instead of void insertionSort(int array[], int size); should use insertionSort(list, size);
3. The array values provided in the main function is of double type, hence we need to change the first parameter in insertionSort function declaration and definition as double. That is - void insertionSort(double ary[], int size);
4. The data type of temp variable that is used to store the value of the array must be declared as double.
5. According to the comments, the insertionSort function is supposed to sort the array in descending order. But it sorts in ascending order. To fix that issue, we need to change the condition in the "while" statement as follows - while(walk >= 0 && temp > ary[walk]) {

Add a comment
Know the answer?
Add Answer to:
C++ problem. Please just find and fix the errors and highlight it afterwards. Do not add...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A) Fix any errors to get the following program to run in your environment.               B)...

    A) Fix any errors to get the following program to run in your environment.               B) Document each line of code with comments and describe any changes you had to make to the original code to get it to work. C) Write a summary of what your final version of the program does. You may also add white space or reorder the code to suit your own style as long as the intended function does not change. Program 3 #include...

  • Can anyone help me with this one? I really appreciate it! Find and fix errors and...

    Can anyone help me with this one? I really appreciate it! Find and fix errors and Simplify the createGroups() function using only one loop instead of two #include using namespace std; const double NO_STU = 40; void createGroups(const string myClass[], int no_stu, string group1, string group2); void printList(const string [], int); int main() { int no_stu = 8; string myClass[NO_STU] = { "Linda", "Bob", "Mary", "Jo", "Tim", "Jamie", "Ann", "Tom" }; string group1[NO_STU % 2]; string group2[NO_STU % 2]; createGroups(no_stu,...

  • Find and fix the errors in this C++ code: * This program illustrates a variety of...

    Find and fix the errors in this C++ code: * This program illustrates a variety of common loop errors. * Fix the errors in each section. */ #include <iostream> using namespace std; int main() { cout << "Welcome to Loop World" << endl; // SECTION I: update comment below on how you fixed this section's code, and tests run // FIX = // TESTS: cout << endl; cout << "******************" << endl; cout << "Section I" << endl; cout <<...

  • Please help fix my code C++, I get 2 errors when running. The code should be...

    Please help fix my code C++, I get 2 errors when running. The code should be able to open this file: labdata.txt Pallet PAG PAG45982IB 737 4978 OAK Container AYF AYF23409AA 737 2209 LAS Container AAA AAA89023DL 737 5932 DFW Here is my code: #include <iostream> #include <string> #include <fstream> #include <vector> #include <cstdlib> #include <iomanip> using namespace std; const int MAXLOAD737 = 46000; const int MAXLOAD767 = 116000; class Cargo { protected: string uldtype; string abbreviation; string uldid; int...

  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

  • C++ getline errors I am getting getline is undefined error messages. I would like the variables...

    C++ getline errors I am getting getline is undefined error messages. I would like the variables to remain as strings. Below is my code. #include <iostream> #include<string.h> using namespace std; int index = 0; // variable to hold how many customers are entered struct Address //Structure for the address. { int street; int city; int state; int zipcode; }; // Customer structure struct Customer { string firstNm, lastNm; Address busAddr, homeAddr; }; // Functions int displayMenu(); Customer getCustomer(); void showCustomer(Customer);...

  • Can I get some help with this question for c++ if you can add some comments...

    Can I get some help with this question for c++ if you can add some comments too to help understand that will be much appreciated. Code: #include <cstdlib> #include <getopt.h> #include <iostream> #include <string> using namespace std; static long comparisons = 0; static long swaps = 0; void swap(int *a, int *b) {     // add code here } void selectionSort(int *first, int *last) {     // add code here } void insertionSort(int *first, int *last) {     // add code here }...

  • please help with the operator overloading lab (intArray) in c++ will provide what it is being...

    please help with the operator overloading lab (intArray) in c++ will provide what it is being required and the code that was given from the book. the code that was provided is below ------------------------------------------------------------------------------------------------------------------------- // iadrv.h #ifndef _IADRV_H #define _IADRV_H #include "intarray.h" int main(); void test1(); void test2(); void test3(); void test4(); void test5(); void test6(); void test7(); void test8(); void test9(); void test10(); void test11(); void test12(); void test13(); void test14(); void test15(); void test16(); void test17(); void test18();...

  • Open with Drive N // Test1B-Debug.cpp .This is a program with 6 errors in it Find and fix each er...

    NEED HELP WITH THIS!! Open with Drive N // Test1B-Debug.cpp .This is a program with 6 errors in it Find and fix each error. // Simple Container made from circular paper // Test-1B Debug Program #include #include

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT