program help c++

So I made the mistake of not even attempting the program before asking for help, so I started on it. This is my current code so far and I have a few questions:
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

const double PI = 3.14159265358979323846;

struct Cone
{
    double height;
    double radius;
};

int main()
{
    struct Cone *ptr;
    double height;
    double radius;
    Cone new;
    input();
    return 0;
}
void input()
{
    int height, radius;
cout << "Enter height of the cone followed by the radius." << endl;
cin >> height;
cin >> radius;

}

void setUp()
{

}


int getVolume(int *ptr)
{
 int Volume;
 Volume = ( PI * radius * height )  / 3;
return Volume;
}


void output()
{


}


My program directions are - - -
The volume of a cone is given by the formula:
V = N r2 h / 3

For the value of N use:
const double PI = 3.14159265358979323846;

Declare a structure named: Cone
containing:

height a double, the height of the cone
radius a double, the radius of the base of the cone

Create the following functions:

main

* Contains three variables:
o A pointer named ptr which points to a Cone structure
o A double named height
o A double named radius
* Uses new to obtain space for the data structure
* Calls the the input, setUp, and output functions
* Deletes the space that was obtained using new

input:

* Takes the height of the cone and radius of the base as reference
parameters
* Reads the height and radius from the user
* Has a return type of void

setUp:

* Takes three parameters by value: height, radius, and a pointer to
the Cone
* Puts the data into the data structure
* Has a return type of void

getVolume:

* Takes one parameter by value: a pointer to the Cone
* Computes the volume
* Returns the volume

output:

* Takes one parameter by value: a pointer to the Cone
* Calls the getVolume function to get the volume
* Prints the height, radius, and volume in a neat format
* Has a return type of void

Put the main function first.
Use the function and variable names specified above.
Arrange the functions in the order listed above.

Test your program with the following data:

height 6
radius 2

So my question(s) is "Uses new to obtain space for the data structure" How do I express this in code? And when something has a return type of void that is just ending the function with a } correct? And in the lab I posted above it uses "parameters" very often, is that just another word for variable? Also I was wondering if I used the ptr pointer correctly that points to cone.
Thanks!
Last edited on
Uses new to obtain space for the data structure" How do I express this in code
Would it be
 
Cone new;
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
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>

using namespace std;


struct Cone{

 int height = 0;
 int radius = 0;
};


int main(){

 /*
  *   So my question(s) is "Uses new to obtain space for the data structure"
  *  How do I express this in code?
  *  see http://www.cplusplus.com/reference/new/operator%20new/
  */
 
 Cone *myCone = new Cone();

/*
 * And when something has a return type of void that is just ending the function with a } correct?
 * 
 * A: sorta.  Look at the main funcion. 
 *
 * int main(){
 * 
 * return 0; 
 * }
 * 
 * it is of type int and returns a int. 
 * 
 * a void function means the function is of type void and returns a void or nothing. 
 * 
 * 
 */
 return 0;
}


Thank you! And what does parameter mean exactly in terms of this lab?
In general you have below to be true.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

using namespace std;

void print(int a) { // (int a <- parameter)
    
    cout << a << endl;
}

int main() {
    
    int a, b, c; //  <- Variables
    
    return 0;
}


First what are NOT parameters?
A: Variables.

Think of variables as buckets that are designed to hold a certain type of data.

For example

int buckets can hold data of type int
float buckets can hold data of type floats
int pointer buckets can hold data of int pointers (not ints - but the address of said int)

excreta...

A parameter in c++ is a part of a function.
type functionName( parameter1, parameter2, ...) { statements }


They allow you as a programmer to send data and/or instructions to functions, so functions can use or have access to data/info they would not have access to otherwise.

See here for more details
http://www.cplusplus.com/doc/tutorial/functions/


The code below shows me setting the height and radius of "myCone" by using reference parameters
You should be able to adapt it to the instructions given.

"* Takes the height of the cone and radius of the base as reference
parameters"

has your instructor is asking for base not height.

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
28
29
30
31
32
33
34
35
36
37
#include <iostream>

using namespace std;

struct Cone
{
    double height;
    double radius;
};
void set_ConeHeightRadius(Cone *myCone, int height, double radius);

int main() {

    Cone *myCone = new Cone();

    int height = 0;
    double radius = 0;

    cout << "please enter height: ";
    cin >> height;
    cout << endl;

    cout << "please enter radius: ";
    cin >> radius;
    cout << endl;

    set_ConeHeightRadius (myCone, height, radius);

    delete (myCone);
    
    return 0;
}

void set_ConeHeightRadius(Cone *myCone, int height, double radius) {
    myCone->height = height;
    myCone->radius = radius;
}


Last edited on
Topic archived. No new replies allowed.