Totally screwed on comp science course!

Pages: 123
Integer division, the result is also an integer. 23/35 = 0 and remains 23..

Cast to float before doing the operation.
Last edited on
how should i do that?
Most of your variables need to be declared as floats instead of ints. No typecasting is needed.

Edit: For the volume program pi should be a float, and you should make sure you're answer isn't truncated.
Last edited on
So i just do this and remove all the int ones?
float p,l1, l2, l3, l4, gp, gw, gp2, gp3, gp4, gw2 ,gw3, gw4, p2, p3, p4;
But those are quantities, they "should" be integers.
http://www.cplusplus.com/doc/tutorial/typecasting/
p = float(gw)/gp;
Sorry, I thought we were still talking about the Volume program. ne555 is correct.
@ ne55 the float works well! Thank you so much!

However, can someone please take a look at the volume program (below) and find out why for the cylinder portion the program doesn't take in the pi variable, even thought I have it set as 3.14....?
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
#include <stdio.h>
#include "simpio.h"
#include "strlib.h"
#include "random.h"
#include <string>

std::string shape;

int main()
{
	int i, cube, box, b1, pi, cylinder, s, s2, height;
	printf ("Please enter the shape:");
	shape=GetLine();
	pi=3.1415167;
	if (shape=="cube")
	{
		printf ("Please enter the side: ");
		s=GetInteger();
	b1=s*s*s;
	printf("The volume of a cube with side=%d, is %d.", s, b1);
	}
	else if (shape=="cylinder")
	{
		printf ("Please enter the height:");
		height=GetInteger();
		printf ("Please enter the radius: ");
		s=GetInteger();
		b1= pi*height*s*s;
		printf("The volume of a cylinder with radius=%d and height=%d, is %d.", s, height, b1);
	}
	else if (shape=="box")
	{
		printf("Please enter side1:");
		s=GetInteger();
		printf("Please enter side2:");
		s2= GetInteger();
		printf ("Please enter the height:");
		height=GetInteger();
		b1= s*s2*height;
		printf ("The volume of a box with sides %d and %d and height of %d is %d.", s, s2, height, b1);
	}
	return 0;
}

When using constants, such as pi, I find it much easier to use
#define PI 3.1415926
instead of inside the function.

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
#include <stdio.h>
#include "simpio.h"
#include "strlib.h"
#include "random.h"
#include <string>
#define PI 3.1415926 // this allows it to be used throughout the code via the preprocessor.
std::string shape;

int main()
{
	int i, cube, box, b1, pi, cylinder, s, s2, height;
	printf ("Please enter the shape:");
	shape=GetLine();
	//pi=3.1415167;        // moved to preprocessor, commented out.
	if (shape=="cube")
	{
		printf ("Please enter the side: ");
		s=GetInteger();
	b1=s*s*s;
	printf("The volume of a cube with side=%d, is %d.", s, b1);
	}
	else if (shape=="cylinder")
	{
		printf ("Please enter the height:");
		height=GetInteger();
		printf ("Please enter the radius: ");
		s=GetInteger();
		b1= PI*height*s*s;// capitalize the pi because I capitalized in preprocessor.
		printf("The volume of a cylinder with radius=%d and height=%d, is %d.", s, height, b1);
	}
	else if (shape=="box")
	{
		printf("Please enter side1:");
		s=GetInteger();
		printf("Please enter side2:");
		s2= GetInteger();
		printf ("Please enter the height:");
		height=GetInteger();
		b1= s*s2*height;
		printf ("The volume of a box with sides %d and %d and height of %d is %d.", s, s2, height, b1);
	}
	return 0;
}


Edit: Revised value of pi, as pointed out by filipe.
Last edited on
#define PI 3.1415167 // this allows it to be used throughout the code via the preprocessor.
That is the problem one of the problems with macros
Last edited on
The value for pi is also wrong. It should be 3.1415926 to keep the precision. And it should be a const:

const double pi = 3.1415926;
cmath defines M_PI as pi. don't define it yourself.
+1 rocketboy9000
Topic archived. No new replies allowed.
Pages: 123