Answer to Question #71583 in C++ for m

Question #71583
Write a program that asks the user to enter an array of random numbers, then sort the numbers (ascending order), then print the new array, after that asks the user for a new two numbers and add them to the same array and keep the array organization.
using while,for and array if possible
1
Expert's answer
2017-12-07T07:14:07-0500
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void printArray(const vector<int>& v)
{
cout << "Content of the array: \n";
for (const auto& n : v)
{
cout << n << " ";
}
cout << '\n';
}

int main()
{
cout << "Enter the number of elements in array: ";
int size;
cin >> size;
vector<int>v(size), copy(size+2);
cout << "Please enter the elements: ";
for (int i = 0; i < size; ++i)
{
cin >> v[i];
copy[i] = v[i];
}

cout << "Original array: \n";
printArray(v);

sort(v.begin(), v.end());
cout << "Sorted original array: \n";
printArray(v);

cout << "Please enter two more numbers to the array: ";
for (int i = 0; i < 2; ++i)
{
cin >> copy[size + i];
}

cout << "Extended original array: \n";
printArray(copy);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS