In: Computer Science
Anything relating to reference variables or arrays will be so described. Assume any others are by value
8. Write the header line for the following functions - each function is named F1
A function returning a long and receiving as data parameters a short, an array of float and an array of double.
b. A function returning a reference to an int and receiving a 2 dimensional array of double with 23 columns.
c. Returning an int and receiving 3 int reference variables.
9. Write the following functions in full, including header line and body. Call each F1.
a. Receives 4 arguments, two arrays of short and two int's, one for each array, containing the count of array elements in that array. The function computes one grand total for both arrays. It then returns the grand total.
b. Receives an array of long. Returns the 2nd value stored in the array.
c. Write a function to receive a 2 dimensional array of long double with 10 columns. Also you will receive a short value representing the number of rows. The function totals all of the amounts in the array. The total of the values is returned as a long double.
d. . This function, F1, receives a pointer to an int array and a pointer to a double as arguments. It then calls the function called F2, which has the following prototype:
void F2( int *arr, double num );
F1 is going to call F2 and send the array and the double value as arguments, receiving nothing back.
Write the entire function F1 which calls F2 as its only operation.
e. Change this function to reference variables:
int * fun1( float *a, int *b, float d)
{
*a = *b * d;
return a;
}
Rewrite the entire function below:
8.a
long F1(short,float[],double[]);
b.
int& F1(double[][23]);
c.
int F1(int&,int&,int&);
9.
a.
#include <iostream>
using namespace std;
int F1(short array1[],short array2[],int length1,int
length2)
{
int total = 0;
for(int i=0;i<length1;i++)
total = total + array1[i];
for(int i=0;i<length2;i++)
total = total + array2[i];
return total;
}
int main() {
short array1[] = {34,65,23};
short array2[] = {4,7,9,10};
cout<<F1(array1,array2,3,4);
return 0;
}
Output:
152
b.
#include <iostream>
using namespace std;
long F1(long array[])
{
return array[1];
}
int main() {
long array[] = {57657,685568,9699,85909};
cout<<F1(array);
return 0;
}
Output:
685568
c.
#include <iostream>
using namespace std;
long double F1(long double array[][10],short rows)
{
long double total = 0;
for(int i=0;i<rows;i++)
{
for(int j = 0;j<10;j++)
{
total = total +
array[i][j];
}
}
return total;
}
int main() {
long double array[][10] =
{{1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0},{11.0,22.0,33.0,44.0,55.0,66.0,77.0,88.0,99.0,100.0
} };
cout<<F1(array,2);
return 0;
}
Output:
650
d.
void F1(int *array,double *x)
{
F2(array,*x);
}
e. Change this function to reference variables:
int & fun1( float &a, int &b, float d)
{
a = b * d;
return a;
}
Do ask if any doubt. Please upvote.