Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8667349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:56:36+00:00 2026-06-12T17:56:36+00:00

Below is my code: I cant seem to use qsort effectively… It turns my

  • 0

Below is my code:

I cant seem to use qsort effectively… It turns my array into 0’s after they are populated with names and start times… Is it a problem with my qsort call? or the qsort itself.

The header with the structures is as follows:

 /**
 * Simulation of a process scheduler
*/

//#ifndef SCHEDULER_H_
#define SCHEDULER_H_

#include <stddef.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>


/* types */
/** units of time */
typedef long time;
/** process identifier */
typedef int pid;

/** Information about a job of interest to the task scheduler */
struct job_data {

/* pid of this process */
    pid pid;
    /* time process starts */
    time start;
    /* time needed to finish */
    time finish;
    /* time spent processing so far */
    time scheduled;
    /* Number of lines */
    int lines;

};

struct job {

    /* Various parameters used by the scheduler */
    char job_id[20];
    struct job_data parameters;
    char *lines[20];


};  




      /* I/O Files */
        //static char *inputFile;
        char * in;
        static FILE *input;
        static FILE *cur;
        /*Scheduled jobs indexed by PID*/
        struct job list[20];

        /* the next job to schedule */
        //static struct job *job_next = NULL;

        /* Time */
        time clock;

        /*Comparison for qsort*/
        int compare_start(const void *x, const void *y)
        {
            const struct job *a = x;
            const struct job *b = y;

                printf("%ld, %ld\n", a->parameters.start, b->parameters.start);

             if (a->parameters.start < b->parameters.start) 
            {
                        return -1;
             }
             if (a->parameters.start > b->parameters.start) 
            {
                    return 1;
             }

            return 0;

        }

        /*Order Jobs*/
        static void order_jobs(void)
        {

            qsort(list, (sizeof list) / (sizeof list[0]), sizeof list[0], compare_start);   

        }


        /** Read and parse input from input file */
        static void parse_input(void) 
        {
            char    buffer[BUFSIZ];
            char    lines[BUFSIZ];
            int jobs = 0;
            struct  job *current;





            while( fgets(buffer, sizeof(buffer), input) )   
            {


                time start;
                char buf[BUFSIZ];
                sscanf(buffer,"./%s/", buf);
                cur = fopen(buf, "r" );

                int n_lines = 0;


                while( fgets(lines, sizeof(lines), cur) )
                {


                    if( n_lines == 0 )
                    {
                        current = &list[jobs];
                        strcpy(current->job_id, buf);
                        sscanf(lines,"%ld", &start);
                        current->parameters.start = start;              
                    }       

                    n_lines++;
                } 
                current->parameters.lines = n_lines;

                jobs++;

                fclose(cur);

            } 
            order_jobs();
            for (int i = 0; i < jobs; i++)
            {
                printf("%s %ld  %d\n", list[i].job_id, list[i].parameters.start, list[i].parameters.lines);
            }   

        }

int main(int argc, char **argv) 
{
    in = argv[1];
    if ( (input = fopen(in, "r")) == NULL ) {
        fprintf(stderr, "cannot open %s\n", argv[1]);
    }

    parse_input();

    fclose(input);

    return EXIT_SUCCESS;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:56:37+00:00Added an answer on June 12, 2026 at 5:56 pm

    You only load jobs entries into the array, but you tell qsort() to sort the entire array (20 elements). This probably puts non-initialized elements at the front, which you then print.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can't seem to figure as to why the javascript code below gives me
I need to use the goto operator in my code as I can't seem
I can't figure out how to modify the below code to include a toggle
I can't get the code below to compile (see errors). Advice on correction would
I can't understand what the below ruby code does. Can anyone give me some
I can't understand how works the code below: class Host: name = None fileList
The code below working for only first div. I can't display other divs when
Below is a cut down example of some c# code I can't get to
I ended up with this horrible code below, I can't get a better result
I can't get fancybox to open my links in iframe. My code below. Thanks!

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.