Thread: Need help with code!

  1. #1
    Registered User
    Join Date
    Jun 2015
    Posts
    4

    Red face Need help with code!

    Hi guys I have to find the cubes of odd numbers between +17 to -17 using a while loop but for some reason I'm getting an infinite loop and I wonder if you guys could help me find the error.

    Code:
    //PROBLEM 1
    //C
    
    
    #include <stdio.h>
    #include <math.h>
    
    
    int cube(int y);  //function prototype
    
    
    int main(void)
    {
        int j; // inner counter
    
    
        puts("PROBLEM 2 PART (C) - CUBES OF ODD NUMBERS FROM 17 TO -17 USING WHILE LOOP:\n\n X VALUES: \t\ FACTORIAL VALUES OF X: \t\t \n\n"); // display table headers and compute square value of i
    
    
        // outer counter counting from -16 to 16
        int i = 17;
        while(i >= -17 )
        {
            i++;
            if(i % 2 == 1 || i % 2 == -1)
            {
                int factor = 1;
                j = 17;
                while (j >= i)
                {
                    factor = cube(j);
                    j++;
    
    
                } // end inner for
    
    
                printf("%d\t\t %d\t\t  \n\n", i, cube(i));
                }
            }
            return 0;
        }
    int cube(int y)
    {
        return y * y * y;
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    int i = 17;
    while(i >= -17 )
    {
        i++;
    "i" will always be >= -17, so when do you expect this loop to end?

    Note that decrementing "i" instead will have consequences on the inner "while()" loop.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    4
    I changed it but now it's not printing out anything
    Attached Images Attached Images Need help with code!-problem-png 

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Post your updated code.

    But first, read the last sentence of my above post and see if that leads you to the problem.

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    4
    Quote Originally Posted by Matticus View Post
    Post your updated code.

    But first, read the last sentence of my above post and see if that leads you to the problem.
    I tried decrementing it and it's still not showing anything

    Here is the updated code:
    Code:
     
    
    
    //PROBLEM 1
    //C
    
    
    #include <stdio.h>
    #include <math.h>
    
    
    int cube(int y);  //function prototype
    
    
    int main(void)
    {
        int j; // inner counter
    
    
        puts("PROBLEM 2 PART (C) - CUBES OF ODD NUMBERS FROM 17 TO -17 USING WHILE LOOP:\n\n X VALUES: \t\ FACTORIAL VALUES OF X: \t\t \n\n"); // display table headers and compute square value of i
    
    
        // outer counter counting from 17 to -17
        int i = 17;
        while(i <= -17 )
        {
            i--;
            if(i % 2 == 1 || i % 2 == -1)
            {
                int factor = 1;
                j = 17;
                while (j <= i)
                {
                    factor = cube(j);
                    j++;
    
    
                } // end inner for
    
    
                printf("%d\t\t %d\t\t  \n\n", i, cube(i));
                }
            }
            return 0;
        }
    
    
    
    
    int cube(int y)
    {
        return y * y * y;
    }

  6. #6
    Registered User
    Join Date
    May 2015
    Posts
    52
    In the last code part, i starts off as 17, so it's never less than or equal to -17. So, while() never runs.

    I'd suggest
    Code:
    for(i=-17;i<=17;i++)
    Then you'd have to take a look in the inside of the for() loop.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    int i = 17;
    while(i <= -17 )
    Well, now "i <= -17" is false from the start, so the outer loop is never executed.



    To be honest, I'm not sure what your inner loop is supposed to be doing. You might be over-thinking your problem here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-19-2012, 01:58 PM
  2. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  3. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 07:09 AM
  4. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 07:51 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

Tags for this Thread