Array and sort help

This is a discussion on Array and sort help within the C Programming forums, part of the General Programming Boards category; I need to have the user input up to 20 integer values. Then, I need to display the subscript of ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    21

    Array and sort help

    I need to have the user input up to 20 integer values. Then, I need to display the subscript of the largest value in the array as well as the largest value itself. I think I have the programming required for the array completed, but I'm not sure how to make the program sort through it.

    Here's what I have so far:

    Code:
    #include <stdio.h>
    #include <string.h>
    #define max_number 20
    
    int main(void)
    {
    int numbers[max_numbers][20],
         arr[20];
    
    int i;
    for(i=0;i<21;i++)
       {printf("Enter a number:   ");
         scanf("%d",&arr);
       }
    
    }
    Does this look sufficient enough to accomplish the array aspect? If so, then how do I go about sorting through the integers to find and display the largest value?


    Thank you.

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Well first of all, this will give you some trouble:

    Code:
    #define max_number 20
    ....
    int numbers[max_numbers][20]
    Arrays are 0 based in C, so your index (assuming an array of 20 elements) would range from element 0 to element 19. In general you control a loop of size n with a counter terminating at 1 less than size n or, in other words, n - 1.

    Code:
    for(i = 0; i < 20; i++)
    If you want to store succesive numbers in your array, then you need to increment the pointer to you array - you have this:
    Code:
     scanf("%d",&arr);
    I suspect that what you intended was to use a subscript (or pointer notation) something like so:

    Code:
    for(i=0;i<21;i++)
       {printf("Enter a number:   ");
         scanf("%d",&arr[i]);
       }
    You should return something from main:

    Code:
    int main(void)
    {
        ...
        return 0;
    }
    Last edited by kermit; 11-18-2005 at 05:46 PM.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    21

    Updated

    Made some updates:

    Code:
    #include <stdio.h>
    #include <string.h>
    #define max_number 20
    
    int main(void)
    {
    int numbers[max_number][20],
         arr[20];
    int max;
    int i;
    arr[0]=max;
    for(i=0;i<20;i++)
       { printf("Enter a number:  ");
          scanf("%d",&arr[i]);
               if (arr[i]>max)
                  max=arr[i];
        }
    printf("Largest entry is %d", max);
    return 0;
    }
    The program currently effectively determines and displays the largest value within the array. Suggestions to determine and display the subscript value for the largest entry?

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Perhaps this is what you had in mind??

    Code:
    #include <stdio.h>
    #include <string.h>
    #define max_number 20
    
    int main(void)
    {
    int numbers[max_number][20],
         arr[20];
    int max;
    int subscript;
    int i;
    arr[0]=max;
    for(i=0;i<20;i++)
       { printf("Enter a number:  ");
          scanf("%d",&arr[i]);
               if (arr[i]>max)
                  max=arr[i];
                  subscript = i;
        }
    printf("Largest entry is %d and is found at arr[%d]\n", max, subscript);
    return 0;
    }

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    21
    Quote Originally Posted by kermit
    Perhaps this is what you had in mind??

    Code:
    #include <stdio.h>
    #include <string.h>
    #define max_number 20
    
    int main(void)
    {
    int numbers[max_number][20],
         arr[20];
    int max;
    int subscript;
    int i;
    arr[0]=max;
    for(i=0;i<20;i++)
       { printf("Enter a number:  ");
          scanf("%d",&arr[i]);
               if (arr[i]>max)
                  max=arr[i];
                  subscript = i;
        }
    printf("Largest entry is %d and is found at arr[%d]\n", max, subscript);
    return 0;
    }
    I had tried variations of that. It appears that the subscript value is being recorded as the last 'i' value in the loop regardless. Therefore, it is getting displayed as 19 everytime. Any suggestions?

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Code:
    if (arr[i]>max) {
                  max=arr; 
                  subscript = i;
    }
    heh - forgot to add the braces to make it into a block (So much for actually checking if the code works on the compiler)

    edit:

    Perhaps this suits you - take a look at what I changed with regards to the use of 'largest' (What you called 'max'):

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <limits.h>
    
    #define MAX_NUMBER 20
    
    int main(void)
    {
            int arr[MAX_NUMBER];
            int largest = INT_MIN;          /* used to be 'max' */
            int subscript;
            int i;
           
            for (i = 0; i < MAX_NUMBER; i++) {
                    printf("Enter a number:  ");
                    scanf("%d", &arr[i]);
                    if (arr[i] > largest) {
                            largest = arr[i];
                            subscript = i;
                    }
            }
            printf("Largest entry is %d and is found at arr[%d]\n", largest,
                   subscript);
           
    	return 0;
    }
    As well, I cleaned up some of your 'magic numbers' - Generally speaking it is a good idea to use symbolic constants where possible. In the above example, if I want to change the amount of numbers from 20 to 10, I need only change one line:

    Code:
    #define MAX_NUMBER 20
    The way it was before there would have been a couple of places needing to be changed. On a small program this may little trouble (But as Kenneth Reek says, 'Little trouble is some trouble.') but on a bigger program it could be a nightmare.
    Last edited by kermit; 11-18-2005 at 10:48 PM.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can get rid of your variable, YES/NO, and if check by simply assigning the variable INT_MIN first.
    Code:
    #include<limits.h>
    int largest = INT_MIN;
    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Hey thats a good idea. I edited my (above) program to incorporate that idea.
    Last edited by kermit; 11-18-2005 at 07:45 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-10-2003, 05:21 PM
  2. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  3. Sorting an array of structures with sort()
    By rmullen3 in forum C++ Programming
    Replies: 3
    Last Post: 01-03-2003, 03:02 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21