C++ Arrays

An array is simply a list or a table. You declare an array to hold a list of variables all of one type. An integer array holds a list of int variables, and a character array holds a list of char variables. At the time you declare an array you also determine its length by placing a number in the brackets that follow the array's name. Thus you declare a character array that can contains 20 characters as follows:

	char myword[20];

Each char variable that is contained in this array is called an element. You may access a single element of an array by placing the number of the element inside the brackets. Remember to start counting at zero. Thus you can print the first character like this:

	cout << myword[0];


Print the last character of the array like this:

	cout << myword[19];

Characters can be captured from the keyboard using cin.get(). You need to declare a char variable and place the variable inside the parentheses.

	char letter;
	cin.get(letter); 
	myword[0] = letter;

You may input an entire string into the elements of an array as follows:


	cin >> myword;	//no brackets needed here


Example:

Examine the following code and determine what it does before compiling and running it:

#include 
using namespace std;

int main()
{
char wkday[10];
cout << "\nWhat is the day after Sunday?  ";
cin >> wkday;
cout << "\nYou are a spelling "; //now rearrange wkday[]
	cout << wkday[3];	 //'d' remember to start from 0
	cout << wkday[5];	 //'y'
	cout << wkday[2];	 //'n'
	cout << wkday[4];
	cout << wkday[0];
	cout << wkday[1];
cout << "!" << endl;
cin.get();
cin.get();
return 0;
}

ASSIGNMENT #2


Lesson: Character strings, char Arrays, and cin.get()
Due Date: February 12

1.Write a program to store and print the name of your favorite book in a char array. Initialize it at the time you declare the array. Declare a second char array to contain the last name of the book's author and enter that name later from the keyboard.
2.Modify the above program to print the name of the book's title both forwards and backwards.

Save your source code and executable programs.