In: Computer Science
C++ code please:
Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Then, get the last value from the input, which indicates how much to multiply the array by. Finally, print out the entire array with each element multiplied by the last input.
Assume that the list will always contain less than 20 integers.
Ex: If the input is
4 4 8 -4 12 -1
the output is
-4 -8 4 -12
Ex: If the input is
7 0 1 2 3 4 5 6 3
the output is
0 3 6 9 12 15 18
Below is the code in C++ for the above question:
#include<iostream>
using namespace std;
int main(){
// size of the list of integers
int n;
// getting size of list of integers as user
input.
cin >> n;
// array to store the list of integers.
int ar[n];
// getting list of integer and storing it in the
array.
for(int i=0;i<n;i++)
cin >> ar[i];
// variable multiplier
int multiplier;
// getting multiplier as user input.
cin >> multiplier;
// multiplying every integer of array by the
multiplier
for(int i=0;i<n;i++)
ar[i] *= multiplier;
// displaying the list of integers
for(int i=0;i<n;i++)
cout << ar[i] << "
";
}
Refer to the
screenshot attached below to better understand the code and
indentation:
Output:
If this answer
helps you then please upvote,
for further queries comment below.
Thank you.