Find Biggest num

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training

Find Biggest num

 
0
  #1
May 29th, 2009
I m trying to find the biggest number in the array but problem is that it keeps returning one
  1. #include <stdio.h>
  2. int FindBiggest(int *arr,int choice)
  3. {
  4. int i;
  5. int num=0;
  6. for(i=0;arr[i]<=sizeof arr;i++) {
  7. if(num<arr[i])
  8. num=arr[i];
  9. }
  10. return num;
  11. }
  12. int main(void)
  13. {
  14. int arr[]={0,5,3};
  15. int result;
  16. result=FIND_NUM(arr,BIGGEST);
  17. printf("%d\n",result);
  18. getchar();
  19. return 0;
  20. }
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,434
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 118
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Find Biggest num

 
0
  #2
May 29th, 2009
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 :)
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training

Re: Find Biggest num

 
0
  #3
May 29th, 2009
yah i changed that i also changed in function the if better and still not working
  1. #include <stdio.h>
  2. int FindBiggest(int *arr)
  3. {
  4. int i;
  5. int num=0;
  6. for(i=0;arr[i]<=3;i++) {
  7. if(arr[i]>num)
  8. num=arr[i];
  9. }
  10. return num;
  11. }
  12. int main(void)
  13. {
  14. int arr[]={0,5,3};
  15. int result;
  16. result=FindBiggest(arr);
  17. printf("%d\n",result);
  18. getchar();
  19. return 0;
  20. }
now ofcourse 1st num is bigger than 0 whish should set num to 5 whats wrong now ?
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,615
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 121
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Find Biggest num

 
0
  #4
May 29th, 2009
your problem is here: for(i=0;arr[i]<=3;i++)

your index is 'i' yet your conditional is based on the value of 'arr' at the index 'i'

fix that.


.
Last edited by jephthah; May 29th, 2009 at 6:12 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,434
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 118
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Find Biggest num

 
0
  #5
May 29th, 2009
#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;
}
I highlighted the changes, also you were looping through too many elements in the array by using <=.
Last edited by William Hemsworth; May 29th, 2009 at 6:15 pm.
I need pageviews! most fun profile ever :)
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training

Re: Find Biggest num

 
0
  #6
May 29th, 2009
i made it work now but what the diff between array[i] and just normal i?
  1. #include <stdio.h>
  2. int FindBiggest(int *arr)
  3. {
  4. int i;
  5. int num=0;
  6. for(i=0;i<3;i++) {//used to be for(i=0;array[i<3;i++)
  7. if(arr[i]>num)
  8. num=arr[i];
  9. }
  10. return num;
  11. }
  12. int main(void)
  13. {
  14. int arr[]={0,5,3};
  15. int result;
  16. result=FindBiggest(arr);
  17. printf("%d\n",result);
  18. getchar();
  19. return 0;
  20. }
in first line in the for loop ?
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,434
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 118
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Find Biggest num

 
0
  #7
May 29th, 2009
>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.
I need pageviews! most fun profile ever :)
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training

Re: Find Biggest num

 
0
  #8
May 29th, 2009
thnaks man but i got one more question i made also a find biggest and smallest but it alawys get last element
  1. #include <stdio.h>
  2. #define BIGGEST 1
  3. #define Smallest 0
  4. int FindBiggest(int *arr,int choice)
  5. {
  6. int i;
  7. int num=0;
  8. for(i=0;i<3;i++) {
  9. if(choice){//find BIGGEST NUM
  10. if(arr[i]>num)
  11. num=arr[i];
  12. }
  13. else
  14. {
  15. num=1000;
  16. if(arr[i]<num)
  17. num=arr[i];
  18. }
  19.  
  20. }
  21. return num;
  22. }
  23. int main(void)
  24. {
  25. int arr[]={0,5,3};
  26. int result;
  27. result=FindBiggest(arr,Smallest);
  28. printf("%d\n",result);
  29. getchar();
  30. return 0;
  31. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,434
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 118
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Find Biggest num

 
0
  #9
May 29th, 2009
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:
  1. #include <stdio.h>
  2. #include <limits.h>
  3.  
  4. #define Biggest 1
  5. #define Smallest 0
  6.  
  7. int FindBiggest(int *arr, int length, int choice)
  8. {
  9. int i;
  10. int num = 0;
  11.  
  12. if ( choice == Biggest ) {
  13. for (i = 0; i < length; i++) {
  14. if ( arr[i] > num ) { // Look for a bigger value
  15. num = arr[i];
  16. }
  17. }
  18. }
  19.  
  20. else if ( choice == Smallest ) {
  21. num = INT_MAX; // Make it start off with the biggest possible value
  22. for (i = 0; i < length; i++) {
  23. if ( arr[i] < num ) { // Look for a smaller value
  24. num = arr[i];
  25. }
  26. }
  27. }
  28.  
  29. return num;
  30. }
  31.  
  32. int main(void)
  33. {
  34. int arr[] = {2, 1, 3};
  35. int length = sizeof(arr) / sizeof(arr[0]); // Works out the length of arr
  36. int result;
  37.  
  38. result = FindBiggest(arr, length, Smallest);
  39. printf("%d\n", result);
  40.  
  41. getchar();
  42. return 0;
  43. }
Last edited by William Hemsworth; May 29th, 2009 at 6:30 pm.
I need pageviews! most fun profile ever :)
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,615
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 121
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Find Biggest num

 
0
  #10
May 29th, 2009
i made also a find biggest and smallest but it alawys get last element
because for the smallest, you set num=1000 every single time you go through the loop. arr[i] will always be less than 1000 each time, so it stores it in num, each time.


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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC