Question

Write program in C. Please ask the user to determine how many numbers to enter, and...

Write program in C.

Please ask the user to determine how many numbers to enter, and then find and display the largest one with two digits.

Please call the following two functions in your main functions. void input_numbers(float *data, int num); void find_largest(float *data, int num);

Note: In this assignment, please save the entered numbers using pointer, pass the pointer as the arguments. Depending upon the number of elements, the required size is allocated which prevents the wastage of memory. If no memory is allocated, error is displayed, and the program is terminated.

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

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

//function to get the input from user
void input_numbers(float *data, int num)
{
//variable declaration
float inputNum;
  
//display message
printf("\nEnter numbers: ");
  
//get input from the user
for(int i=1; i<=num; i++)
{
scanf("%f", &inputNum);
*data = inputNum;
data++;
}
}

//function to find the largest element
void find_largest(float *data, int num)
{
//variable declaration and initialization
float largest = *data;
  
//increment the pointer
data++;
  
for(int i=1; i<=num; i++)
{
if(largest < *data)
{
largest = *data;
}
data++;
}
  
//display the largest number up to two decimal digit
printf("\nThe largest number is: %.2f", largest);
}

int main()
{
//variable declaration
int num;
  
//display message
printf("How many element you want to enter: ");
scanf("%d", &num);
  
float *dataPtr = (float*) calloc(num, sizeof(float));
  
//check if memory is not allocated
if (dataPtr == NULL)
{
printf("Could not allocate memory\n");
exit(-1);
}
  
//function calling
input_numbers(dataPtr, num);
  
//function calling
find_largest(dataPtr, num);
return 0;
}

OUTPUT:

How many element you want to enter: 5 Enter numbers: 12.25 20.21 56.55 98.50 10.20 The largest number is: 98.50

Add a comment
Know the answer?
Add Answer to:
Write program in C. Please ask the user to determine how many numbers to enter, and...
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
  • Purpose The purpose of the lab is to learn how to pass pointers in C. Process Create the function...

    Programming in C Purpose The purpose of the lab is to learn how to pass pointers in C. Process Create the functions as defined in the following table. Each function will add the last two integer parameters together Function PrototypeReturr int add2a(int,int); Returns the sum as a return value void add2b(int* int,int); Returns the sum in the first parameter int add2c(int*,int,int); Returns the sum as a return value and in the first parameter Returns the sum as a pointer to...

  • Develop a flowchart and then write a menu-driven C++ program that uses several FUNCTIONS to solve...

    Develop a flowchart and then write a menu-driven C++ program that uses several FUNCTIONS to solve the following program. -Use Microsoft Visual C++ .NET 2010 Professional compiler using default compiler settings. -Use Microsoft Visio 2013 for developing your flowchart. -Adherence to the ANSI C++  required -Do not use <stdio.h> and <conio.h>. -Do not use any #define in your program. -No goto statements allowed. Upon execution of the program, the program displays a menu as shown below and the user is prompted to make a selection from the menu....

  • Ask user to enter three numbers (a,b,c). Write a program (c++) that will print the value...

    Ask user to enter three numbers (a,b,c). Write a program (c++) that will print the value to the following equations: a. maximum value of one of these equations (functions separated by commas) (a, a+b, a-c) + maximum of (b, 2b-c, b+2a) b. maximum value of (3, c+3a, 0) + minimum of (ab-2, 3c, ac) Define functions max and min which will accept 3 numbers as arguments and return the mix and min of them correspondingly. Use these functions in order...

  • You are to write a program IN C++ that asks the user to enter an item's...

    You are to write a program IN C++ that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: if the an item's wholesale cost is 5.00 and its markup percentage is 100%, then the item's retail price is 10.00 If an item's wholesale cost is 5.00 and its markup percentage is 50%, then the item's retail price is 7.50 Program design specs. You should have 5...

  • I need a c++ code please. 32. Program. Write a program that creates an integer constant...

    I need a c++ code please. 32. Program. Write a program that creates an integer constant called SIZE and initialize to the size of the array you will be creating. Use this constant throughout your functions you will implement for the next parts and your main program. Also, in your main program create an integer array called numbers and initialize it with the following values (Please see demo program below): 68, 100, 43, 58, 76, 72, 46, 55, 92, 94,...

  • For the following C# program: Let’s add one more user defined method to the program. This...

    For the following C# program: Let’s add one more user defined method to the program. This method, called ReverseDump is to output the values in the array in revere order (please note that you are NOT to use the Array.Reverse method from the Array class). The approach is quite simple: your ReverseDump method will look very similar to the Dump method with the exception that the for loop will count backwards from num 1 to 0 . The method header...

  • Write a MIPS program that will ask the user to enter two floating point numbers at...

    Write a MIPS program that will ask the user to enter two floating point numbers at the console and pass the values to a function that does multiplication

  • C programming Ask the user how many numbers they would like generated. Your program will then...

    C programming Ask the user how many numbers they would like generated. Your program will then generate that many numbers (between 0 and 1000, inclusive) into an array of the same size. Find the smallest and largest numbers in the array, printing out the values and locations of those numbers in the array. Then, find the sum and average of the numbers in the array. Finally, print out the entire array to verify. You MUST use functions where appropriate!

  • C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) L...

    C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) Last Name (char[]) Age (int) Height in Inches (double) Weight in Pounds (double) You will use pass-by-reference to modify the values of the arguments passed in from the main(). Remember that arrays require no special notation, as they are passed by reference automatically, but the other...

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