Thread: homework help! C program that sorts array of 5 integer numbers

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    23

    Exclamation homework help! C program that sorts array of 5 integer numbers

    Hi, this is my problem that I have to do.

    Write a C program thatsorts an unsorted array of 5 integer numbers in ascending order byswapping numbers repeatedly. Specifically, first prompt the user toinput 5 numbers and store the numbers in an array. Then, if thenumbers in the array are not in ascending order, ask the user toprovide the indices of two numbers in the array that the user wantsto swap. If the user does not enter valid indices (i.e., outside 0 to4), then re-prompt the user to enter new indices. Continue promptingthe user to provide indices of numbers to swap until the arraybecomes sorted in ascending order. Finally, when the array becomessorted, the program should print “The array is now sorted inascending order”.


    ImplementationRequirements



    1. Use the #define directive to define the size of the array.




    1. Implement a function called “checkArrayOrder” to check whether an array is in ascending order or not. The prototype of the function is

    “intcheckArrayOrder(int arr[], int size);”. The function should return

      1. 1 if the array passed to the function is in ascending order
      2. 0 if the array passed to the function is not in ascending order


    Define the function prototype in your program.



    1. Implement a function called “swap”, whose prototype is

    “intswap(int arr[], int size, int i, int j);” which swaps the numbersin positions “i” and “j” of the array passed to the functionand returns

      1. 1 if the indices “i” and “j” are within bounds (i.e., between 0 and 4) and the swap operation is performed correctly.
      2. 0 if the indices “i” and “j” are outside bounds so that the swap operation cannot be performed.



    Define the function prototype in your program.



    1. Use the function “checkArrayOrder” whenever your program needs to check whether an array is in ascending order or not; and the function “swap” to swap the numbers the user specifies (Note that, you should check whether the user provided indices are valid inside the function “swap” and not inside “main”).

      here is my code so far

      Code:
      #define ARRAY_SIZE 5
      
      
      void main()
      
      
      {
         int i, array[ARRAY_SIZE];
      
      
         printf("Enter %d numbers\n", ARRAY_SIZE);
         for(i=0; i<ARRAY_SIZE; ++i)
          scanf("%d", &array[i]);
      
      
      }
      
      
      int checkArrayOrder(int array[], int size)
      
      
      {
          for (int i=0; i<size-1; i++)
          {
             if (array[i] > array[i+1])
             {
      
      
             }
              else
              {
                  printf("The array is now in ascending order.");
              }
          }
      }
      
      
      int swap(int array[], int size, int i, int j)
      
      
      {
          for (int i=0; i<size-1; i++)
          {
              if ((i||j < 0) || (i||j > 4))
              {
                  printf("Invalid Indices.");
              }
              else
              {
                  size=0;
                  size = array[i];
                  array[i] = array[j];
                  array[j] = size;
              }
          }
      I'm just not sure how to proceed.
      I'm not really sure how to progress the checkArrayOrder function to the swap function (in the if statement).
      If anybody could help me with this it would be much appreciated.
      Thank you for all your help in advance.

  2. #2
    Registered User
    Join Date
    Sep 2013
    Posts
    23
    I have been working on this and got to this

    Code:
    #define ARRAY_SIZE 5
    
    
    void main()
    
    
    {
       int i, array[ARRAY_SIZE];
    
    
       printf("Enter %d numbers\n", ARRAY_SIZE);
       for(i=0; i<ARRAY_SIZE; ++i)
        scanf("%d", &array[i]);
    
    
    }
    
    
    int checkArrayOrder(int array[], int size)
    
    
    {
        for (int i=0; i<size-1; i++)
        {
           if (array[i] > array[i+1])
           {
                return size;
           }
            else
            {
                printf("The array is now in ascending order.");
            }
        }
    
    
        return 0;
    }
    
    
    int swap(int array[], int size, int i, int j)
    
    
    {
        for (i=0; i<size-1; i++)
        {
            printf("Enter the indices of the numbers you want to swap");
            if ((i||j < 0) || (i||j > 4))
            {
                printf("Invalid Indices.");
            }
            else
            {
                size=0;
                size = array[i];
                array[i] = array[j];
                array[j] = size;
            }
        }
    }
    I don't think this works, but hopefully I am getting on the right track. If anybody can tell me and help me, it would really be much appreciated!!! PLEASE HELP!!!

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > void main()
    main returns an int.

    Code:
    int checkArrayOrder(int array[], int size)
    {
        for (int i=0; i<size-1; i++)
        {
           if (array[i] > array[i+1])
           {
                return size;
           }
            else
            {
                printf("The array is now in ascending order.");
            }
        }
        return 0;
    }
    Read the program spec.
    “intcheckArrayOrder(int arr[], int size);”. The function should return
    1 if the array passed to the function is in ascending order
    0 if the array passed to the function is not in ascending order
    Some things
    - return size isn't either 0 or 1
    - there is nothing in the spec about printing the result
    - you need to check all elements of the array, and not return after the first success/fail test.

    > if ((i||j < 0) || (i||j > 4))
    C doesn't compare lists of numbers with other numbers.
    You need
    i < 0 || j < 0 || i > 4 || j > 4
    Actually, you should replace 4 with size.

    Finally, you need some more code in main to call these two functions in a loop, as described by your program specification.

    It's a good start, keep going!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2013
    Posts
    23
    ok, I have took what you said and fixed some things up, but I am still confused.

    Code:
    #define ARRAY_SIZE 5
    
    
    int checkArrayOrder(int array[], int size);
    int swap(int array[], int size, int i, int j);
    
    
    int main()
    
    
    {
       int i, sort1, sort2, array[ARRAY_SIZE];
    
    
       printf("Enter %d numbers\n", ARRAY_SIZE);
       for(i=0; i<ARRAY_SIZE; ++i)
        scanf("%d", &array[i]);
    
    
        for (int i=0; i<size-1; i++)
        {
                sort1 = checkArrayOrder(i);
    
    
                if(sort1 == 1)
                {
                    sort2 = swap(i);
    
    
                    if(sort2 == 0)
                    {
                        printf("Invalid Indices.");
                    }
                    if(sort2 == 1)
                    {
    
    
                    }
                }
        }
    
    
    }
    
    
    int checkArrayOrder(int array[], int size)
    
    
    {
        for (int i=0; i<size-1; i++)
        {
           if (array[i] > array[i+1])
           {
                return 1;
           }
    
    
            printf("The array is now in ascending order.");
            return 0;
        }
    }
    
    
    int swap(int array[], int size, int i, int j)
    
    
    {
        int temp;
    
    
        for (i=0; i<size-1; i++)
        {
            printf("Enter the indices of the numbers you want to swap");
            scanf("%d", "%d", & i, j);
            if (i<0 || j<0 || i>size || j>size)
            {
                return 0;
            }
    
    
            temp=0;
            temp = array[i];
            array[i] = array[j];
            array[j] = temp;
            return 1;
        }
    }
    How do I (for the if(sort2 == 1) get it to loop back into the checkArrayOrder function because I think that is what needs to be done. any input would definitely help.
    Thanks for all help in advance again.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    It would seem that swap() does not need a for loop.

    Also, checkArrayOrder() should loop over ALL elements (hint: set a flag where you presently have a return statement), and then return.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2013
    Posts
    23
    If you could explain to me what set a flag means, that would be most helpful. (I tried looking it up and can't find anything)
    Thank you for all your help so far.

  7. #7
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Most often for me, a flag is a print statement that helps me track where my code is and prints relevant values.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Simply put, a flag is a variable that contains one of two values (think of signal flags like in water skiing -- flag down means all is okay, flag raised means skier down in the water). This is similar to the general idea of a Boolean variable/value, though often with the added implication that one is tracking state for later use, or for use in two places that otherwise might not share share such information.

    A few links that might help:
    Flag (Computer Science Definition) - Computing Students
    language agnostic - Which variables can be referred to as flag variables? - Stack Overflow

    @MutantJohn:
    Don't think I've ever heard that usage before, but that doesn't necessarily mean it's wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-04-2011, 01:16 PM
  2. Replies: 12
    Last Post: 10-27-2011, 11:25 AM
  3. Replies: 5
    Last Post: 10-27-2011, 06:28 AM
  4. Replies: 12
    Last Post: 02-28-2008, 06:19 PM
  5. Replies: 6
    Last Post: 04-12-2002, 08:33 AM