Q1. Write a program that 1) reads 10 numbers from the screen, save each number t
ID: 3599692 • Letter: Q
Question
Q1. Write a program that 1) reads 10 numbers from the screen, save each number to an array 2) calculates the mean (average) and outputs it to the screen. *You MUST use arrays to solve this question. Q2. Write a program to swap the contents of two integer arrays of six elements. The output of your program should clearly show original array contents and the result. For example, the output for might look as follows: Contents of array A before swap: 1 2 3 45 6 Contents of array B before swap: 7 8 9 10 11 12 Contents of array A after swap: 789 10 11 12 Contents of array B after swap: 1 2 3 45 6 * Before you start, first thinking about how you can swap two numbers, and then generalize the means to the array. *You MUST actually swap the contents of the two arrays, rather than merely satisfy the output above. In other words, at the end of the program, array A should contain the contents of arrayB and vice versa.Explanation / Answer
Question 1
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int a[10];
cout<<"Enter 10 numbers:"<<endl;
for(int i=0;i<10;i++) {
cin >> a[i];
}
int sum = 0;
for(int i=0;i<10;i++) {
sum+=a[i];
}
double average = sum/(double)10;
cout<<"Mean: "<<average<<endl;
return 0;
}
Output:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int A[6] = {1,2,3,4,5,6};
int B[6]={7,8,9,10,11,12};
cout<<"Contents of array A before swap: ";
for(int i=0;i<6;i++) {
cout<<A[i]<<" ";
}
cout<<endl;
cout<<"Contents of array B before swap: ";
for(int i=0;i<6;i++) {
cout<<B[i]<<" ";
}
cout<<endl;
for(int i=0;i<6;i++) {
int t = A[i];
A[i]=B[i];
B[i]=t;
}
cout<<"Contents of array A after swap: ";
for(int i=0;i<6;i++) {
cout<<A[i]<<" ";
}
cout<<endl;
cout<<"Contents of array B after swap: ";
for(int i=0;i<6;i++) {
cout<<B[i]<<" ";
}
cout<<endl;
return 0;
}
Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.