Write a program that reads in pairs of integers and computes the average for eac
ID: 3586970 • Letter: W
Question
Write a program that reads in pairs of integers and computes the average for each pair. The program should first prompt the user to input the number of pairs of integers to be read. For each pair of integers input by the user, compute and output the average of the two integers. Example run: % hw02 How many pairs of integers to be read? 3 -Iteration 1 Input first value: 12 Input second value: 9 The average for 12 and 9 is 10.5 -Iteration 2 Input first value: 3 Input second value: 4 The average for 3 and 4 is 3.5 -Iteration 3 Input first value: 1 Input second value: 2 The average for 1 and 2 is 1.5Explanation / Answer
main.cpp
#include <iostream>
using namespace std;
int main()
{
int p,i;
float v1,v2,avg;
cout<<"How many pairs of integers to be read?";
cin>>p;
i=1;
do{
cout<<"---Iteration "<<i<<" ---"<<endl ;
cout<<"Input first value : ";
cin>>v1;
cout<<"Input second value : ";
cin>>v2;
avg=(v1+v2)/2;
cout<<"The average of "<<v1<<" and "<<v2<<" is "<<avg;
cout<<endl<<endl;
p--;
i++;
}while(p>0);
return 0;
}
Output :-
How many pairs of integers to be read?3
---Iteration 1 ---
Input first value : 12
Input second value : 9
The average of 12 and 9 is 10.5
---Iteration 2 ---
Input first value : 3
Input second value : 4
The average of 3 and 4 is 3.5
---Iteration 3 ---
Input first value : 1
Input second value : 2
The average of 1 and 2 is 1.5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.