EECS 183 Labs
EECS 183 Lab 6: Arrays
Lab due on your scheduled lab day (October 7-11)
Lab accepted for full credit until Tuesday, October 15 Wednesday, October 16 2024, 11:59 pm Eastern
Arrays code autograder direct submission link
In this lab, you are writing code and solving practice exam questions to master the use of arrays.
After completing this lab, you will be able to:
- Understand how arrays are used in C++.
- Understand how to read RMEs and utilize them in defining and calling functions.
You should complete this lab in small groups of about four students. For all labs in EECS 183, to receive a grade, every student must individually submit the Lab assignment.
Starter Files
You can download the starter files using this link.
After downloading and unzipping, you will find the following files in the starter-files folder:
arrays.cpp
: source code file you will use for the Arrays exercise.arrays.h
: header file you will use for the Arrays exercise.test.cpp
: source code file you will use for the Arrays exercise.
IMPORTANT: For this lab, you must include all three files in a single project in Xcode or Visual Studio.
Lab Assignment
Tasks to Complete
- To complete the lab assignment, you need to do the following steps:
- Start a new project with your IDE using the starter files according.
- Stub all of the functions in
arrays.cpp
- see Function Stubs for details. - Write your test cases in
test.cpp
to test the functions you will implement inarrays.cpp
. - Write the implementations for the functions in
arrays.cpp
:negate_all()
is_sorted()
copy_positive_elements()
sort_array()
- Test your functions using the test cases you have written.
- Submit your
arrays.cpp
to the autograder.- For this lab, you will not submit
test.cpp
- For this lab, you will not submit
Multiple Files
-
This lab will use multiple files for one project, similar to Lab 5.
-
Most programs in the real world are written in more than just one file so as to break down the functionality into smaller parts and to keep the program’s organization clean. As you start writing more complex and more involved (and more exciting!) programs, you too will work with multiple files.
-
For Project 1, you worked with just one file, such as
focaccia.cpp
. It had amain()
function, where the execution began, and then some other functions that were called frommain()
or from other functions. But as a program gets more complicated, you can imagine that the file would just get longer and it would be much more difficult to keep it organized, let alone test the program. -
And so a common practice is to put (at least some) functions into separate files. The functions declarations (aka prototypes) go in what is known as header files that end in
.h
, and the function definitions (aka implementations) go in.cpp
files. Each.cpp
file will#include
the.h
files that contain the declarations for the functions it implements or calls. For example, in this lab, you will find#include "arrays.h"
in thearrays.cpp
file.
Function Stubs
A function stub is a temporary substitute for a function that needs to be implemented. In all projects and labs so far in EECS 183, we have provided all of the function stubs for you. Like the previous lab, you will need to create function stubs for all of the functions you will implement.
- If you submit to the autograder without all function stubs or completed definitions, you will see an error for any missing function stub or definition. If you see an error message like
undefined reference to negate_all(int*, int)
it is a missing or incorrect function stub for the function named.
negate_all()
-
This function takes in an integer array and size and modifies the array by negating each element.
-
For example, this test case…
int values[5] = {1, -2, 3, -4, 5}; negate_all(values, 5); print_array(values, 5);
- Should print…
{-1, 2, -3, 4, -5}
is_sorted()
-
This function determines whether or not an integer array is in sorted order from least to greatest. Adjacent duplicate elements are considered to be in sorted order.
-
For example, this test case…
int values1[5] = {1, -2, 3, -4, 5}; int values2[6] = {1, 2, 3, 3, 4, 5}; cout << is_sorted(values1, 5) << endl; cout << is_sorted(values2, 6) << endl;
- Should print…
0 1
- Remember that
true
andfalse
are printed as1
and0
, respectively.
copy_positive_elements()
- This function takes in two integer arrays of equal size and copies the positive values from the first array into the second array. The second array is initialized with all positions equal to 0. Values are copied to adjacent locations in the second array, starting at index 0, so that the positive elements appear in the same order as in the first array. If the first array contains non-positive elements (negative values and/or zero), the end of the second array should have trailing 0s at the end that correspond to these non-positive elements that were not copied over.
NOTE: Zero is not a positive number.
- For example, this test case…
int values1[5] = {1, -2, 3, -4, 5}; int values2[5] = {2, 0, 8, 5, 1}; copy_positive_elements(values1, values2, 5); print_array(values2, 5);
- Should print…
{1, 3, 5, 0, 0}
sort_array()
-
This function takes in an integer array and sorts all elements from least to greatest.
-
For example, this test case…
int values1[5] = {1, -2, 3, -4, 5}; sort_array(values1, 5); print_array(values1, 5);
- Should print…
{-4, -2, 1, 3, 5}
-
When approaching this function, it is especially important to design an algorithm before writing code. Utilizing the
is_sorted()
function will simplify the algorithm and implementation of this function. You should review the zyBooks section on how to swap two values before attempting to implement this function. Consider the following step-by-step example: - First Pass
{*7, -1,* 2, 0, 8}
becomes{*-1, 7,* 2, 0, 8}
swap 7, -1 since 7 > -1{-1, *7, 2,* 0, 8}
becomes{-1, *2, 7,* 0, 8}
swap 7, 2{-1, 2, *7, 0,* 8}
becomes{-1, 2, *0, 7,* 8}
swap 7, 0{-1, 2, 0, *7, 8*}
remains{-1, 2, 0, *7, 8*}
since 8 > 7
- Second Pass
{*-1, 2,* 0, 7, 8}
remains{*-1, 2,* 0, 7, 8}
since 2 > -1{-1, *2, 0,* 7, 8}
becomes{-1, *0, 2,* 7, 8}
swap 2, 0{-1, 0, *2, 7,* 8}
remains{-1, 0, *2, 7,* 8}
since 7 > 2{-1, 0, 2, *7, 8*}
remains{-1, 0, 2, *7, 8*}
since 8 > 7
- Third Pass
{-1, 0, 2, 7, 8}
is_sorted()
.
How to Submit
- When ready to submit to the autograder, visit https://autograder.io/web/project/2678. You will submit your
arrays.cpp
file.
IMPORTANT: For all labs in EECS 183, to receive a grade, every student must individually submit the Lab Submission. Late submissions for labs will not be accepted for credit. For this lab, you will receive ten submissions per day with feedback.
- Once you receive a grade of 10 of 10 points from the autograder you will have received full credit for this lab.