Overview:
This assignment exercises the basic elements
of C++ programming.
As you write your programs, remember to pay attention to the following
issues of style:
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.
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;
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!
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;
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!
Send me only the .cpp files in the XCode project. 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 in Lab 5 I would rename it as ltomaL5p1.cpp.
If you work in a team submit only one file per team.