| | |
Find Biggest num
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
I m trying to find the biggest number in the array but problem is that it keeps returning one
even though in my function i set the number to 0 first then after that we check with the if num<array[i] whish ofcourse it is now it we set the num to array[i] untill there no we found our biggest num i dunno its problem :s
C Syntax (Toggle Plain Text)
#include <stdio.h> int FindBiggest(int *arr,int choice) { int i; int num=0; for(i=0;arr[i]<=sizeof arr;i++) { if(num<arr[i]) num=arr[i]; } return num; } int main(void) { int arr[]={0,5,3}; int result; result=FIND_NUM(arr,BIGGEST); printf("%d\n",result); getchar(); return 0; }
•
•
Join Date: Mar 2008
Posts: 1,434
Reputation:
Solved Threads: 118
Is there any code you aren't posting? what is FIND_NUM and BIGGEST? You can't use sizeof to work out the length of an array passed to a pointer, instead you have to allow the caller to pass the length of the array as a parameter.
I need pageviews! most fun profile ever :)
yah i changed that i also changed in function the if better and still not working
now ofcourse 1st num is bigger than 0 whish should set num to 5 whats wrong now
?
C Syntax (Toggle Plain Text)
#include <stdio.h> int FindBiggest(int *arr) { int i; int num=0; for(i=0;arr[i]<=3;i++) { if(arr[i]>num) num=arr[i]; } return num; } int main(void) { int arr[]={0,5,3}; int result; result=FindBiggest(arr); printf("%d\n",result); getchar(); return 0; }
? •
•
Join Date: Mar 2008
Posts: 1,434
Reputation:
Solved Threads: 118
#include <stdio.h> int FindBiggest(int *arr, int length) { int i; int num = 0; for (i = 0; i < length; i++) { if ( arr[i] > num ) num = arr[i]; } return num; } int main(void) { int arr[] = {0, 5, 3}; int length = sizeof(arr) / sizeof(arr[0]); int result; result = FindBiggest( arr, length ); printf( "%d\n", result ); getchar(); return 0; }
Last edited by William Hemsworth; May 29th, 2009 at 6:15 pm.
I need pageviews! most fun profile ever :)
i made it work now but what the diff between array[i] and just normal i?
in first line in the for loop ?
C Syntax (Toggle Plain Text)
#include <stdio.h> int FindBiggest(int *arr) { int i; int num=0; for(i=0;i<3;i++) {//used to be for(i=0;array[i<3;i++) if(arr[i]>num) num=arr[i]; } return num; } int main(void) { int arr[]={0,5,3}; int result; result=FindBiggest(arr); printf("%d\n",result); getchar(); return 0; }
•
•
Join Date: Mar 2008
Posts: 1,434
Reputation:
Solved Threads: 118
>what the diff between array[i] and just normal i?
It's two completely different things, i is just an integer, array[i] is the element which is located at the index of i.
It's two completely different things, i is just an integer, array[i] is the element which is located at the index of i.
I need pageviews! most fun profile ever :)
thnaks man but i got one more question i made also a find biggest and smallest but it alawys get last element
C Syntax (Toggle Plain Text)
#include <stdio.h> #define BIGGEST 1 #define Smallest 0 int FindBiggest(int *arr,int choice) { int i; int num=0; for(i=0;i<3;i++) { if(choice){//find BIGGEST NUM if(arr[i]>num) num=arr[i]; } else { num=1000; if(arr[i]<num) num=arr[i]; } } return num; } int main(void) { int arr[]={0,5,3}; int result; result=FindBiggest(arr,Smallest); printf("%d\n",result); getchar(); return 0; }
•
•
Join Date: Mar 2008
Posts: 1,434
Reputation:
Solved Threads: 118
If you are working out the biggest number, you have to start off with the smallest possible value and keep looking for a bigger value.
To find the smallest, you have to do the opposite. Start off with the biggest possible value, and keep looking for a smaller one.
Also it is more efficient to put the for loop inside the if block instead of the other way round, here is the corrected code:
To find the smallest, you have to do the opposite. Start off with the biggest possible value, and keep looking for a smaller one.
Also it is more efficient to put the for loop inside the if block instead of the other way round, here is the corrected code:
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <limits.h> #define Biggest 1 #define Smallest 0 int FindBiggest(int *arr, int length, int choice) { int i; int num = 0; if ( choice == Biggest ) { for (i = 0; i < length; i++) { if ( arr[i] > num ) { // Look for a bigger value num = arr[i]; } } } else if ( choice == Smallest ) { num = INT_MAX; // Make it start off with the biggest possible value for (i = 0; i < length; i++) { if ( arr[i] < num ) { // Look for a smaller value num = arr[i]; } } } return num; } int main(void) { int arr[] = {2, 1, 3}; int length = sizeof(arr) / sizeof(arr[0]); // Works out the length of arr int result; result = FindBiggest(arr, length, Smallest); printf("%d\n", result); getchar(); return 0; }
Last edited by William Hemsworth; May 29th, 2009 at 6:30 pm.
I need pageviews! most fun profile ever :)
•
•
•
•
i made also a find biggest and smallest but it alawys get last element
EDIT: i see William already re-wrote your code for you. consider yourself (un)lucky, because i would have made you fix it yourself.
Last edited by jephthah; May 29th, 2009 at 6:34 pm.
![]() |
Similar Threads
- Find the biggest element in that array (C++)
- Pls help.I have To Implent Biggest function to find biggest element in that array! (C++)
- urgent Homework help!! (Pascal and Delphi)
- overloading operator [] (C++)
- HD Max Size Help Needed (Storage)
- Linked List (C)
- Sorting Strings (Java)
Other Threads in the C Forum
- Previous Thread: c compiler
- Next Thread: valid c-syntax ?
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch char cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fflush fgets file fork forloop framework frequency getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. match matrix meter microsoft motherboard mqqueue multi mysql number odf opensource overwrite owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research scanf scripting segmentationfault sequential shape socket socketprograming standard string strings systemcall testing threads turboc unix user voidmain() wab windows.h






