# Thread: Help on Homework

1. ## Help on Homework

Hey all, I'm new here, but I figured I should join a forum since I'm taking a C programming class, and it's really my only programming experience outside of some low level Visual Basic. I'm stumped on an assignment I have right now. What we have to do in this assignment is calculate the volumes of cylinders using the height and radius, which is the easy part, but we have to assign v the highest calculated value of all the cylinders and have it display ONLY that value. I'll show you what the output is supposed to look like, and what I have in my code. Any help is much appreciated. Here is what the output should look like:

----jGRASP exec: H:\My Documents\comp1200\HUNDLEY\2009sp\assign_03\a.exe

Volume of Cylinders
Enter the number of cylinders: 3
Enter radius and height: 5 9
Enter radius and height: 8 4
Enter radius and height: 4 10

Information about the cylinder with the largest volume
Volume: 804.25
Height: 4.00
----jGRASP: operation complete.

AND here is what I currently have in my code:

Code:
```#include <stdio.h>
#include <math.h>

int main()
{
double v, h, r, PI;
int num, count, c;
PI = 3.14159;

printf("How many cylinders are there?");
scanf("%d", &num);

printf("Enter the radius and height of cylinder 1:  ");
count = 0;
c = 0;
while (count < num)
{
scanf("\n%lf" "\n%lf", &r, &h);
count = count + 1;
if (count < num)
{
printf("Enter the radius and height of cylinder %d:  ", count +1);
}
}
v = PI * pow(r,2) * h;
printf("The volume of the cylinder is %.2lf\n", v);
return 0;
}```
I would prefer if you didn't straight up write the code for me, but if you could make recommendations that'd be great, or tell me the correct function or statement to use.

Again, thanks so much for help.

2. So after you calculate the volume, don't print it out, but compare it with the previous high value and store it if necessary.

3. That's what I don't know how to do. This is only my third assignment in c, my knowledge is very basic. What I need to know how to do is store v, and compare it to previous/future values.

4. Have variables for the largest volume, and its radius and height. Set these to the first set you get. Then each iteration check if the new volume is larger. If it is, reset the variables.

5. Also, just a note on style. An increment like this:
Code:
`count = count + 1;`
is usually accomplished though the increment operator:
Code:
`count++;`
Similarly, you can do the same for decrement, replacing
Code:
`count = count - 1;`
with
Code:
`count--`

6. Hey thanks for the input. As for the increment, I didn't know that, and I appreciate the help. The reason I did it that way is because that's the way my professor did it in class when demonstrating increments, and typically she likes us to do what she does. Thanks though, expanding my knowledge is what I need.

7. ## How to right justify

Ok, so I figured out the code, now I need to know how to justify the last printed info to the right. IE the Volume Radius and Height need to be in right justified columns. Here's the code. It's ok to just give me the answer to this. Here's the code:

Code:
```#include <stdio.h>
#include <math.h>

int main()
{
double v, h, r, PI, vmax, rmax, hmax;
int num, count;
PI = 3.14159;

printf("How many cylinders are there?  ");
scanf("%d", &num);

count = 0;
vmax = 0;
while (count < num)
{
if (count < num)
{
printf("Enter the radius and height of cylinder %d:  ", count +1);
}
scanf("\n%lf" "\n%lf", &r, &h);
count = count + 1;
v = PI * pow(r, 2) * h;
if (vmax < v)
{
vmax = v;
rmax = r;
hmax = h;

}

}
printf("Details of the cylinder with largest volume:\n Volume: %0.2lf\n Radius: %0.2lf\n Height: %0.2lf\n", vmax, rmax, hmax);
return 0;
}```
Thanks again.

8. Originally Posted by Sentiax
IE the Volume Radius and Height need to be in right justified columns.
Either use a tab between "word:" and "%0.2lf", or use a width specifier with "word":
Code:
`printf("%10s\n","hi!");`
hi! is right justified in a ten character column.

9. Numbers and letters are printed to the right in their field, by default, Looks like you're setting your field width modifier to 0, so your numbers are being printed "sans field".

(Left side field alignment is done by adding a hyphen btw. printf("%-5s, "hi"); prints hixxx, where x is unused field spaces.

That's the value to work with, to get it all to line up. In a format with columns, every value may need to be set to a specified field width, not just the values in one column, but every value in every column.

10. note that printf should use %f not %lf for doubles

11. Hey thanks guys, I figured it out. It turns out I was trying to do it right with the %x.ylf I just wasn't using a high enough value for x, so it wouldn't align large numbers correctly. As for the %f instead of %lf, why is that, my professor says that's how you are supposed to use them for double variables.

12. Originally Posted by Sentiax
Hey thanks guys, I figured it out. It turns out I was trying to do it right with the %x.ylf I just wasn't using a high enough value for x, so it wouldn't align large numbers correctly. As for the %f instead of %lf, why is that, my professor says that's how you are supposed to use them for double variables.
You use %lf with scanf, but not with printf.

13. Why not use it with printf, it works fine. What's the difference?

14. Originally Posted by Sentiax
Why not use it with printf, it works fine. What's the difference?
The difference is that the Standard says that %f should be used for both doubles and floats with respect to printf() and friends, but not so for scanf() and friends.

Popular pages Recent additions