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...
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" } ...
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
(JAVA) Implementing a Program Design a program that prompts the user for twenty numbers. If the...
(JAVA) Implementing a Program Design a program that prompts the user for twenty numbers. If the number is positive, then add the number to the current sum. If the number is negative, then subtract the sum by one. Implement just the main method. Assume that all libraries are imported, and class has been declared.
The program will first prompt the user for a range of numbers. Then the game will...
The program will first prompt the user for a range of numbers. Then the game will generate a random number in that range. The program will then allow the user to guess 3 times. Each time the person takes a guess, if it is not the number then the game should tell them to guess higher or lower. They only get 3 guesses. If they have not guessed the number in three tries then the game should tell them the...
in java code Modify your program as follows: Ask the user for the number of hours...
in java code Modify your program as follows: Ask the user for the number of hours worked in a week and the number of dependents as input, and then print out the same information as in the initial payroll assignment. Perform input validation to make sure the numbers entered by the user are reasonable (non-negative, not unusually large, etc). Let the calculations repeat for several employees until the user wishes to quit the program. Remember: Use variables or named constants...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT