Lab 1 (Setup)

Programming Workshop 2 (CSCI 1061U)

Winter 2021

Faculty of Science

Ontario Tech University


Introduction

The goal of this lab is two folds: 1) set up C++ build system on your machine and 2) flex your C++ programming chops and write a program that return character counts in an array.

Task

Assume that your program has the following array:

"TIDCMDCDiVOjRMXmxAKhtbfZeHOFsZgDWFwejMbzptuOuSTnEvYQCSsHGNbcRwbVPPZzqsFDXktsjdLuiPUjIdoYniTRvjEhRyFongUkDTzQHczxDchBpplZ"

The program will print out the following output.

$ g++ main.cpp -o ccount
$ ./ccount
Input array:
TIDCMDCDiVOjRMXmxAKhtbfZeHOFsZgDWFwejMbzptuOuSTnEvYQCSsHGNbcRwbVPPZzqsFDXktsjdLuiPUjIdoYniTRvjEhRyFongUkDTzQHczxDchBpplZ
Counts:
A: 1
B: 5
C: 6
D: 9
E: 4
F: 5
G: 3
H: 6
I: 5
J: 5
K: 3
L: 2
M: 4
N: 4
O: 5
P: 6
Q: 3
R: 4
S: 6
T: 7
U: 5
V: 4
W: 3
X: 4
Y: 3
Z: 8
Total number of characters: 120

Starter code

The following file uses random number generator to fills an array arr with random values. The random values lie between 0 and RAND_MAX.

// main.cpp

#include <iostream>
#include <ctime>

const int N = 120;

char generate_random_char()
{
    int i = 26.0 * rand() / RAND_MAX;

    if (static_cast<double>( rand() ) / RAND_MAX  < 0.5) {
        return static_cast<char>( 65+i );
    }
    
    return static_cast<char>( 97+i );
}

int main()
{
    srand(time(0));

    // Set up an array of characters
    char arr[N];
    for (int i=0; i<N; ++i) {
        arr[i] = generate_random_char();
    }

    // Printing the contents of the input array
    std::cout << "Input array:" << std::endl;
    for (int i=0; i<N; ++i) {
        std::cout << arr[i];
    }
    std::cout << std::endl;

    // TO DO 
    //
    // Complete the following to get the output seen above.
    // Notice that the characters consists of both lowercase and uppercase letters. 
    // For the purposes of counting, treat all letters as uppercase letters.

    return 0;
}

Afterword

After the successful completion of this lab, your machine should be ready to compile and execute C++ programs. Similar to CSCI 1060U, we will be using commandline tools in this course. The easiest way to access these tools is through linux. The good news is that modern windows includes a unix sub-system, so you don’t really need to have a standalone installation of linux on your machine (meaning, you can still play Cyberpunk 2077 on your machines). Similarly, BSD unix underpins OSX, and these tools are also available on Macs.

Submission

Please submit main.cpp via Canvas.