Answered step by step
Verified Expert Solution
Question
1 Approved Answer
7.5 Programming: Create and till array pased on input the Here's a realistic scenario where dynamically-allocated arrays are helpful. Suppose a file contains temperature readings,
7.5 Programming: Create and till array pased on input the Here's a realistic scenario where dynamically-allocated arrays are helpful. Suppose a file contains temperature readings, one per line. The first line of the file denotes the total number of readings. For example, here's an input file named "data1.txt" that contains 5 temperature readings: 5 41 38 21 39 43 Write a function named inputData that given a filename, dynamically allocates an array based on the first line of the file, and then inputs the temperature readings into the array. The function then returns a pointer to the dynamically-allocated array, along with the size of the array via a reference parameter named N. Here's the function declaration: int* inputData (string filename, int& N) {...} Notice the return type is int*, since the function returns a pointer to the array; the size of the array is "returned" via N. If the input file cannot be successfully opened, set N = 0 and return nullptr (do not output an error message, but instead return the value nullptr which signals that no array was allocated). The main program is written for you, and cannot be modified. The inputData function must be defined in "util.cpp" you select this file from the drop-down list above the editing window. You can test in "Develop mode" by clicking "Run my program", clicking anywhere in the terminal window, and then entering filenames such as "data1.txt" and "data2.txt. The main program will input the filename and then call your function. When you are ready to submit, switch to "Submit mode" and click "Submit for Grading". You have unlimited submissions; the system records your highest score across all submissions. O Type here to search 1:52 PM 1/30/2021 Working with zylabs Some background on the zyLabs programming environment. Between submissions, the zyLab system does not save your work. For example, if you write your program and then close the browser, when you come back your program will be gone. For this reason, save your work in a separate document using an editor or word processor. Each time you submit your work is saved, but nothing is saved until the first submission, and nothing is saved between submissions. If your program is not working, switch to "Develop mode", add some output/debugging statements, and run.... 301246.1517390 LAB ACTIVITY 7.5.1: Programming: create and fill array based on input file 0/50 File is marked as read only Current file: main.cpp cout 2 #include 3 4 using namespace std; 5 6 7 int* inputData(string filename, int& N); // defined in "util.cpp" 8 9 void output_i(int A[], int N); 10 11 12 int main() 13 { 14 int N; // array size 15 int *A; 16 string filename; 17 18 cout > filename; 20 O Type here to search > TE 1:52 PM 1/30/2021 Working with zylabs Some background on the zyLabs programming environment. Between submissions, the zyLab system does not save your work. For example, if you write your program and then close the browser, when you come back your program will be gone. For this reason, save your work in a separate document using an editor or word processor. Each time you submit your work is saved, but nothing is saved until the first submission, and nothing is saved between submissions. If your program is not working, switch to "Develop mode", add some output / debugging statements, and run..... 301246.1517390 LAB ACTIVITY 7.5.1: Programming: create and fill array based on input file 0/50 Current file: util.cpp Load default template... 1 #include 2 #include 3 #include 4 using namespace std; 5 // 6 // inputData 7 // 8 // Opens the given file, dynamically-allocates an array based on the first integer, 9 // and then inputs the remaining values into the rest of the array. Returns a pointer 10 // to the array, and the size of the array via the reference parameter N. 11 // 12 // NOTE: if the file cannot be opened, nullptr is returned and N = 0. 13 // 14 int* inputData(string filename, int& N) 15 { 16 // 17 // TODO: 18 // 19 return nullptr; 20 } O Type here to search 2 #include 3 4 using namespace std; 5 6 7 int* inputData(string filename, int& N); // defined in "util.cpp" 8 9 void output_i(int A[], int N); 10 11 12 int main() 13 { 14 int N; // array size 15 int *A; 16 string filename; 17 18 cout > filename; 20 O Type here to search > TE 1:52 PM 1/30/2021 Working with zylabs Some background on the zyLabs programming environment. Between submissions, the zyLab system does not save your work. For example, if you write your program and then close the browser, when you come back your program will be gone. For this reason, save your work in a separate document using an editor or word processor. Each time you submit your work is saved, but nothing is saved until the first submission, and nothing is saved between submissions. If your program is not working, switch to "Develop mode", add some output / debugging statements, and run..... 301246.1517390 LAB ACTIVITY 7.5.1: Programming: create and fill array based on input file 0/50 Current file: util.cpp Load default template... 1 #include 2 #include 3 #include 4 using namespace std; 5 // 6 // inputData 7 // 8 // Opens the given file, dynamically-allocates an array based on the first integer, 9 // and then inputs the remaining values into the rest of the array. Returns a pointer 10 // to the array, and the size of the array via the reference parameter N. 11 // 12 // NOTE: if the file cannot be opened, nullptr is returned and N = 0. 13 // 14 int* inputData(string filename, int& N) 15 { 16 // 17 // TODO: 18 // 19 return nullptr; 20 } O Type here to search
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started