Any suggestions on whats wrong with this C Program?

This is a discussion on Any suggestions on whats wrong with this C Program? within the C Programming forums, part of the General Programming Boards category; Im taking a C programming course in my crappy community college and one of the projects is to: Write a ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    4

    Any suggestions on whats wrong with this C Program?

    Im taking a C programming course in my crappy community college and one of the projects is to:

    Write a C function named PowerTable() that will create a table of squares and cubes. The function should accept as parameters the starting number for the table, the number of rows in the table and the increment (always an integer) for each row of the table

    The function will output to the screen the following table:

    Number Square Cube
    -----------------------------
    1.5000 2.2500 3.3750
    3.5000 12.2500 42.8750
    5.5000 30.2500 166.3750

    My problem is I can only get the "number" colum to work,
    the square and cube all come out as 0.00000.
    Ive spent hours trying to figure out what I did wrong, and I can't see it. Any suggestions?

    Thanks everyone, you all rock

    <Someone just mentioned that my scanf's should be %1f instad of %d's, but when I change it I get even more jibberish...any ideas?>
    Code:
    #include <stdio.h> 
    
    double start; 
    int rows; 
    double incr; 
    
    
    
    /* double for starting number, integer for rows (can't have half a row), double for incriment value 
    (can incrimint by 1.5 and etc) */ 
    
    
    int PowerTable(double, int, double); 
    
    void main(void) 
    { 
    
    rows = 0; 
    
    printf("How many rows do you wish to have?\n"); 
    scanf("%d", &rows); 
    
    printf("What do you wish the starting number for the table to be?\n"); 
    scanf("%d", &start); 
    
    printf("How much do you wish each number to be incriminted?\n"); 
    scanf("%d", &incr); 
    
    
    PowerTable(start, rows, incr); 
    
    } 
    
    PowerTable(double fstart, int frows, double fincr ) 
    { 
    int timesdone; 
    double square[500]; 
    double cube[500]; 
    timesdone = 0; 
    
    printf("\tNumber \t\t\t Square\t\t\t Cube\n"); 
    printf("\t--------------------------------------------------\n"); 
    
    while(timesdone <= frows) 
    { 
    
    square[timesdone] = (fstart * fstart); 
    cube[timesdone] = fstart * fstart * fstart; 
    
    printf("\t%d \t\t\t \t %5.2f \t\t\t %5.2f\n", fstart, square[timesdone], cube[timesdone]); 
    
    fstart += fincr; 
    timesdone++; 
    
    } 
    
    printf("Done\n"); 
    
    
    
    return(0); 
    }
    Code tags added by Kermi3

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code Tags

    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happy about helping you


    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found at the link in my signature. I also suggest you take a look at the board guildlines if you have not done so already. Any further questions or ways I can help please feel free to PM me.

    Good Luck,
    Kermi3
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>void main(void)
    No, main returns an int. So use
    >>int main(void)
    and return 0; at the end.

    Change these:
    >>scanf("%d", &start);
    >>scanf("%d", &incr);
    to become
    >>scanf("%lf", &start);
    >>scanf("%lf", &incr);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    I would write the function loop more like this:

    Code:
    for(i=startnum;i<startnum+numrow*incr;i+=incr)
          printf("%1f \t %1f \t %1f\n",i,i*i,i*i*i);
    of course startnum, incr and i are all double while numrow is int.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  4. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 01:03 PM
  5. command line program, something wrong in the argv
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 09-26-2001, 09:36 AM

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