Question

1. Write code for a function that receives two parameters (a,and b) by value and two...

1. Write code for a function that receives two parameters (a,and b) by value and two more parameters (c and d) by reference. All parameters are double.
The function works by assigning c to (a/b) and assigning d to (a*b). The function has no return value.

From main, use scanf to get two numbers, then call the function, and then display both outcome values to the output in a printf statement.

2. After part 1 is completed, write code to get 20 integer numbers from the user. The code then displays how many of those numbers are above the average. To get proper credit you must follow these steps:

a. write a for loop to fill the array with numbers from the user. Use an array for this.

b. use a pointer to calculate the average of the numbers entered into the array of part a above, in another loop.

c. write a function, then pass the array, its size, and the average to that function. The function arguments would be a pointer to int, an int, and a float and it returns the count of the number of elements that are above the average.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Question 1:

Answer:

#include <iostream>

using namespace std;

//function to calculate division and multiplication using reference variable
void calculateValues(double a, double b, double &c, double &d)
{
c = a / b;
d = a * b;
}
int main()
{
//variable declaration
double a, b, c, d;
  
//display message
printf("Enter the first number: ");
scanf("%lf", &a);
  
//display message
printf("Enter the second number: ");
scanf("%lf", &b);
  
//function calling
calculateValues(a, b, c, d);
  
//display result
printf("\na / b = %.2lf", c);
printf("\na * b = %.2lf", d);

return 0;
}

OUTPUT:

Question 2:

Answer:

#include <iostream>

using namespace std;

//function to count the number above average
int countAboveAvg(int a[], int size, float avg)
{
int count=0;
for(int i=0; i<20; i++)
{
if(a[i] > avg)
{
count++;
}
}
  
//return statement
return count;
}

int main()
{
//array and variable declaration
int num[20], total = 0, aboveAvg;
float avg;
  
//display message
cout<<"Enter 20 integer numbers: "<<endl;
for(int i=0; i<20; i++)
{
cin>>num[i];
}
  
for(int i=0; i<20; i++)
{
total = total + *(num+i);
}
  
avg = total / 20;
  
//function calling
aboveAvg = countAboveAvg(num, 20, avg);
  
//display result
printf("\nThe number of elements above average is: %d", aboveAvg);

return 0;
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
1. Write code for a function that receives two parameters (a,and b) by value and two...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are...

    IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are developing a program that works with arrays of integers, and you find that you frequently need to duplicate the arrays. Rather than rewriting the array-duplicating code each time you need it, you decide to write a function that accepts an array and its size as arguments. Creates a new array that is a copy of the argument array, and returns a pointer to the...

  • Define an ordinary function called DisplayMin that has three parameters: the first two parameters are parallel...

    Define an ordinary function called DisplayMin that has three parameters: the first two parameters are parallel arrays of different types and the third argument is of type size_t representing the size of both arrays. The function DisplayMin must work when called with various types of actual arguments, for example string DisplayMin(string names[], int calories[], int size) or int DisplayMin(int calories[], float prices[], int size). (Parallel Arrays are two arrays whose data is related by a common index. For example: with...

  • C language 3. (10 pts) Write a function readSegment that takes three parameters, a float array...

    C language 3. (10 pts) Write a function readSegment that takes three parameters, a float array as a pointer p as the first parameter, and two int indices startldx and endldx. The function readSegment reads the content of the array between startIdx and endldx, without using additional local variables. 4. (10 pts) Write a function called doubleOdds that takes two parameters, an array of double pointed to by pointer p and endldx of int type. The function doubleOdds doubles (multiply...

  • Write in C code What to do Write a function with the following signature: float* matrix...

    Write in C code What to do Write a function with the following signature: float* matrix multiplication(float* left, float* right, int rows, int shared, int columns); The first two arguments are two pointers to the beginning of two matrices with the dimensions (rows,shared) and (shared, columns) correspondingly. The remaining arguments specify these dimensions. The return value will return a pointer to the very beginning of the result matrix. That said, you need to provide the space for the result matrix...

  • using c/c++ Q1 (15 Marks) Console Application for Student Data 1- Write a function named Average...

    using c/c++ Q1 (15 Marks) Console Application for Student Data 1- Write a function named Average that Receives two integer numbers as parameter and returns the average Is used in a main function. Main() stays in a loop and asks user to enter 2 numbers and then shows the average using the Average function above Define a class named Student with following members: private data: 2 grades and a GPA (average). public constructor to initialize the members a public function...

  • Write code in C to demo how to test the content of a variable and do...

    Write code in C to demo how to test the content of a variable and do some action accordingly. Add a one line comment above the code to explain the code. Here is an example: /* the following code checks if variable x is divisible by variable y and displays a message accordingly */ int x,y; printf( "Enter two numbers \n" ); scanf("%d %d",&x,&y); if (x % y ==0) { // use == to test for equality % returns remainder...

  • write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

    write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...

  • Write code in C to demo how to test the content of a variable and do...

    Write code in C to demo how to test the content of a variable and do some action accordingly. Add a one line comment above the code to explain the code. Here is an example code: /* the following code checks if variable x is divisible by variable y and displays a message accordingly */ int x,y; printf( "Enter two numbers \n" ); scanf("%d %d",&x,&y); if (x % y ==0) { // use == to test for equality % returns...

  • C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) L...

    C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) Last Name (char[]) Age (int) Height in Inches (double) Weight in Pounds (double) You will use pass-by-reference to modify the values of the arguments passed in from the main(). Remember that arrays require no special notation, as they are passed by reference automatically, but the other...

  • Convert the below C code to basic MIPS. The result of this code should be a^b...

    Convert the below C code to basic MIPS. The result of this code should be a^b of the two numbers that you input. For example if you input the numbers 3 and 5 you should get a result of 3^5=6 #include int main(void) { printf("Insert two numbers\n"); int a,b,c; scanf("%d",&a); scanf("%d",&b); c=a^b; printf("%d^%d=%d\n",a,b,c); return 0; }

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT