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
  • I need help programming this question using C language. This program uses input data entered in...

    I need help programming this question using C language. This program uses input data entered in command line at the same time when the command or .exe file is entered. They are called command-line arguments. Because everything entered in command line is taken as a string, these arguments including the command itself or the .exe file name are stored in an array of char pointers, each pointer points to the first character of a string (i.e., argument). The inputs can...

  • 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....

  • 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...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • 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...

  • The answer need to write in C programming. QUESTION 2 Write a program using array to...

    The answer need to write in C programming. QUESTION 2 Write a program using array to generate a multiplication table based on the user's input. For example if user enter 5 as input then a multiply table for 1 to 5 is printed as output as shown in Figure Q2. There are three main steps in this program which are: Print rows (a) (b) Print columns Print multiplication of data inside table (c) Select C:\example1 \bin\Debuglexample 1.exe enter the value...

  • 2 COS1511 MAY/JUNE2020 SECTION A 20 MARKS Choose one option for every question. If, for example...

    2 COS1511 MAY/JUNE2020 SECTION A 20 MARKS Choose one option for every question. If, for example you choose option 2 as the correct answer for Question 1, and option 4 as the correct answer for Question 2, please answer as follows: 1. 2 2.4 etc. QUESTION 1 2 marks Suppose the following declarations appear in the nai nfunction of a C++ program: string nane, course; char sex; int age; float cost; bool approved; If the following function header is given:...

  • 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...

  • Please help. I need a very simple code. For a CS 1 class. Write a program...

    Please help. I need a very simple code. For a CS 1 class. Write a program in Java and run it in BlueJ according to the following specifications: The program reads a text file with student records (first name, last name and grade). Then it prompts the user to enter a command, executes the command and loops. The commands are the following: "printall" - prints all student records (first name, last name, grade). "firstname name" - prints all students with...

  • C++: Need help debugging my code I am writing this and using some other examples as reference code while rewriting it with my own understanding of the material but am having trouble making it finally...

    C++: Need help debugging my code I am writing this and using some other examples as reference code while rewriting it with my own understanding of the material but am having trouble making it finally compile. Below is a picture of the error messages. //main.cpp //Semester Project //Created by J---on 5/6/2019 #include <iostream> #include <fstream> #include <string> #include <sstream> #include <bits/stdc++.h> using namespace std; void instructions(); //displays program details and instructions void openFile(); void takeInput(int*); void switchBoard(int*); struct price {...

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