Finding the next highest value in a int variable

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 01-02-2012
spendotw
Finding the next highest value in a int variable
Hi there
I need a method or mechanism to find/sort the values stored in a struct member of type int. e.g proc[i].priority

How or what can i use to deal with the values stored in the variable, so that i can process them in an ascending order such as all the 0's first and all the 20's last ( 0 - 20 ).

I was thinking something like:
Code:

```if ( proc[i].priority > previousproc[i].priority )         previousproc[i].priority = proc[i].priority;```
But i dont think this actually picks up the next number in the sequence and that it will choose any number up to 20, when i need it to choose all the 2 values stored in .priority first. then the 3's, 4's etc.

• 01-02-2012
Sounds like you don't have NUMBERS, you have digits in a string. So sort them as a string, (using strcmp() and including string.h naturally), and deal with any logic, in a "digit in a string", manner, rather than trying to deal with the digits as a number.
• 01-02-2012
oogabooga
It depends on whether this list is created once and after that nothing is added or removed or whether the list is dynamically changing. If it's dynamically changing, then this is a job for something called a priority queue (which see). If it's fixed, you can just sort it however you wish (e.g. selection sort, if you want simplicity over efficiency) and process it in order.

And of course you can use the standard library qsort as well.
• 01-02-2012
iMalc
Quote:

Sounds like you don't have NUMBERS, you have digits in a string. So sort them as a string, (using strcmp() and including string.h naturally), and deal with any logic, in a "digit in a string", manner, rather than trying to deal with the digits as a number.

I have no idea where you got that from. He said that they were "of type int".

It sounds to me like you want to just sort your array of structs by their priority member. Then you can just walk through the array in order to process them. This can easily be achieved through the use of qsort and a custom comparison function. I suggest you have at using qsort yourself and perhaps post the definition of your struct and your attempt at using qsort.
• 01-03-2012
spendotw
Quote:

Originally Posted by oogabooga
It depends on whether this list is created once and after that nothing is added or removed or whether the list is dynamically changing. If it's dynamically changing, then this is a job for something called a priority queue (which see). If it's fixed, you can just sort it however you wish (e.g. selection sort, if you want simplicity over efficiency) and process it in order.

And of course you can use the standard library qsort as well.

Code:

``` for ( i = 0 ; i <= 3 ; i++ )         {                 for ( j = i + 1 ; j <= 4 ; j++ )                 {          if( proc[i].priority > proc[k].priority ) {                 proc[i].temp = proc[i].priority;                 proc[i].priority = proc[k].priority;                 proc[k].priority = proc[i].temp; printf ( "\n\nArray after sorting:\n") ;         for ( i = 0 ; i <= 4 ; i++ )         printf ( "%d\t", proc[i].priority );         }```
As im reading from a file and the process scheduler i am writting is to be written in a round robin fashion the priority stays the same throughout so i choose to look into selection sort

But the code above doesn't seem to look through my structure member or compare them correctly

my output is as follows:
Array after sorting:
10 0 0 0 0 id: Process198
Qaunta: 188
Priority: 3

Array after sorting:
10 0 0 0 0

Array after sorting:
10 0 0 0 0

Array after sorting:
10 0 0 0 0

And the same throughout the whole of the output.
It seems that it recognizes my first priority but after that it doesn't agree that any other priority value is higher when there are many more larger. And instead printing out the first priority i wanted it to locate the lowest first and then compare that value to the rest and so on....

I did have a look into qsort but that was difficult dealing with struct members :(
• 01-03-2012
oogabooga
You need to post a small but COMPLETE program. Otherwise it's hard to understand what's going on. For instance, where do the constants 3 and 4 come from?

Also, you need to improve the spacing of your code.

Also, you need to look at your code more closely to see if there's any obvious errors. I see at least three. First, you use k inside your loops, but your loops are indexed by i and j. Second, you are not swapping the whole structure, just the priority. Third, you're print loop should be after the sort has completed, not during it.

qsort is easy when you get the hang of it. It would sort your array something like this (PROCTYPE stands in for whatever the type of your proc array elements are):
Code:

```/* comparison function for qsort */ int comp(const void *a, const void *b) {     return *(PROCTYPE*)a - *(PROCTYPE*)b; } /* elsewhere in the program */     qsort(proc, number_of_procs, sizeof(*proc), comp);```
• 01-03-2012
spendotw
Quote:

Originally Posted by oogabooga
You need to post a small but COMPLETE program. Otherwise it's hard to understand what's going on. For instance, where do the constants 3 and 4 come from?

Also, you need to improve the spacing of your code.

Also, you need to look at your code more closely to see if there's any obvious errors. I see at least three. First, you use k inside your loops, but your loops are indexed by i and j. Second, you are not swapping the whole structure, just the priority. Third, you're print loop should be after the sort has completed, not during it.

qsort is easy when you get the hang of it. It would sort your array something like this (PROCTYPE stands in for whatever the type of your proc array elements are):
Code:

```/* comparison function for qsort */ int comp(const void *a, const void *b) {     return *(PROCTYPE*)a - *(PROCTYPE*)b; } /* elsewhere in the program */     qsort(proc, number_of_procs, sizeof(*proc), comp);```

Code:

``` int comp(const void *a, const void *b) {     return *(int*)a - *(int*)b; }```
As you said i changed PROCTYPE to type int as my priority member is of type int.
qsort(proc, 199, sizeof(*proc), comp);
printf("%d ", proc[i].priority);
[/CODE]
Okay so i changed the second argument of qsort to 199 as thats how many processes there are and respectively the same amount of priority elements.

But the first part of the argument will only allow me to settle for putting proc rather than proc[i].priority. Which i thought would be correct to put there as i am comparing the priority member not the whole of structure proc.

If i do put proc[i].priority then i get the following warning:
warning: passing argument 1 of ‘qsort’ makes pointer from integer without a cast [enabled by default]
/usr/include/stdlib.h:761:13: note: expected ‘void *’ but argument is of type ‘int’

Whilst leaving the first argument of qsort as proc the program did compile but i was getting zero's for each priority:

id: Process194
Qaunta: 68
Priority: 17
0
id: Process195
Qaunta: 127
Priority: 7
0
id: Process196
Qaunta: 6
Priority: 17
0
id: Process197
Qaunta: 103
Priority: 18
0
• 01-03-2012
oogabooga
Sorry, I made a mistake. The comp function should be like this:
Code:

```int comp(const void *a, const void *b) {     return ((Process*)a)->priority - ((Process*)b)->priority; }```
This assumes your typedef'ed struct is called Process.
• 01-03-2012
spendotw
Quote:

Originally Posted by oogabooga
Sorry, I made a mistake. The comp function should be like this:
Code:

```int comp(const void *a, const void *b) {     return ((Process*)a)->priority - ((Process*)b)->priority; }```
This assumes your typedef'ed struct is called Process.

I'm still getting this error when implementing the code you provided:

Code:

``` int comp(const void *a, const void *b) {     return ((proc*)a)->priority - ((proc*)b)->priority; }```
example.c: In function ‘comp’:
example.c:29:19: error: expected expression before ‘)’ token
example.c:29:42: error: expected expression before ‘)’ token

Im abit unsure on typedef, but in terms of structs this is what i have declare, so i used proc. Or is structure process the correct one to be used?
Code:

```struct process {     char *id; int priority;     int qaunta; struct process *next; }; struct process proc[MAXPROCS];```
• 01-03-2012
iMalc
proc is an array of process objects. In the comparison function you need to tell it the type. You want this:
Code:

```int comp(const void *a, const void *b) {     return ((process*)a)->priority - ((process*)b)->priority; }```
Then elsewhere:
Code:

` qsort(proc, number_of_procs, sizeof(*proc), comp);`
That will see it working correctly for you.
• 01-03-2012
spendotw
Quote:

Originally Posted by iMalc
proc is an array of process objects. In the comparison function you need to tell it the type. You want this:
Code:

```int comp(const void *a, const void *b) {     return ((process*)a)->priority - ((process*)b)->priority; }```
Then elsewhere:
Code:

` qsort(proc, number_of_procs, sizeof(*proc), comp);`
That will see it working correctly for you.

yeah i tried that before but i kept getting these errors :(

example.c: In function ‘comp’:
example.c:29:14: error: ‘process’ undeclared (first use in this function)
example.c:29:14: note: each undeclared identifier is reported only once for each function it appears in
example.c:29:22: error: expected expression before ‘)’ token
example.c:29:48: error: expected expression before ‘)’ token
• 01-03-2012
oogabooga
Use struct process, not just process.
• 01-03-2012
spendotw
Quote:

Originally Posted by oogabooga
Use struct process, not just process.

Ahh thanks that got it to compile. Do i need to add anything to the comp function, such as comparison between int A and int B? Because when I run my program my output is this and it doesn't seem to be comparing the integers :s
id: Process194
Qaunta: 68
Priority: 17
sorted priority: 0
id: Process195
Qaunta: 127
Priority: 7
sorted priority: 0
id: Process196
Qaunta: 6
Priority: 17
sorted priority: 0
id: Process197
Qaunta: 103
Priority: 18
sorted priority: 0
id: Process198
Qaunta: 188
Priority: 3
sorted priority: 0

As you can see all the priority that was suppose to be sorted by qsort returns 0's and not a list from 0 - 20
• 01-03-2012
oogabooga
You need to post a complete program that demonstrates the problem.
• 01-03-2012
iMalc
Quote:

Originally Posted by oogabooga
Use struct process, not just process.

Doh, spot the C++ programmer!

Why is there a Priority and a sorted priority in that output? Your struct only has one priority variable.

What do you mean by "qsort returns 0's"? qsort doesn't return anything and it doesn't assign zero to anything. All it does is move your data around to put it into the correct order.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last