Thread: array[] problem

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    16

    Angry array[] problem

    The question asks to take the following list{12,31, 43, 23,25, 78, 200, 109, 345, 5, 188} into an integer array, and let the program find the smallest, median, and largest value and print them out.
    This is what i done so far
    Code:
    #include<stdio.h>
    #define MAX 11
    
    int array[MAX] = {12,31, 43, 23,25, 78, 200, 109, 345, 5, 188};
    int largest(int array[]);
    
    void main() 
    {
          printf("the largest is : %d\n", largest(array);
    }
    
    int largest(int array[])
    {
    My profesoor told me to use
    PHP Code:
    Bsort, list 
    , but i don't know where to put them, so can anybody help, thx

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well at least you started. Ok, so what do you do next? How would you go about finding what one is the largest?

    Do you know what a loop is? How about using one to run through the list? Read each value and compare it with whatever value you have stored in a temp value. If the one you're comparing is bigger, put it in the temp valud.

    When you're done with the loop, the temp value will hold the biggest value.

    The same goes for the smallest.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    45
    Finally, for the median, you might think about a function that sorts the array, then find the middle of the array for your median number.

    Have fun.

  4. #4
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    Bubble sort ?

    I don't know what you mean by Bsort... Bubble sort ?

    Bubble sort is one of the quickest way to arrange the numbers in ascending/descending orders...

    here's a simple bubble sort program:
    PHP Code:
    #include <stdio.h>

    main()
    {
        
    int a[10] = {1,2,3,4,5,6,7,8,9,10};
        
    int i,c,j;
        
        for (
    j=0j<=8;j++){
        for (
    i=0;i<=8i++){
            if (
    a[i]<a[i+1]){
            
    c=a[i];
            
    a[i]=a[i+1];
            
    a[i+1]=c;
            }
        }
    }
        for (
    i=0i<=9i++){
        
    printf("a[%d]=%d\n",ia[i]);
        }

    If bubble sort is what you're looking for, you can change this program to fit your needs.
    Last edited by moonwalker; 07-23-2002 at 02:31 PM.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: Bubble sort ?

    >Bubble sort is one of the quickest way to arrange the numbers in ascending/descending orders...
    It is certainly not the quickest, in fact for sorting large array, its probably one of the slowest. But for small arrays the programmer probably won't notice too much time delay.

    There's a good demo here for sorting algorithms.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Bubble sort is one of the quickest way to arrange the numbers in ascending/descending orders...
    That's a good one. Bubble sort is probably the least efficient sorting method in existance next to bogo-sort. But I do have to admit that anything below about 5000 elements is technically okay using bubble sort (though you don't want to use it in the real world). There are certainly faster and easier to understand methods which don't suffer quite as much under a realistic stress test.

    >But for small arrays the programmer probably won't notice too much time delay.
    Have you ever sorted a small array that remained small outside of a classroom?

    -Prelude
    My best code is written with the delete key.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >Have you ever sorted a small array that remained small outside of a classroom?
    Yes! But I know where your coming from..
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By jalex39 in forum C Programming
    Replies: 5
    Last Post: 03-29-2008, 07:27 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM