Write a program that takes an array of 10 integer numbers and calculates the ave
ID: 3919369 • Letter: W
Question
Write a program that takes an array of 10 integer numbers and calculates the average and the sum of the array using two different functions.
a) (5 pts) Declare and write a function called calcavg. Returns a double, passes the array. Calcavg adds the 10 elements of the array and divides by 10 to return the average. Assume there are always 10 elements. Calcavg does not display any result.
b) (5 pts) Declare and write a function called calcsum. Returns void. Passes the array and an integer variable by reference. Sums the 10 elements of the array and sets the summation to the variable passed by reference. Calcsum does not display any result.
c) (10 pts). Write a program that has the appropriate headers and variable declarations. The program should create an integer array, nums of 10 elements containing the numbers 1,2,3,4,5, 6,7,8,9,10. Please add several comments throughout your program and functions.
d) (5 pts). The main program should display the elements of the array using a for loop. The display should look exactly as shown below. Use field width manipulators.
e) (5 pts). Call the two functions and display the average and sum of the elements of the array using statements in the main function. The terminal window should look exactly as shown. Program 2 (50 pts). Developing a Class called Fractions.
a) (10 pts) Create a class called fractions , that has two integer data members called num and denom. Create a constructor that initializes all objects to 1 for both members. The constructor must prohibit a 0 denominator value.
b) (10 pts) Create an accessor functions called display that access and displays the objects of the class fractions. For num = 1 and denom = 2, display outputs 1/2 .
c) (20 pts) Create a class function that overloads each of the four operators.
d) (5pts) Create a main function with 2 fraction objects a = (1/2) and b = (3/7). Use display to display each object.
e) (5 pts) Call each of the overload functions and display the results. Your output should be something like, The fraction 1/2 + 3/7 equals 13/14. for each operator.
Rubric: 1_1 (5 pts) Declare and write a function called calcavg. Returns a double.
1_2 (5 pts) Declare and write a function called calcsum. Returns void
1_3 (10 pts) main. create an integer 1D array, nums of 10 elements
1_4 (5 pts) main. display the elements. Use field width manipulators.
1_5 (5 pts) main. Call the two functions and display the average and sum
2_1 (10 pts) Create a class called fractions , integer data members called num and denom
2_2 (10 pts) accessor functions called display
2_3 (20 pts). class functions that overload each of the four operators.
2_4 (5pts) Create a main function with 2 fraction objects a = (1/2) and b = (3/7). Display
2_5 (5 pts) Call each of the overload functions and display the results.
Explanation / Answer
Problem 1:
#include <iostream>
#include <iomanip> // header file for width specifier
using namespace std;
// function prototypes
double calcavg(int[]);
void calcsum(int[],int&);
int main() {
int nums[] = {1,2,3,4,5, 6,7,8,9,10};// declaration of nums[] array
int sum = 0; // initialize sum of elements of array to 0
for(int i=0;i<10;i++)
{
cout<<nums[i]; // display elements of array
cout.width(5); // use width of 5 spaces after each element
}
cout<<endl;
cout<<"Average of all elements of array : "<<calcavg(nums)<<endl; // call to function calcavg()
cout<<"Sum of all elements of nums array : ";
calcsum(nums,sum);// call to function calcsum()
cout<<sum;
return 0;
}
// functions definitions
double calcavg(int arr[10])
{
int sum = 0;
for(int i=0;i<10;i++)
sum = sum + arr[i];
return (double)sum/10; // return average of all elements of array
}
void calcsum(int arr[10],int &sum)
{
int s = 0;
for(int i=0;i<10;i++)
s = s + arr[i];
sum = s;
}
Output:
1 2 3 4 5 6 7 8 9 10
Average of all elements of array : 5.5
Sum of all elements of nums array : 55
Problem 2:
#include <iostream>
using namespace std;
class fractions
{
private:
int num,denom;
public:
fractions()// default constructor
{
num = 1;
denom = 1;
}
fractions(int num,int denom)// argument constructor
{
this->num = num;
if(denom == 0) // prohibit denom = 0
denom = 1;
else
this->denom = denom;
}
void display()
{
cout<<num<<"/"<<denom;
}
// overload operator functions for operators +,-,/ and *
fractions operator+(fractions x)
{
fractions temp;
temp.num = this->num*x.denom + x.num*this->denom;
temp.denom = this->denom * x.denom;
return temp;
}
fractions operator-(fractions x)
{
fractions temp;
temp.num = this->num*x.denom - x.num*this->denom;
temp.denom = this->denom * x.denom;
return temp;
}
fractions operator*(fractions x)
{
fractions temp;
temp.num = this->num * x.num;
temp.denom = this->denom * x.denom;
return temp;
}
fractions operator/(fractions x)
{
fractions temp;
temp.num = this->num*x.denom ;
temp.denom = this->denom * x.num;
return temp;
}
};
int main() {
cout<<"Fractions : "<<endl;
fractions a(1,2);
a.display();
cout<<endl;
fractions b(3,7);
b.display();
fractions sum,diff,mul,div;
sum = a+b;
cout<<" Sum : ";
sum.display();
diff = a-b;
cout<<" Difference : ";
diff.display();
mul = a*b;
cout<<" Multiply : ";
mul.display();
div = a/b;
cout<<" Division : ";
div.display();
return 0;
}
Output:
Fractions :
1/2
3/7
Sum : 13/14
Difference : 1/14
Multiply : 3/14
Division : 7/6
Do ask if any doubt. Please upvote.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.