Like Tree4Likes

Finding the next highest value in a int variable

This is a discussion on Finding the next highest value in a int variable within the C Programming forums, part of the General Programming Boards category; Hi there I need a method or mechanism to find/sort the values stored in a struct member of type int. ...

  1. #1
    Registered User spendotw's Avatar
    Join Date
    Dec 2011
    Location
    England
    Posts
    40

    Question 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.

    If anyone could provide a suitable way about going about this it would be appreciated thanks

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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.
    Last edited by oogabooga; 01-02-2012 at 03:39 PM.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Quote Originally Posted by Adak View Post
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User spendotw's Avatar
    Join Date
    Dec 2011
    Location
    England
    Posts
    40

    Question

    Quote Originally Posted by oogabooga View Post
    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

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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);

  7. #7
    Registered User spendotw's Avatar
    Join Date
    Dec 2011
    Location
    England
    Posts
    40
    Quote Originally Posted by oogabooga View Post
    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

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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.

  9. #9
    Registered User spendotw's Avatar
    Join Date
    Dec 2011
    Location
    England
    Posts
    40

    Question

    Quote Originally Posted by oogabooga View Post
    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];

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    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.
    spendotw likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Registered User spendotw's Avatar
    Join Date
    Dec 2011
    Location
    England
    Posts
    40
    Quote Originally Posted by iMalc View Post
    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

  12. #12
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Use struct process, not just process.
    spendotw likes this.

  13. #13
    Registered User spendotw's Avatar
    Join Date
    Dec 2011
    Location
    England
    Posts
    40
    Quote Originally Posted by oogabooga View Post
    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

  14. #14
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You need to post a complete program that demonstrates the problem.

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Quote Originally Posted by oogabooga View Post
    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.
    Last edited by iMalc; 01-03-2012 at 04:19 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with finding the highest average grade
    By Rockie in forum C++ Programming
    Replies: 4
    Last Post: 11-10-2011, 10:55 AM
  2. finding highest average
    By Vontrapp in forum C Programming
    Replies: 6
    Last Post: 06-07-2010, 06:38 AM
  3. C program for finding highest common factor!!
    By visham in forum C Programming
    Replies: 11
    Last Post: 08-02-2007, 07:10 AM
  4. finding highest number entered
    By volk in forum C++ Programming
    Replies: 11
    Last Post: 03-23-2003, 12:22 AM
  5. finding the type of a input variable
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2002, 08:40 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21