CIT020 Index > C++ Assignment: Arrays and Strings

C++ Assignment: Arrays and Strings

Read everything before doing anything.

Part 1: Arrays

A game that has five levels of play has the score for each level stored in an array. You are to write a program that goes through that array and finds:

The score data you must use for this program are as follows:

Game LevelScore
1450
2316
3148
4775
5200

Your program must use this set of statements to initialize the array:

const int N_LEVELS = 5;
int levelScore[N_LEVELS] = {450, 316, 148, 775, 200};

For this data, your program must produce exactly this output:

The low score is 148 at level 3.
The high score is 775 at level 4.
The average score is 377.8.

Your program must not be tied to this specific data; it has to work with any set of five numbers, no matter how big or small.

Hints

The scores are integers, as is the constant telling how many levels you have. The average, however, can have a decimal part. In order to get the decimal part, the total must be a double, because dividing a double by an int will give a double result.

Remember that array indices begin at zero, not one. The maximum score for this set of data is at game level four, which is at array index three. Make sure you adjust your output properly.


Part 2: Pig Latin Translator

This program will repeatedly ask a user for a single word and translate it into Pig Latin until the user enters the word “quit”.

Here are the rules for Pig Latin:

  1. If a word begins with a vowel, add “way” at the end. Thus, art becomes artway.
  2. If a word begins with the letter “y” or one or more consonants, move them all to the end of the word and add “ay” Thus car becomes arcay, year becomes earyay, and friend becomes iendfray.

These rules will not handle words like “queen” or “quit” properly, but don’t worry about it. You may also presume that the word is entirely in lowercase.

You may find the substr() member function useful, although it is not necessary to solve this problem. The substr() member function extracts a substring, or a part of a string. The function takes two arguments. The first argument is the starting position in a string, and the second argument tells how many characters to extract. Just as with find(), positions start at zero.

string s1="hamburger";
string s2="";

s2 = s1.substr(0, 3);
cout << s2 << endl; // produces the word ham

Notes & Hints

To read a string from the keyboard, you will use the getline( ) function.

string yourName = "";
cout << "What is your name? ";
cin >> yourName; // don't do this!
getline( cin, yourName ); // do this instead.
cout << "Thanks, " << yourName << "!" << endl;

Using << on strings reads only one word at a time; using getline() reads a whole line at a time. In this program, we are only processing one word at a time, but I want you to get in the habit of using the more useful form.

The hardest part of this program is collecting a series of consonants at the beginning of a word. Hint: You can use a while loop that works like this:

while (the first letter is not a vowel, and there are letters left)
{
   put the first letter at the end of a new string;
   erase the first letter of the word;
}

Given the word friend, this would leave the word as iend and the new string as fr.


When You Finish

The program for the first part of this assignment must be in a file named in the form lastname_firstname_array.cpp. The program for the second part of the assignment will be named in the form lastname_firstname_piglatin.cpp. Thus, if your name were Victor Vasarely, your files would be named vasarely_victor_array.cpp and vasarely_victor_piglatin.cpp. If your files are not named properly, they will not be graded.

Email both programs to the instructor. You must put the words CIT020 C++ Assignment: Arrays and Strings in the subject line of your email, or your programs will not be graded.