Like Tree2Likes
  • 1 Post By laserlight
  • 1 Post By TheBigH

C Program - Creating a table with squared and cubed results from user inputs

This is a discussion on C Program - Creating a table with squared and cubed results from user inputs within the C Programming forums, part of the General Programming Boards category; I am supposed to write a C program with a function selectTable that accepts values for numbers, tablesize, and increment ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    98

    C Program - Creating a table with squared and cubed results from user inputs

    I am supposed to write a C program with a function selectTable that accepts values for numbers, tablesize, and increment in which the numbers increase by and produce a table with those inputs. What am I doing wrong?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #pragmawarning (disable:4996)
    int main()
    {
      void selectTable(float, float, float);
      float tablesize, numbers, increment;
      printf("Ener the table size: ");
      scanf("%f", &tablesize);
      printf("Enter the number to begin with: ");
      scanf("%f", &numbers);
      printf("Enter the increment the numbers will increase by: ");
      scanf("%f", &increment);
      selectTable(numbers, tablesize, increment);
      system("pause");
      return 0;
    }
    
    void selectTable(float numbers, float tablesize, float increment)
    {
      int i;
      printf("NUMBER SQUARE CUBE \n");
      printf("------ ------ ---- \n");
       i = 0;
      for (numbers; numbers += increment;)
        for (i = numbers; i <= (numbers + (tablesize - 1) * increment);
             i += increment);
      printf("%f %f %f\n", numbers, numbers * numbers,
             numbers * numbers * numbers);
    }
    Last edited by Salem; 03-06-2012 at 07:44 PM. Reason: demunging the horrible choice of tags - paste as TEXT in future

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Remove the ; at the end of line 27
    You have loops which do nothing,
    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.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    I removed the ; on 27 my solution doesn't end. How do I correct the loop I wrote?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Well you could fix one of your for loops to actually have an exit condition.
    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.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    Lol, well the loop is the one thing the instructor gave me. I am just beginning in C, how would I write an exit condition.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    Quote Originally Posted by LearnOnTheFly
    I am just beginning in C, how would I write an exit condition.
    Compare the one without a proper exit condition:
    Code:
    for (numbers; numbers += increment;)
    with one that does:
    Code:
    for (i = numbers; i <= (numbers + (tablesize - 1) * increment); i += increment);
    Basically, numbers += increment became your exit condition, and unless numbers ever evaluates to 0 (false), you have an infinite loop.
    LearnOnTheFly likes this.
    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

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    I removed the invalid loop. When I run my solution. I get the correct table size and starting number but my numbers dont increase in increment

  8. #8
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    The solutions displays with the iputs tablesize = 4 starting number = 2 and increment = 3

    2 4 8
    2 4 8
    2 4 8
    2 4 8

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    What is your current code, and how does it not work?
    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

  10. #10
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #pragmawarning (disable:4996)
    int main ()
    {
    void selectTable(float, float, float);
    float tablesize, numbers, increment;
    printf("Ener the table size: ");
    scanf("%f", &tablesize);
    printf("Enter the number to begin with: ");
    scanf("%f", &numbers);
    printf("Enter the increment the numbers will increase by: ");
    scanf("%f", &increment);
    selectTable(numbers, tablesize, increment);
    system("pause");
    return 0;
    }
    void selectTable(float numbers, float tablesize, float increment)
    {
    int i;
    printf("NUMBER SQUARE CUBE \n");
    printf("------ ------ ---- \n");
     
    i = 0;
    for (i = numbers; i <= (numbers + (tablesize -1) * increment); i += increment)
    printf("%f %f %f\n", numbers, numbers*numbers, numbers*numbers*numbers);
    }
    

    The tablesize and starting number is correct but instead of the next number increasing by the incremnt amount it just repeats the starting value until the tablesize is satisfied.


  11. #11
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    That's because you're not printing a number that changes within the loop. I think you mean to do
    Code:
          printf("%f %f %f\n", i, i*i, i*i*i);
    although even this will have a few issues when you try to compile it.
    LearnOnTheFly likes this.
    Code:
    while(!asleep) {
       sheep++;
    }

  12. #12
    Registered User
    Join Date
    Mar 2012
    Posts
    98
    Thanks! I also had to change %f to %d, I am assuming becuase i was an int

  13. #13
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    That's right. You could also change "i" to a float. That might actually be better, because you're prompting the user for floats.
    Code:
    while(!asleep) {
       sheep++;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculator Program To Allow User 3 Inputs
    By Interista in forum C Programming
    Replies: 14
    Last Post: 10-19-2011, 11:11 AM
  2. How to handle inputs if the user inputs the wrong thing
    By bassist11 in forum C Programming
    Replies: 5
    Last Post: 09-22-2010, 04:28 AM
  3. User inputs two characters and...
    By dre in forum C Programming
    Replies: 6
    Last Post: 09-18-2009, 01:51 PM
  4. program is checking for ent when user inputs out?
    By Blizzarddog in forum C++ Programming
    Replies: 1
    Last Post: 04-07-2003, 01:16 PM
  5. Replies: 0
    Last Post: 03-15-2002, 01:07 PM

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