[C programing] find largest and smallest number in an array

Discussion in 'Mac Programming' started by gotenks05, Oct 1, 2009.

  1. gotenks05 macrumors member

    Joined:
    Jan 1, 2009
    #1
    I am working on a program that takes an integer array and puts it into separate functions that display the sum, the product, the average, and which number in the array is largest and smallest. I got it to work somewhat, but I'm having difficulties. The functions for sum, product, and average give me the correct results, but the functions to compare the numbers are not so lucky. I was able to get one of them to work, but not the other. When comparing for the smallest number, it display a number that does not exist in the array. In a different method, which I don't remember what it was, it displayed -3, when -4 is the smallest. How do I correct this?

    here is the source for finding the smallest number:

    Code:
    int comparelow(int n[], int size)
    {
    	int count2, part;
    	int low;
    	part = 0;
    
    	for (count2 = 0; count2 >= size -1; count2++)
    	{
    		while (part != size -1)
    		{
    			if (n[count2] < n[part])
    			{
    				low = n[count2];
    			}
    			part++;
    		}
    	}
    
    	return low;
    }
    and here is the source for the program, if it helps:

    Code:
    #include <stdio.h>
    #include "simple.h"
    #define Size 10
    
    int main()
    {
    	int number[Size] = {12, 2, 6, -4, 2, 9, 7, 1, 8, -3};
    	int items = Size;
    
    	printf("Sum is %d\n", sum(number, items));
    	printf("Average is %d\n", mean(number, items));
    	printf("Product is %d\n", product(number, items));
    	printf("Smallest is %d\n", comparelow(number, items));
    	printf("Largest is %d\n", compare(number, items));
    
    	return 0;
    }
     
  2. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #2
    You definitely don't need a while loop inside of the for loop. Just use the for loop to go through the array once, and at every iteration check to see if the number at that location in the array is smaller than the smallest number at any location you have already seen. In other words, you will be keeping track of the smallest number you have seen so far while iterating through the array.
     
  3. Guiyon macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #3
    Double check your logic in the for loop. It's not doing what you thing it's doing.

    The loop in english:
    Set count2 to 0 and, as long as count2 is greater than or equal to (size-1), perform the loop and increment count by one.

    Thing about how often count2 is greater than or equal to (size-1) if you set it to 0 initially...
     
  4. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #4
     
  5. ChrisA macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #5
    Code:
    #include <stdio.h>
    
    int main()
    {
    	int number[Size] = {12, 2, 6, -4, 2, 9, 7, 1, 8, -3};
    	int items = Size;
    
    	printf("Sum is %d\n", sum(number, items));
    	printf("Average is %d\n", mean(number, items));
    	printf("Product is %d\n", product(number, items));
    	printf("Smallest is %d\n", comparelow(number, items));
    	printf("Largest is %d\n", compare(number, items));
    
    	return 0;
    }
    This would work for a homework problem but is really not a good design. You make five loops over your data. Better to make one loop and look for all five numbers inside that one loop.

    The trick for finding the smallest is

    if(array < current_smallest) current_smallest = array;

    But that and something like "sum += array"

    inside one for loop;

    A hint about initializing current_smallest: It is reasonable to assume any random array element is the smallest until you have reason to change that assumption. The IF statement above provides that reason.
     
  6. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #6
    Give yourself additional information to work with -

    Code:
    int comparelow(int n[], int size)
    {
    	int count2, part;
    	int low;
    	part = 0;
    
    	for (count2 = 0; count2 >= size -1; count2++)
    	{
    printf("Top of loop 'count2': %d and 'low': %d\n", count2, low);
    		while (part != size -1)
    		{
    			if (n[count2] < n[part])
    			{
    				low = n[count2];
    			}
    			part++;
    		}
    printf("bottom of loop 'count2': %d and 'low': %d\n\n", count2, low);
    	}
    
    	return low;
    }
    
     
  7. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #7
    In your other method, I suspect it came up with -3 the one time just because that happens to be the last number (element) in your array. Any time you get an answer that's the first or last element in an array and you know it's wrong, that should be a clue to check the loop and compare logic because it's probably related to your counting variable either never advancing, or advancing every time (comparison will always get the same result), or your result variable receiving the current element no matter what.

    And the other guys are right too, you have a logic error and also the loop is far more complicated than it needs to be. There doesn't need to be more than a couple lines inside it.
     
  8. gotenks05 thread starter macrumors member

    Joined:
    Jan 1, 2009
    #8


    Uh, I did not want to them all in the same function. In reality, there are only four loops. The sum function is used inside the mean function, as you need a sum before finding an average. admanimal helped a lot, as I figured out that count2 was not supposed to be used in the for loop from his/her post.

    That helped a bit. I forgot that I tried specifying elements through descending order.

    Well, thanks for the help. I got it figured out.

    Note to Mods or admin only: Can somebody mark this as solved? I don't see the option anywhere.
     
  9. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #9
    Since you've got it figured out I'll give you this -

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    #define minimum(n, m)	(((n < m) ? n : m))
    #define maximum(n, m)	(((n > m) ? n : m))
    
    int main()
    {
    	int elements[]		= { 12, 2, 6, -4, 2, 9, 7, 1, 8, -3 };
    	int	element_count	= (sizeof(elements) / sizeof(elements[0]));
    	int	low;
    	int	i;
    	
    	low = elements[0];
    	for ( i = 1; i < element_count; i++ )
    	{
    		low = minimum(low, elements[i]);
    	}
    	
    	printf("The lowest value element is: %d\n", low);
    }
     

Share This Page