Lab: Pointers, arrays

Requirements

  1. Modify the supplied program (ptrLab.cpp) to make it work properly.
  2. No global variables are allowed.
  3. 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

/*
    ptrLab.cpp
    Name: 
    CIS 150
    Date: 
    
    CIS 150 Pointer Lab
*/

/*
    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 <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
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 << '\n';
    // 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
The sum of the array is 1265
Records in file: 25
Sum of records in file: 1265

Points (10 points total)