Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,557 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,603 people online right now. Registration is fast and FREE... Join Now!




HW help: quicksort and stacks and queues

 
Reply to this topicStart new topic

HW help: quicksort and stacks and queues

Irishancest
1 Dec, 2007 - 02:38 PM
Post #1

New D.I.C Head
*

Joined: 2 Nov, 2007
Posts: 14


My Contributions
I need help with some my quicksort algorithm and also implementing stacks and queues in C for a homework assignment. Should I just copy and paste all my code here, or should I add my code as an attachment. I made it using microsoft visual studio and it is a .c filetype.
User is offlineProfile CardPM
+Quote Post

Bench
RE: HW Help: Quicksort And Stacks And Queues
1 Dec, 2007 - 02:59 PM
Post #2

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 684



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
Pasting your code using the [CODE] [/CODE] tags is the usual etiquette on this forum (to make the code easier to read). Please try to provide some detail about where you're stuck, or what exactly isn't working the way you expect;
this will make it easier for someone here to help you.
if you're getting any compiler errors which you can't fix, then you can paste those too.
User is offlineProfile CardPM
+Quote Post

Irishancest
RE: HW Help: Quicksort And Stacks And Queues
3 Dec, 2007 - 09:54 AM
Post #3

New D.I.C Head
*

Joined: 2 Nov, 2007
Posts: 14


My Contributions
Here is my updated quicksort code. I am getting 29 errors when I try to compile it and i don't know what to do...I'll also post the list of errors
CODE

c:\docume~1\default\mydocu~1\visual~1\projects\quicks~1.c:4: parse error before `*'
c:\docume~1\default\mydocu~1\visual~1\projects\quicks~1.c:11: parse error before `inputArray'
c:\docume~1\default\mydocu~1\visual~1\projects\quicks~1.c: In function `QuickSort':
c:\docume~1\default\mydocu~1\visual~1\projects\quicks~1.c:13: `myLength' undeclared (first use in this function)
c:\docume~1\default\mydocu~1\visual~1\projects\quicks~1.c:13: (Each undeclared identifier is reported only once
c:\docume~1\default\mydocu~1\visual~1\projects\quicks~1.c:13: for each function it appears in.)
c:\docume~1\default\mydocu~1\visual~1\projects\quicks~1.c:14: `inputArray' undeclared (first use in this function)
c:\docume~1\default\mydocu~1\visual~1\projects\quicks~1.c:21: request for member `length' in something not a structure or union
c:\docume~1\default\mydocu~1\visual~1\projects\quicks~1.c:43: invalid type argument of `unary *'
c:\docume~1\default\mydocu~1\visual~1\projects\quicks~1.c:44: parse error before `int'
c:\docume~1\default\mydocu~1\visual~1\projects\quicks~1.c:50: parse error before `int'
c:\docume~1\default\mydocu~1\visual~1\projects\quicks~1.c:55: parse error before `int'
c:\docume~1\default\mydocu~1\visual~1\projects\quicks~1.c:63: `med' undeclared (first use in this function)
c:\docume~1\default\mydocu~1\visual~1\projects\quicks~1.c:64: `temp' undeclared (first use in this function)
c:\docume~1\default\mydocu~1\visual~1\projects\quicks~1.c:72: parse error before `int'
c:\docume~1\default\mydocu~1\visual~1\projects\quicks~1.c:74: `i' undeclared (first use in this function)
c:\docume~1\default\mydocu~1\visual~1\projects\quicks~1.c:74: `j' undeclared (first use in this function)
c:\docume~1\default\mydocu~1\visual~1\projects\quicks~1.c:79: `medo30' undeclared (first use in this function)
c:\docume~1\default\mydocu~1\visual~1\projects\quicks~1.c:79: parse error before `{'
c:\docume~1\default\mydocu~1\visual~1\projects\quicks~1.c:89: parse error before `else'
c:\docume~1\default\mydocu~1\visual~1\projects\quicks~1.c:106: parse error before `int'
c:\docume~1\default\mydocu~1\visual~1\projects\quicks~1.c:108: `h' undeclared (first use in this function)
c:\docume~1\default\mydocu~1\visual~1\projects\quicks~1.c:110: `smallerArray' undeclared (first use in this function)
c:\docume~1\default\mydocu~1\visual~1\projects\quicks~1.c:119: parse error before `int'
c:\docume~1\default\mydocu~1\visual~1\projects\quicks~1.c:121: parse error before `int'
c:\docume~1\default\mydocu~1\visual~1\projects\quicks~1.c:125: `k' undeclared (first use in this function)
c:\docume~1\default\mydocu~1\visual~1\projects\quicks~1.c: At top level:
c:\docume~1\default\mydocu~1\visual~1\projects\quicks~1.c:128: parse error before `.'
c:\docume~1\default\mydocu~1\visual~1\projects\quicks~1.c:128: warning: data definition has no type or storage class
c:\docume~1\default\mydocu~1\visual~1\projects\quicks~1.c:132: `k' used prior to declaration
c:\docume~1\default\mydocu~1\visual~1\projects\quicks~1.c:132: warning: data definition has no type or storage class
c:\docume~1\default\mydocu~1\visual~1\projects\quicks~1.c:133: parse error before `while'


And here is my programming code:
CODE

#include<stdio.h>

int main ();
void QuickSort (int[] *inputArray, int myLength);

int main ()
{

}

void QuickSort(int[] inputArray, int myLength)
{
    int  length = myLength;
    int  myArray[] = inputArray;
    //                                                                          
    //Makes sure that quicksort won't try to do median of 3 with less than three
    //characters, and sorts the arrays.
    //                                                                          
    if (length <= 2)                                                            
    {
        if (myArray.length = 2)
        {
            if (myArray[0] > myArray[1])
            {
                int temp = myArray [0];
                myArray[0] = myArray[1];
                myArray[1] = temp;
            }
        }
    }

    else
    {
    //                                                                          
    //Determines median of three numbers (with those numbers being the first,
    //last, and center numbers in the array.
    //                                                                          
        int medo3;
        int mid = (length/2);
        if ((myArray[0] < myArray[length] && myArray[0] > myArray[mid]) ||            //determines if  myArray[0]
            (myArray[0] > myArray[length] && myArray[0] < myArray[mid]))            //is median
        {
            medo3 = *myArray[0];
            int med = 0;
        }
        else if ((myArray[length] < myArray[0] && myArray[length] > myArray[mid]) ||//determines if  myArray[length]
            (myArray[length] > myArray[0] && myArray[length] < myArray[mid]))        //is median
        {
            medo3 = myArray[length];
            int med = length;
        }
        else                                                                        //establishes  myArray[mid] as
        {                                                                            //median if the values at 0
            medo3 = myArray[mid];                                                    //and length are not
            int med = length;
        }
    
    //                                                                          
    //Swaps the median number with the number at the end so it doesn't get
    //sorted.
    //                                                                          
        int temp = myArray [med];
        myArray[med]=myArray[length-1];
        myArray[length-1]=temp;
    
    //                                                                          
    //"i" moves from 0 up the array and stops on number that are greater than
    //the median. "j" moves down the array from the number below the median and
    //stops at numbers that are smaller than the median. When both i and j are
    //stopped they swap.
    //                                                                          
        int i = 0;
        int j = 2;
        while (i < j)                                                                //stops i and j from crossing so they don't
        {                                                                            //resort the array
            if (myArray[i] <= medo3)
            {
                if (myArray[j] >= medo30
                {
                    temp = myArray[i];                                                //i and j swap
                    myArray[i] = myArray[j];
                    myArray[j] = temp;
                }
                else                                                                //j moves down if not on correct value
                {
                    j--;    
                }
            }
            else                                                                    //i moves up if not on correct value
            {
                i++;
            }
    
    //                                                                    
    //swaps the median with the value at i so that everything below the
    //median is lower than it and vice versa
    //                                                                    
            temp =  myArray[i];
            myArray[i] =  myArray[length-1];
            myArray[length-1] = temp;
    
    //                                                                    
    //Creates array for the elements smaller than the median to be placed
    //into so they can be passed into the recursive call of quicksort
    //                                                                    
            int smallerArray[i-1];
            int h = 0;
            while (h < i)                    
            {
                smallerArray[h] = myArray[h];
                h++;
            }
            QuickSort( smallerArray, i);
    
    //                                                                    
    //Creates array for the elements smaller than the median to be placed
    //into so they can be passed into the recursive call of quicksort
    //                                                                    
            int largerArray[length-i];
            h = i;
            int k = 0
            while (h < largerArray.length-1 && i < length+1)
            {
                largerArray[k] =  *myArray[i+1];
                k++;
                i++;
            }
            Quicksort( largerArray, largerArray.length);
    //                                                                    
    //Adds the sorted smaller list to the beginning of *myArray
    //                                                                    
            k = 0;
            while (k < smallerArray.length)
            {
                myArray[k] = smallerArray[k];
                k++;
            }
    //                                                                    
    //Adds the sorted larger list to the end of *myArray after the median.
    //                                                                    
            k++;                                                                    //moves k to index after median
            while (k < length)
            {
                myArray[k] = largerArray[k];
                k++;
            }
        }
    }
}


This post has been edited by Irishancest: 4 Dec, 2007 - 07:14 AM
User is offlineProfile CardPM
+Quote Post

Bench
RE: HW Help: Quicksort And Stacks And Queues
4 Dec, 2007 - 09:01 AM
Post #4

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 684



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
these two lines

CODE

void QuickSort (int[] *inputArray, int myLength);

/* ...  and ...  */

void QuickSort(int[] inputArray, int myLength)


Assuming you want an array containing int data, the syntax needs to look like this
CODE

void QuickSort (int inputArray[], int myLength);

void QuickSort(int inputArray[], int myLength)

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 10:05PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month