## CS107 - Lab 5

Due Wednesday 10/13 at the beginning of class.

Overview:

This assignment introduces the elements of C++ programming.

### Compiling and Running a C++ program

Open Xcode by going to Macintosh HD->Developers->Applications->Xcode and open a new project. This will create for you automatically a file named main.cpp. Edit the file and save it when you are ready. To build and run it, click on Build->Build and Run (or press Apple-R). If there are errors, go back to the editor window and fix them, and recompile.

### Problems

1. Write a C++ program that reads from the user a value n and prints an n by n multiplication table. The interface to your program should be as follows:
```Dear user, please enter a number, and I will compute a multiplication table for you: 7

You typed in 7. Here is the multiplication table:

1  2  3  4  5  6  7
2  4  6  8 10 12 17
3  6  9 12 15 18 21
4  8 12 16 20 24 28
5 10 15 20 25 30 35
6 12 18 24 30 36 42
7 14 21 28 35 42 49

Happy? (y/n)
```
If the user answers n the program should print: Try again... If the user answers y the program should print: Great! now you can try the next problem :) .

Note: Be careful with the formatting! It should look exactly as above. Assume the size of the table is always smaller than 10.

#### Arrays

An array is the C++ implementation of a list of variables. An array is declared as follows:
```int a[10];
```
This declares an array variable called a that holds 10 integers. The elements of the array are accessed as a[0], a[1], a[2],...,a[9]. That is, the first element of the array is a[0], the second one is a[1], the last (10th) one is a[9]. Note that in C++ the convention is that the indexing starts at 0, rather than 1.

Each element of an array a[0], a[1], a[2],... is essentially a variable that holds one integer. It can be read into (using cin statements), printed out (using cout statements), assigned values, used in calculations, and so on.

Here is example code for reading the elements of an array from the user, one at a time.

```int a[12]; //this declares an array that can hold 12 integers
int i = 0;

while (i < 12) {
cout << "Enter value " << i << ":";
cin >> a[i];
i = i+1;
}
```

Here is example code for summing up all the elements of an array (which was read from the user):

```int a[12]; //this declares an array that can hold 12 integers
int i = 0;

//here comes the code for reading the array from the user

int sum = 0;
while (i < 12) {
sum = sum + a[i];
i = i+1;
}
cout << "sum is " << sum << endl;
```

2. Write a C++ program that reads an array of 10 integers from the user and a target integer and prints out how many times the target occurs in the input list and at what positions. For instance:
```Enter 10 values:
0: 2
1: 4
2: 3
3: 5
4: 6
5: 7
6: 1
7: 3
8: 2
9: 3

Enter target: 3
Target 3 occurs in the input  at positions 2, 7, 9, in total 3 times.
Good bye!
```

#### Characters (letters)

One of the basic types in C++ is char. A variable of this type can hold a letter. For instance the following piece of code asks the user to print a character and then prints it out.
```chat c;

cout << "Enter a letter: ";
cin >> c;
cout << "You entered: " << c << endl;
```
Generalizing the notion of arrays from above, one can have arrays of chars.
```char name[10]; //this declares an array that can hold 10 characters
int i=0;

cout << "Enter a word of 10 letters, one at a time: ";
while (i<10) {
cin >> name[i];
i = i+1;
}

//count how many times the letter 'a' occurs in the word:
int occ=0;
i=0;
while (i<10) {
if (name[i] == 'a') {
occ = occ+1;
}
i = i+1;
}
cout << "The letter a ccurs in the word " << occ << "times." << endl;
```

3. Write an algorithm that reads a word or phrase of 6 letters from the user and decides whether it is a palindrome or not. A palindrome is a word that reads the same forward and backward, like abba or ala (For examples of more involved palindrome check out this). The interface of your program should be as follows:
```Enter a word/phrase of 6 letters, one at a time:
a
b
b
b
b
a
Thanks. YES, it is a palindrome.
Again? (y/n)y
Enter a word/phrase of 6 letters, one at a time:
f
a
l
l
b
r
Thanks. NO, it is not a palindrome.
Again? (y/n)n
Bye!

```

### What to turn in:

• The 3 programs.

To submit the algorithms first rename (Xcode->File->Rename) the file main.cpp, then drag it to the Desktop, then drag it into the csci107bf04->drop-box. The names that you choose should be your loginname followed by problem number. For instance if I were to submit the file for the first problem i would rename it as ltoma1.cpp.

The drop-box has been set up such that you don't have read access (or you could copy the solutions of your colleagues), only write access. Therefore you will not be able to see whether your file has been submitted or not. Check with me in class to make sure your files got submitted.

If you work in a team submit only one file per team.

Make sure you include your name on the top line of all your .cpp programs.