Question

Do it on (c) pls..... 1. Assume you have an input file with 6 integer values....

Do it on (c) pls.....

1. Assume you have an input file with 6 integer values. Scan these values into an appropriate array.

2. Create a user defined function that takes your array as an input and

order your array from lowest to highest. Ex. If you have an array called num, then num[0] should

contain the smallest value while num[5] will contain the largest value.

3. Output the values between 50-75 into an output file called “results.txt”

4. Any other numbers save to “error.txt”

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

// i have written this code in c

// i have used fscanf and fprintf for file operations

// i have used bubble sort for sorting elements

// i have done all operations as you required and thereafter

// written an extra function which displays the content of all three files after all operations are done

#include<stdio.h>

#include <stdlib.h>

// this function swaps two elements

// this function is used by bubble sort

// this does inplace swapping i.e. it uses call by reference

void swapping(int *a, int *b)

{

int flag = *a;

*a = *b;

*b = flag;

}

// this function displays the content of all three files after all operations are done

// this displays the content of original file in which there exist six integers "program.txt"

// this also displays content of "results.txt" which contains integers of range "50-75"

// this also displays content of "error.txt" which contains values other than 50-75 range values

void displayFiles()

{

FILE *fptr;

fptr = fopen("program.txt", "r");

int num;

// original file content "program.txt"

printf("\n The contents of original file \n");

while(fscanf(fptr, "%d", &num)!=EOF){

printf("%d ", num );

}

printf("\n");

fclose(fptr);

// content of results.txt file

// contains integer in range 50-75

fptr = fopen("results.txt", "r");

printf("\n The contents of result file \n");

while(fscanf(fptr, "%d", &num)!=EOF){

printf("%d ", num );

}

printf("\n");

fclose(fptr);

fptr = fopen("error.txt", "r");

// contains integer in range other than 50-75 range

// error.txt file

printf("\n The contents of error file \n");

while(fscanf(fptr, "%d", &num)!=EOF){

printf("%d ", num );

}

printf("\n");

fclose(fptr);

}

void main()

{

// file pointers of FILE data type

// used for reading and writing from files

FILE *fptr1,*fptr2;

if ((fptr1 = fopen("program.txt", "r")) == NULL)

{

printf("Error! opening file");

exit(1);

}

// array of six integers

int arr[6];

int x;

// fscanf returns number of values it read

// we will use that returned number to verify that we read six integers

x=fscanf(fptr1,"%d %d %d %d %d %d",&arr[0],&arr[1],&arr[2],&arr[3],&arr[4],&arr[5]);

fclose(fptr1);

// if we didnot get six integers then close operation

if(!(x==6))

{

printf("Error! reading file");

exit(1);

}

// from here

// there is sorting code

// i have used bubble sort for sorting integers in ascending order

int i, j, n=6;

for (i = 0; i < n-1; i++)

for (j = 0; j < n-i-1; j++)

if (arr[j] > arr[j+1])

// in place swapping used here

swapping(&arr[j], &arr[j+1]);

// till here

// after we have read six integers from "program.txt" file and sorted them

// its time to put them in results and error file

fptr1 = fopen("results.txt", "w");

fptr2 = fopen("error.txt", "w");

if (fptr1 == NULL || fptr2 == NULL) {

printf("File open error\n");

exit(1);

}

// iterate array element by element

// if element is between 50-75 put it in results file

// else put that elements in error file

for(x=0;x<6;x++)

{

if(arr[x]>=50 && arr[x]<=75)

{

fprintf(fptr1, "%d\n", arr[x]);

}

else

{

fprintf(fptr2, "%d\n", arr[x]);

}

}

// close file pointers

fclose(fptr1);

fclose(fptr2);

// display file results

displayFiles();

}

OUTPUT n CODE n FILES screenshot:

Add a comment
Know the answer?
Add Answer to:
Do it on (c) pls..... 1. Assume you have an input file with 6 integer values....
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
  • You are provided with an input file called "sensor.txt" and a "code skeleton" of the exam questio...

    You are provided with an input file called "sensor.txt" and a "code skeleton" of the exam questions. The text file has 10 numbers. 1. You will scan the 10 values located in “sensor.txt” to an array of variable type double, using a loop of your choice. 2. You will create a user defined function that will take your array as an input and find the average value. (Ex. arr[] = {15, 12, 13, 10}, the sum will be 50 then...

  • ****Using C language***** Assume you have an input file with 10 characters. Scan these values in...

    ****Using C language***** Assume you have an input file with 10 characters. Scan these values into an appropriate array. Convert all the characters into Upper Case, then find their ascii value (like done in the homework) and save to a new array, order these values from highest to lowest. Save to file called “upper.txt” Convert all the characters into Lower Case, then find their ascii value (like done in the homework) and save to a new array, order these values...

  • use C++ programming language and follow the instruction carefully. thanks! Inputs: . [integer] values (10 times)...

    use C++ programming language and follow the instruction carefully. thanks! Inputs: . [integer] values (10 times) Outputs: • [integer) highest value . [integer] lowest value Description: Write a program that lets the user enter 10 values. These values should be stored in an array. Your program will then find the highest and lowest values in the array. Your program will display the list of number as a comma separated list, and print which values is the highest and lowest Do...

  • :Pls help for this programing problem in c++ (standard IO without file read/write),better with some comment...

    :Pls help for this programing problem in c++ (standard IO without file read/write),better with some comment about why coding that way,thanks Problem A: Counting Numbers Problem Description Write a program that reads numbers from the keyboard into an integer array. You may assume that there will be 50 or fewer entries in the array. Your program allows any number of numbers to be entered, up to 50 numbers. The output is to be a two-column list. The first column is...

  • Java Program Create a class to store an array of with enough space to store 10 integer values. Us...

    Java Program Create a class to store an array of with enough space to store 10 integer values. Using the principle of recursion, implement the following: *getSize : returns the size of the array. *get (i): returns the i-th element of the array. If the element does not exist, it throws a "NoSuchElementException” which is a subclass of Java class RunTimeException. *add (val): inserts value as the last element of the array. If necessary, double the size of the current...

  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

  • 2D Lists + File I/O In a comma-separated input file named results.txt, you have been given...

    2D Lists + File I/O In a comma-separated input file named results.txt, you have been given the following information that records the weekly (movie) box office sales for 5 movies. A sample input file will include the following. The movie’s title is listed first, then its sales (in million dollars) for 7 days are listed. Avengers,169.1,125.8,101.7,40.5,38.2,24.2,55.7 Shazam!,8.6,14.1,8.2,7.3,31.4,44.2,26.8 Breakthrough,14.8,16.1,18.0,18.9,19.8,21.8,24.6 The Best of Enemies,4.7,5.4,5.8,6.1,6.7,7.6,8.1 Dumbo,9.9,14.8,9.0,7.9,40.6,52.5,36.3 Write a complete Python program that includes code to do the following: read in the data from...

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

  • Four Integer Stats Write an ARM Assembly Language (I will not accept Intel Assembly code) program...

    Four Integer Stats Write an ARM Assembly Language (I will not accept Intel Assembly code) program to prompt the user to enter four integers. Have your program output to the screen the four integers that were entered at the keyboard, along with the following: sum of the four integers, smallest value, largest value, and the average of the four values. You must utilize the scanf function for reading in the user input and the printf function for outputting the results...

  • *Coral Language* Write a program that first gets a list of six integers from input. The first five values are the intege...

    *Coral Language* Write a program that first gets a list of six integers from input. The first five values are the integer list. The last value is the upper threshold. Then output all integers less than or equal to the threshold value. Ex: If the input is 50 60 140 200 75 100, the output is: 50 60 75 For coding simplicity, follow every output value by a space, including the last one. Such functionality is common on sites like...

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