2010 Lab-7

Elements of this lab...
   arrays
   functions
   C-strings (character arrays)
   C-string library functions
   file I/O

Write your programs in your /2010/7/ folder on Odin.

The name of each program is given.
Getting started...

We will start the lab together on the big-screen.

Follow along as we learn to read a file.

Then you will continue with the lab.

spellcheck.cpp
Write a program that will spell-check a word entered.

1. Ask the user to enter a word.

2. Search the dictionary file for the word.
   filename: /home/fac/gordon/public_html/2010/dictionary.txt

3. Display a message to the user indicating if the word was spelled correctly.

How to spell-check:

     1. Read each word from the file.
     2. Call the strcmp function to compare words.
        Look up strcmp in your textbook.
        We will review it.
        The strcmp function accepts 2 arguments, each a C-string.

Lab-7 spell check program. Enter a word now: classroom Searching dictionary... Your word is spelled correctly!

Extra credit - show a suggested word... As you are reading the dictionary words, check the first 2 letters of each word against the user's word. If the letters match, save the word in a C-string using strcpy(). If the user's word is not found, then show the saved word as a suggestion of a word to use in place of the user's misspelled word. Word misspelled! May I suggest using this word: _______ <--- show the word there
order.cpp
Write a program that will display numbers in order from low to high.

1. Declare a float or integer array of size 100 or more.
   Ask the user how many numbers to work with (n).
   It can be less than 100.

2. If float array, generate random numbers between 10.0 to 50.0
   Each number should have one decimal-place of accuracy.
   Gordon will remind you how to generate floating-point random numbers.

   If integer array, generate random numbers between 10 and 99.
   Each value is a 2-digit number.

3. Write the random numbers to an output file named random.dat.
   Close the file.

4. Open random.dat for input and read all data values into an array.
   Pass your array to a function named bubble_sort, to sort them.
   Use the bubble-sort we learned on Monday.

5. Write the sorted array to an output file named ordered.dat. 
   Write each value separated by one space. No commas.

sample output...
$ ./order Lab-7 ordered numbers. How many numbers to generate? 19 Generating random values now. Writing to random.dat Reading from random.dat Sorting the numbers. Writing to ordered.dat Program complete $ cat random.dat ordered.dat 49 10 14 37 22 37 38 21 36 19 14 49 10 11 12 33 17 11 40 10 10 11 11 12 14 14 17 19 21 22 33 36 37 37 38 40 49 49
array2d.cpp
2D Array Operations

Write a program that creates a two-dimensional array initialized with
test data. Use any data type you wish. The program should have the following

---------------- New requirements ---------------- 1. Do not make your array a square. Number of rows and columns must be different. Different by at least 2. 2. Do not make your array 4-rows by 5-columns. Think up your own size. I recommend 10x4, or 4x9, or 12x3, or your own creation!
functions: getTotal() This function will have 3 arguments 1. the 2D array 2. the number of rows in the array 3. the number of columns in the array The function will return the total of all the values in the array. getRowTotal() This function will have 4 arguments 1. the 2D array 2. the number of rows in the array 3. the number of columns in the array 4. a row number of the array The function will return the total of the values in the specified row. getColumnTotal() This function will have 4 arguments 1. the 2D array 2. the number of rows in the array 3. the number of columns in the array 4. a column number of the array The function will return the total of the values in the specified column. You may define a small array such as int arr[4][5]; Display your array when the program starts so that you will know if your functions are working properly. Prompt the user for row and column. The user will enter human-oriented row and column numbers, not indexes. Example: If the user wants the first row, they will enter 1, not 0. Give the program simple and clear output. sample output...
Lab-7 2D array. the array 3 6 3 2 8 1 5 4 8 6 2 4 5 8 6 9 8 5 6 7 sum of all array elements: 106 Enter a row number: 1 sum of row is: 22 Enter a column number: 3 sum of column is: 17 Program complete.
secret.cpp
Write a program that reads the same dictionary file as in spellcheck.cpp

Read every word.

If the word has any capital letters...
   1. Display the word followed by 1-space.
   2. Do not print a new-line.

When you finish reading the dictionary file...
   1. Close the file.
   2. Print a few new-lines using endl.


anim2.cpp

Get the working Wednesday animation program.

Copy and paste the code into your 2010/7/anim.cpp file.

Compile and run the program. It will look like this:




Assignment:

1. Copy anim.cpp to anim2.cpp.

2. Add a character that moves vertically.

   Start the character at the bottom of the animation area.
   When the character gets to the top, start it over at the bottom.
   The starting column can be random.
   The column may even be randomized each time the character starts.

   • Wait about 2-seconds before starting the vertical character.
     A call to time(NULL) returns a value that increases by 1 every second.
     Use time(NULL) to time the 2-second delay.

   It will look like this: