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; Originally Posted by iMalc Doh, spot the C++ programmer! Why is there a Priority and a sorted priority in that ...

  1. #16
    Registered User spendotw's Avatar
    Join Date
    Dec 2011
    Location
    England
    Posts
    40
    Quote Originally Posted by iMalc View Post
    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.
    Well after i called qsort, i wanted to print proc[i].priority afterwards to see if it has moved my data around in the correct order as you said, so that i can oricess it in the correct order. This was the aim of what i wanted to do, but when i print out proc[i].priority it prints out the value 0. I am not printing out any other priority variable. Sorry for the confusion.

    Code:
    int comp(const void *a, const void *b) {
        return ((struct process*)a)->priority - ((struct process*)b)->priority;
    
    /* elsewhere in the program */
        qsort(proc, 199, sizeof(*proc), comp);
        printf("sorted priority: %d\n", proc[i].priority);

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

    Question

    Okay, i think i'v found the issue

    Code:
     
    /* elsewhere in the program */
        qsort(proc, 199, sizeof(*proc), comp);
    
    for ( i = 0; i < 199; i++)
        printf("sorted priority: %d\n", proc[i].priority);
    The printf was printing 0's as that is the lowest value, but as the i wasn't being incremented it was stuck on proc[0].priority.

    Could someone confirm that this is correct for me please

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

    Question

    Actually looking back out the output this qsort function still doesn't seem to give the correct output as it doesn't print any priority variable with the value of 0. It seems to order the priority from 1 to 20 not 0 to 20. I have multiple of (NULL) 0 0
    at the begging out my output when i try and print the following:
    [CODE]
    qsort(proc, 199, sizeof(*proc), comp);
    printf("ID: %s Qaunta: %d Priority: %d\n", proc[i].id, proc[i].qaunta, proc[i].priority);
    }
    [CODE]

    Other than the initial processes that have been sorted which are all the priorities equaling 0 they all seem to be ordered correctly. :S
    Last edited by spendotw; 01-05-2012 at 09:24 AM.

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't actually have any arguments for your printf statement. Turn on your compiler warnings.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #20
    Registered User spendotw's Avatar
    Join Date
    Dec 2011
    Location
    England
    Posts
    40
    Quote Originally Posted by quzah View Post
    You don't actually have any arguments for your printf statement. Turn on your compiler warnings.


    Quzah.
    Oh sorry that was because i re typed it and was a bit sloppy there....

    iv compiled with warning and get no warnings at all now

    any reason why qsort doesn't sort from 0 ?

  6. #21
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you write a small working example that qsorts an array of ints and see if you can get that working right.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #22
    Registered User spendotw's Avatar
    Join Date
    Dec 2011
    Location
    England
    Posts
    40
    Quote Originally Posted by quzah View Post
    Why don't you write a small working example that qsorts an array of ints and see if you can get that working right.


    Quzah.
    I have, and it did work but when i try to implement the same to my program i cant seem to find where i am going wrong :\

    i mean i know the compare function is different in my program to a function that compares an array of ints so im thinking its got something to do with that, which was given earlier in the thread or the only other thing i could see it being is strtok, as that deals with NULL terminating the buffer

    Code:
     
    procstr = strtok(buffer, delim);
            while (procstr)
            {
                proc[i].id = strdup(procstr);
                //printf("id: %s\n", proc[i].id);
     
                procstr = strtok(NULL, delim);
                proc[i].qaunta = atoi(procstr);
                //printf("Qaunta: %d\n", proc[i].qaunta);
    
     
                procstr = strtok(NULL, delim);
                proc[i].priority = atoi(procstr);
                //printf("Priority: %d\n", proc[i].priority); 
     	
                procstr = strtok(NULL, delim); 
    
    /* order the priority in the correct order */
       	qsort(proc, 199, sizeof(*proc), comp);
    }

  8. #23
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You should do the qsort after you've read all your processes into your array, not after each one is read. Think, man!

  9. #24
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Wait, what?! You're calling qsort from inside the while loop?
    If so then there's your bug right there!

    I also don't see where you're incrementing i. I think you better post the real code.
    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"

  10. #25
    Registered User spendotw's Avatar
    Join Date
    Dec 2011
    Location
    England
    Posts
    40
    Quote Originally Posted by iMalc View Post
    Wait, what?! You're calling qsort from inside the while loop?
    If so then there's your bug right there!

    I also don't see where you're incrementing i. I think you better post the real code.
    no, qsort is not in a while loop...

    Let me start fresh
    Heres the source code:
    Code:
    #include <stdio.h> 
    #include <stdlib.h>
    #include <sys/types.h> 
    #include <sys/stat.h> 
    #include <fcntl.h> 
    #include <string.h> /* strtok() */
    #include <unistd.h>  
    #include <stdbool.h> 
    
    #define BUFFSIZE 10000 /* buffer allocation for file size */ 
    #define MAXPROCS 1000 /* number of max processes */ 
    #define INIT /* Process states */ 
    #define READY
    #define RUNNING
    #define DONE
    
    struct process {
        char *id; 
        int state; 
        int priority;
        int qaunta;
        int temp;
        int working; 
        int waiting;
    };
    
    struct process proc[MAXPROCS]; /* Current process */
    
    int comp(const void *a, const void *b)
    {
        return ((struct process*)a)->priority - ((struct process*)b)->priority;
    }
    
    int main (int argc, char **argv)
    { 
    
        int fd, i;
        char buffer[BUFFSIZE+1]; /* extra byte for null */
        char *procstr = NULL;
        char *delim = " \n";
        int status;
    
        fd = open(argv[1], O_RDONLY);
        if ( fd == -1 ) 
        {
            printf("There was an error opening the file \n"); 
            exit(1);
        }
     
        i = 0;
        while ((status = read(fd, buffer, BUFFSIZE)) > 0) /* read until EOF or error */
        {
            /* null terminate buffer */
            buffer[status] = 0;
      
            /* Tokenize 'process string' attributes */
            procstr = strtok(buffer, delim);
            while (procstr)
            {
                proc[i].id = strdup(procstr);
                //printf("id: %s\n", proc[i].id);
     
                procstr = strtok(NULL, delim);
                proc[i].qaunta = atoi(procstr);
                //printf("Qaunta: %d\n", proc[i].qaunta);
    
     
                procstr = strtok(NULL, delim);
                proc[i].priority = atoi(procstr);
                //printf("Priority: %d\n", proc[i].priority); 
         
                procstr = strtok(NULL, delim); 
        }
    }
    /* order process priority from 0 to 20 */
           qsort(proc, 199, sizeof(*proc), comp);
     printf("%s %d %d\n", proc[i].id, proc[i].qaunta, proc[i].priority); //test to see whats in the variables after qsort 
    
    ool needToContinue = 0;
    //keep processing until all have been processed (ie all quanta == 0)
    while(needToContinue)
    {    
        
        //reset the flag each loop of the array
        needToContinue = false;
        //loop thru the entire array
        for(i=0;i<199;++i)//loop through the array
        {    
            printf("Id: %s Qaunta: %d Priority: %d\n", proc[i].id, proc[i].qaunta, proc[i].priority);
            //check to see if this element is finished
            if(proc[i].qaunta > 0)
            {
                //not finished so process
                proc[i].qaunta--;
                printf("ID: %s Qaunta Remaining: %d \n",proc[i].id, proc[i].qaunta);
                //now check to see if we have finished processing this element 
                //or need to keep going next loop
                if(proc[i].qaunta > 0)
                {
                    //we found an element that will need to be processed in the next loop, 
                    //so set the flag to continue looping
                    needToContinue = true;
                }
            }
        }
    }
    //show processing is finished
    printf("Goodbye\n");
    
        close(fd);
        return 0; 
    }
    When i compile it my output is as follows:

    (null) 0 0
    Goodbye

    What i desire it to output is something as follows:
    Processid Qaunta Priority
    Process5 33 0
    Process67 7 0
    Process 34 87 1
    Process 11 99 2
    .......................
    Process 188 156 20

  11. #26
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Two problems.

    You're reading every process into element 0. You need to increment i at the end of your while(procstr) loop.

    You need a loop on your printf to display the array.
    Code:
    for (i=0; i<199;++i)
    printf("%s %d %d\n", proc[i].id, proc[i].qaunta, proc[i].priority);
    spendotw likes this.

  12. #27
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    You should only qsort however many entries you read. I'm guessing it wont always be 199. Once you fix your while(procstr) loop as oogabooga suggested, you should use i as the second parameter to qsort. Honestly, I would use a more sensible variable name like num_procs instead of i.
    spendotw likes this.

Page 2 of 2 FirstFirst 12
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, 11:55 AM
  2. finding highest average
    By Vontrapp in forum C Programming
    Replies: 6
    Last Post: 06-07-2010, 07:38 AM
  3. C program for finding highest common factor!!
    By visham in forum C Programming
    Replies: 11
    Last Post: 08-02-2007, 08:10 AM
  4. finding highest number entered
    By volk in forum C++ Programming
    Replies: 11
    Last Post: 03-23-2003, 01:22 AM
  5. finding the type of a input variable
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2002, 09: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