3 Replies - 2586 Views - Last Post: 04 December 2007 - 10:01 AM Rate Topic: -----

#1 Irishancest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 02-November 07

HW help: quicksort and stacks and queues

Posted 01 December 2007 - 03:38 PM

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.
Is This A Good Question/Topic? 0
  • +

Replies To: HW help: quicksort and stacks and queues

#2 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: HW help: quicksort and stacks and queues

Posted 01 December 2007 - 03:59 PM

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.
Was This Post Helpful? 0
  • +
  • -

#3 Irishancest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 02-November 07

Re: HW help: quicksort and stacks and queues

Posted 03 December 2007 - 10:54 AM

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
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:
#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: 04 December 2007 - 08:14 AM

Was This Post Helpful? 0
  • +
  • -

#4 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: HW help: quicksort and stacks and queues

Posted 04 December 2007 - 10:01 AM

these two lines

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
void QuickSort (int inputArray[], int myLength);

void QuickSort(int inputArray[], int myLength)


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1