While loop not working on C.
-
THE WORKING CODE
Quote:
#include #include int main(){ int num[50], i=0, j; int count = 0; printf("Enter your integer: \n"); do{ scanf(" %d", &num[i]); i++; count++; }while(num[i-1]!=0); for(j=1; j<= count-1; j++){ if(num[0] < num[j]){ num[0] = num[j]; } } printf("%d is the greatest of them all !", num[0]); return 0; }
Suggestions: 1) The variable
count
is not necessary. You can usei
instead. 2) The separatefor()
loop is not necessary. You can do the min/max checking in thewhile()
loop."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
In this program i have tried to store all the input in the array. The while should continue till the user input 0, but the loop exits only after one input.
#include
#includeint main(){
int num\[50\], i, j; int count = 0; printf("Enter your integer: \\n"); do{ scanf(" %d", &num\[i\]); i++; count++; }while(num\[i\]!=0); for(j=1; j<= count; j++){ if(num\[1\] > num\[j\]){ num\[1\] = num\[j\]; } } printf("%d is the greatest of them all !", num\[0\]); return 0;
}
here you go
#include
#includeint main(){
int num\[50\], i = -1, j; int count = 0; printf("Enter your integer: \\n"); do{ scanf(" %d", &num\[i+1\]); i++; count++; }while(num\[i\]!=0); for(j=1; j<= count; j++){ if(num\[1\] > num\[j\]){ num\[1\] = num\[j\]; } } printf("%d is the greatest of them all !", num\[0\]); return 0;
}
-
In this program i have tried to store all the input in the array. The while should continue till the user input 0, but the loop exits only after one input.
#include
#includeint main(){
int num\[50\], i, j; int count = 0; printf("Enter your integer: \\n"); do{ scanf(" %d", &num\[i\]); i++; count++; }while(num\[i\]!=0); for(j=1; j<= count; j++){ if(num\[1\] > num\[j\]){ num\[1\] = num\[j\]; } } printf("%d is the greatest of them all !", num\[0\]); return 0;
}
The first problem is that the variable i has not been initialized. The second problem is that you are checking the wrong array index. See the comments in the modified version of your code below.
#include #include int main(){
int num\[50\], i, j; int count = 0; i = 0; // Added this printf("Enter your integer: \\n"); do{ scanf(" %d", &num\[i\]); count++; }while(num\[i++\]!=0); // Changed this for(j=1; j<= count; j++){ if(num\[1\] > num\[j\]){ num\[1\] = num\[j\]; } } printf("%d is the greatest of them all !", num\[0\]); return 0;
}
-
First suspicious point : you declare i but you do not initialize it (you also declare j but at least you plan to initialize it in the for loop so it is acceptable). Second suspicious point: you are to hasty to increment i. If you want to insist using a
do {
...
} while (condition)loop , and not a
while (condition)
{ ... }then you must realize that you need to check the value just read (if it was zero) and then , if it not 0 , meaning the user want to keep giving values , increase i++ , getting ready for reading the next element. Try to implement those 2 points , and also as an extra exercise , implement the while(condition) { ... } It is not hard, you can do it in around half an hour or less, and you will benefit a lot by pondering on how it will differ from your do { ... } while(condition)
-
The first problem is that the variable i has not been initialized. The second problem is that you are checking the wrong array index. See the comments in the modified version of your code below.
#include #include int main(){
int num\[50\], i, j; int count = 0; i = 0; // Added this printf("Enter your integer: \\n"); do{ scanf(" %d", &num\[i\]); count++; }while(num\[i++\]!=0); // Changed this for(j=1; j<= count; j++){ if(num\[1\] > num\[j\]){ num\[1\] = num\[j\]; } } printf("%d is the greatest of them all !", num\[0\]); return 0;
}
asa76 wrote:
for(j=1; j<= count; j++)
{
if (num[1] > num[j])
{
num[1] = num[j];
}
}printf("%d is the greatest of them all !", num[0]);
You do realize this is wrong, don't you?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
First suspicious point : you declare i but you do not initialize it (you also declare j but at least you plan to initialize it in the for loop so it is acceptable). Second suspicious point: you are to hasty to increment i. If you want to insist using a
do {
...
} while (condition)loop , and not a
while (condition)
{ ... }then you must realize that you need to check the value just read (if it was zero) and then , if it not 0 , meaning the user want to keep giving values , increase i++ , getting ready for reading the next element. Try to implement those 2 points , and also as an extra exercise , implement the while(condition) { ... } It is not hard, you can do it in around half an hour or less, and you will benefit a lot by pondering on how it will differ from your do { ... } while(condition)
-
here you go
#include
#includeint main(){
int num\[50\], i = -1, j; int count = 0; printf("Enter your integer: \\n"); do{ scanf(" %d", &num\[i+1\]); i++; count++; }while(num\[i\]!=0); for(j=1; j<= count; j++){ if(num\[1\] > num\[j\]){ num\[1\] = num\[j\]; } } printf("%d is the greatest of them all !", num\[0\]); return 0;
}
-
THE WORKING CODE
Quote:
#include #include int main(){ int num[50], i=0, j; int count = 0; printf("Enter your integer: \n"); do{ scanf(" %d", &num[i]); i++; count++; }while(num[i-1]!=0); for(j=1; j<= count-1; j++){ if(num[0] < num[j]){ num[0] = num[j]; } } printf("%d is the greatest of them all !", num[0]); return 0; }
Since you present a working solution you deserve to receive some more elegant alternatives. Here is how I improved your code , there are some nice tricks for you to learn from it
#include
#define SIZE 15
int main()
{
int num[SIZE], i=0 , max;
int count = 0; // be sure that we do not read more than 15 elements
printf("Enter your integers seperated by Enter (give 0 to finish input): \n");
/*
scanf(" %d", &num[i]);
while (num[i]!=0 && count < SIZE )
{
++count;
scanf(" %d", &num[++i]);
}
*/
/* Either the above while loop */
do{
scanf(" %d", &num[i]);
count++;
} while(num[i++]!=0 && count < SIZE);
/* Or the above do { } while loop */
max = num[0]; /* Avoid to altert any of your array elements */
for(i=1; i< count; i++)
if(max < num[i]) max = num[i]; /* each time we find a better max, update our max value */
printf("%d is the greatest of them all !\n", max);
return 0;
}I have deliberately put a smaller array of 15 elements so you can test what happens if you really try to input more than 15 elements. And if you are more lazy , you can even change it to 7 elements , you only have to go to one place and do your modification now , SIZE ! Also , comment the do-while block and uncomment the while block , and compile and run again the program with various values (pay attention to provide the max at the 1st and last positions at many of your tests) . Modify everything that you do not understand ehy it is like it is [for example change the pre-increments ++i and ++count that I use , with post-increments i++ , count++ , and compile/run/test with edge cases (max given at 1st and last positions) to see what wrong things happen ].
-
Since you present a working solution you deserve to receive some more elegant alternatives. Here is how I improved your code , there are some nice tricks for you to learn from it
#include
#define SIZE 15
int main()
{
int num[SIZE], i=0 , max;
int count = 0; // be sure that we do not read more than 15 elements
printf("Enter your integers seperated by Enter (give 0 to finish input): \n");
/*
scanf(" %d", &num[i]);
while (num[i]!=0 && count < SIZE )
{
++count;
scanf(" %d", &num[++i]);
}
*/
/* Either the above while loop */
do{
scanf(" %d", &num[i]);
count++;
} while(num[i++]!=0 && count < SIZE);
/* Or the above do { } while loop */
max = num[0]; /* Avoid to altert any of your array elements */
for(i=1; i< count; i++)
if(max < num[i]) max = num[i]; /* each time we find a better max, update our max value */
printf("%d is the greatest of them all !\n", max);
return 0;
}I have deliberately put a smaller array of 15 elements so you can test what happens if you really try to input more than 15 elements. And if you are more lazy , you can even change it to 7 elements , you only have to go to one place and do your modification now , SIZE ! Also , comment the do-while block and uncomment the while block , and compile and run again the program with various values (pay attention to provide the max at the 1st and last positions at many of your tests) . Modify everything that you do not understand ehy it is like it is [for example change the pre-increments ++i and ++count that I use , with post-increments i++ , count++ , and compile/run/test with edge cases (max given at 1st and last positions) to see what wrong things happen ].
-
here you go
#include
#includeint main(){
int num\[50\], i = -1, j; int count = 0; printf("Enter your integer: \\n"); do{ scanf(" %d", &num\[i+1\]); i++; count++; }while(num\[i\]!=0); for(j=1; j<= count; j++){ if(num\[1\] > num\[j\]){ num\[1\] = num\[j\]; } } printf("%d is the greatest of them all !", num\[0\]); return 0;
}
See here.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles