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

  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 08:44 PM. Reason: demunging the horrible choice of tags - paste as TEXT in future

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    What is your current code, and how does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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.
    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, 02:07 PM