Question

WRITE A PROGRAM IN C++ THAT DECLARES AN INTEGER VECTOR V 1.)INITIALIZE THE VECTOR, V, WITH...

WRITE A PROGRAM IN C++ THAT DECLARES AN INTEGER VECTOR V

1.)INITIALIZE THE VECTOR, V, WITH 10 INTEGERS WITH VALUES FROM 1 TO 10

2.)OUTPUT THE VECTOR IN DESCENDING ORDER, THEN ASCENDING ORDER

2.)ADD ALL THE EVEN NUMBERS
3.)ADD ALL THE ODD NUMBERS

4.)OUTPUT THE SUM OF EVEN INTEGERS

5.)OUTPUT THE SUM OF ODD INTEGERS

7.)OUTPUT THE PRODUCT OF EVEN INTEGERS

8.)OUTPUT THE PRODUCT OF ODD INTEGERS

SAMPLE:

Vector:

2 4 3 5 2 3 8 9 1 10
Ascending Order: 1 2 2 3 3 4 5 8 9 10

Descending order: 10 9 8 5 4 3 3 2 2 1

Even Sum: 2 + 4 + 2 + 8 + 10 = 26

Odd Sum: 3 + 5 + 3 + 9 + 1 = 21

Even PRODUCT: 2 * 4 *  2 * 8 * 10 = 1280

Odd PRODUCT: 3 * 5 * 3 * 9 * 1 = 405

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main() {
vector<int> v{2 ,4 ,3 ,5 ,2 ,3 ,8 ,9 ,1 ,10};
// v.push_back(2);
// v.push_back(4);
// v.push_back(3);
// v.push_back(5);
// v.push_back(2);
// v.push_back(3);
// v.push_back(8);
// v.push_back(9);
// v.push_back(1);
// v.push_back(10);
sort(v.begin(), v.end());
cout << "Ascending \n";
for (auto x : v)
cout << x << " ";

cout<<endl;
  
sort(v.begin(), v.end(), greater<int>());
cout << "Descending \n";
for (auto x : v)
cout << x << " ";
  
cout<<endl;
int even=0;
for(int i=0;i<v.size();i++){
if(v[i]%2==0){
even+=v[i];
}
}
cout<<"even sum"<<even<<endl;
  
int odd=0;
for(int i=0;i<v.size();i++){
if(v[i]%2!=0){
odd+=v[i];
}
}
cout<<"odd sum"<<odd<<endl;


int evenprod=1;


int oddprod=1;
for(int i=0;i<v.size();i++){
if(v[i]%2!=0){
oddprod*=v[i];
}
if(v[i]%2==0){
evenprod=evenprod*v[i];
  
}
}
cout<<"even product "<<evenprod<<endl;

cout<<"odd product "<<oddprod <<endl;


   return 0;
}

Add a comment
Know the answer?
Add Answer to:
WRITE A PROGRAM IN C++ THAT DECLARES AN INTEGER VECTOR V 1.)INITIALIZE THE VECTOR, V, WITH...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C language 1. Write a program to declare and initialize an array of size 5...

    In C language 1. Write a program to declare and initialize an array of size 5 and place odd numbers 3, 5, 7,9 and 11 in it. Declare and initialize another array of size 5 and place even numbers 4, 6, 8, 10 and 12 in it. Write a for loop to add each element and place it in a third array of the same dimensions. Display each array.

  • Write code that declares an array of integer values that will represent the first five even...

    Write code that declares an array of integer values that will represent the first five even numbers: 2, 4, 6, 8, 10. Add code to initialize each element of the array with the even number values shown above. Add code to calculate the product of the array elements, and store the result in a variable named product. You must access the array elements to accomplish this. Do not write product = 2 * 4 * 6 * 8 * 10;...

  • Write a C program to do the following 1) request user to enter 10 integer into...

    Write a C program to do the following 1) request user to enter 10 integer into an array 2) sort the array in ascending order 3) display the sorted array 4) count the number of odd and even number in the array and print out the result 5) add the value of the odd number and even number and calculate the average of the odd and even number. display the result 6) write function addNumber() to add all the number...

  • Develop c program and give me screenshot of output? Develop a program that accepts integers from...

    Develop c program and give me screenshot of output? Develop a program that accepts integers from command line and uses fork() to have 4 child processes that will do sorting the integers into ascending order, computing the sum of the integers, and counting the number .of even numbers and the number of odd numbers respectively.

  • in c++ Program 1 Write a program that sorts a vector of names alphabetically using the...

    in c++ Program 1 Write a program that sorts a vector of names alphabetically using the selection sort and then searches the vector for a specific name using binary search. To do that, you need to write three functions I. void selSort (vector string &v: sorts the vector using selection sort 2. void display (const vector <string & v): displays the vector contents . int binSearch (const vector <ing& v, string key): searches the vector for a key returns the...

  • Write C program to take four integer inputs from user (A, B, C, D) then, your...

    Write C program to take four integer inputs from user (A, B, C, D) then, your program should return the sum of even numbers (if there any even numbers) and the multiplication of the odd numbers (if there any odd numbers). For example: if A=1, B=2, C=3, D=4, your program will return: sum=6, multiplication=3. if A=6, B=2, C=8, D=4, your program will return: sum=20, multiplication=0. if A=1, B=3, C=5, D=4, your program will return: sum=4, multiplication=15.

  • In C++: Write a program that reads a list of integers, and outputs the two smallest...

    In C++: Write a program that reads a list of integers, and outputs the two smallest integers in the list, in ascending order. The list is preceded by an integer indicating how many numbers are in the list. If the input is: 5 10 5 3 21 2, the output is: 2 3 To achieve the above, first read the integers into a vector.

  • Please write a C++ program that will accept 3 integer numbers from the user, and output...

    Please write a C++ program that will accept 3 integer numbers from the user, and output these 3 numbers in order, the sum, and the average of these 3 numbers. You must stop your program when the first number from the user is -7. Design Specifications: (1) You must use your full name on your output statements. (2) You must specify 3 prototypes in your program as follows: int max(int, int, int); // prototype to return maximum of 3 integers...

  • C programming! Write a program that reads integers until 0 and prints the sum of values...

    C programming! Write a program that reads integers until 0 and prints the sum of values on odd positions minus the sum of values on even positions. Let ?1, ?2, … , ??, 0 be the input sequence. Then the program prints the value of ?1 − ?2 + ?3 − ?4 + ⋯ ??. The input is a sequence of integers that always contains at least 0 and the output will be a single number. For example, for input...

  • Please write a C++ Program for the following problem >> Vector: Calculation the sum of each...

    Please write a C++ Program for the following problem >> Vector: Calculation the sum of each two adjacent Numbers.... >>【Description】 Read integer numbers from keyboard and store them into a vector. Calculation the sum of each two adjacent Numbers, store the result into another vector. Then output the result inversely. For example, if the input is 1 2 3 4 5 then the output is 9 7 5 3 【 Input】 There are 5 test cases. For each case, there...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT