gaza165 Posted February 17, 2009 Share Posted February 17, 2009 im looking to write a function to find the smallest number in an array. is this right?? for(i = 0; i < count; i ++) { printf("%2d> ",i+1); scanf("%d", &numbers[i]); sum += numbers[i]; smallest = numbers[0]; if(numbers[i] < smallest) { smallest = numbers[i]; } } Quote Link to comment https://forums.phpfreaks.com/topic/145571-finding-smallest-number-from-an-array/ Share on other sites More sharing options...
allworknoplay Posted February 17, 2009 Share Posted February 17, 2009 Why don't you just use asort(); Quote Link to comment https://forums.phpfreaks.com/topic/145571-finding-smallest-number-from-an-array/#findComment-764202 Share on other sites More sharing options...
gaza165 Posted February 17, 2009 Author Share Posted February 17, 2009 i want to be able to write my own function to show my brain power. Quote Link to comment https://forums.phpfreaks.com/topic/145571-finding-smallest-number-from-an-array/#findComment-764204 Share on other sites More sharing options...
cola Posted February 17, 2009 Share Posted February 17, 2009 U can use this function to find smallest element in array. $smallest = min($array); Quote Link to comment https://forums.phpfreaks.com/topic/145571-finding-smallest-number-from-an-array/#findComment-764207 Share on other sites More sharing options...
allworknoplay Posted February 17, 2009 Share Posted February 17, 2009 i want to be able to write my own function to show my brain power. Good for you. No shame in that....however looking at that code makes my head hurt...LOL.... I can tell you real quick that your for statement is wrong. You need to use $i, not just "i".... Quote Link to comment https://forums.phpfreaks.com/topic/145571-finding-smallest-number-from-an-array/#findComment-764209 Share on other sites More sharing options...
gaza165 Posted February 17, 2009 Author Share Posted February 17, 2009 yeah i know that, but i dont want to use in built functions to do it i want to hard code my own if statements. Quote Link to comment https://forums.phpfreaks.com/topic/145571-finding-smallest-number-from-an-array/#findComment-764210 Share on other sites More sharing options...
Mchl Posted February 17, 2009 Share Posted February 17, 2009 This doesn't look like php code to me Quote Link to comment https://forums.phpfreaks.com/topic/145571-finding-smallest-number-from-an-array/#findComment-764215 Share on other sites More sharing options...
allworknoplay Posted February 17, 2009 Share Posted February 17, 2009 This doesn't look like php code to me It looks psuedo or maybe from another language... Quote Link to comment https://forums.phpfreaks.com/topic/145571-finding-smallest-number-from-an-array/#findComment-764216 Share on other sites More sharing options...
cola Posted February 17, 2009 Share Posted February 17, 2009 Thi si C Quote Link to comment https://forums.phpfreaks.com/topic/145571-finding-smallest-number-from-an-array/#findComment-764218 Share on other sites More sharing options...
Mchl Posted February 17, 2009 Share Posted February 17, 2009 It's C(++), and apart from finding minimum, also reads data from input (scanf) and calculates the sum (which is not necessary for finding minimum) The code for PHP would look a bit different, but the basic idea stays the same. Quote Link to comment https://forums.phpfreaks.com/topic/145571-finding-smallest-number-from-an-array/#findComment-764220 Share on other sites More sharing options...
sasa Posted February 17, 2009 Share Posted February 17, 2009 move smallest = numbers[0]; before for loop, and then you can start your loop with 1 Quote Link to comment https://forums.phpfreaks.com/topic/145571-finding-smallest-number-from-an-array/#findComment-764225 Share on other sites More sharing options...
gaza165 Posted February 17, 2009 Author Share Posted February 17, 2009 you are right guys, it is C but i didnt know who else to turn too!!! i have got it now guys thankyou and sorry if i have wasted ur time #include <stdio.h> int main(void) { int numbers[10]; int count; long sum = 0L; float average = 0.0f; int i; int j; int smallest; int largest; printf("\nEnter the numbers:\n"); for(i = 0; ; i++) { printf("%2d> ",i+1); scanf("%d", &numbers[i]); if(numbers[i] < 0) { break; } sum += numbers[i]; count = i + 1; //get smallest number from array. smallest = numbers[0]; for (j =0; j < count; j++) { if (numbers[j] < smallest) smallest = numbers[j]; } //get smallest number from array. largest = numbers[0]; for (j =0; j < count; j++) { if (numbers[j] > largest) largest = numbers[j]; } } average = (float)sum/count; printf("\nAverage of the ten numbers entered is: %f\n", average); printf("\nSmallest number is: %d\n", smallest); printf("\nLargest number is: %d\n", largest); return 0; } Quote Link to comment https://forums.phpfreaks.com/topic/145571-finding-smallest-number-from-an-array/#findComment-764227 Share on other sites More sharing options...
Mchl Posted February 17, 2009 Share Posted February 17, 2009 I guess there aren't much examples for this written in PHP, since it has built in functions for that Quote Link to comment https://forums.phpfreaks.com/topic/145571-finding-smallest-number-from-an-array/#findComment-764233 Share on other sites More sharing options...
premiso Posted February 17, 2009 Share Posted February 17, 2009 i have got it now guys thankyou and sorry if i have wasted ur time Next time post this in the right forum ( PHP Core Hacking ) And I would also state at the beginning what language you are using, as in this forum it is assumed PHP, where as C programming is not the same Syntax as PHP, thus your answers are going to be interpreted that you do not know PHP syntax and thus pointed in that direction. Quote Link to comment https://forums.phpfreaks.com/topic/145571-finding-smallest-number-from-an-array/#findComment-764242 Share on other sites More sharing options...
Mchl Posted February 17, 2009 Share Posted February 17, 2009 That doesn't really belong to PHP Core Hacking. As I understand, gaza165 wanted to write a function in PHP. It's just the code example he's found, is in C. Or am I wrong? Even then, it would rather go to Other Programming Languages Quote Link to comment https://forums.phpfreaks.com/topic/145571-finding-smallest-number-from-an-array/#findComment-764257 Share on other sites More sharing options...
premiso Posted February 17, 2009 Share Posted February 17, 2009 That doesn't really belong to PHP Core Hacking. As I understand, gaza165 wanted to write a function in PHP. It's just the code example he's found, is in C. Or am I wrong? Even then, it would rather go to Other Programming Languages Oh...I thought he wanted that function as an element of php via being compiled....I could be wrong, my mind is in a daze and I cannot think straight today, but yea. I would probably be in the Other Programming Languages if that was what he was after. Quote Link to comment https://forums.phpfreaks.com/topic/145571-finding-smallest-number-from-an-array/#findComment-764263 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.