Returning multiple variables in a function, passing by reference

Hello and thank you for any help in advance.

I am a student currently working on a C++ assignment as follows:

Specific Tasks:
Write a program that repeatedly
1. Inputs an initial height and initial velocity for a ball being thrown

2. Echoes back (displays to the console) the initial height and velocity

3. Displays a table of times, height and velocity, with time increasing in 1/4 second intervals, until the ball hits the ground

4. Calculates and displays the maximum height of the ball

5. Calculates and displays the exact time the ball hits the ground

You must have functions that do the following:
•Does the input for the initial height and initial velocity (hint: since you’re getting two pieces of information, you’ll need to use reference parameters)

•Calculate current height of the ball, given an initial height, initial velocity, and a time

•Calculate the maximum height of the ball, given an initial height and initial velocity (question: when is the ball at its maximum height?)

•Calculate the current velocity of the ball, given an initial velocity and a time

•Calculate the time the ball hits the ground, given an initial height and initial velocity

•Calculates and displays the table discussed above.



I have the code pretty much completed; however, I am struggling a bit with figuring out how to return both the initial velocity and the initial height in the function which imputs them in order to use them in the rest of my functions.

How should I use reference parameters to do so? Do I need to use reference variables (double& initialHeight instead of passing by value with just initialHeight) in all of my other functions as well? How should I call the function in the int main()?

Thanks!
closed account (z05DSL3A)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

void getValues(int& height ,int& velocity)
{ 
    //code to get initial height from user goes here
    //and 'returns' it via the height reference
    height = 9;  
    
    //code to get initial velocityfrom user goes here
    //and 'returns' it via the velocity reference
    velocity=4;

    return;
}

int main( )
{
    int x =0;
    int y=0;
    
    //get the values from the user
    getValues(x,y);
    
    std::cout << "x = " << x << " and y = " << y;

    return 0;
}
Last edited on
Thank you so much!

I had the function type as double instead of void... I feel kind of silly. It seems to be working now.

Although, it is now claiming that a ball with an initial height of 0 and an initial velocity of 0 hits the round after 0.5000 seconds.... :( So now I get to go bck over my equations and cout a bunch of variables to see where I went wrong. Darn propagating bugs.....
Last edited on
Topic archived. No new replies allowed.