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...
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...
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" } ...
Write a java program that asks user to enter a set of positive integer values. When...
Write a java program that asks user to enter a set of positive integer values. When the user stops (think of sentinel value to stop), the program display the maximum value entered by the user. Your program should recognize if no value is entered without using counter.
In C++, Write a program that accepts exactly ten (10) integer numbers from the user. When...
In C++, Write a program that accepts exactly ten (10) integer numbers from the user. When the user is done inputting these numbers, the program prints back: (i) the sum of the 10 numbers, (ii) the minimum value from the 10 numbers, and (iii) the maximum value from the 10 numbers.
Develop a Java program for this problem where the user inputs an Earth age and the...
Develop a Java program for this problem where the user inputs an Earth age and the program will then display the age on Mercury, Venus, Jupiter, and Saturn. The values for d are listed in the table. Planet d = Approximate Number of Earth Days for This Planet to Travel Around the Sun Mercury 88 Venus 225 Jupiter 4380 Saturn 10767
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT