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 7700981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:52:28+00:00 2026-05-31T22:52:28+00:00

I forgot to include the programming language (It should be in the C language).

  • 0

I forgot to include the programming language (It should be in the C language).

I need help in doing this program. Sample code will be greatly appreciated.

The program reads a file containing:

a. the number of integers to be sorted, followed by
b. the integers to be sorted (one integer per line) (must be the same with the indicated number of integers).

It will then be sorted out in another text file and a binary file from lowest to highest.

Other specs:

  1. Use dynamic memory allocation
  2. The format in the terminal should be:

    ./program.out  original-file.txt  output-file.txt  output-file.bin
    

where program.out is the program itself, original-file.txt is the text file which contains the number of integers to be sorted and the unsorted integers and the output-file.txt and output-file.bin contains the sorted integers.

Error checking:

  1. check if malloc() was returned successfully

the original file will look like:

3 #number of integers to be sorted
3 #the integers-separated by new line
2
1

the output file:

3
1
2
3

Thank you very much in advance 🙂 God Bless!

  • 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-05-31T22:52:30+00:00Added an answer on May 31, 2026 at 10:52 pm
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    struct tree {
      int num, cnt;
      struct tree *left, *right;
    };
    struct tree *add(struct tree *t, int val) {
      if (!t) {
        if (!(t = malloc(sizeof(struct tree))))
          perror("Not enough memory"), exit(-1);
        memset(t, 0, sizeof(struct tree));
        t->num = val;
        ++t->cnt;
        return t;
      }
      if (val < t->num)
        t->left = add(t->left, val);
      else if (val > t->num)
        t->right = add(t->right, val);
      else
        ++t->cnt;
      return t;
    }
    int walk(struct tree *t, int (*f)(struct tree *, void*), void *data) {
      int rc;
      if (!t)
        return 0;
      rc = walk(t->left, f, data);
      rc += f(t, data);
      rc += walk(t->right, f, data);
      return rc;
    }
    struct tree *clean(struct tree *t) {
      if (!t)
        return NULL;
      t->left = clean(t->left);
      t->right = clean(t->right);
      free(t);
      return NULL;
    }
    int save(struct tree *t, void *data) {
      int i, rc = 0;
      FILE *fp = (FILE *) data;
      for (i = 0; i < t->cnt; ++i)
        rc += (fprintf(fp, "%d\n", t->num) < 0);
      return rc;
    }
    int saveb(struct tree *t, void *data) {
      int i, rc = 0;
      FILE *fp = (FILE *) data;
      for (i = 0; i < t->cnt; ++i)
        rc += (fwrite((void *) &t->num, sizeof t->num, 1, fp) != 1);
      return rc;
    }
    int main(int argc, char **argv) {
      int rc = 0;
      struct tree *t = NULL;
      char buff[0x200];
      FILE *fin, *fout, *foutb;
      if (argc < 4) {
        fprintf(stderr, "Usage: %s SRCFILE OUTFILE.TXT OUTFILE.BIN\n", argv[0]);
        exit(0);
      }
      if (!((fin = fopen(argv[1], "r")) && (fout = fopen(argv[2], "w"))
            && (foutb = fopen(argv[3], "wb")))) {
        perror("fopen");
        exit(-1);
      }
      while (fgets(buff, sizeof buff, fin))
        t = add(t, atoi(buff));
      rc += walk(t, save, (void *) fout);
      rc += walk(t, saveb, (void *) foutb);
      t = clean(t);
      fclose(fin);
      fclose(fout);
      fclose(foutb);
      return rc;
    }
    

    Just noticed the “number of integers in first string of a file” spec; I think your teacher ment you’d put them into array and sort it; but anyway do it yourself 🙂

    #include <stdio.h>
    #include <stdlib.h>
    int cmp(const void *a, const void *b) {
      return *(int*)a > *(int*)b ? 1 : (*(int*)a < *(int*)b ? -1 : 0);
    }
    int main(int argc, char **argv) {
      char buff[0x200];
      FILE *fin, *fout, *foutb;
      int i, *arr, sz = 0, rc = 0;
      if (argc < 4) {
        fprintf(stderr, "Usage: %s SRCFILE OUTFILE.TXT OUTFILE.BIN\n", argv[0]);
        exit(0);
      }
      if (!((fin = fopen(argv[1], "r")) && (fout = fopen(argv[2], "w"))
            && (foutb = fopen(argv[3], "wb")))) {
        perror("fopen");
        exit(-1);
      }
      if (fgets(buff, sizeof buff, fin)) {
        sz = atoi(buff);
        if (!(arr = malloc(sizeof(int) * sz)))
          perror("Not enough memory"), exit(-1);
        for (i = 0; i < sz && fgets(buff, sizeof buff, fin); ++i)
          arr[i] = atoi(buff);
      }
      qsort(arr, sz, sizeof(int), cmp);
      for (i = 0; i < sz; ++i)
        rc += (fprintf(fout, "%d\n", arr[i]) < 0);
      for (i = 0; i < sz; ++i)
        rc += (fwrite((void *) &arr[i], sizeof(int), 1, foutb) != 1);
      fclose(fin);
      fclose(fout);
      fclose(foutb);
      return rc;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hi i forgot the code which in a sample class you have to add
I need some help with a program that I am writing for my Systems
I just forgot the password. Can anyone help me how to get back the
[Update]: Forgot to include ComponentArt... Hi, An interaction designer employed by the client I
I have the following code (works only on gcc): #include <iostream> #include <cstdlib> #include
So I did a bit of C programming a while ago and basically forgot
EDIT: forgot to include my environment info... Win7x64, RubyInstaller Ruby v1.9.1-p378 EDIT 2: just
One example is described here . But the author apparently forgot to include the
I'm using the following short program to test std::clock() : #include <ctime> #include <iostream>
I have the following XML: <employees> <employee> <!--forgot to include an attribute--> <name>John</name> <jobs>

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.