Sunteți pe pagina 1din 4

Arrays Lab C++

This lab has 2. Please put all parts into one file. Title it ArrayLab_StudentName, and comment the code
appropriately to distinguish different parts of the lab:

Part 1 – 40% write a few array functions to get more comfortable with them. Remember to add
appropriate comments as you are working.

1) Create a function call sumArray which will return the int sum of an array that is passed in. Note
that a length of the array should also be specified.
input: arr={1, 2, 3, 4}, length=3
returns: 6 (because the 4 was ignored because it was past the length)

2) Create a function call convertToDecimal. This method will take an int array { 3, 2, 4 } and a
length of the array, and will return a single integer where each part of the array is a digit in the
new integer (324). To do this you will need to multiply:
3*10^2 +
2*10^1 +
4*10^0 = 324

Input {1,4,2,1,6}, length = 5


1*10^4 +
4*10^3 +
2*10^2 +
1*10^1 +
6*10^0 = 14216
Input {1,1,0,1}, length = 4, base=2

For 5% extra credit add an optional parameter that allows it to take a base to multiply it to such
as base 2 or base 8 (in the example I’m using base 10 of course).

3) Write a function printAccending, that will take an int array and the length as its input and output
each integer on a line that is greater than the last. If one is smaller it will drop to a new line and
continue from there:
input {2, 5, 8, 3, 9, 9, 7}
output:
2, 5, 8
3, 9, 9
7
4) Write a function reverseArrayEven which will loop through a given array (with a size provided as
well), and will reverse the digits if they are even numbers.
input: {1, 2, 4, 5, 6, 9, 10}
returns: {10, 9, 4, 5, 6, 2, 1}

Part 2: 30%

5) Create a separate function called Part2. Here we are going to create an array of strings with 20 places
in it. Start by setting all the strings to be empty “”

6) To facilitate inserting new items into our string array you should create a separate function that you
pass your array, and the user information to. The user will specify what to add and what position to add
it to. Something like:

void insertInArray(string arr[], const int position, const string newItem)

If the user specifies a position in the array larger than 19 then the program should skip the adding and
report the error to the user. As you insert into the array remember that you must move down any
remaining items (starting at the position specified). Anything past the [19]th position should be deleted
(or ignored).

7) Additionally, we will create an erase function to allow us to delete a set of items in our array. The
erase function should take the array (of course) a position to start erasing, and a number specifying how
many items to erase. If they try to start erasing past the length of the array the program should report
the error and carry out no erasing. If they start at a valid position and try to erase more elements than
are left then the function should just erase until the end of the array without an error. Like the insert
that moves items down as it inserts, the erase function should close the gap by moving all the elements
down to take the place of the lost elements. The erase function should look something like:

void eraseInArray(string arr[], const int startingPosition, const int howMany)

Finally, with the insert and erase functions added the user should create an interface for the user where
they allow the user to either print the array, erase an item, insert an item, or exit. If they insert it should
ask them what to insert and where and of course if they erase it should ask them where and how many.
If you want, you may use an additional function to display the array. When finished with insert or erase
the function should loop back and ask the user what to do next.

10% for the insert function


10% for the erase function
10% for the part2 function
Part 3:
8) Write a function that takes a 2D array as input as well as 2 x,y positions . The function should swap
the values in those two places (assume an array length 0f 8).

const int ARRAY_SIZE = 8;

void swap2D(int[ARRAY_SIZE][ARRAY_SIZE] arr, int x1, int y1, int x2, int y2)

9) This function should take a 2D array and should check each row and each column for any value that is
repeated/matched 3 times consecutively on a row or column. It should also recognize if there are more
than 3 and should consider it all 1 match. The matched values should all be set to zero. Use the same
const size. You do not know what the values could be (in the example below they are 1,2 and 3 but it
could be any value other than zero).

void checkMatches(int[ARRAY_SIZE][ARRAY_SIZE] arr)

example given this array:

11233132
12333111
12331212
31213122
32211111
12321223
32132121
12311233

Becomes this:

01203132
02000000
02301212
31213122
30200000
10321223
30132121
10311233

S-ar putea să vă placă și