Write a method to compute the average of an int array and return the value as a
ID: 3762075 • Letter: W
Question
Write a method to compute the average of an int array and return the value as a double. The
int array and the number of elements in the array are both passed as parameters to the method in
that order.
2.
Write code fragment to swap the two Strings stored by variables a and b.
3.
Write a code fragment to create a two-dimensional 10x10 array and initialize every element to
be the value of i * j where i and j are the two indices (for instance, element [5][3] is 5 * 3 = 15).
Write a method to compute the average of an int array and return the value as a double. The
int array and the number of elements in the array are both passed as parameters to the method in
that order.
2.
Write code fragment to swap the two Strings stored by variables a and b.
3.
Write a code fragment to create a two-dimensional 10x10 array and initialize every element to
be the value of i * j where i and j are the two indices (for instance, element [5][3] is 5 * 3 = 15).
Explanation / Answer
#include<iostream>
using namespace std;
double average(int a[],int n){
int sum = 0;
for(int i = 0 ; i < n ; i++){
sum += a[i];
}
return (double)sum/n;
}
int main(){
int a[50],n;
cout<<"Enter how many numbers to input:"<<endl;
cin>>n;
cout<<"Enter "<<n<<" numbers:"<<endl;
cout<<"Average:"<<average(a,n)<<endl;
return 0;
}
2.
#include<iostream>
using namespace std;
void swap(string &a,string &b){
string temp;
if(a > b)
{
temp = a;
a = b;
b = temp;
}
}
int main(){
string a,b,c;
cin>>a>>b>>c;
swap(a,b);
cout<<a<<b;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.