Help on Homework

This is a discussion on Help on Homework within the C Programming forums, part of the General Programming Boards category; Hey all, I'm new here, but I figured I should join a forum since I'm taking a C programming class, ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    6

    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
    Radius: 8.00
    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. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    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. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    213
    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. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,641
    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. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    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. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    6

    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. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Sentiax View Post
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.
    Last edited by Adak; 02-03-2009 at 01:45 PM.

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    note that printf should use %f not %lf for doubles
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    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. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Sentiax View Post
    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. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    Why not use it with printf, it works fine. What's the difference?

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 03:39 PM
  2. Homework
    By kermi3 in forum C Programming
    Replies: 10
    Last Post: 09-27-2001, 04:49 PM
  3. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 03:16 PM
  4. Homework
    By kermi3 in forum Windows Programming
    Replies: 5
    Last Post: 09-15-2001, 11:48 AM
  5. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 01:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21