Question

In C,Write a function maxmin(float x, float * pmax, float * pmin) where x is a...

In C,Write a function maxmin(float x, float * pmax, float * pmin) where x is a new value which is to be compared with the largest and the smallest values pointed to by pmax and pmin, respectively. The function should indirectly update the largest and the smallest values appropriately. Write a program that reads a sequence of numbers and uses the above function to update the maximum and the minimum until end of file, when the maximum and the minimum should be printed.

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

PROGRAM:

#include <stdio.h>

#include <stdlib.h>

void maxmin(float x, float * pmax, float * pmin)

{

if(x>*pmax)//comparing x with maximum value if x is greater replacing the max with x

{

*pmax=x;

}

if(x<*pmin)//comparing x with minimum value if x is lesser replacing the min with x

{

*pmin=x;

}

}

int main()

{

FILE* file = fopen ("D:/test.txt", "r"); //reading test.txt file from D: drive

float i = 0;

float max=0.0; //considering max to be 0.0 you can place max to be minimum possible value of float

float min=1000.0; //considering min to be 1000.0 you can place min to be maximum possible value of float

//fscanf (file, "%f", &i);

while (!feof (file)) //traversing through file till end of file

{

fscanf (file, "%f", &i); //scanning every float value from file

maxmin(i, &max, &min); //valling the maxmin function and passing max and min in call by reference way

}

printf("%f\n",max);//printing the maximum value

printf("%f",min);//printing the minimum value

fclose (file);//closing the file

return 0;

}

OUTPUT:

test Notepad DACProgram\ fileread\bin\Debug\fileread.exe File Edit Format View Help 22.3 23.2 89.5 90.5 0.500000 2.299999 P

Add a comment
Know the answer?
Add Answer to:
In C,Write a function maxmin(float x, float * pmax, float * pmin) where x is a...
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
  • Write a program in C that reads 20 float numbers from a file. The values are...

    Write a program in C that reads 20 float numbers from a file. The values are stored in a vector (1-dimensional array). Find and display the smallest number, the second smallest number, and the third smallest number of the set. Use only these libraries: stdio.h, stdlib.h, math.h, and string.h. Below is an example of the input file (in1.txt) and the resulting output file (output.txt). In1.txt: 3.14       4.05       -3.73      4.13       1.32 -2.21      0.46       4.57       4.64       -3.42 4.57       -0.14      3.00       -3.58      -0.78...

  • (C++) Write a function that accepts an int array and the array’s size as arguments. The function...

    (C++)Write a function that accepts an int array and the array’s size as arguments.The function should create a new array that is twice the size of the argument array.The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0.The function should return a pointer to the new array.Demonstrate the function by using it in a main program that reads an integer N (that is not more...

  • C language 3. (10 pts) Write a function readSegment that takes three parameters, a float array...

    C language 3. (10 pts) Write a function readSegment that takes three parameters, a float array as a pointer p as the first parameter, and two int indices startldx and endldx. The function readSegment reads the content of the array between startIdx and endldx, without using additional local variables. 4. (10 pts) Write a function called doubleOdds that takes two parameters, an array of double pointed to by pointer p and endldx of int type. The function doubleOdds doubles (multiply...

  • Write a C++ program that reads the values from a data file til the end of...

    Write a C++ program that reads the values from a data file til the end of the data file is reached. It displays the values read, and computes and displays the largest, smallest, and the average of these values

  • write the program in c++ Pointen & A???ys: Write u function that is passed a pointer...

    write the program in c++ Pointen & A???ys: Write u function that is passed a pointer to a float array, and the size the array as an int. The function should return a pointer to the maximum element in the array The function should be: int *maxp(float* ap, int size) Write a main() program to lest the function with different input arrays. Grading Criteria (1 point each): Uses cin/cout correct function definition prompts user for input, reads it in correct...

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

  • Write a program to read the contents of data.txt file, and determine the smallest value (min),...

    Write a program to read the contents of data.txt file, and determine the smallest value (min), largest value (max), and the average value (ave). The minimum, maximum, and average values must be calculated in the function calc(). Remember to declare the stdlib.h library, so that you can use the atof function, which will convert the number in the text file into float number. Also, remember to check if there is an error opening the file or not. If there is...

  • 9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments

    9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function by using it in the main program that reads an integer N  (that is not more than 50) from standard input and then reads N  integers from a file named...

  • help me please Q1 (10 pts) Write a java program that reads and saves 10 values...

    help me please Q1 (10 pts) Write a java program that reads and saves 10 values from the user in an array. Then calls function smallestDistance The smallest Distance function will finds two neighboring numbers in an smallest distance to each other. The function should return the index of the first number In the sequence 4 8 6 12 9 4 the minimum distance is 1 (between 1 and 2). The function should return the index 3 (of number 1).

  • C++ Manually create a file Numbers.txtand fill it with integers, one below the other. Write a...

    C++ Manually create a file Numbers.txtand fill it with integers, one below the other. Write a program that reads ANY sequence of integers (can be positive, 0 or negative) from this file. Using a looping construct, you will read numbers from the file till end of the file is reached. Calculate the largest and the smallest numbers among the data in the file. Your code must display both of those on the terminal screen along with total count of numbers...

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
ADVERTISEMENT