allocate memory to an array of the size specified by the user at runtime. Use malloc and free. Fill the array, print the elements and calculate the average value. A. In the main function: a) Ask the user for the size of the array. b) Usin

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter7: Arrays
Section: Chapter Questions
Problem 3PP: (Numerical) Given a one-dimensional array of integer numbers, write and test a function that...
icon
Related questions
Question

Language: C

Write a program that will allocate memory to an array of the size specified by the user at runtime. Use malloc and free. Fill the array, print the elements and calculate the average value. A. In the main function: a) Ask the user for the size of the array. b) Using the malloc function allocate the double array of the size specified by the user. c) Check if the allocation was successful. - If the address returned by malloc is not NULL, use the rand function in a for loop and assign pseudorandom values to the array elements. Then call the function averagevalue. Print the result. Free up memory with the free function. - If the allocation failed and the address returned by malloc is NULL, print the message and exit the program. B. Define the function averagevalue and then call it in main. The function calculates the average value of the elements of the array passed as an argument and prints the array elements to the screen. The function returns the average value. C. Use valgrind to check memory usage.

Problem 1:
LAB NR. 7
C PROGRAMMING
December 16, 2022
Write a program that will allocate memory to an array of the size specified by the user at
runtime. Use malloc and free. Fill the array, print the elements and calculate the average value.
A. In the main function:
a) Ask the user for the size of the array.
b) Using the malloc function allocate the double array of the size specified by the user.
c) Check if the allocation was successful.
If the address returned by malloc is not NULL, use the rand function in a for loop and assign
pseudorandom values to the array elements. Then call the function averagevalue. Print the result.
Free up memory with the free function.
If the allocation failed and the address returned by malloc is NULL, print the message and
exit the program.
B. Define the function averagevalue and then call it in main.
The function calculates the average value of the elements of the array passed as an argument
and prints the array elements to the screen. The function returns the average value.
C. Use valgrind to check memory usage.
Transcribed Image Text:Problem 1: LAB NR. 7 C PROGRAMMING December 16, 2022 Write a program that will allocate memory to an array of the size specified by the user at runtime. Use malloc and free. Fill the array, print the elements and calculate the average value. A. In the main function: a) Ask the user for the size of the array. b) Using the malloc function allocate the double array of the size specified by the user. c) Check if the allocation was successful. If the address returned by malloc is not NULL, use the rand function in a for loop and assign pseudorandom values to the array elements. Then call the function averagevalue. Print the result. Free up memory with the free function. If the allocation failed and the address returned by malloc is NULL, print the message and exit the program. B. Define the function averagevalue and then call it in main. The function calculates the average value of the elements of the array passed as an argument and prints the array elements to the screen. The function returns the average value. C. Use valgrind to check memory usage.
Expert Solution
steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr