# some help with this code

• (2 Pages)
• 1
• 2

## 18 Replies - 1190 Views - Last Post: 31 October 2008 - 11:01 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=69544&amp;s=02a5e99cad296a279b0ca5836a0cf55e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 hacerg

Reputation: 0
• Posts: 16
• Joined: 21-October 08

# some help with this code

Posted 29 October 2008 - 07:22 AM

Hi,

All I want to do is to sort the array [P] from the highest to the lowest with the index values of the variables. When looking from this perspective, it seems easy. But there is another constraint that the first two will be sorted in themselves with the indexes and the last two will be sorted in themselves with the indexes but this time the highest of these two will get the index of 1 and the second will take 2. The written code below does not do this and I can not find where the error is. I am stuck with this code. Any help will be appreciated.
Thanks,

#include <stdio.h>
#include <stdlib.h>

#define M 2 //
#define P 4 //

int NItems[M]={2,2};

int cmp(const void *v1, const void *v2)
{
const int i1 = **(const int **)v1;
const int i2 = **(const int **)v2;

if(i1>i2)
return -1;

else
return 1;

}

int main(void)
{
int array[P] = {10,20,5,15};
int *parray[P],i;

for(int j=0;j<M;j++)
{if(j==0)
{for(i = (j*NItems[j]); i <=(j*NItems[j]+NItems[j]-1);i++)
{parray[i] = &array[i];
}
qsort(parray,NItems[j], sizeof *parray,cmp);
}
else
for(i = (j*NItems[j-1]); i <=(j*NItems[j-1]+NItems[j]-1);i++)
{parray[i] = &array[i];
}
qsort(parray,NItems[j], sizeof *parray,cmp);
}

puts("Sorting the values:");
for(i=0;i<P;i++)
printf("%d\n ",array[i]);

for(int j=0;j<M;j++)
{if(j==0)
{for(i = (j*NItems[j]); i <=(j*NItems[j]+NItems[j]-1);i++)
{printf("value: %-6d position: %d\n",*parray[i],(parray[i]-array)+1);}
system("pause");
}

else
{for(i =(j*NItems[j-1]);i<=(j*NItems[j-1]+NItems[j]-1);i++)
printf("value: %-6d position: %d\n",*parray[i],(parray[i]-array-NItems[j])+1);}

system("pause");
}

return 0;
}

Is This A Good Question/Topic? 0

## Replies To: some help with this code

### #2 Gloin

• Expert Schmexpert...

Reputation: 235
• Posts: 4,489
• Joined: 04-August 08

## Re: some help with this code

Posted 29 October 2008 - 07:29 AM

I'm not sure I understand what you're asking for can you give an example? Sounds like you're supposed to implement the mergesort algorithm.

### #3 jjsaw5

• D.I.C Lover

Reputation: 92
• Posts: 3,063
• Joined: 04-January 08

## Re: some help with this code

Posted 29 October 2008 - 07:29 AM

Please make sure you are using code tags.

### #4 hacerg

Reputation: 0
• Posts: 16
• Joined: 21-October 08

## Re: some help with this code

Posted 29 October 2008 - 09:58 AM

the output of the code shouil be like this:
sorted values indexes
20 2
10 1
15 2
5 1

i hope this is clear.

Gloin, on 29 Oct, 2008 - 07:29 AM, said:

I'm not sure I understand what you're asking for can you give an example? Sounds like you're supposed to implement the mergesort algorithm.

### #5 Gloin

• Expert Schmexpert...

Reputation: 235
• Posts: 4,489
• Joined: 04-August 08

## Re: some help with this code

Posted 29 October 2008 - 10:13 AM

Yes, thanks, much clearer now. And it is indeed mergesort you're doing (not quicksort as some parts of your code implies).

I would have recommended that you made the function recursively since it would be very easy to get the output you want then. Also it would adapt very well for input of size larger than 4.

### #6 hacerg

Reputation: 0
• Posts: 16
• Joined: 21-October 08

## Re: some help with this code

Posted 29 October 2008 - 10:16 AM

Thanks a lot. I will try that.

quote name='Gloin' date='29 Oct, 2008 - 10:13 AM' post='449365']
Yes, thanks, much clearer now. And it is indeed mergesort you're doing (not quicksort as some parts of your code implies).

I would have recommended that you made the function recursively since it would be very easy to get the output you want then. Also it would adapt very well for input of size larger than 4.
[/quote]

### #7 Gloin

• Expert Schmexpert...

Reputation: 235
• Posts: 4,489
• Joined: 04-August 08

## Re: some help with this code

Posted 29 October 2008 - 10:43 AM

Maybe that wasn't part of your assignment...
What output are you getting cuz there might just be some parentheses missing..

### #8 hacerg

Reputation: 0
• Posts: 16
• Joined: 21-October 08

## Re: some help with this code

Posted 29 October 2008 - 11:13 AM

I tried the mergesort as follows. but I did not quiet understand it.
The problem is I want to sort the number of items according to the M value. For example. If the value of NItems[1] is 2, then I should sort two values of array and put them in order. The other 3 elements should be sorted in themselves.

Thanks for all help.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define M 2

int NItem[M]={2,3};

void printv(char* in, int *v, int n) {
printf("%s", in);
int i = 0;
for (; i < n; ++i)
printf("%d ", v[i]);
printf("\n");
}

void merge(int *v, int p, int q, int r) {
int i = p;
int j = q + 1;

int *tmp = (int*)malloc((r - p + 1) * sizeof(int));
int k = 0;

while ((i <= q) && (j <= r)) {
if (v[i] > v[j])
tmp[k++] = v[i++];
else
tmp[k++] = v[j++];
}

while (i <= q)
tmp[k++] = v[i++];

while (j <= r)
tmp[k++] = v[j++];

memcpy(v + p, tmp, (r - p + 1) * sizeof(int));
free(tmp);
}

void mergeS(int *v, int p, int r) {
if (p < r) {
int q = (p + r) / 2;
mergeS(v, p, q);
mergeS(v, q + 1, r);
merge(v, p, q, r);
}
}

int main(int argc, char *argv[]) {
int n = 5;
int v[] = {20,10,15,5,25};

printv("V: ", v, 5);
system("pause");

for(int j=0;j<M;j++)
{mergeS(v, 0, NItem[j]);
mergeS(v, NItem[j],NItem[j+1]);
}
printv("V: ", v, 5);
system("pause");
return 0;
}

Gloin, on 29 Oct, 2008 - 10:43 AM, said:

Maybe that wasn't part of your assignment...
What output are you getting cuz there might just be some parentheses missing..

### #9 Gloin

• Expert Schmexpert...

Reputation: 235
• Posts: 4,489
• Joined: 04-August 08

## Re: some help with this code

Posted 29 October 2008 - 11:33 AM

It's not quite what I meant but it looks like you're close to a working solution.

Just one final question, is the final output supposed to be a complete sorted list? or is it only supposed to show the sorted subsets?

### #10 hacerg

Reputation: 0
• Posts: 16
• Joined: 21-October 08

## Re: some help with this code

Posted 29 October 2008 - 11:40 AM

the final output will supposed to show the sorted subsets in a whole array. as I show in the example. which one is close to a woaking solution the earlier one or this one?because both of them is close to working but some small errors that i can not find.
Thanks

Gloin, on 29 Oct, 2008 - 11:33 AM, said:

It's not quite what I meant but it looks like you're close to a working solution.

Just one final question, is the final output supposed to be a complete sorted list? or is it only supposed to show the sorted subsets?

### #11 Gloin

• Expert Schmexpert...

Reputation: 235
• Posts: 4,489
• Joined: 04-August 08

## Re: some help with this code

Posted 29 October 2008 - 11:43 AM

Keep working on the first solution. I'll look over it again but I don't have a compiler so I can't test code.

### #12 Gloin

• Expert Schmexpert...

Reputation: 235
• Posts: 4,489
• Joined: 04-August 08

## Re: some help with this code

Posted 29 October 2008 - 11:50 AM

I'm guessing the first program doesn't compile because you have uneven number of { and }

### #13 Gloin

• Expert Schmexpert...

Reputation: 235
• Posts: 4,489
• Joined: 04-August 08

## Re: some help with this code

Posted 29 October 2008 - 12:02 PM

The thing is, you have some interresting ideas in your second code as well..

It seems to me you're pretty good with C so it's probably easier to just foolow these outlines..

You have an unsorted array of numbers where you wish to sort and output subsets of this array along with indexes.
The subsets are given in another array Nitem.

Dynamically allocate a new array of the size of the first value of Nitem and copy values 0 to Nitem[0]-1 into this new array.
Sort the new Array
Display the values in this new array along with the indexes
free the allocated memory

Do the exact same thing for the second part, copy the values Nitem[0] to Nitem[1] - 1
Since you have a temporary array it will automatically show the indexes from 0 to Nitem[i]-1 (adjust them with +1)

### #14 hacerg

Reputation: 0
• Posts: 16
• Joined: 21-October 08

## Re: some help with this code

Posted 29 October 2008 - 12:33 PM

Thank you for the idea but I can not get it right. If I extend the array size of NItems, what shall I do? In that case, it is not a flexible code.

Gloin, on 29 Oct, 2008 - 12:02 PM, said:

The thing is, you have some interresting ideas in your second code as well..

It seems to me you're pretty good with C so it's probably easier to just foolow these outlines..

You have an unsorted array of numbers where you wish to sort and output subsets of this array along with indexes.
The subsets are given in another array Nitem.

Dynamically allocate a new array of the size of the first value of Nitem and copy values 0 to Nitem[0]-1 into this new array.
Sort the new Array
Display the values in this new array along with the indexes
free the allocated memory

Do the exact same thing for the second part, copy the values Nitem[0] to Nitem[1] - 1
Since you have a temporary array it will automatically show the indexes from 0 to Nitem[i]-1 (adjust them with +1)

### #15 Gloin

• Expert Schmexpert...

Reputation: 235
• Posts: 4,489
• Joined: 04-August 08

## Re: some help with this code

Posted 29 October 2008 - 12:37 PM

You only need a loop for that. You can easily get the size of the Nitem array so you know how many times you need to allocate memory for a temporary array.