Lab 4: Arrays

Due: Wednesday, July 21, 2021 (11:59 PM PDT)

Introduction

The assignment for this week will utilize concepts of arrays.

We will also be grading for meeting requirements, using “class legal” code, and plagiarism. So, it is not enough for your lab to just pass the Gradescope autograder! Please read the instructions herein carefully.

It is highly recommended that you develop the algorithms for each program first and then develop the C++ code for it.

Step 1: Getting ready

Either through a graphical file system explorer or through the terminal:

  1. Navigate to your cs16 directory:
    $ cd cs16
  2. Create and navigate to the lab4 directory:
    $ mkdir lab4
    $ cd lab4

Step 2: Create and edit your C++ programs

This week, you will need to create one (1) C++ program called arrays.cpp. It is worth 100 points and submitted for full assignment credit.

IMPORTANT NOTE: We will take major points off if you use C++ instructions/libraries/code that either (a) was not covered in class, or (b) was found to be copied from outside sources (or each other) without proper citation.

arrays.cpp

Setup

For this project, you will start with four (4) files: ArrayFile.txt, constants.h, headers.h, and arrays.cpp. To get the starter files, either:

  1. Download these starter files from the course website next to the Lab 4 instructions link: https://sites.cs.ucsb.edu/~zsisco/cs16/#labs

  2. Or, if you are using CSIL, run the following commands from your lab4 directory:

    $ cp ~zsisco/public_html/cs16/lab4-files.zip . 
    
    $ unzip lab4-files.zip

You will only be submitting arrays.cpp to Gradescope, but you will need the other 3 files to be able to run your program. The autograder will have those same 3 files (constants.h, headers.h, and ArrrayFile.txt). The first thing you should do is open and read arrays.cpp. Upon closer examination, you notice that there are two “new” types of instructions there:

#include "headers.h"
#include "constants.h"

These lines are there to tell the compiler to go get the files headers.h and constants.h and place them in the program there. This is one way to make your programs be a little more compact. This is what each contains:

(a.) headers.h contains all the function declarations that you will need for arrays.cpp. There are 8 function declarations in there. You will need to create the function definitions from these:

void print_array (int arr[], int asize);
int maxArray (int arr[], int asize);
int minArray (int arr[], int asize);
int sumArray (int arr[], int asize);
void evensArray (int arr[], int asize);
void primesArray (int arr[], int asize);
int SeqSearch (int arr[], int array_size, int target);
void AllSearches (int arr[], int array_size);

In addition, this file also contains a definition for a function called getArray(). It involves getting data from an external file and you are not responsible for doing anything with it (we haven’t learned how to do file I/O yet). DO NOT CHANGE THE getArray() DEFINITION AT ALL!!

(b.) The file constants.h contains declarations for constant global variables that you will need for arrays.cpp. Your program will be reading in a text file called ArrayFile.txt, which contains a MAXSIZE number of integers. The program assigns those integers as elements of an array called array[] via the function called getArray(). Again, this is a function that you do not have to define yourself—it is written for you in headers.h. More on this in a bit. The other 2 things in the constants.h file are:

  1. a constant int called NSEARCHES, which is set to 10.
  2. a constant int array called SEARCHES[] that contains NSEARCHES elements. SEARCHES[] is initialized to certain int values. Again, more on this in a bit.

Design your program

Your program arrays.cpp will do eight (8) tasks (they’re not big tasks, individually). Each one of these 8 tasks has to do with one of the 8 function declarations mentioned earlier. I’ve placed a hint in the skeleton file for you about how to call these functions. Once your program reads the array data (via getArray(), which you will not modify), it should do the following things (these should all be done with 8 functions that you will have to define in your submitted file):

  1. Print the array that it just read in. It does this using the function print_array(), which is declared in the headers.h file. You are required to do this using this function only. The function call should be all it takes to execute this task.

  2. Find the largest integer in this array and return it. It does this using the function maxArray(), which is declared in the headers.h file. You are required to do this using this function only. Once this function is called, you should print out the result (see the example run below).

  3. Find the smallest integer in this array and return it. It does this using the function minArray(), which is declared in the headers.h file. You are required to do this using this function only. Once this function is called, you should print out the result (see the example run below).

  4. Will find the sum of all the integers in this array and return it. It does this using the function sumArray(), which is declared in the headers.h file. You are required to do this using this function only. Once this function is called, you should print out the result (see the example run below).

  5. Find all the even number integers in this array and print them. It does this using the function evensArray(), which is declared in the headers.h file. You are required to do this using this function only. Once this function is called, you should print out the result (see the example run below).

  6. Find all the prime number integers in this array and print them. It does this using the function primesArray(), which is declared in the headers.h file. You are required to do this using this function only. A prime number is a number that is only divisible by itself and 1. Note that 1 is, itself, not considered a prime number (2 is, however). Once this function is called, you should print out the result (see the example run below).

  7. Will search the array for every integer number in the global array SEARCHES[]. It will use both the function SeqSearch() and the function AllSearches() to do this, both of which are declared in the headers.h file. You are required to do this using these functions only. Here’s what you have to pay mind to: SeqSearch() only finds one target in an array (the first incidence of the target). AllSearches() runs all the targets from the global array SEARCHES[].

Remember that the program must print out a mini-report about what it found and did not find (see the example run below).

Here is an example run using the ArrayFile.txt data:

$ ./arrays
23, 22, -5, -21, 21, 0, -12, -55, 9111, 1233, 1, 3, 5, 6, 7, 8, 9, 12, -11, 17
Max = 9111
Min = -55
Sum = 10374
Evens: 22, 0, -12, 6, 8, 12, end
Primes: 23, 3, 5, 7, 17, end
Searches:
Looking for -5. Found at index: 2
Looking for -4. Not Found!
Looking for -3. Not Found!
Looking for -2. Not Found!
Looking for -1. Not Found!
Looking for 0. Found at index: 5
Looking for 1. Found at index: 10
Looking for 2. Not Found!
Looking for 3. Found at index: 11
Looking for 4. Not Found!

Note that the first line is a print out of the array, then it shows the maximum, the minimum, the sum, the evens, the primes, and the searches “mini-report”. The integer array is the one that is read in at the start of the program (by the getArray() function) and the array of searched numbers is the one defined in the SEARCHES[] declaration.

Please make sure your output matches the above. For example, notice how the printout of the array has commas and a space between each element, except for at the last one. Also notice that the evens and primes lists also has commas and a space between each element and they end with the word “end”.

REQUIREMENT: Do not create/define any functions other than the ones that are declared in the headers.h file. Do not change the function declarations or modify headers.h.

When you test your program, it will use the ArrayFile.txt data as your source. When you want to test your program further, you can create another data file (give it another name, like MyTest.txt or something) with just integers in it (separated by whitespaces). You will have to modify the MAXSIZE and FILENAME global variables accordingly in the constants.h file (only modify these).

Step 3: Create a makefile and compile the code

In order to learn another way to manage our source codes and their compilations, we will first create a makefile and put in the usual g++ commands in it. Afterwards, whenever we want to compile our programs, the command is a lot shorter—so this is a convenience.

Using your text editor, create a new file called makefile and enter the following into it:

arrays: arrays.cpp headers.h constants.h
    g++ -o arrays arrays.cpp -Wall -std=c++11

Then from the command line, you can issue one command that will compile this code, like so:

$ make

If the compilation is successful, you will not see any output from the compiler. You can then run your program like this:

$ ./arrays

If you encounter an error, use the compiler hints and examine the line in question. If the compiler message is not sufficient to identify the error, you can search online to see when the error occurs in general.

Remember to re-compile the relevant files after you make any changes to the C++ code.

Step 4: Submit your programs for grading

Once you are satisfied your programs are correct, then it’s time to submit them. While working with others is OK, you still must submit your own lab. Even if you’re working with another person, do not copy each other’s code.

Log into Gradescope and select CMPSC 16 under Summer 2021, and navigate to the Lab 4 assignment. Then click on the “Upload Submission” button on the bottom right corner to make a submission.

You will be given the option of uploading files from your local machine or submitting code from a GitHub repo. Follow the steps to upload only arrays.cpp to Gradescope (do not upload any other files). If your files do not have those exact names as presented in this lab, the autograder will not grade them. You can resubmit your files as many times as you like before the assignment deadline.