Question

In: Computer Science

1)     The provided reorder.cpp file has some code for a function that reorders the values in 3...

1)     The provided reorder.cpp file has some code for a function that reorders the values in 3 parameters so that they are in ascending order.

·Start with a function void swap(int &val1, int &val2) that swaps the values of val1 and val2. You will need a local variable to hold the value of one of the parameters to let you do this.

·Write the reorder function called in main(). It should call swap() to exchange values when appropriate.

·Add statements to main() to be able to try out the 3 cases below

Driver to test reorder()

Case 1: values are already in correct order -- leave them alone

1 2 3

Case 2: first > second and first < third

-5 -2 0

Case 3: first > second > third

6 8 10

reorder.cpp

// Reordering values in variables
#include <iostream>

using namespace std;

// swap the values in the two reference integer parameters
void swap(int &val1, int &val2) {

}

// reorder 3 integer values so that
// first parameter has smallest value, second parameter has middle value,
// third parameter has largest value


int main()
{
    int val1 = 1, val2 = 2, val3 = 3;

    cout << "Driver to test reorder()" << endl;
    cout << "Case 1: values are already in correct order -- leave them alone" << endl;
    reorder(val1, val2, val3);
    cout << val1 << " " << val2 << " " << val3 << endl;


    return 0;
}

Solutions

Expert Solution

// Reordering values in variables
#include <iostream>

using namespace std;

// swap the values in the two reference integer parameters
void swap(int &val1, int &val2) {
    int temp = val1;
    val1 = val2;
    val2 = temp;
}

// reorder 3 integer values so that
// first parameter has smallest value, second parameter has middle value,
// third parameter has largest value
void reorder(int &val1, int &val2, int &val3) {
    if (val1 >= val2) {
        swap(val1, val2);
    }
    if (val2 >= val3) {
        swap(val2, val3);
    }
    if (val1 >= val2) {
        swap(val1, val2);
    }
}

int main() {
    int val1 = 1, val2 = 2, val3 = 3;

    cout << "Driver to test reorder()" << endl;
    cout << "Case 1: values are already in correct order -- leave them alone" << endl;
    reorder(val1, val2, val3);
    cout << val1 << " " << val2 << " " << val3 << endl;

    val1 = -2;
    val2 = -5;
    val3 = 0;
    cout << "Case 2: first > second and first < third" << endl;
    reorder(val1, val2, val3);
    cout << val1 << " " << val2 << " " << val3 << endl;

    val1 = 10;
    val2 = 8;
    val3 = 6;
    cout << "Case 3: first > second > third" << endl;
    reorder(val1, val2, val3);
    cout << val1 << " " << val2 << " " << val3 << endl;
    return 0;
}


Related Solutions

Code needed in C++, make changes to the file provided (18-1, has 3 files) Chapter 18...
Code needed in C++, make changes to the file provided (18-1, has 3 files) Chapter 18 Stacks and Queues ----------------------------------------------------------------------------------------------------- capacity is just 5 1. push 6 numbers on the stack 2. catch the overflow error in a catch block 3. pop one element, which means your capacity is now down to 4 4. push the element that was rejected earlier 5. verify your entire stack by popping to show the new numbers. IntStack.h #include <memory> using namespace std; class...
please use text only! Instructions Consider the provided C++ code in the main.cpp file: The function...
please use text only! Instructions Consider the provided C++ code in the main.cpp file: The function func2 has three parameters of type int, int, and double, say a, b, and c, respectively. Write the definition of func2 so that its action is as follows: Prompt the user to input two integers and store the numbers in a and b, respectively. If both of the numbers are nonzero: If a >= b, the value assigned to c is a to the...
The code file is attached. Portion of the code is given (1, 2, 3, 4). You...
The code file is attached. Portion of the code is given (1, 2, 3, 4). You have to write the code for the remaining items (5, 6, 7). #include <iostream> using namespace std; struct node { int data; node* next; }; node* head=NULL; void appendNode(int value) { node *newNode, *curr; newNode = new node(); newNode->data=value; newNode->next=NULL; if (!head) head=newNode; else { curr=head; while (curr->next) curr = curr->next; curr->next = newNode; } } void insertNode(int value) { node *newNode, *curr, *previous;...
1)     The provided sched.cpp file is missing a few lines to read in some of the data....
1)     The provided sched.cpp file is missing a few lines to read in some of the data. The comments in the code describe how there is yet another stream library that works like iostream and fstream, but for processing string’s. Fill in the necessary statements and uncomment the indicated cout statements to get the output below for the attached “sched.txt” file: CS100 Section 1 has 17 open seats It is held on MW from 8:00A to 9:15A in SC-S146 It is...
Python 3 Fix the code. It is not saving the data into the xls file Code:...
Python 3 Fix the code. It is not saving the data into the xls file Code: import tkinter as tk from tkcalendar import DateEntry from openpyxl import load_workbook from tkinter import messagebox from datetime import datetime window = tk.Tk() window.title("daily logs") window.grid_columnconfigure(1,weight=1) window.grid_rowconfigure(1,weight=1) # labels tk.Label(window, text="Bar code").grid(row=0, sticky="W", pady=20, padx=20) tk.Label(window, text="Products failed").grid(row=1, sticky="W", pady=20, padx=20) tk.Label(window, text="Money Lost").grid(row=2, sticky="W", pady=20, padx=20) tk.Label(window, text="sold by").grid(row=3, sticky="W", pady=20, padx=20) tk.Label(window, text="Working product").grid(row=4, sticky="W", pady=20, padx=20) #Working product label tk.Label(window, text="Failed...
Exercise 9 – Writing values from a list into a file Complete the function design for...
Exercise 9 – Writing values from a list into a file Complete the function design for the write_most_frequent() function, which takes 4 parameters, a string, a list of tuples, an integer, and another string: • The first string represents the name of the file to write to (with the append usage mode) • The list of tuples contains the information to write. Assume the list has already been sorted. • The integer represents the number of elements to read from...
This code assigns the maximum of the values 3 and 5 to the int variable max...
This code assigns the maximum of the values 3 and 5 to the int variable max and outputs the result int max; // your code goes here This code prompts the user for a single character and prints "true" if the character is a letter and "false" if it is not a letter // your code goes here
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...
Python 3 Code does not save properly the data in the excel file. it stores the...
Python 3 Code does not save properly the data in the excel file. it stores the data in the same row over and over # required library import tkinter as tk from tkcalendar import DateEntry import xlsxwriter # frame window = tk.Tk() window.title("daily logs") #window.resizable(0,0) # labels tk.Label(window, text="Bar code").grid(row=0, sticky="W", pady=20, padx=20) tk.Label(window, text="Products failed").grid(row=1, sticky="W", pady=20, padx=20) tk.Label(window, text="Money Lost").grid(row=2, sticky="W", pady=20, padx=20) tk.Label(window, text="Failed date").grid(row=3, sticky="W", pady=20, padx=20) # entries barcode = tk.Entry(window) product = tk.Entry(window) money...
Python 3 Fix the code so the program reads the file and see if the bar...
Python 3 Fix the code so the program reads the file and see if the bar code was already inputted 3 times if so, it ishows a warning indicating that the item was already tested 3 times Code: import tkinter as tk from tkcalendar import DateEntry from openpyxl import load_workbook from tkinter import messagebox from datetime import datetime window = tk.Tk() window.title("daily logs") window.grid_columnconfigure(1,weight=1) window.grid_rowconfigure(1,weight=1) # labels tk.Label(window, text="Bar code").grid(row=0, sticky="W", pady=20, padx=20) tk.Label(window, text="Products failed").grid(row=1, sticky="W", pady=20, padx=20) tk.Label(window,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT