Newbie in need of help

This is a discussion on Newbie in need of help within the C Programming forums, part of the General Programming Boards category; I'm doing homework right now, and it wanted me to do this. Code: Sphere ( 1) R = 1.10 A ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    24

    Newbie in need of help

    I'm doing homework right now, and it wanted me to do this.
    Code:
    Sphere ( 1)      R = 1.10      A =  15.2053      V =   5.5753
    Sphere ( 2)      R = 1.20      A =  18.0956      V =   7.2382
    ...........      ........      ............      ............
    ...........      ........      ............      ............
    Sphere (20)      R = 3.00      A = 113.0973      V = 113.0973
    The first one is just the sphere's name, second column is the sphere's radius, third is its area with that radius, and fourth is the volume.

    I'm pretty sure I can do most of the programming, but I don't understand two things.

    1) How do you get the numbers to go up by in the sphere and R = ? (R is going up by .1! I only learned how to increment it by 1)

    2) How do I number the spheres with two characters?
    Like
    Code:
    Sphere( 1)
    Not
    Code:
    Sphere(1)
    ?

    Much thanks guys, this looks like a very helpful site.
    Last edited by Tarento; 04-12-2006 at 10:18 PM.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Addition instead of increment (on a floating point variable)

    Code:
    for(double R = 1.0; R < 3.0; R += .1)
    This example has parentheses only to demonstrate it. The %2d specifies a field of width 2.

    Code:
    printf("(%2d) (%2d)", 1, 11);

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    If you only need 1 digit of accuracy, and you don't need huge numbers, you can represent them as ints, only 10 times larger, then convert back at run-time for display/calculations.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    24
    This example has parentheses only to demonstrate it. The %2d specifies a field of width 2.

    Code:
    printf("(%2d) (%2d)", 1, 11);
    [/QUOTE]

    I get this as a result.

    Code:
    Sphere ( 1) (11)Sphere ( 1) (11)Sphere ( 1) (11)Sphere ( 1) (11)Sphere ( 1) (11)
    etc... How do I get it to go down like the example below? *I'm using SSH Secure Shell since it's a required program*

    Code:
    Sphere ( 1)
    Sphere ( 2)
    Sphere ( 3)
    Sphere ( 4)
    Sphere ( 5)
    Much thanks.

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Just by slightly modifying my code.

    Code:
    int main()
    {
    	for(int i = 1; i <= 5; i++)
    	{
    		printf("Sphere (%2d)\n", i);
    	}
    }

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    24
    Much thanks for the help Tonto, it worked! This is a bit harder than I thought. I think I can handle it from here.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    24
    Yep, I'm back.

    I've been making really slow, but sure progress, but now I'm stuck.

    Code:
    {
    int i;
    int k;
    
            for(i=1; i<21; i++)
    for(k=1; k<21; k++)
            {
                    printf("Sphere (%2d)     Area = %2d\n", i, k);
    
    }
    }
    This gives me...

    Code:
    Sphere ( 1)     Area =  1
    Sphere ( 1)     Area =  2
    Sphere ( 1)     Area =  3
    Sphere ( 1)     Area =  4
    Sphere ( 1)     Area =  5
    Sphere ( 1)     Area =  6
    Sphere ( 1)     Area =  7
    Sphere ( 1)     Area =  8
    Sphere ( 1)     Area =  9
    Sphere ( 1)     Area = 10
    Sphere ( 1)     Area = 11
    Sphere ( 1)     Area = 12
    Sphere ( 1)     Area = 13
    Sphere ( 1)     Area = 14
    Sphere ( 1)     Area = 15
    Sphere ( 1)     Area = 16
    Sphere ( 1)     Area = 17
    Sphere ( 1)     Area = 18
    Sphere ( 1)     Area = 19
    Sphere ( 1)     Area = 20
    Sphere ( 2)     Area =  1
    Sphere ( 2)     Area =  2
    etc...
    How would I avoid the spheres being repeated 20 times? And how would I make it like the example below?

    Code:
    Sphere ( 1)     Area = 1
    Sphere ( 2)     Area = 2
    etc...
    Sorry for so many Questions.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,588
    Because you should be performing some kind of calculation for each sphere
    Code:
    for(i=1; i<21; i++)
    {
        printf("Sphere (%2d)     Area = %d\n", i, i*i);
    }
    Note that i*i is not the function for calculating the surface area of a sphere, that's for you to research.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  2. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 06:49 PM
  3. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 11:53 AM
  4. C++ newbie / linux not so newbie question
    By goldmonkey in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2003, 11:27 AM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM

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