Question
C++ code... Need help asap

QUESTION 2 (20 MARKS) The following program shows a simple example to calculate average of three marks. Explain how the progr
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Your Code:
#include<iostream>
using namespace std;
float calculateAverage(int *marks)
{
int sum =0;
float average;
for(int i=0; i<3; i++)


{
sum = sum + (*(marks+i));
}
average = float(sum)/3;
return average;
}
void displayAverage(float *avg)
{
cout<<*avg<<endl;
}
int main()
{
int marks[3]={88,42,73};
float avg;
float *ptrAvg = &avg;
avg = calculateAverage(marks);
displayAverage(ptrAvg);
return 0;
}

The code flow is as follows
The execution starts from main function
The first line in main function denotes an array marks with 3 elements
Then we declare avg as float
The next line float *ptrAvg = &avg; assigns the address of the variable avg to the pointer ptrAvg. This can be also said as a pointer variable ptrAvg points to the address of avg
Now we have the address of avg in *ptrAvg.
Then next call the function calculateAverage(marks). This call the function calculateAverage with an argument marks.
The program flow changes to the function calculateAverage. Now lets see what happens in that function
It reads the argument as pointer. that means *marks is a pointer which points towards the array marks[] which we have declared in the main function. Now we can access the array directly. Which means in this function we are using the original array instead of copy.
Inside the for loop, we are adding the marks
the statement sum = sum + (*(marks+i)); , Indicates as follows,
*(marks+i) it point towards the ith index of the array which is pointed, each time.and take the value in that index.
Next we calculate the average and return it.
Now we are back in our main function which next calls displayAverage function with ptrAvg, The ptrAvg is actually pointing towards the address of avg. The value returned from calculateAverage is avg.
Now flow changes to displayAverage
it prints the value in the adress denoted as *avg.
This is the basic flow of the program.

Now coming to memory management, we have mainly 3 widely used variables
marks
avg
ptrAvg
It can be illustrated as

オmaors さn

Hope this is helpful

please comment if you have any doubts

Add a comment
Know the answer?
Add Answer to:
C++ code... Need help asap QUESTION 2 (20 MARKS) The following program shows a simple example...
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
  • Arrays C++ #include <iostream> using namespace std; int main() {       int   tests[6]; // array declaration...

    Arrays C++ #include <iostream> using namespace std; int main() {       int   tests[6]; // array declaration       int   sum = 0;       float avg;       //input test scores       cout << " Enter " << 6 << " test scores: " << endl;       for (int i = 0; i < 6; i++)       {             cout << "Enter Test " << i + 1 << ": ";             cin >> tests[i];       }       return 0; }    Type...

  • Need help with a C++ program. I have been getting the error "this function or variable...

    Need help with a C++ program. I have been getting the error "this function or variable may be unsafe" as well as one that says I must "return a value" any help we be greatly appreciated. I have been working on this project for about 2 hours. #include <iostream> #include <string> using namespace std; int average(int a[]) {    // average function , declaring variable    int i;    char str[40];    float avg = 0;    // iterating in...

  • I need a program in c++ the same as below code but I want it to...

    I need a program in c++ the same as below code but I want it to prompt the user to enter the number of elements after that I want it to ask the user to enter the array elements #include<algorithm> #include<stdio.h> #include<string.h> #include<iostream> using namespace std; int a[50]={2,5,4,3}; bool x[100]; int N=4;//number of elements    int k=10;//target sum int sum;//current target sum int cmp(const void *a,const void *b) { return *(int *)b-*(int *)a; } void backtrace(int n) { if(sum>k) return...

  • Please I need help in C language, I am trying to modify the code per the...

    Please I need help in C language, I am trying to modify the code per the below instructions, but I am getting errors. Can't fgure it out. The attempted code modification and data file is privided below, after the instructions. Thank you. Instructions:                  1.      First, add a function named printMsg that displays a message, a greeting, or an introduction to the program for the user. Add a statement that calls that function from the main function when your program starts....

  • C++ Redo PROG8, a previous program using functions and/or arrays. Complete as many levels as you...

    C++ Redo PROG8, a previous program using functions and/or arrays. Complete as many levels as you can. Level 1: (20 points) Write FUNCTIONS for each of the following: a) Validate #students, #scores. b) Compute letter grade based on average. c) Display student letter grade. d) Display course average. Level 2: (15 points) Use ARRAYS for each of the following. e) Read a student's scores into array. f) Calculate the student's average based on scores in array. g) Display the student's...

  • Fix the following program (C++). #include <iostream> #include <cmath> #include <vector> #include <limits> using namespace std;...

    Fix the following program (C++). #include <iostream> #include <cmath> #include <vector> #include <limits> using namespace std; /* * Calculate the square of a number */ float square (float x) { return x*x; } /* * Calculate the average from a list of floating point numbers */ float average(vector<float>& values) { float sum = 0.0f; float average; for (float x : values) sum += x; average = sum / values.size(); return average; } /** Calculate the standard deviation from a vector...

  • I need help with the code below. It is a C program, NOT C++. It can...

    I need help with the code below. It is a C program, NOT C++. It can only include '.h' libraries. I believe the program is in C++, but it must be a C program. Please help. // // main.c // float_stack_class_c_9_29 // // /* Given the API for a (fixed size), floating point stack class, write the code to create a stack class (in C). */ #include #include #include #include header file to read and print the output on console...

  • I need help with my coding for C++! The goal of this is to calculate the...

    I need help with my coding for C++! The goal of this is to calculate the total cost of a product by combining the products price and the amount of tax, then display it to the user. The numbers we have to test out is 100 for the price and 8.25 for the percentage. When I test is out, the formula works but it isn't rounding the 108.25 to 108.3, which is the total price we are supposed to display...

  • Rewrite the C++ code below so the the following conditions are met: You may create any...

    Rewrite the C++ code below so the the following conditions are met: You may create any number of classes. You may edit the main() function. Remove the variable string type completely from the entire program. -- Note: Entire program includes all classes, all functions, and main(). Get rid of every if and if else statement completely from the entire program. -- Note: Entire program includes all classes, all functions, and main(). Hint: Use polymorphism. #include <iostream> #include <vector> #include <algorithm>...

  • c++ Write the following 2 functions and test them. Both of them need to be recursive...

    c++ Write the following 2 functions and test them. Both of them need to be recursive functions. int sum(int n); // recursive version to calculate the sum of 1 + 2 + ..... + n int str_length(char s[]; // Returns length of the string s[] and the null character, '0\', is not counted in the length). Example of program execution; Enter a positive integer: 10 (user input) The sum of 1+ 2+....+10 is: 55 Enter a sentence: Hello World! (user...

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