Question

In: Computer Science

"4. (Modify) Modify Program 7.14 so that the user inputs the initial set of numbers when...

"4. (Modify) Modify Program 7.14 so that the user inputs the initial set of numbers when the program runs. Have the program request the number of initial numbers to be entered."

//C++ Program 7.14 as follows

#include
#include
#include
#include
using namespace std;
int main()
{
const int NUMELS = 4;
int n[] = {136, 122, 109, 146};
int i;
vector partnums(n, n + NUMELS);
cout << "\nThe vector initially has the size of "
<< int(partnums.size()) << ",\n and contains the elements:\n";
for (i = 0; i < int(partnums.size()); i++)
cout << partnums[i] << " ";
partnums[3] = 144;
cout << "\n\nAfter replacing the fourth element, the vector has a size of "
<< int(partnums.size()) << ",\n and contains the elements:\n";
for (i = 0; i < int(partnums.size()); i++)
cout << partnums[i] << " ";
partnums.insert(partnums.begin()+1, 142);
cout << "\n\nAfter inserting an element into the second position, "
<< "\n the vector has a size of " << int(partnums.size()) << ","
<< "and contains the elements: \n";
for (i = 0; i < int(partnums.size()); i++)
cout << partnums[i] << " ";
partnums.push_back(157);
cout << "\n\nAfter adding an element to the end of the list,"
<< "\n the vector has a size of " << int(partnums.size()) << ","
<< "and contains the elements:\n";
for (i = 0; i < int(partnums.size()); i++)
cout << partnums[i] << " ";
sort(partnums.begin(), partnums.end());
cout << "\n\nAfter sorting, the vector's elements are: \n";
for (i = 0; i < int(partnums.size()); i++)
cout << partnums[i] << " ";
cout << endl;
return 0;

}

Solutions

Expert Solution

//C++ Program 7.14 as follows

#include<bits/stdc++.h>
using namespace std;
int main()
{
int num;
cout<<"Enter the number of elements in the array : ";
cin>>num;
int arr[num];

cout<<"Now enter "<<num<<" elements "<<endl;
for(int i=0;i<num;i++)
cin>>arr[i];
int i;
vector<int> partnums(arr, arr + num);
cout << "\nThe vector initially has the size of "
<< int(partnums.size()) << ",\n and contains the elements:\n";
for (i = 0; i < int(partnums.size()); i++)
cout << partnums[i] << " ";
partnums[3] = 144;
cout << "\n\nAfter replacing the fourth element, the vector has a size of "
<< int(partnums.size()) << ",\n and contains the elements:\n";
for (i = 0; i < int(partnums.size()); i++)
cout << partnums[i] << " ";
partnums.insert(partnums.begin()+1, 142);
cout << "\n\nAfter inserting an element into the second position, "
<< "\n the vector has a size of " << int(partnums.size()) << ","
<< "and contains the elements: \n";
for (i = 0; i < int(partnums.size()); i++)
cout << partnums[i] << " ";
partnums.push_back(157);
cout << "\n\nAfter adding an element to the end of the list,"
<< "\n the vector has a size of " << int(partnums.size()) << ","
<< "and contains the elements:\n";
for (i = 0; i < int(partnums.size()); i++)
cout << partnums[i] << " ";
sort(partnums.begin(), partnums.end());
cout << "\n\nAfter sorting, the vector's elements are: \n";
for (i = 0; i < int(partnums.size()); i++)
cout << partnums[i] << " ";
cout << endl;
return 0;

}

Related Solutions

In c++, modify this program so that you allow the user to enter the min and...
In c++, modify this program so that you allow the user to enter the min and maximum values (In this case they cannot be defined as constants, why?). // This program demonstrates random numbers. #include <iostream> #include <cstdlib> // rand and srand #include <ctime> // For the time function using namespace std; int main() { // Get the system time. unsigned seed = time(0); // Seed the random number generator. srand(seed); // Display three random numbers. cout << rand() <<...
Assume user will enter letters or numbers that are out of range. When user inputs invalid...
Assume user will enter letters or numbers that are out of range. When user inputs invalid values, show an alert message and ask user to enter a valid value again. Validate first and then proceed with the program. isNaN() method may be useful. Must validate user input. 1. Suggested Filename: fortune.html & fortune.js Write a program that simulates a fortune cookie. The program should display one of five unique fortunes, depending on user input. The user must enter a number...
Java Write a program that reads 4 integer numbers from the user combine it into a...
Java Write a program that reads 4 integer numbers from the user combine it into a single number. You need to ask the user to first specify how to combine the numbers (forward or backward). Note that for the backward option you need to check that the last digits should be 0, also for the forward option you need to check that the fist digit is not 0. Use for loops and switch for the menu.                       4. Write an application...
Modify the Movie List 2D program -Modify the program so it contains four columns: name, year,...
Modify the Movie List 2D program -Modify the program so it contains four columns: name, year, price and rating (G,PG,R…) -Enhance the program so it provides a find by rating function that lists all of the movies that have a specified rating def list(movie_list): if len(movie_list) == 0: print("There are no movies in the list.\n") return else: i = 1 for row in movie_list: print(str(i) + ". " + row[0] + " (" + str(row[1]) + ")") i += 1...
Python Jupiter Notebook Write a program that keeps getting a set of numbers from user until...
Python Jupiter Notebook Write a program that keeps getting a set of numbers from user until the user enters "done". Then shows the count, total, and average of the entered numbers. Example: Enter a number: 55 Enter a number: 90 Enter a number: 12 Enter a number: done You entered 3 numbers, total is 157, average is 52.33333
Write a program that asks the user to input a set of floating-point values. When the...
Write a program that asks the user to input a set of floating-point values. When the user enters a value that is not a number, give the user a second chance to enter the value. After two chances, quit reading input. Add all correctly specified values and print the sum when the user is done entering data. Use exception handling to detect improper inputs.5 pts Your code with comments A screenshot of the execution Test Case:       Enter float: 1.0...
Modify program so that input comes in as command line arguments. Sample run: ./a.out W8 4...
Modify program so that input comes in as command line arguments. Sample run: ./a.out W8 4 ME 2 finish 2! Output: Consonants: WMfnsh 1) Name your program command_consonants.c. 2) If no command line arguments was provided, your program should print a usage message “Usage: ./a.out input #include<stdio.h> int main() { printf("Input: "); int i = 0; char ch; // array to store consonants char consonant[100]={""}; //do loop to take input do { ch = getchar(); //checks to see if consonants...
Write a function printTwoLargest() that inputs an arbitrary number of positive numbers from the user. The...
Write a function printTwoLargest() that inputs an arbitrary number of positive numbers from the user. The input of numbers stops when the first negative or zero value is entered by the user. The function then prints the two largest values entered by the user. If no positive numbers are entered a message to that effect is printed instead of printing any numbers. If only one number is inputted, only the largest is printed out (see 2nd example below). Sample output:...
What is the output of the following program? Slightly modify the program so that: [Pts. 10]...
What is the output of the following program? Slightly modify the program so that: [Pts. 10] The Parent process calculates the val such a way that its value is 20 and it prints “This is parent process and val = 20”. Also, slightly modify The child process calculates the val such a way that its value is 25 and it prints “This is child process and val = 25”. int main() { int val = 15; int pid; if (pid...
PYTHON Modify the program in section Ask the user for a first name and a last...
PYTHON Modify the program in section Ask the user for a first name and a last name of several people.  Use a loop to ask for user input of each person’s first and last names  Each time through the loop, use a dictionary to store the first and last names of that person  Add that dictionary to a list to create a master list of the names  Example dictionary: aDict = { "fname":"Douglas", "name":"Lee" } ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT