# Getting the loop control form the user

• 08-10-2005
Extropian
Getting the loop control form the user
A quick question:

Normally I've seen that the programmer sets the loops control variable for a for statement. How does one get the user to set it?
Thanks,
Extro
• 08-10-2005
hk_mp5kpdw
Code:

```int numtimes, i; printf("How many times do you want the loop to run: "); scanf("%d",&numtimes); for( i = 0; i < numtimes; ++i ) {     // Do stuff }```
• 08-10-2005
Extropian
I got it to work but any feedback is appreciated -can I make it better?(constructive criticism welcome).
Regards,
Extro

Code:

```         int                num1, num2;                        int                integers;                /* How many numbers the user wants analyzed */         int                counter = 0;        /* Allows more than two numbers to be analyzed */         int                total;         int                smallest;                        int                largest;         float        average;         int total2;         printf("Enter number of digits to be analyzed: ");        /* Ask for how many digits are to be analyzed */         scanf("%d" , &integers);                        printf("Enter the first number: ");                scanf("%d" , &num1);         for(counter = 1; counter < integers; counter++){        /* Allows the user to have several numbers analyzed */                 printf("Enter the next number: ");                 scanf("%d" , &num2);                                 if(num1 > 2){                        /* Sets the smallest and largest numbers */                         largest = num1;                         smallest = num2;                                         }                 if(num2 > num1){                         largest = num2;                         smallest = num1;                         }                                                                 if(integers < counter){                                                         }                         total = num1 + num2;                                /* Totals the numbers */                                                         }         total2 = total + num2;         average = (float)total2/counter;                /* Averages the numbers */         printf("\n");        /* Prints a blank line */         printf("The smallest number is %d\n " , smallest);        /* Prints the smallest number */         printf("The largest number is %d \n" , largest);                /* Prints the largest number */         printf("The total is %d\n" , total2);                                        /* Prints the total */         printf("The average is %f\n " , average);                                /*        Prints the aeverage */         return 0;                                                                                        /*Ends program */ }```
• 08-10-2005
kermit
Gnu indent would be a start. ;)
• 08-10-2005
Extropian
Gnu indent?
lol, ya lost me... :confused:
• 08-11-2005
Machoscorpion
hey ,

Y don't you make use of array to hold the numbers. ? you will be able to
hold the list of number the user has entered.

you can initialise small & large to say 0 and then compare the number you
have read to with small and large and make the change to small and large..

like

Code:

``` if( num <small)       small=num;     else       {           if(num>large)             large=num;       }```
you can also do it in online. with conditional operator..
• 08-11-2005
itsme86
Code:

`you can initialise small & large to say 0`
That's a bad idea. What if the smallest number the user enters is 3. Since small was initialized to 0 then it's going to report false information. A better idea would be to initialize small to INT_MAX and large to INT_MIN.