Question: I need help for Pointers and arrays Objectives Dynamically allocate an array Free dynamically allocated memory Access array elements using pointers Pass pointers to functions

I need help for Pointers and arrays

Objectives

Dynamically allocate an array

Free dynamically allocated memory

Access array elements using pointers

Pass pointers to functions

Requirements

Modify the supplied program (ptrLab.cpp) to make it work properly.

No global variables are allowed.

No [ ] are allowed except for steps 2 (dynamic allocation) and 9 (de-allocation). All array access must be done using pointer notation.

Program to be modified

/*

Place your name and section at the top in comments. Read the instructions for each section and then implement your solution within this program.

*/

#include

#include

#include

#include

using namespace std;

// 1. Create prototypes for the four functions described at the end of this lab.

void checkFile(const char* fname);

int main(int argc, char** argv) {

const int SIZE = 25;

int sum;

// 2. Dynamically create an array of ints with SIZE elements named: array

// 3. Call fillArray to fill the array with random ints from 1 - 100

// 4. Call displayArray to print the elements of array on the screen.

// 5. Call sumArray to get the sum of the elements of the array and store that value in sum.

cout << "The sum of the array is " << sum << ' ';

// 6. Open a file for sequential text output. The file name is nums.txt

// 7. Call writeArray to store the elements in that file.

// 8. Close that file.

// 9. Release the memory you allocated for the array.

checkFile("nums.txt"); return 0;

}

void checkFile(const char* filename) {

int numRecs = 0;

int sum = 0;

int num; ifstream infile(filename);

if (!infile) {

cout << "Error reading file" << endl;

} else {

infile >> num;

while (!infile.eof()) {

sum += num;

numRecs++;

infile >> num;

}

}

infile.close();

cout << "Records in file: " << numRecs << endl;

cout << "Sum of records in file: " << sum << endl;

}

// Create the following functions:

// 1. fillArray

// input: a pointer to an int array and an int length of the array

// output: nothing

// effect: fills the array with random ints between 1 and 100

// 2. displayArray

// input: a pointer to an int array and an int length of the array

// output: nothing

// effect: displays the elements of the array on stdout with a space separator

// with a newline after all the elements are printed

// 3. writeArray

// input: a reference to an ofstream object, a pointer to an int array,

// and an int length of the array

// output: nothing

// effect: writes the elements of the array to the specified file with a

// newline after each element

// 4. sumArray

// input: a pointer to an int array and an int length of the array

// output: returns the sum of the integers in the array

Example run of program

68 58 58 52 44 43 74 55 69 12 16 12 58 62 11 85 17 80 48 49 84 77 9 66 58

he sum of the array is 1265

Records in file: 25

Sum of records in file: 1265

And My Code is:

#include

#include

#include

#include

using namespace std;

// 1. Create prototypes for the four functions described at the end of this lab.

void fillArray(int* ar, int len);

void checkFile(const char* fname);

void displayArray(int* ar, int len);

void writeArray(ofstream& f, int* ar, int len);

void sumArray(int* ar, int len);

int main(int argc, char** argv) {

const int SIZE = 25;

int sum;

int* array = new int[SIZE];

// 3. Call fillArray to fill the array with random ints from 1 - 100

fillArray(array, len);

// 4. Call displayArray to print the elements of array on the screen.

displayArray(array, SIZE);

// 5. Call sumArray to get the sum of the elements of the array and store that value in sum.

sumArray();

cout << "The sum of the array is " << sum << ' ';

// 6. Open a file for sequential text output. The file name is nums.txt

ofstream ofile("nums.txt");

// 7. Call writeArray to store the elements in that file.

writeArray(ofile, int* ar, int len);

// 8. Close that file.

file.close();

// 9. Release the memory you allocated for the array.

delete[] array;

checkFile("nums.txt");

return 0;

}

void checkFile(const char* filename) {

int numRecs = 0;

int sum = 0;

int num;

ifstream infile(filename);

if (!infile) {

cout << "Error reading file" << endl;

} else {

infile >> num;

while (!infile.eof()) {

sum += num;

numRecs++;

infile >> num;

}

}

infile.close();

cout << "Records in file: " << numRecs << endl;

cout << "Sum of records in file: " << sum << endl;

}

// Create the following functions:

// 1. fillArray

// input: a pointer to an int array and an int length of the array

// output: nothing

// effect: fills the array with random ints between 1 and 100

void fillArray(int* ar, int len) {

for (int i=0; i

*(array+i) = rand() % 100 + 1;

}

cout << endl;

}

// 2. displayArray

// input: a pointer to an int array and an int length of the array

// output: nothing

// effect: displays the elements of the array on stdout with a space separator

// with a newline after all the elements are printed

void displayArray(int* ar, int len) {

for (int i=0; i

cout << ar[i] << ' ';

}

cout << endl;

}

// 3. writeArray

// input: a reference to an ofstream object, a pointer to an int array,

// and an int length of the array

// output: nothing

// effect: writes the elements of the array to the specified file with a

// newline after each element

void writeArray(ofstream& f, int* ar, int len) {

for (int i=0; i

f >> ar[i] >> ' ';

}

cout << endl;

}

// 4. sumArray

// input: a pointer to an int array and an int length of the array

// output: returns the sum of the integers in the array

void sumArray(int* ar, int len) { // return data type incorrect

for (int i=0; i

cout << ar[i] << ' ';

}

cout << endl;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!