Homework Help!!

This is a discussion on Homework Help!! within the C Programming forums, part of the General Programming Boards category; I have a project due that I'm stumped on. The problem states: Read 5 integers from keyboard and place them ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    3

    Homework Help!!

    I have a project due that I'm stumped on. The problem states:

    Read 5 integers from keyboard and place them in an array, then sort them in ascending and descending order, then print the sorted lists. "The program must not change the original array or create any other integer arrays."

    The output is to be a vertical list of the 3 arrays:

    Ascending | Original | Descending

    Also states the solution requires two pointer arrays...we're basically on sorts (selection, bubble) pointers and arrays...no advanced stuff.

    I have no problem reading in the numbers to an array and sorting but no matter what i do it changes the original array.

    Any ideas to get me in the right direction?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So on the assumption that you have two arrays of pointers for the sorted lists, then you be swapping the pointers themselves, not the values pointed to. That is to say, this:
    Code:
    temp = a;
    a = b;
    b = temp;
    as opposed to this:
    Code:
    temp = *a;
    *a = *b;
    *b = temp;

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    3

    Post

    Ok, so here's what I have so far. Problem is I'm changing the original input array and I'm not sure why...

    I also need to print as a vertical list like below...

    Ascending | Original | Descending



    What I have so far
    Code:
    #include "stdafx.h"
    #include "stdlib.h"
    
    #define SIZE 5
    #define ASC 1
    #define DESC 0
    
    
    //Function Declarations:
    int *sortAry (int source[], int size, int sort_order);
    void printAry(int a[], int size);
    
    
    int main (void)
    {
    
    	int i;
    	int inputAry[SIZE];
    	int *ascSort;
    	int *descSort;
    	
    
    	printf("Please enter 5 integers to be sorted: \n");
    		for (i=0; i < 5; i++)
    			scanf_s("%d", &inputAry[i]);
    				
    		
    printf("\nOriginal Input\n");
    printAry (inputAry, SIZE);
    
    printf("\nAscending Order\n");
    ascSort = sortAry(inputAry, SIZE, ASC);
    printAry (ascSort, SIZE);
    
    printf("\nDescending Order\n");
    descSort = sortAry(inputAry, SIZE, DESC);
    printAry (descSort, SIZE);
    
    printf("\nVerify Original Array\n");
    printAry (inputAry, SIZE); 
    printf("\n");
    
    
    
    
    return 0;
    }//main
    	
    	
    
    /*=============================sortAry==================================
    **Sorts array list
    */
    
    int *sortAry (int source[], int size, int sort_order)
    
    {
        int x;
    	int y;
        int temp[SIZE];
    
        for(int x=0; x < size; x++)   
        {
            for(int y=0; y < size-1; y++)
            {
                if ( sort_order == ASC ) 
                {
                   if(source[y]>source[y+1])  
                   {
                        int temp = source[y+1];        
                        source[y+1] = source[y];    
                        source[y] = temp;           
                   }
                }
                else                       
                {
                   if(source[y]<source[y+1])    
                   {
                        int temp = source[y+1];        
                        source[y+1] = source[y];    
                        source[y] = temp;            
                   }
                }
            }
        }
        return source;    
    }
    
    
    
    //====================================printAry===================================
    
    void printAry(int a[], int size)
    {
    for (int i = 0; i < SIZE; i++)
    	     printf("%3d,", a[i]);
    		 printf("\n");
    return;
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You need an array of pointers, not one single solitary pointer. Then you can set each pointer to point at a different element of the original array (by moving where the pointers point to, not by adjusting actual values in the original array).

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    3
    I'm just not getting it, I thought that was what I had. The book we're using isn't helping at all, I've been working on this forever.

    Could you provide some examples or links to something? I'm just not understand an array of pointers vs a pointer to an array I guess?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Jdub View Post
    I'm just not getting it, I thought that was what I had. The book we're using isn't helping at all, I've been working on this forever.

    Could you provide some examples or links to something? I'm just not understand an array of pointers vs a pointer to an array I guess?
    The difference between one thing and an array of things is pretty easy to spot, I should think:
    Code:
    int* one_single_thing;
    int* a_bunch_of_things[how_many_things];
    So make your array of pointers and set them to point at each of your data element things:
    Code:
    int* ascSort[SIZE];
    for (int i = 0; i < SIZE; i++) {
        ascSort[i] = &inputAry[i];
    }
    It is perhaps a good idea to mention at this point that you can't write one function that will print both your main array and your sorted data, since inputAry and ascSort are of different types.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void p( void *a, size_t c, int i )
    {
        if( a )
        {
            size_t x;
            for( x = 0; x < c; x++ )
            {
                printf( "%d:", x );
                if( i )
                    printf(" %d\n", *(int *)(a+x) );
                else
                    printf(" %p\n", *(int**)(a+x) );
            }
        }
    }
    Something like that?


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

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by quzah View Post
    Code:
    void p( void *a, size_t c, int i )
    {
        if( a )
        {
            size_t x;
            for( x = 0; x < c; x++ )
            {
                printf( "%d:", x );
                if( i )
                    printf(" %d\n", *(int *)(a+x) );
                else
                    printf(" %p\n", *(int**)(a+x) );
            }
        }
    }
    Something like that?


    Quzah.
    Except for
    Code:
    error: pointer of type `void *' used in arithmetic
    Otherwise sure. (And hey, why not make the third parameter an enum? That's more fun than something like
    Code:
    #define ORIGINAL_ARRAY 1
    #define SORTED_ARRAY 0

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void p( void *a, size_t c, int i )
    {
        if( a )
        {
            size_t x;
            for( x = 0; x < c; x++ )
            {
                printf( "%d:", x );
                if( i )
                {
                    int **ip = a;
                    printf(" %d\n", ip[x] );
                }
                else
                {
                    int *ipp = a;
                    printf(" %p\n", (void*)(ipp+x) );
                }
            }
        }
    }
    Good enough for who it's for.

    Anyway, the point is, you really can print two different types of things in the same function.


    Quzah.
    Last edited by quzah; 04-23-2010 at 10:18 PM.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    10
    I am giving you the logic just try it out:

    Take two variables max and min
    and find the max number put of these and print it
    then again print the second maximum element and so on

    Repeat the step same with the min

    find the max number put of these and print it
    then again print the second maximum element and so on

    and then print original array along eith these numbers.

  11. #11
    Registered User
    Join Date
    Jun 2008
    Posts
    54
    Code:
    int comp(const void* a, const void* b)
    {
    	int num1=**((int**)(a)), num2=**((int**)(b));
    	return num1<num2 ? -1 : num1==num2 ? 0 : 1 ;
    }
    int comp_rev(const void* a, const void* b)
    {
    	return -comp(a, b);
    }
    You could try using something like that with the qsort() function. You just need to assign the arrays of integer pointers first for the ascending and descending.
    Code:
    	int *ascSort[SIZE];
    	int *descSort[SIZE];
    	for(i=0;i<SIZE; i++)
    	{
    		ascSort[i]=&inputAry[i];
    		descSort[i]=&inputAry[i];
    	}	
    qsort(ascSort, SIZE, sizeof(int*), comp);
    qsort(descSort, SIZE, sizeof(int*), comp_rev);
    This method neither changes the original array or creates additional integer arrays.

    Code:
    Please enter 5 integers to be sorted: 
    5
    8
    3
    1
    2
    
    Ascending order:
      1,  2,  3,  5,  8,
    Descending order:
      8,  5,  3,  2,  1,
    Original order:
      5,  8,  3,  1,  2,
    Edit:
    The way it works is that it passes the data and the comparison functions to the qsort() function, which does all the heavy lifting for you. qsort() is part of the C standard library.
    Here is its prototype:
    void qsort ( void * base, size_t num, size_t size, int ( * comparator ) ( const void *, const void * ) );

    The void * base argument just means that you can pass a pointer to any kind of array there. The function uses size_t num (the number of elements in the array) and size_t size (the size of each individual element) to tell how it can move around in base. Void pointers can be pointers to anything; you just need to recast them as pointers to other types to dereference them as those.

    The last argument to qsort() is a function pointer that gives qsort a way to determine what's higher or lower in the array. You can use function pointers more or less like regular pointers. One way to think about how function pointers work is that, instead of pointing to data memory, function pointers point to instruction memory.
    int ( * comparator ) ( const void *, const void * ) just means that the function the function pointer comparator points to has to return an int and take two const void pointers as arguments.

    For qsort() to work as expected, whatever function comparator points to must return a negative number if the first number is smaller than the second, 0 if the two numbers are equal, and a positive number if the first number is larger than the second. If you negate whatever that function returns ( like in comp_rev() ), qsort will give you the reverse of the order it would have normally.

    It is important to note that qsort() rearranges the elements of base, which in our case are just pointers to the elements of the original array - the original array is left untouched.
    Last edited by Boxknife; 04-24-2010 at 03:54 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 03:39 PM
  2. Homework
    By kermi3 in forum C Programming
    Replies: 10
    Last Post: 09-27-2001, 04:49 PM
  3. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 03:16 PM
  4. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 01:26 PM

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