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
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
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 }
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
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 */ }
Last edited by Extropian; 08-10-2005 at 08:35 PM. Reason: Managed to fix it
Gnu indent?
lol, ya lost me...
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
you can also do it in online. with conditional operator..Code:if( num <small) small=num; else { if(num>large) large=num; }
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.Code:you can initialise small & large to say 0
If you understand what you're doing, you're not learning anything.