Thread: help with looping

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    2

    Unhappy help with looping

    Hi, everyone from C Board. I'm very new at C, and I'm absolutely stuck here with loops within loops in this program I have to write.

    I'll be honest and say that this is an assignment problem, but I don't expect anyone to help me write it, I'm just dumbfounded as to the things I need to do to it to have it work. I have the plan designed for it, but all these loopings is getting me nowhere.

    1) what i want to do is allow the user to input a number which will be divided into 3 sizes that the user chooses. eg. the number is 500 and we can make chunks of 20,10, and 30 from it with different combinations. (20 x 20 + 10 x 10 + 0 x 30 for example as long as it doesn't go over the max which is 500)

    - after they enter the numbers and it satisfies the if statement in blue, i want to initialize counters for 3 integers i,j,k starting at 0, which will count from 0 to maxlength+1 while a*i+b*j+c*k<=maxlength and then the output will be a list of all the combinations that can be generated from those 3 numbers.


    Here's my program as of now:

    Code:
    #include <stdio.h>    
    #include <math.h>
    #define maxlength 100
    int main(void)
    {
        int total, a, b, c;
        int i,j,k;
    
        printf("...\n");
        scanf("%d",&total);
        { if (total=<maxlength)
    	printf("Enter 3 numbers\n", total);
    	scanf("%d%d%d", &a,&b,&c);
    
    	{if ((a>=0) && (b>=0) && (c>=0) && (a+b+c<=maxlength))
    { 
    	
    	for (i=0;i<maxlength+1;i++) 
        	for (j=0;j<maxlength+1;j++)
    	for (k=0;k<maxlength+1;k++)
    do{
    	a*i+b*j+c*k
    
    	while (a*i+b*j+c*k<=maxlength){
    	printf ("%d x %d + %d x %d + %d x %d\n", i+1, j+1, k+1, a,b,c);
    	}
    	}
    
    
    
    
    	else {
    	printf("Enter 3 valid numbers again");
    }
    
    
        	else {
    	printf("Please enter a length between 0 and 100 and try again");
    
    }
    }
    }
    
    
    
    
        return (0);
    }
    I don't know how to continue especially the part with the for loop which seems to be a mess. I'm hoping you guys can help me resolve this problem. I'd appreciate it a lot.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    It's a welcome surprise to see a first post looking like this. The first thing I might recommend is not to use scanf for user input -- perhaps take a look at User Input: Strings and Numbers [C] (which also contains links to this site's FAQ). And then perhaps clean up your indentation: what you posted does not compile -- some braces seem really out of place. Starting with working code makes things easier. Consider adding some debugging printfs to "see" what's going on.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    2
    Thank you for your advice, Dave.

    I'm trying to do it over step by step instead of building the codes all at once to try to figure out the problem.

    I have a problem here.

    #include <stdio.h>
    #include <math.h>
    #define maxlength 100
    int main(void)
    {
    int total, a, b, c;
    int i,j,k;

    printf("...\n");
    scanf("%d",&total);
    if (total<maxlength+1)
    { printf("Enter 3 numbers\n", total);
    scanf("%d%d%d", &a,&b,&c);
    if ((a>=0) && (b>=0) && (c>=0) && (a+b+c<maxlength+1))
    { printf("Generating list...");
    }
    else if (a+b+c>maxlength){
    printf("Enter 3 valid size again");
    }

    }
    else {
    printf("Enter length between 1 and 100 and try again\n");
    }

    return (0);
    }
    This program compiles, but it bypasses the statement a+b+c>maxlength when I enter 3 numbers that sums more than maxlength and output the same line "Generating list..." instead of "Enter 3 valid size again" which I intended. What do I do to fix it?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I don't see what you're saying, because I didn't see the values you tried or the associated outputs.

    Perhaps go with something along this line.
    Code:
    #include <stdio.h>
    
    #define maxlength 100
    
    int main(void)
    {
       int a, b, c;
       for ( ;; )
       {
          printf("Enter 3 numbers: ");
          fflush(stdout);
          if ( scanf("%d%d%d", &a, &b, &c) == 3 )
          {
             if ( (a >= 0) && (b >= 0) && (c >= 0) && (a + b + c < maxlength + 1) )
             {
                break;
             }
          }
          puts("Enter 3 valid size again");
       }
       printf("Generating list...");
       /* ... */
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wierd looping effect after exporting 3ds to .x annimation
    By Anddos in forum Game Programming
    Replies: 3
    Last Post: 01-06-2009, 01:43 PM
  2. problems with prototype function looping
    By dezz101 in forum C Programming
    Replies: 5
    Last Post: 04-29-2008, 06:03 AM
  3. looping went berserk
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 09-21-2004, 01:59 PM
  4. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2003, 11:01 PM
  5. looping and input
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 07:12 AM