(No Subject) Younes Zahr Add to contacts 4:59 PM To: jaafarnasser@live.com Defin
ID: 644342 • Letter: #
Question
(No Subject)
Younes Zahr
Add to contacts
4:59 PM
To: jaafarnasser@live.com
Define two arrays x and f, each of size 7, using call-by reference(that is use pointer) to pass the array to a function, named summation.
In main: define arrays, print out the array, pass the array to the function and print the summation result on screen.
In function summation: take arrays from main and sum the arrays using the formula below
Sum= {summation from 0 to 6} xi * fi
Note: you must use call-by-reference ( the pointer) to call functions, as required; otherwise, no credits will be given
Explanation / Answer
Program plan:
In call by referece, we use reference of the actual variable in function call. the reference can be denoted by "&". In this program we use the call by reference technic in all the three funtions. Here,we defined three funtions.they are:
void readarr(int (&a)[7]) : readarr is the method used to read the elements for array.(&a) is the reference variable of the array.
void printarr(int (&a)[7]) : printarr is the method tio display the elements of the array.(&a) is the reference variable of the array.
void summation(int (&x)[7],int (&f)[7]): summation is method where the sum of the elements of the array is calculated. the formula also be calculated.
Program code:
//Include directives
#include<iostream.h>
#include<conio.h>
//function Declarations
void summation(int (&x)[7],int (&y)[7]);
void readarr(int (&a)[7]);
void printarr(int (&a)[7]);
//main()method start
void main()
{
int x[7],f[7],i; //variable declaration.arrays x and f size is 7 integer elements
clrscr(); //clears the screen
cout<<"enter elements for array x: ";
readarr(x); //calling the funtion readarr for reading elements for array x
cout<<"enter elements for array f: ";
readarr(f); //calling the funtion readarr for reading elements for array f
cout<<"x array elements are: ";
printarr(x); //printarr function displays the elements of array x
cout<<"f array elements are: ";
printarr(f); //printarr function displays the elements of array f
summation(x,f); //calling the function summation with two array arguments.
getch();
} // end of main
void readarr(int(&a)[7]) //definition of readarr function
{
for(int i=0;i<7;++i)
{ cout<<"enter element";
cin>>a[i];
}//end of function
void printarr(int (&a)[7]) //definition of printarr
{
for(int i=0;i<7;++i)cout<<a[i]<<" ";
}//end of printarr function
void summation(int (&x)[7],int (&f)[7]) //definition of summation method with reference arguments
{
int xsum,fsum,sum,i;
for(i=0;i<7;++i)xsum+=x[i];
for(i=0;i<7;++i)fsum+=f[i];
sum=xsum*fsum; //calculation of given formula.
cout<<" summation of x array is: "<<xsum;
cout<<" summation of f array: "<<fsum;
cout<<" In the given formula,sum="<<sum;
} //end of summation function
Sample output:
enter elements for arrayx:
enter element:1
enter element:2
enter element:1
enter element:5
enter element:1
enter element:1
enter element:4
enter elements for array f:
enter element:2
enter element:2
enter element:1
enter element:2
enter element:2
enter element:2
enter element:1
x array elements are:
1215114
f array elements are:
2212221
summation of x array: 15
suumation of f array: 13
In the given formula,sum=195
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.