Thread: Need some help C

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

    Need some help C

    My assignment is as followed: Write a program that generates two arrays with double numbers (randomly). Then, write a function tosort each array utilizing RECURSIVE selection sort. Then write a function that will merge the contents of the two sorted(ascending order) arrays, storing the result in a third array (still in ascending order).

    As for right now my code looks like this, my problem with it is if i were to write it all under main it would populate the array with random numbers like its suppose to but i cant figure out how to get it to work when i make it into its own function and call it in main. Any help is appreciated.

    Code:
    #include <stdio.h>
    #include<time.h>                  // for time()
    #include<stdlib.h>                //for rand()
    
    
    void sortArray(int *array);
    
    
    int main()
    {
        int x;
        int array[100];
        
        //srand(time(NULL));                       //random number generator
        
        printf("Enter number of elements\n");
        scanf("%d", &x);
        
        //printf("Enter %d integers\n", n);
        
        sortArray(array);
        
        return 0;
    }
    
    
    void sortArray(int *array)
    {
        int c, n, d, position, swap;
        
        for ( c = 0 ; c < n ; c++ )
            array[c] = rand()%100;                 // storing a random number between 0 and 100
        
        for ( c = 0 ; c < ( n - 1 ) ; c++ )
        {
            position = c;
            
            for ( d = c + 1 ; d < n ; d++ )
            {
                if (array[position] > array[d])
                    position = d;
            }
            if ( position != c )
            {
                swap = array[c];
                array[c] = array[position];
                array[position] = swap;
            }
        }
        
        printf("Sorted list in ascending order:\n");
        
        for (c = 0; c < n; c++)
            printf("%d\n", array[c]);
    
    
    }

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Why have you commented out the random seed generator functions? That could be part of the problem. If your splitting the program up into functions make sure you pass all parameters and arguments. Your passing the the reference of the array to said function, what about the index size?

    Code:
    void myArray(int arr[], SIZE) 
    {
    }
    Of course, I am just guessing here - could you explain which pieces of the code need to be implemented into their own function?
    Double Helix STL

  3. #3
    Registered User
    Join Date
    Oct 2016
    Posts
    21
    I actually figured out my problem, took a while but finally got my code to work. Now I just have to figure out how to implement the third array so no repeats are stored in the array. Not really sure how to do this part yet.

    Code:
    #include <stdio.h>
    #include<time.h>                  // for time()
    #include<stdlib.h>                //for rand()
    
    
    void sortArray(int array[], int);
    void sortArrayb(int arrayb[], int);
    void merge(int array[], int x, int arrayb[], int y, int sorted[]);
    
    
    int main()
    {
        int x,y,c;
        int array[100];
        int arrayb[50];
        int sorted[150];
        
        srand(time(NULL));                       //random number generator
        
        printf("Enter number of elements in the first array:\n");
        scanf("%d", &x);
        
        printf("Enter number of elements in the second array:\n");
        scanf("%d", &y);
        
        sortArray(array, x);
        sortArrayb(arrayb, y);
        merge(array, x, arrayb, y, sorted);
        
        printf("Sorted array:\n");
        for (c = 0; c < x + y; c++)
        {
            printf("%d\n", sorted[c]);
        }
        
        return 0;
    }
    
    
    void sortArray(int array[], int x)
    {
        int c, d, position, swap;
        
        
        for ( c = 0 ; c < x ; c++ )
            array[c] = rand()%100;                 // storing a random number between 0 and 100
        
        for ( c = 0 ; c < ( x - 1 ) ; c++ )
        {
            position = c;
            
            for ( d = c + 1 ; d < x ; d++ )
            {
                if (array[position] > array[d])
                    position = d;
            }
            if ( position != c )
            {
                swap = array[c];
                array[c] = array[position];
                array[position] = swap;
            }
        }
        
        //printf("Sorted list in ascending order:\n");
        
        //for (c = 0; c < x; c++)
            //printf("%d\n", array[c]);
        
    }
    
    
    void sortArrayb(int arrayb[], int y)
    {
        int e, f, pos, change;
        
        for (e = 0; e < y; e++)
            arrayb[e] = rand()%50;                 // storing a random number between 0 and 100
        
        for (e = 0; e < (y - 1); e++)
        {
            pos = e;
            
            for (f = e + 1; f < y; f++)
            {
                if (arrayb[pos] > arrayb[f])
                    pos = f;
            }
            if (pos != e)
            {
                change = arrayb[e];
                arrayb[e] = arrayb[pos];
                arrayb[pos] = change;
            }
        }
    
    
    }
    
    
    void merge(int array[], int x, int arrayb[], int y, int sorted[])
    {
            int i, j, k;
            
            j = k = 0;
            
            for (i = 0; i < x + y;)
            {
                if (j < x && k < y)
                {
                    if (array[j] < arrayb[k])
                    {
                        sorted[i] = array[j];
                        j++;
                    }
                    else
                    {
                        sorted[i] = arrayb[k];
                        k++;
                    }
                    i++;
                }
                else if (j == x)
                {
                    for (; i < x + y;)
                    {
                        sorted[i] = arrayb[k];
                        k++;
                        i++;
                    }
                }
                else
                {
                    for (; i < x + y;)
                    {
                        sorted[i] = array[j];
                        j++;
                        i++;
                    }
                }
            }
        }

Popular pages Recent additions subscribe to a feed

Tags for this Thread