Random quicksorted array.

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

41 Replies - 2005 Views - Last Post: 19 April 2010 - 05:58 PM Rate Topic: -----

#1 willist  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 38
  • Joined: 11-April 10

Random quicksorted array.

Posted 18 April 2010 - 08:03 PM

Good Evening. I am working on a homework problem and I have hit a little snag. The problem has three parts. First, fill an array with 20 random values. Second, quicksort the array using a quicksort algorithm. Third, call look_up and print the result. You may prompt the user to enter a value to look up.
Okay so I have sucesfully completed the first and second part but am clueless as to how to proceed for the third step. The code is posted below. Any help would be appreciated. Regards, Willis.
#include <stdio.h>
#define MAXIMUMARRAYSIZE 20

void quicksort(int arr[], int low, int high);

int main(void) {
int array[MAXIMUMARRAYSIZE] = {0};
int i = 0;
for(i = 0; i < MAXIMUMARRAYSIZE; i++)
array[i] = rand() % 100; 
printf("Array filled with random values: ");
 for(i = 0; i < MAXIMUMARRAYSIZE; i++) {
  printf(" %d ", array[i]); 
 }
 printf("\n--------------------");

quicksort(array, 0, (MAXIMUMARRAYSIZE - 1));
printf("Array sorted using quicksort algorithm: ");
 for(i = 0; i < MAXIMUMARRAYSIZE; i++) {
  printf(" %d ", array[i]); 
 }
 printf("\n--------------------");
 return 0;
}

void quicksort(int arr[], int low, int high) {
 int i = low;
 int j = high;
 int y = 0;
 int z = arr[(low + high) / 2];
 do {
 while(arr[i] < z) i++;
 while(arr[j] > z) j--;
 if(i <= j) {
    y = arr[i];
   arr[i] = arr[j]; 
   arr[j] = y;
   i++; 
   j--;
  }
 } while(i <= j);


 if(low < j) 
  quicksort(arr, low, j);

 if(i < high) 
  quicksort(arr, i, high); 
}



Is This A Good Question/Topic? 0
  • +

Replies To: Random quicksorted array.

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: Random quicksorted array.

Posted 18 April 2010 - 08:30 PM

what do you mean clueless? you have already done the hard part, just copy and paste the method into a void function.

look
void look_up(int *arr)
{
        int i;
	printf("Array sorted using quicksort algorithm: ");
	for(i = 0; i < MAXIMUMARRAYSIZE; i++)
		printf(" %d ", arr[i]); 
	
}

int main()
{
	//your sorting stuff
        //then output 
	look_up(array);


This post has been edited by ImaSexy: 18 April 2010 - 08:36 PM

Was This Post Helpful? 0
  • +
  • -

#3 willist  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 38
  • Joined: 11-April 10

Re: Random quicksorted array.

Posted 18 April 2010 - 08:33 PM

View PostImaSexy, on 18 April 2010 - 07:30 PM, said:

what do you mean clueless? you have already done the hard part, just copy and paste the method into a void function.

look
void look_up(int *arr)
{
	printf("Array sorted using quicksort algorithm: ");
	for(i = 0; i < MAXIMUMARRAYSIZE; i++)
		printf(" %d ", arr[i]); 
	
}

int main()
{
	//your sorting stuff
        //then output 
	look_up(array);



The only part that I don't get is how do i get it to search for a user-input value?
Was This Post Helpful? 0
  • +
  • -

#4 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Random quicksorted array.

Posted 18 April 2010 - 08:38 PM

If in real life, I gave you a page containing a list of numbers. Then I told you to find if the number 2 was in the list. What would you do?
Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: Random quicksorted array.

Posted 18 April 2010 - 08:39 PM

like for a specific index?
void look_up(int *arr, int index)
{
		printf(" %d ", arr[index]); 
}




but off topic, your code is missing headers. Also the rand() functions depends on something called a "seed" (number for random algorithm). A computer cannot just spit out a random number, it needs a seed that is always changing so the random numbers are different everytime your run the program. User srand(time(0)); to set the "seed" to the computer clock so it is always changing
#include <stdio.h>
//neeed these
#include <stdlib.h>
#include "time.h"

#define MAXIMUMARRAYSIZE 20

void quicksort(int arr[], int low, int high);
void look_up(int *arr);

int main(void) 
{
	srand(time(0)); //set the seed for the rand() algorithm to the clock so it really is RANDOM
	int array[MAXIMUMARRAYSIZE] = {0};
	int i;
	for(i = 0; i < MAXIMUMARRAYSIZE; i++)
		array[i] = rand() % 100; 
	printf("Array filled with random values: ");
	for(i = 0; i < MAXIMUMARRAYSIZE; i++) {
		printf(" %d ", array[i]); 
	}
	printf("\n--------------------");

	quicksort(array, 0, (MAXIMUMARRAYSIZE - 1));
	look_up(array);
	
	printf("\n--------------------");
	getchar();
	return 0;
}

void look_up(int *arr, int index)
{
		printf(" %d ", arr[index]); 
}
void quicksort(int arr[], int low, int high)
{
	int i = low;
	int j = high;
	int y = 0;
	int z = arr[(low + high) / 2];
	do {
		while(arr[i] < z) i++;
		while(arr[j] > z) j--;
		if(i <= j) {
			y = arr[i];
			arr[i] = arr[j]; 
			arr[j] = y;
			i++; 
			j--;
		}
	} while(i <= j);


	if(low < j) 
		quicksort(arr, low, j);

	if(i < high) 
		quicksort(arr, i, high); 
}


Was This Post Helpful? 0
  • +
  • -

#6 willist  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 38
  • Joined: 11-April 10

Re: Random quicksorted array.

Posted 18 April 2010 - 08:46 PM

View PostOler1s, on 18 April 2010 - 07:38 PM, said:

If in real life, I gave you a page containing a list of numbers. Then I told you to find if the number 2 was in the list. What would you do?


I would scan the page and take note every time I found a 2?
Was This Post Helpful? 2
  • +
  • -

#7 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Random quicksorted array.

Posted 18 April 2010 - 08:59 PM

Quote

I would scan the page
Define scan the page. Cast voodoo magic that makes every number that is 2 glow? Or what?
Was This Post Helpful? 0
  • +
  • -

#8 willist  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 38
  • Joined: 11-April 10

Re: Random quicksorted array.

Posted 18 April 2010 - 09:03 PM

I think maybe I posed the question wrong. This is the third step as written on the instruction...

5. (here, you may want to ask the user to enter the value for v to
look up in a),
6. call look_up(), and
7. print the result of lookup.


View PostOler1s, on 18 April 2010 - 07:59 PM, said:

Quote

I would scan the page
Define scan the page. Cast voodoo magic that makes every number that is 2 glow? Or what?


I wish that would make my life alot easier. Okay I would start as the beggining and scan the page line by line. Every time I saw a two I would note it's location in the array and then reference that location later if I wanted to find the number again?

This post has been edited by willist: 18 April 2010 - 09:03 PM

Was This Post Helpful? 0
  • +
  • -

#9 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Random quicksorted array.

Posted 18 April 2010 - 09:04 PM

When I look at those steps, I think of two perfectly valid ways to interpret them.

1. You are looking up the data at a certain position (i.e. you know position, want to know data). So if I have a list of numbers, I'm asking for things like, what's the 5th number, what's the 12th number? etc.

2. You want to find the position of certain data (i.e. you know data, want to know position). So if I have a list of numbers, I'm asking for things like, where is the number 2? where is the number 56? etc.

Question 2 is more complex of course. What's more complex is that a) the data may not exist, and you need to handle this properly and B) there may be more than one instance of that data.

In the case of b, you need to find out how it should be handled. Sane defaults are: list of positions, first position, last position.

EDIT: "Every time I saw a two I would note it's location in the array and then reference that location later if I wanted to find the number again? "

Yes, precisely. And in trying to break this problem down, you have to determine what "note it's location" and "reference that location" mean.

Everything in the end collapses down to understanding data structures and algorithms. When you have a complex problem, you cannot solve it until you understand how data is being stored, and how it being manipulated.

This post has been edited by Oler1s: 18 April 2010 - 09:06 PM

Was This Post Helpful? 0
  • +
  • -

#10 willist  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 38
  • Joined: 11-April 10

Re: Random quicksorted array.

Posted 18 April 2010 - 09:07 PM

View PostOler1s, on 18 April 2010 - 08:04 PM, said:

When I look at those steps, I think of two perfectly valid ways to interpret them.

1. You are looking up the data at a certain position (i.e. you know position, want to know data). So if I have a list of numbers, I'm asking for things like, what's the 5th number, what's the 12th number? etc.

2. You want to find the position of certain data (i.e. you know data, want to know position). So if I have a list of numbers, I'm asking for things like, where is the number 2? where is the number 56? etc.

Question 2 is more complex of course. What's more complex is that a) the data may not exist, and you need to handle this properly and B) there may be more than one instance of that data.

In the case of b, you need to find out how it should be handled. Sane defaults are: list of positions, first position, last position.


Okay this is what I was thinking. I want to search the array for a value and then print the location of that value in the array if that value exists in the array. Otherwise, if the value is not found, maybe use a printf statement to inform the user. An if-else statement might work for this.
Was This Post Helpful? 0
  • +
  • -

#11 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Random quicksorted array.

Posted 18 April 2010 - 09:16 PM

Quote

Otherwise, if the value is not found, maybe use a printf statement to inform the user.
Really? You sure about this?

Let me give you an absurd example, but one that illustrates my point. Let's assume the following code:

// ... necessary junk
    std::cout << "Hello, World\n";
    //... remaining part
    


Now imagine that the << operator, in addition to spitting out Hello, World on the screen, also flushes your bathroom toilet. Err. what? But it does this.

You might be really, really unhappy. You see, the job of << should be output. But it's doing something that isn't it's job. Furthermore, you can't do anything about it! Output and flushing the toilet are (very) stupidly coupled together.

Coupling is an important concept is software development. It's a really bad thing to do. Going to back to your original question. You have a function lookUp that needs to find the position of something. If it fails, your function prints to screen the failure. It is coupling printing to screen to failure. What if you needed to print to file, later on? Print to database. Print to website. Make red bulb glow. Make a beep sound. Who knows?

You want to communicate that the function failed. How can you do this, without printing anything to screen? (Hint: everyone is going to look at what you return, right?)
Was This Post Helpful? 0
  • +
  • -

#12 willist  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 38
  • Joined: 11-April 10

Re: Random quicksorted array.

Posted 18 April 2010 - 09:19 PM

View PostOler1s, on 18 April 2010 - 08:16 PM, said:

Quote

Otherwise, if the value is not found, maybe use a printf statement to inform the user.
Really? You sure about this?

Let me give you an absurd example, but one that illustrates my point. Let's assume the following code:

// ... necessary junk
    std::cout << "Hello, World\n";
    //... remaining part
    


Now imagine that the << operator, in addition to spitting out Hello, World on the screen, also flushes your bathroom toilet. Err. what? But it does this.

You might be really, really unhappy. You see, the job of << should be output. But it's doing something that isn't it's job. Furthermore, you can't do anything about it! Output and flushing the toilet are (very) stupidly coupled together.

Coupling is an important concept is software development. It's a really bad thing to do. Going to back to your original question. You have a function lookUp that needs to find the position of something. If it fails, your function prints to screen the failure. It is coupling printing to screen to failure. What if you needed to print to file, later on? Print to database. Print to website. Make red bulb glow. Make a beep sound. Who knows?

You want to communicate that the function failed. How can you do this, without printing anything to screen? (Hint: everyone is going to look at what you return, right?)


Return a -1?
Was This Post Helpful? 0
  • +
  • -

#13 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Random quicksorted array.

Posted 18 April 2010 - 09:34 PM

Quote

Return a -1?
That sounds like an excellent idea, doesn't it?
Was This Post Helpful? 0
  • +
  • -

#14 willist  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 38
  • Joined: 11-April 10

Re: Random quicksorted array.

Posted 18 April 2010 - 09:48 PM

View PostOler1s, on 18 April 2010 - 08:34 PM, said:

Quote

Return a -1?
That sounds like an excellent idea, doesn't it?


Your logic is impeccable. So how would I go about searching for this value in the array?
Was This Post Helpful? 0
  • +
  • -

#15 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: Random quicksorted array.

Posted 18 April 2010 - 09:54 PM

well your gonna need a loop right?
void look_up(int *arr, int value)
{
	for(i = 0; i<MAXIMUMARRAYSIZE; i++)
	{
		//if the value inputted equals any value in the array
		// return that position (i)
		//else return a error value
	}


}


Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3