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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education