Thread: Help with Homework C

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

    Help with Homework C

    My assignment: Write a program to sort an array of integers via arrays of pointers to those integers as shown in thefigure. The code should contain the following functions: 1) to randomly generate an array of integernumbers; 2) to allocate memory and build the arrays of pointers as shown in the figure (two sucharrays are required); 3) to sort one of the arrays of pointers to point to the integer numbers in ascendingorder (must use recursive selection sort from Homework 4); 4) to sort one of the arrays of pointers topoint to the integer numbers in descending order (must use recursive insertion sort from Homework 4);5) to print the array of integers as shown in the bottom figure. Implement pass by reference and pass byvalue as appropriate.

    Honestly as for code I have legit nothing. I don't understand this at all for the most part and have tried a few things that have not worked out for me at all. I really just need a better understanding of my homework is asking me and way to start. Any and all help is appreciated.

    Thank you.

  2. #2
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Can you sort an array of integers using the "recursive selection sort from Homework 4?" You should use this as a starting reference. You can make some minor modifications to the sorting function(s) to achieve this "indirect" sort via pointers.

    Assuming you have the homework 4 reference available, you will need to make the following changes:

    1. Create an initialize the array of integers to be sorted. (This should already be done). Call this one array A.
    2. Now create another array of pointers to int. Call it array B. The first pointer in B will point to the address of the first integer in array A, the second pointer in B to the second element of array A, and so on.
    3. Modify the sorting algorithm so that instead of modifying array A directly, it swaps the pointers in array B instead. So you will need to modify the function parameters to take array of pointer to int, instead of just array of int.
    4. Now one can walk array B in order, printing out the values its elements point to in array A. This should produce a sorted list of integers.

    Implement pass by reference and pass byvalue as appropriate
    Minor caveat here: C is pass by value always, but you can achieve the effect of pass by reference in other languages that lack pointer types, by using C pointers.

  3. #3
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Is it possible that even "instructors" of the C language can mistake pass by value vs. pass by reference in such a well defined language such as C? Apparently so...
    Last edited by MacNilly; 11-28-2016 at 06:21 AM. Reason: grammaz

  4. #4
    Registered User
    Join Date
    Oct 2016
    Posts
    21
    So ive made some progress in my code and I think im on the right track. But i cant seem to find the reason when i run my program i get segmentation fault error.

    Code:
    #include <stdio.h>
    #include<time.h> // for time();
    #include<stdlib.h> //for rand();
    
    
    void selectionSort(int arr[], int *p_arr[], int *);
    void insertionSort(int arr[], int *q_array[], int *x);
    void printArray(int, int arr[], int *p_arr[]);
    void blah(int arr[], int *p_arr[], int *q_array[], int *x);
    void generate(int arr[], int *x);
    
    
    
    
    int main()
    {
        int arr[25];
        int *p_arr[25];
        int *q_array[25];
        int x;
        
        srand(time(NULL)); //random number generator
        
        printf("Please enter the number of elements in array: \n");
        scanf("%d", &x);
        
        generate(arr, &x);
        selectionSort(arr, p_arr, &x);
        insertionSort(arr, q_array, &x);
        blah(arr, p_arr, q_array, &x);
        //printArray(arr, p_arr, x);
        
        return 0;
    }
    
    
    //Function to randomly generate an array of integer numbers
    void generate(int arr[], int *x)
    {
        int c;
        
        for(c = 0; c < *x; c++)
        {
            arr[c] = rand()%100;
        }
    
    
    }
    
    
    void blah(int arr[], int *p_arr[], int *q_array[], int *x)
    {
        int i;
        
        for(i = 0; i < *x; i++)
        {
            p_arr[i] = &arr[i];
        }
        for(i = 0; i < *x; i++)
        {
            q_array[i] = &arr[i];
        }
    }
    
    
    
    
    //Function to sort one of the arrays of pointers to point to the integer numbers in ascending order
    void selectionSort(int arr[], int *p_arr[], int *x)
    {
        int c, d, position, swap;
        
        
        //selection sort for double array[]
        for (c = 0; c < *x; c++)
        {
            //*p_arr[c] = (rand()%100); // storing a random number between 0 and 100
            printf("%d\n", *p_arr[c]);
        }
        
        for (c = 0; c < (*x - 1); c++)
        {
            position = c;
            
            for (d = c + 1; d < *x; d++)
            {
                if (*p_arr[position] > *p_arr[d])
                    position = d;
            }
            if (position != c)
            {
                swap = *p_arr[c];
                *p_arr[c] = *p_arr[position];
                *p_arr[position] = swap;
            }
        }
    
    
    }
    
    
    
    
    
    
    //Function to sort one of the arrays of pointers to point to the integer numbers in descending order
    void insertionSort(int arr[], int *q_array[], int *x)
    {
        int e,f,temp;
        
        //prints all the elements that have been generated into array 2
        printf("\n");
        printf("Elements in array 2: \n");
        printf("\n");
        
        for(f = 0; f < *x; f++)
        {
            for(e = f + 1; e < *x ; e++)
            {
                if(*q_array[f] < *q_array[e])
                {
                    temp = *q_array[f];
                    *q_array[f] = *q_array[e];
                    *q_array[e] = temp;
                }
                else
                    break;
            }
        }
    
    
    
    
    }
    
    
    
    
    //Function to print the array
    void printArray(int c,int arr[], int *p_arr[])
    {
        
    }

  5. #5
    Registered User
    Join Date
    Oct 2016
    Posts
    21
    Updated Code: Still have the Segmentation fault.

    Code:
    #include <stdio.h>
    #include<time.h> // for time();
    #include<stdlib.h> //for rand();
    
    
    void selectionSort(int arr[], int *p_arr[], int *);
    void insertionSort(int arr[], int *q_array[], int *x);
    void printArray(int arr[], int *p_arr[], int *q_array[], int *x);
    void blah(int arr[], int *p_arr[], int *q_array[], int *x);
    void generate(int arr[], int *x);
    
    
    
    
    int main()
    {
        int arr[25];
        int *p_arr[25];
        int *q_array[25];
        int x;
        
        srand(time(NULL)); //random number generator
        
        printf("Please enter the number of elements in array: \n");
        scanf("%d", &x);
        
        generate(arr, &x);
        selectionSort(arr, p_arr, &x);
        insertionSort(arr, q_array, &x);
        blah(arr, p_arr, q_array, &x);
        printArray(arr, p_arr, q_array, &x);
        
        return 0;
    }
    
    
    //Function to randomly generate an array of integer numbers
    void generate(int arr[], int *x)
    {
        int c;
        
        for(c = 0; c < *x; c++)
        {
            arr[c] = rand()%100;
        }
    
    
    }
    
    
    void blah(int arr[], int *p_arr[], int *q_array[], int *x)
    {
        int i;
        
        for(i = 0; i < *x; i++)
        {
            p_arr[i] = &arr[i];
        }
        for(i = 0; i < *x; i++)
        {
            q_array[i] = &arr[i];
        }
    }
    
    
    
    
    //Function to sort one of the arrays of pointers to point to the integer numbers in ascending order
    void selectionSort(int arr[], int *p_arr[], int *x)
    {
        int c, d, position, swap;
        
        
        //selection sort for double array[]
        for (c = 0; c < *x; c++)
        {
            //*p_arr[c] = (rand()%100); // storing a random number between 0 and 100
            printf("%d\n", *p_arr[c]);
        }
        
        for (c = 0; c < (*x - 1); c++)
        {
            position = c;
            
            for (d = c + 1; d < *x; d++)
            {
                if (*p_arr[position] > *p_arr[d])
                    position = d;
            }
            if (position != c)
            {
                swap = *p_arr[c];
                *p_arr[c] = *p_arr[position];
                *p_arr[position] = swap;
            }
        }
    
    
    }
    
    
    
    
    
    
    //Function to sort one of the arrays of pointers to point to the integer numbers in descending order
    void insertionSort(int arr[], int *q_array[], int *x)
    {
        int e,f,temp;
        
        //prints all the elements that have been generated into array 2
        printf("\n");
        printf("Elements in array 2: \n");
        printf("\n");
        
        for(f = 0; f < *x; f++)
        {
            for(e = f + 1; e < *x ; e++)
            {
                if(*q_array[f] < *q_array[e])
                {
                    temp = *q_array[f];
                    *q_array[f] = *q_array[e];
                    *q_array[e] = temp;
                }
                else
                    break;
            }
        }
    
    
    
    
    }
    
    
    
    
    //Function to print the array
    void printArray(int arr[], int *p_arr[], int *q_array[], int *x)
    {
        int c,e;
        
        printf("Ascending Order: \n");
        for(c = 0; c < *x; c++)
        {
            selectionSort(arr, p_arr, x);
            printf("%d\n", *p_arr[c]);
        }
        
        printf("Original Array: \n");
        for(c = 0; c < *x; c++)
        {
            generate(arr, x);
            printf("%d\n", arr[c]);
        }
        
        printf("Descending Order: \n");
        for(c = 0; c < *x; c++)
        {
            insertionSort(arr, q_array, x);
            printf("%d\n", *q_array[e]);
        }
    }

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Turn up the warning level of your compiler. It should have told you that in printArray you're using 'e' uninitialized in the last printf.

    However, that's not where your current segfault is happening. You should run the code in a debugger, which will show you the line that is segfaulting and allow you to peruse the variable values and stack frames.

    Using gcc and gdb:

    $ # -g flag adds debugging info; -Wall turns up the warning level
    $ gcc -g -Wall -o myprog myprog.c
    $ gdb myprog
    (gdb) run

    gdb will show you the line where the segfault occurred.
    Use the 'p' (print) command to print variable values:

    (gdb) p c
    $1 = 0
    (gdb) p p_arr[0]
    $2 = (int *) 0x1

    Note that 0x1 is a bad pointer value!
    Enter 'q' to quit gdb.
    You'll need to go through a gdb tutorial to learn to use it.

  7. #7
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    After a quick glance at the code.. A few things...

    1. Your function parameter names are horrible. Whats a p_arr and a q_arr? Why should anyone know x is the size of the array without having to read and understand all of your code?
    1.1. DO NOT pass simple parameters like x as a pointer. There is no need for the function to modify x, so it should be simply passed as "int size" or even "const int N", as N typical denotes a natural number size in programming as well as mathematics and there is no need for it to change.
    2. In order to sort an array (whether directly or indirectly), you only need 1 function which takes 2 parameters: an array (of pointers, or integers) and the size of this array. Do NOT pass 2 arrays to the sorting functions. Its unnecessary and unwieldy.
    3. You have 2 sorting functions. Sorting functions are idempotent: meaning, calling sort1(array); sort2(array) is equivalent to calling sort1(array) and the call to sort2(array) is useless.. either do an selection sort, or an insertion sort, but no need to do both, one right after another.
    4. AND, for God's sake, don't have a function called BLAH. LOL!
    Last edited by MacNilly; 11-28-2016 at 10:37 AM.

  8. #8
    Registered User
    Join Date
    Oct 2016
    Posts
    21
    According to my assignment I have to have two sorting functions, one using selection sort and the second using insertion sort. I kind of understand the rest of what you are saying. I know my function parameters are complete bs, at this point im so frustrated that names didnt matter to me or anything. I simply cant get the integers to print and figuring out this seg fault is killing me right now.

  9. #9
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Well, you gotta start from a good basis, in order to figure out what subset of your code has an error.

  10. #10
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    I just compiled and ran it, commenting out the calls to the sort functions... just generate and print. I think you'll find that the variable x is uninitialized.

    EDIT: my bad, its read it.. but still seg fault.. hmm give me a minute.

  11. #11
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Well, one problem is that you do read in X but you're passing its address to the other functions, not its value.

  12. #12
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466

    Try this

    Code:
    #include <stdio.h>
    #include<time.h> // for time();
    #include<stdlib.h> //for rand();
    
    void generate(int arr[], const int x);
    void print(int A[], const int N);
    
    int main()
    {
        int arr[25];
        int *p_arr[25];
        int *q_array[25];
        int x;
        
        srand(time(NULL)); //random number generator
        
        printf("Please enter the number of elements in array: \n");
        scanf("%d", &x);
        
        generate(arr, x);
        print(arr, x);
        
        return 0;
    }
    
    
    //Function to randomly generate an array of integer numbers
    void generate(int arr[], const int x)
    {
        int c;
        
        for(c = 0; c < x; c++)
        {
            arr[c] = rand()%100;
        }
    }
    
    void print(int A[], const int N)
    {
      for (int i = 0; i < N; ++i)
        fprintf(stdout, "%d\n", A[i]);
    }
    At least it will get you starting with something that doesn't crash. And the signatures of your other functions can be almost as simple.
    Last edited by MacNilly; 11-28-2016 at 11:07 AM.

  13. #13
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Hey Zante, did you read my post above (number 6) ?
    Learn to fish, buddy.

  14. #14
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Also note this program will (should!) seg-fault if you enter more than 25 at the prompt for the array size..

  15. #15
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    I think your seg fault is due to passing the address of the array size
    Code:
    &x
    instead of the value of
    Code:
    X
    .

    Most likely, the address of X is some huge integer, whereas your arrays are only defined to be of size 25... certainly causing an array out of bounds exception, probably indexing into the address space outside of your program's process's memory space!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Do my homework
    By heiroglikano in forum C Programming
    Replies: 3
    Last Post: 05-31-2009, 06:26 AM
  2. Homework, please help!
    By FandaR in forum C Programming
    Replies: 4
    Last Post: 04-30-2009, 08:59 AM
  3. C homework
    By wilson5182004 in forum C Programming
    Replies: 6
    Last Post: 03-01-2009, 02:21 PM
  4. help with homework
    By abhiii in forum C Programming
    Replies: 2
    Last Post: 02-13-2009, 01:48 PM
  5. Homework =)
    By Raeliean in forum C++ Programming
    Replies: 9
    Last Post: 07-16-2005, 10:27 PM

Tags for this Thread