Question

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 are 2 lines. The first line contains an integer number n, which indicates the number of data store in the vector. The second line contains n integer numbers.

  

【Output】

There is 1 row for each input case. And each row contains n-1 integer numbers.

【Sample Input】

5

1 2 3 4 5

5

1 -1 0 -1 1

4

100 900 10000 4000

【Sample Output】

9 7 5 3

0 -1 -1 0

14000 10900 1000

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

#include<iostream>

#include<vector>

using namespace std;

int main()

{

    int n, i, x, j;

   

    for( j = 1 ; j <= 5 ; j++ )

    {

        // create a vector to store values of type int

        vector<int> arr;

       

        // get user input

        cin>>n;

       

        // loop n times

        for( i = 0 ; i < n ; i ++ )

        {

            // get user input

            cin>>x;

           

            // add element to vector

          arr.push_back(x);

        }

       

        vector<int> adjacent_sum;

       

        for( i = arr.size() - 1 ; i > 0 ; i-- )

        {

            // calculate the sum of adjacent elements

            int sum = arr[i] + arr[i - 1];

          

            adjacent_sum.push_back(sum);

        }

       

        // traverse the vector

        for( i = 0 ; i < adjacent_sum.size() ; i++ )

            cout<<adjacent_sum[i]<<" ";

           

        cout<<endl;

    }

       

    return 0;

}

Sample Output

1 2 3 45 9 7 5 3 1 -1 e -1 1 14000 10900 1000 5698 74 11 15 17 15 11

Add a comment
Know the answer?
Add Answer to:
Please write a C++ Program for the following problem >> Vector: Calculation the sum of each...
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
  • please code in basic c++ program Write a program that uses the following formula: n (ax...

    please code in basic c++ program Write a program that uses the following formula: n (ax - ib) i=1 Your program will prompt the user for the number of iterations for the calculation. input values for n, x, a, b, and c. n must be a positive integer, but x, a, b, and c can be any real numbers. Then it will perform the calculation and output the result. Then, it will ask the user if they would like to...

  • write the solution of the program by python 3 language : I need the program using...

    write the solution of the program by python 3 language : I need the program using list or string or loops (while and for) or if,elif,else : You are given a sequence of n non-zero integers a1,a2,…,an. Determine if the given sequence contains a pair of adjacent elements of the same sign (both negative or both positive). Two elements are called adjacent if they stand next to each other in the sequence. Input The first line contains an integer n...

  • 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...

  • Write a C++ program that creates a ragged table (i.e. a table with rows of different...

    Write a C++ program that creates a ragged table (i.e. a table with rows of different length) using a vector of vector of integers. Your program should prompt the user for a file name, open the file, read the content of the file and store it in a vector or vectors, and finally print the content of the vector of vectors. Sample Input: Enter the file name: data.dat A sample file might contain: 6 5 2 4 5 3 2...

  • write the solution of the program by python 3 language : I need the program using...

    write the solution of the program by python 3 language : I need the program using list or string or loops (while and for) or if,elif,else : i have 15 min to submit the solution please help me   You are given a sequence of n non-zero integers a1,a2,…,an. Determine if the given sequence contains a pair of adjacent elements of the same sign (both negative or both positive). Two elements are called adjacent if they stand next to each other...

  • 3) (10 marks) Write a C program named a5q3.c which removes adjacent duplicates from a list...

    3) (10 marks) Write a C program named a5q3.c which removes adjacent duplicates from a list of integers. The program reads the standard input expecting a list of integers separated by whites- pace. After reading each integer, it should print it on a separate line by itself, unless the integer is equal to the integer read before. This means that the program behaves in the same way as the Unix utility uniq but for integers instead of lines. You must...

  • Write a C program convert.c that converts each number in an array by the sum of...

    Write a C program convert.c that converts each number in an array by the sum of that number plus 6 modulus 10. A sample input/output: Enter the length of the array: 5 Enter the elements of the array: 3 928 4 14 77 Output: 9 4 0 0 3 The program should include the following function: void convert(int *a1, int n, int *a2) The function converts every element in array a1 of length n to an output array a2. The...

  • write the solution of the program by python 3 language : I need the program using...

    write the solution of the program by python 3 language : I need the program using list or string or loops (while and for) or if,elif,else : Alex got a sequence of n integers a1,a2,…,an as a birthday present. Alex doesn't like negative numbers, so he decided to erase the minus signs from all negative numbers. As a result, he got a sequence of non-negative numbers. Print the resulting sequence. For example, if the sequence is  1, 5, -3, 4,...

  • Write a program to find product of a row vector, U having dimension 1 x 2...

    Write a program to find product of a row vector, U having dimension 1 x 2 with a 2 x 2 matrix, A. The input to the program includes initialized row vector, U and matrix, A. The expected output is a transformed row vector. You can test your program using following example: A = 2 4 5 1 4 5 1 and U = [3 2] The result of product U.A is a transformed row vector: [16 14] Q2. Write...

  • (C programming) Given a sequence of numbers a1, a2, a3, ..., an, find the maximum sum...

    (C programming) Given a sequence of numbers a1, a2, a3, ..., an, find the maximum sum of a contiguous subsequence of those numbers. Note that, a subsequence of one element is also a contiquous subsequence. Input The input consists of multiple datasets. Each data set consists of: n a1 a2 . . an You can assume that 1 ≤ n ≤ 5000 and -100000 ≤ ai ≤ 100000. The input end with a line consisting of a single 0. Output...

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