Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ (Urgent Help Needed please) 1) Vectors: ( User Input ) : Write a program tha

ID: 3758554 • Letter: C

Question

C++ (Urgent Help Needed please)

1) Vectors: (User Input) :

Write a program that: (a) Asks the user to enter a value and place that value into a vector (Note: user can input any numbers). (b) Keeps asking for values and placing those values into that vector until the user enters -1. (c) Prints out all the elements. (d) Prints out all the elements backward. (e) Prints out every other elements.

2) Functions with Arrays and Vectors:

2a) Arrays

#include <iostream>

using namespace std;

int main(){

int x;

cout<<"How many elements do you want to enter? ";

cin>>x;

int fib[x];

cout<<"Enter number 1 and 2: "<< endl;

for (int i=0; i<x; i++) {

cin>>fib[i];

}

for (int i=0; i<x; i++)

{

cout<<fib[i]<<" ";   

}

return 0;

  }

Modify the program above and do the following: (Note: user can input only integer numbers). (a) Change the three print out into functions. (b) Write a function that computes and returns the sum of the elements. (c) Write a function that computes and returns the mean of the elements using the previous function. (d) Write a function that returns the number of elements divisible by 2.

3) Vectors

Modify the program you wrote in q1 (Vectors: (User Input) :the question above:#1) and do the following:

(Note: user will input only integer numbers). (a) Change the three print out into functions. (b) Write a function that computes and returns the sum of the elements. (c) Write a function that computes and returns the mean of the elements using the previous function. (d) Write a function that returns the number of elements divisible by 2.

Explanation / Answer

int main()
{
int input = 0;
while(input != -1){
vector<int> V;
cout << "Enter your numbers to be evaluated: " << endl;
cin >> input;
V.push_back(input);
write_vector(V);
}
return 0;
}

To print in reverse :

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
vector<int> vectorObject;
int i;

while(i!=-1)
vectorObject.push_back(i);

cout << "Initial: ";
for(i = 0; i <vectorObject.size(); i++)
cout << vectorObject[i] << " ";
cout << endl;

reverse(vectorObject.begin(), vectorObject.end());

cout << "Reversed: ";
for(i = 0; i <vectorObject.size(); i++)
cout << vectorObject[i] << " ";

return 0;
}

2. function with arrays:

#include <iostream>

using namespace std;

int sum (int n[],int times);
int average (int n[] ,int times);
int divisible(int n[],int times);

int main(){
  
int n, i,times;
int inputtednumbers[100];

do
{

cout << " How many numbers would you like to enter?";
cin >> i;
  
times = 0;
  
if ( i == 0)
return 0;

do{
cout << "Enter a number 1: ";
cin >> n;
inputtednumbers[times] = n;

times++;
}while( times < i);

cout << " The sum of the numbers you entered is: " << sum(inputtednumbers,i)<< endl;

cout << "The average of the numbers you entered is: " <<average(inputtednumbers,i)<< endl;
cout<<"The number of elemnts divisible by 2 is:"<< divisible(inputtednumbers,i)<<endl;
}while(i != 0);

return 0;
}

int sum (int n[],int times){
int sumTotal = 0;

for(int x = 0; x < times; x++)
sumTotal += n[x];

return sumTotal;
}

int average (int n[],int times){
int total = sum(n,times);
return total/times;
}
int divisible(int n[],int times)
{
int c=0;
for(int i=0;i<times;i++)
{
if(n[i]%2==0)
c++;
}
return c;
}

2. modified the given code and written using the functions:

#include <iostream>
using namespace std;
int sum (int n[],int y);
int average (int n[] ,int y);

int main(){
int x;
cout<<"How many elements do you want to enter? ";
cin>>x;
int fib[x];
cout<<"Enter numbers "<< endl;
for (int i=0; i<x; i++) {
cin>>fib[i];
}
for (int i=0; i<x; i++)
{
cout<<fib[i]<<" ";
}
cout << " The sum of the numbers you entered is: " << sum(fib,x)<< endl;
cout << "The average of the numbers you entered is: " <<average(fib,x)<< endl;

return 0;
}
int sum (int n[],int y){
int sumTotal = 0;

for(int x = 0; x < y; x++)
sumTotal += n[x];

return sumTotal;
}

int average (int n[],int y){
int total = sum(n,y);
return total/y;
}

3. Using vectors:

#include <iostream>
#include <vector>

using namespace std;
double average(vector<double> v)
{
if (v.size() == 0) return 0;
double sum = 0;
for (int i = 0; i < v.size(); i++)
sum = sum + v[i];
return sum / v.size();
}
double add(vector<double> v)
{
if (v.size() == 0) return 0;
double sum = 0;
for (int i = 0; i < v.size(); i++)
sum = sum + v[i];
return sum ;
}
double divisible(vector<double> v)
{
if (v.size() == 0) return 0;
double s = 0;
for (int i = 0; i < v.size(); i++)
{
if(v[i]%2==0)
s++;
}
return s;
}
int main()
{
vector<double> num;
bool more = true;
while (more)
{
double s;
cout << "Please enter a number, 0 to quit: ";
cin >> s;
if (s == 0)
more = false;
else
num.push_back(s);
}
vector <int>sum=add(num);
cout<<sum of numbers is:<<sum<<endl;

vector<int> avg= average(num);
cout<<average is:<<avg<<endl;
vector<int>div=divisible(num);
cout<<number of elements divisible by 2 :<<div<<endl;

return 0;
}

}