Lab 6 - Working With Arrays

Start with lab6a & lab6b

Write two or more of the programs starting at lab61.cpp
Do as many as you can.
Finishing one program perfectly should be your first goal.
Then, try more as you have time. Each program is given a name indicated.
Do your work in your /2010/6 folder on Odin.

Steps in completing this assignment...
1. Write a program for each of the options below.
2. Each program will use one or more arrays.
3. Functions are optional.
4. Output should look just like the sample given.

In all of your programs, your main function header should look like this:

   int main(int argc, char *argv[])

Near the top of the main function, enter this code...

   if (argc > 1)
       srand(atoi(argv[1]));
   else
       srand(time(NULL));

--- practice program 1 ---

Run /2010/lab-start.sh

lab6a.cpp

Declare an array that will hold 10 integers.
Use a for-loop to fill the array with random numbers from 1 to 80.
Display the array values forward.
Display the array values in reverse order.

Hint: use two different for-loops.

Sample output:
lab6a - 10 random numbers

array forward: 43 35 58 66 55 80 4 13 69 46 
array reverse: 46 69 13 4 80 55 66 58 35 43 

done.

--- practice program 2 ---

lab6b.cpp

Declare a character array that will hold 100 characters.
Prompt the user to enter their name.
Do a cin into the array.
Display their name downwards. ↓

Hints:
1. Use a while loop.
2. Step through the array elements.
3. Display the character you find, followed by endl.
4. Stop when you find this character: '\0'
    (backslash zero inside single-quotes.)

Sample output:
lab6b - your name

please enter your name here: Gordon

G
o
r
d
o
n 

program complete.


lab61.cpp

Write a program that declares an integer array with size 10.
Fill the array with random numbers from 0 to 20.
Display the array elements in forward and reverse order, side-by-side.

Sample output:
lab61 - forward & reverse

 3  18
11  10
 2  16
12   4
15  10
10  15
 4  12
16   2
10  11
18   3

lab62.cpp

Write a program that declares an integer array with size 10 or more.
Fill the array with random numbers from 0 to 30.
All array values must be unique.
No duplicate values are allowed in the array.

1. Keep track of how many good values are in the array.
   There will be zero to start.

2. Use a for-loop to read through the array and test for duplicates.
   If no duplicates found, add the new value to the array.
Sample output:
lab62 - unique numbers only

9 8 3 0 23 15 26 18 6 30

lab63.cpp

Write a program that declares an integer array with size of at least 20.
Fill the array with random numbers from 0 to 100.
Indicate all duplicate values when displaying the array.

Sample output:
lab63 - show duplicates

17 
65  <---duplicate
99  <---duplicate
37 
0 
36 
44 
5 
25 
99  <---duplicate
78 
10 
69 
77 
32 
20 
65  <---duplicate
13 
12 
31 

lab64.cpp

Write a program that prompts the user to enter a sentence.
Store the sentence in one character array.
Step through the sentence characters using a loop.
Display the words individually.

Sample output:
lab64 - parse a sentence

Enter a sentence: Please buy Gordon a new laptop.

word 1: Please
word 2: buy
word 3: Gordon
word 4: a
word 5: new
word 6: laptop.
Hints:
Display each character individually.
When you find a space, go to a new line.
Stop when you find a NULL character in the sentence.
A break statement is not allowed.

A space character is: ' ' or the value 32.
A NULL character is: '\0' or the value 0.

To input a whole sentence, you will need to use cin.getline().
We did this in class together.
Or, look it up in our textbook.


lab65.cpp

Write a program that declares an integer array of size 10.
Fill the array elements with 10 random integers between 0 and 100.
Add all the array elements together.
If the total is not 100, fill the array again.
Continue trying until the total is exactly 100.

Sample output:
lab65 - array sums to 100

  9    9
 12   21
  3   24
 22   46
  5   51
  9   60
  0   60
  0   60
 13   73
 27  100

number of tries: 670375
First column is array values.
Second column is running total.
Keep track of how many tries it takes.

lab66.cpp

Write a program that declares an integer array with size 10.
Fill the array with random numbers from 0 to 20.
Display the array elements like in the sample.
At the bottom, display the 2 smallest values in the array.

Sample output:
lab66 - two smallest

 8
12
19
15
17
12
 1
14
19
 9

 1 8  <--- 2 smallest values

lab67.cpp

Write a function that declares an integer array of size 100.
Fill the array with random numbers from 10 to 20.
Display the array values in ascending sequence.
One space between numbers. Do not let any number wrap around on an 80-character terminal window.

Note:
Do not use an algorithm you find online. We will learn to sort in class.

Sample output:
$ ./a.out 

lab67 - sorted order

10 10 10 10 10 10 10 10 10 10 10 10 11 11 11 11 11 11 11 11 11 11 11 12 12 12 
12 12 12 12 12 12 12 12 12 12 13 13 13 13 13 13 13 14 14 14 14 14 14 14 14 14 
14 14 14 15 15 15 15 16 16 16 16 16 16 16 16 17 17 17 17 18 18 18 18 18 18 18 
18 19 19 19 19 19 19 19 19 19 20 20 20 20 20 20 20 20 20 20 20 20 

Sort complete.