To find biggest of n numbers ?
-
Tarun Jha wrote:
num[i] > num[j];
I suspect that you think that line is doing something that it is not. Put some curly braces around that line then print out the values before and after it.
-
i need to find the biggest number, but it doesn't print the biggest number.
#include
#includeint main(){
int num\[50\], i=0, j, count =0; //int k; printf("Enter 0 to exit entering integers.\\n\\nEnter your integers:\\n"); do{ scanf("%d", &num\[i\]); i++; count++; }while(num\[i-1\]!=0); for(i=0; i<=count -1; i++){ for(j=0; j<=count-1; j++){ if(i != j) num\[i\] > num\[j\]; else break; } printf("%d is the biggest number", num\[i\]); } //printf("%d is the biggest number", num\[j\]); return 0;
}
-
Most of that code is redundant, all you need is something like:
int num = -1, largest = 0;
do{
printf("Enter an integer, 0 to exit:\n");scanf("%d", &num); if (num > largest) largest = num;
}while(num != 0);
printf("%d is the biggest number", largest); -
Start with the hint that jschell gave, look at that line of code and see what it is doing if anything, or better yet step though each line of code with a debugger.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
-
i need to find the biggest number, but it doesn't print the biggest number.
#include
#includeint main(){
int num\[50\], i=0, j, count =0; //int k; printf("Enter 0 to exit entering integers.\\n\\nEnter your integers:\\n"); do{ scanf("%d", &num\[i\]); i++; count++; }while(num\[i-1\]!=0); for(i=0; i<=count -1; i++){ for(j=0; j<=count-1; j++){ if(i != j) num\[i\] > num\[j\]; else break; } printf("%d is the biggest number", num\[i\]); } //printf("%d is the biggest number", num\[j\]); return 0;
}
The problem is obvious
if(i != j)
num[i] > num[j];
else
break;Start with i=0 j = 0 what does the break do ... i==j doesn't it so it will call the break .. single step debug and watch :-)
In vino veritas
-
Tarun Jha wrote:
what is wrong with the one i am working with.
Your second loop makes no sense as you are using two index varaibles when you only need one, and you never compare the values to see which is largest, or keep a note of the largest value you have found so far.
-
Tarun Jha wrote:
what is wrong with the one i am working with.
Your second loop makes no sense as you are using two index varaibles when you only need one, and you never compare the values to see which is largest, or keep a note of the largest value you have found so far.
ohh! :omg: Thank you. but the code written below only works some time.. :sigh:
#include
#includeint main(){
int num\[50\], i=0, j, count =0; int k, b; printf("Enter 0 to exit entering integers.\\n\\nEnter your integers:\\n"); do{ scanf("%d", &num\[i\]); i++; count++; }while(num\[i-1\]!=0); for(k=0; k<=count-2; k++){ for(j=0; j<=count-2; j++){ if(k != j){ num\[k\] > num\[j\]; b = k; //printf("%d", num\[k\]); continue; } else break; } //printf("%d is the biggest number \\n", num\[k\]); } printf("%d is the biggest number", num\[b\]); return 0;
}
-
ohh! :omg: Thank you. but the code written below only works some time.. :sigh:
#include
#includeint main(){
int num\[50\], i=0, j, count =0; int k, b; printf("Enter 0 to exit entering integers.\\n\\nEnter your integers:\\n"); do{ scanf("%d", &num\[i\]); i++; count++; }while(num\[i-1\]!=0); for(k=0; k<=count-2; k++){ for(j=0; j<=count-2; j++){ if(k != j){ num\[k\] > num\[j\]; b = k; //printf("%d", num\[k\]); continue; } else break; } //printf("%d is the biggest number \\n", num\[k\]); } printf("%d is the biggest number", num\[b\]); return 0;
}
Tarun Jha wrote:
...only works some time
Which technically means it does not work. If your algorithm is dependent upon its input in order to produce the correct output, it is wrong, period. You should be able to do this entire exercise using pencil and paper before ever committing anything to code. Short of that and you are just guessing (hoping) at best.
"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
-
ohh! :omg: Thank you. but the code written below only works some time.. :sigh:
#include
#includeint main(){
int num\[50\], i=0, j, count =0; int k, b; printf("Enter 0 to exit entering integers.\\n\\nEnter your integers:\\n"); do{ scanf("%d", &num\[i\]); i++; count++; }while(num\[i-1\]!=0); for(k=0; k<=count-2; k++){ for(j=0; j<=count-2; j++){ if(k != j){ num\[k\] > num\[j\]; b = k; //printf("%d", num\[k\]); continue; } else break; } //printf("%d is the biggest number \\n", num\[k\]); } printf("%d is the biggest number", num\[b\]); return 0;
}