Question 3 - A dynamically growing array Problem Definition This is based on your answer to question 1. One problem with

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Question 3 - A dynamically growing array Problem Definition This is based on your answer to question 1. One problem with

Post by answerhappygod »

Question 3 A Dynamically Growing Array Problem Definition This Is Based On Your Answer To Question 1 One Problem With 1
Question 3 A Dynamically Growing Array Problem Definition This Is Based On Your Answer To Question 1 One Problem With 1 (140.77 KiB) Viewed 13 times
Question 3 - A dynamically growing array Problem Definition This is based on your answer to question 1. One problem with the solution to questions 1 and 2 of of this practical is that the user has to know, in advance, how many values they will enter so we can allocate an array of the right size. In some cases it is better for a program just to keep reading values until the user types in a finishing value such as: -100.0. In this problem you are to save your code from question 1 to write a new program called temperatures02. Then modify the code to: • initally allocate an array to hold up to 5 temperatures. • prompt the user to enter temperatures and type the value (-100.0) when they are finished. • if the user fills up the array your program should • using malloc(), dynamically allocate a new array which is double the size. • copy the old values across to the new array. • deallocate the old array. • continue reading into the new array. After you finish reading your array your program should, as before, output the temperatures read in reverse (most recent to oldest) with 1 decimal point of scale and a new line at the end of the input, as in Question 1. Note: you must NOT use realloc() for this question as we are assessing your ability to work with the memory allocation process and realloc() hides this. After submitting this question, feel free to experiment with realloc() to solve this problem. Test Cases As before two test cases for your program would be: temperatures output 15.0 17.0 19.5 -100.0 19.5 17.0 15.0 16.5 18.8 20.5 21.3 16.2 -100.0 16.2 21.3 20.5 18.8 16.5 In a text file called temperatures02.txt) write two more test cases (make sure you have cases that would cause the array to be re-allocated). Coding Implement the problem definition above in a file called temperatures02.c.) Compile your program with Wall to help improve the quality of your code and -- Werror=vla to ensure your code does not rely on variable length arrays. Make sure you save your code into your week10practice folder. Note: your loop to read values should be a while loop that terminates when you read the stopping value of -100.0. Inside your loop you will need to have a check to see if you have reached the end of the currently allocated array and have code to perform the new allocation, copying and deallocation. You might want to write a small program to implement and test the logic for allocation, copying and deallocation so that you are confident that you have the process working before
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply