What's wrong with my code for finding the highest integer in a given data? (C Programming)
``` #include <stdio.h> #include <conio.h> main() { int numData, cntr, max; int data[10]; printf("How many numbers? "); scanf("%d", &numData); max = data[0]; for (cntr = 0; cntr < numData; cntr++) { printf("Data #%d > ", cntr + 1); scanf("%d", &data[cntr]); } for (cntr = 0; cntr < numData; cntr++) { if (data[cntr] < data[cntr + 1]) { max = data[cntr + 1]; data[cntr + 1] = data[cntr]; if (numData > cntr + 1) continue; else break; } else { max = data[cntr]; if (numData > cntr + 1) continue; else break; } } printf("The highest number is %d", max); getch(); } ```
well first it says "getch" I believe you meant to say fetch();?
Also you have the same for loop twice, I would combine then if possible.
actually getch() doesn't matter....it's just a function so that the program doesn't exit immediately
also...the two loops have different functions....
Yeah I see that now with the loops... I had written a program like this awhile ago... But In Java there's a comparator that does all the work for you so :P.
I'll quickly try to code it and see if my code can show any errors.
Does the first for loop read in the data?
why are you doing such a difficult programming?
the program you are writing is for sorting .
let me help you put to find out the maximum number. tak 1 variable max. put the condition in 2nd for loop that if max > data[cntr] then max=data[cntr]. you will get the answer.
Enjoy C coding.
@rambo2210 so my 2nd loop is the mistake?
yeap.
let me help you put to find out the maximum number. tak 1 variable max. put the condition in 2nd for loop that if max > data[cntr] then max=data[cntr]. you will get the answer.
follow those steps you will geth answer.
your 2nd for loop must look like dis:- for (cntr = 0; cntr < numData; cntr++) { if(max < data[cntr]) { max=data[cntr]; } }
no need to reloop it?
this doesn't work ``` #include <stdio.h> #include <conio.h> main() { int numData, cntr, max; int data[10]; printf("How many numbers? "); scanf("%d", &numData); max = data[0]; for (cntr = 0; cntr < numData; cntr++) { printf("Data #%d > ", cntr + 1); scanf("%d", &data[cntr]); } for (cntr = 1; cntr < numData; cntr++) { if (max < data[cntr]) { max = data[cntr]; if (numData > cntr) continue; else break; } } printf("The highest number is %d", max); getch(); } ```
if (max < data[cntr]) { max = data[cntr]; if (numData > cntr) continue; else break; change above loop to below loop:- if(max<data[cntr]) { max = data[cntr]; } }//end od for loop
but how will it check the other numbers?
it will check you just try. once you will get output. then I will explain you.
still doesn't work ``` #include <stdio.h> #include <conio.h> main() { int numData, cntr, max; int data[10]; printf("How many numbers? "); scanf("%d", &numData); max = data[0]; for (cntr = 0; cntr < numData; cntr++) { printf("Data #%d > ", cntr + 1); scanf("%d", &data[cntr]); } for (cntr = 1; cntr < numData; cntr++) { if (max < data[cntr]) max = data[cntr]; } printf("The highest number is %d", max); getch(); } ```
even if i change cntr to cntr = 0...still doesn't work
initialize max = 0. and remove max = data[0];
it worked... i get why the if statement is that way...but i don't get why initialize max as 0
you were assigning the max value before getting the value from user. so It will be some garbage value which might be greater than all of the number you have entered.
well my plan was just to assign the first variable as temporary maximum then compare it with the others
then you should put that after following loop. for (cntr = 0; cntr < numData; cntr++) { printf("Data #%d > ", cntr + 1); scanf("%d", &data[cntr]); } max = data[0]; for (cntr = 1; cntr < numData; cntr++) ... an so on. try this it will work for sure.
Got the answer. or still confused?
yes that helps
SO If you have any question about C programming.please contact me.I can surely help you out.
Join our real-time social learning platform and learn together with your friends!