For this lab, you will perform the following on each algorithm one at a time:

1. translate the algorithm to C++ and write the C++ using Xcoral
2. Compile your C++ program using g++
3. Test the program on several inputs, convince yourself that it works under different conditions. There may be some inputs that make the program not work properly, bring those to the attention of your professor before you proceed to fixing those. To fix any problem in the program, use Xcoral to edit the program, save the program, and then recompile using g++.

REMEMBER: if you are looking at a compiler error for more than 5 mins or so, then ask for help.

Getting Started

Always set up a directory for each lab or programming assignment to help keep your files organized. All directories for this class will be under the cis121 directory we created in the previous lab.

		         
host% cd
host% cd cis121
host% mkdir lab-translate
host% cd lab-translate

Algorithm #1

Search a list of positive numbers and report each occurence of that number and then compute and print the average. Use the file name of "search_average.cpp" (no quotes). You can type in the following to create this file:
host% xcoral search_average.cpp &
Now here is the algorithm:
    get the values of numbers L1, L2,...LN
    get the value of Search
    set the value of i to 1
    while (i <= N)
       if (Search equals Li)
          print Li
       add 1 to i
    End While
    set average to result of subalgorithm ComputeAverage(L, N)
    if average < 0
       print "divide by 0 error in computing average"
    else
       print "the average is " average
    endif
    Stop

    ComputeAvarege Alg (L, N) 
    set the value of i to 1
    set the value of sum to 0
    While i <= N
          set the value of sum to sum + Li
    End While
    if N greater than 0
        return sum/N
    else
        return -1
    endif
    Stop

Remember to save the program after each change before you compile. Look back at the previous lab for the command to compile programs. If you do not have it with you, then you may get it from the class website (print and keep a copy of that lab with you at all times).

Checking Turnin

In this class programming assignments are submitted using turnin. Now we are going to test it with the previous C++ program. In the same window where you compiled your program do the following:
host%  turnin cis121a testturnin search_average.cpp
if you get an error message then notify your professor.

Algorithm #2

Printing a pattern. Call this file "patterns.cpp" (no quotes).:

   
    get the value of N
    set the value of i = 1
    set the value of j = 0
    While (j < N)
	  set the value of i to 1
	  While (i <= N)
	     print i
	     set the value of i to i + 1
	  End While
	  print endl
	  set the value of j to j + 1
   End While

Printing

Printing your program for submission for lab is easy using the print command: (from the same directory of your programs)

host% print patterns.cpp search_average.cpp

select either printer that is in room 223, and go get your output off of the printer.