Question

3. Write a program to read a (binary) file of integers, sort the integers, and write...

3. Write a program to read a (binary) file of integers, sort the integers, and write them back to the same file. Assume that all the numbers can be stored in an array. (Exercise 3)

4. Repeat exercise 3, but assume that only 20 numbers can be stored in memory (in an array) at any one time.

Hint: you will need to use at least two additional files for temporary output.

Please finish the question in C language programming, thank you!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// numbers1.txt (input file)

78 43 89 67 75 90 34 97 78  89

___________________________

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

void sort(int nos[],int cnt);
int main() {
FILE *f1;
int cnt=0,i=0,num;

//Opening the intput file in read mode
f1 = fopen("numbers1.txt", "r");
if (f1 == NULL) {
printf("** File not found **");
exit(1);
}
else
       {
           while(fscanf(f1, "%d", &num)!=EOF)
           {
               cnt++;
           }
           fclose(f1);
          
           int nos[cnt];
       f1 = fopen("numbers1.txt", "r");
       for(i=0;i<cnt;i++)
       {
          fscanf(f1, "%d", &nos[i]);
       }  
fclose(f1);
sort(nos,cnt);
  
f1 = fopen("numbers1.txt", "w");
for(i=0;i<cnt;i++)
{
   fprintf(f1,"%d ",nos[i]);
   if((i+1)%10==0)
   fprintf(f1,"\n");
       }
       fclose(f1);
}
   return 0;
  
}
void sort(int array[],int cnt)
{
       //This Logic will Sort the Array of elements in Ascending order
   int temp,i,j;
   for (i = 0; i < cnt; i++)
{
for (j = i + 1; j < cnt; j++)
{
if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}

__________________________

// numbers1.txt (output file)

numbers 1 - Notepad File Edit Format View Help 34 43 67 75 78 78 89 89 90 97

_________________________

3)

numbers.txt (input file)

143 11 286 37 173 234 -265 -286 85 186
267 266 62 -139 -3 80 -225 10 141 142
166 241 -26 3 -167 76 169 31 -27 167
17 -65 77 -32 13 265 46 245 -261 22

__________________________

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

void sort(int nos[],int cnt);
int main() {
FILE *f1;
const int SIZE=20;
int cnt=0,i=0,num;

//Opening the intput file in read mode
f1 = fopen("numbers.txt", "r");
if (f1 == NULL) {
printf("** File not found **");
exit(1);
}
else
       {
           int nos[SIZE];
           while(fscanf(f1, "%d", &num)!=EOF)
           {
               cnt++;
           }
           fclose(f1);
          
           if(cnt>SIZE)
           {
               cnt=SIZE;
           }
          
       f1 = fopen("numbers.txt", "r");
       for(i=0;i<cnt;i++)
       {
          fscanf(f1, "%d", &nos[i]);
       }  
fclose(f1);
sort(nos,cnt);
  
f1 = fopen("numbers.txt", "w");
for(i=0;i<cnt;i++)
{
   fprintf(f1,"%d ",nos[i]);
   if((i+1)%10==0)
   fprintf(f1,"\n");
       }
       fclose(f1);
}
   return 0;
  
}
void sort(int array[],int cnt)
{
       //This Logic will Sort the Array of elements in Ascending order
   int temp,i,j;
   for (i = 0; i < cnt; i++)
{
for (j = i + 1; j < cnt; j++)
{
if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}

____________________________

// numbers.txt (output file)

numbers - Notepad Eile Edit Format View Help -286 -265 -225 -139 -3 10 11 37 62 80 85 141 142 143 173 186 234 266 267 286

_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
3. Write a program to read a (binary) file of integers, sort the integers, and write...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C programming Strictly - Write a program to sort an array of integers via arrays of...

    C programming Strictly - Write a program to sort an array of integers via arrays of pointers to those integers as shown in the figure. Problem 1 (68 points): Write a program to sort an array of integers via arrays of pointers to those integers as shown in the figure. The code should contain the following functions: 1)to randomly generate an array of integer numbers; 2) to allocate memory and build the arrays of pointers as shown in the figure...

  • Python DESCRIPTION Write a program that will read an array of integers from a file and...

    Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....

  • 1. write a java program using trees(binary search treee) to read integers from input file( .txt)...

    1. write a java program using trees(binary search treee) to read integers from input file( .txt) and add +1 to each number that you read from the input file. then copy result to another (.txt) file. example: if inpu File has numbers like 4787 79434 4326576 65997 4354 the output file must have result of 4788 79435 4326577 65998 4355 Note: there is no commas in between the numbers. dont give images or theory please.

  • Please write in java program Write an application that generates 100 random integers between 1 and...

    Please write in java program Write an application that generates 100 random integers between 1 and 75 and writes them to a file named "file_activity.txt". Read the data back from the file and display the following: The sum of the numbers in the file The average of the numbers in the file The numbers in the file in increasing order To sort an array: int[] arr = new int[20]; Arrays.sort(arr); -- this sorts an array To sort an ArrayList: ArrayList<Integer>...

  • Write a program to subtract large unsigned integers. Your program should prompt and read in two...

    Write a program to subtract large unsigned integers. Your program should prompt and read in two large unsigned integers. The two large integers should be stored arrays, one array for each integer. The integers should be stored with one digit per location in the two arrays. The first integer should be no smaller than the second. Your program should subtract the second integer from the first. The result should be stored in an array, again one digit per location in...

  • 6. Write a program to read a text file and produce another text file in which...

    6. Write a program to read a text file and produce another text file in which all lines are less than some given length. Make sure and break lines in sensible places; for example, avoid breaking words or putting isolated punctuation marks at the beginning of a line. Please finish the question in C language programming, thank you!

  • Assembly Language////Write a program that read in 10 integers from the user. Save the numbers into...

    Assembly Language////Write a program that read in 10 integers from the user. Save the numbers into an array; reverse the array and display the reversed array. .data arrayInt DWORD 10 DUP(?) Your program consists of 4 procedures: 1. main procedure: call procedures getInput, reverseArray, displayArray 2. getInput procedure: prompt user to enter 10 integer numbers, save the numbers into the memory for the arrayInt 3. reverseArray: reverse arrayInt 4. displayArray: display the reversed array

  • Write a program to subtract large unsigned integers. Your program should prompt and read in two...

    Write a program to subtract large unsigned integers. Your program should prompt and read in two large unsigned integers. The two large integers should be stored arrays, one array for each integer. The integers should be stored with one digit per location in the two arrays. The first integer should be no smaller than the second. Your program should subtract the second integer from the first. The result should be stored in an array, again one digit per location in...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program...

    Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program that contains the following functions: 1. A function to read integer values into a one-dimensional array of size N. 2. A function to sort a one-dimensional array of size N of integers in descending order. 3. A function to find and output the average of the values in a one dimensional array of size N of integers. 4. A function to output a one-dimensional...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT