Toggle navigation
Home
Ask A Question
Answer Questions
Users
Support/FAQ
Login
Register
333t02.pdf
Details
Question Title:
tut 2
View the Question
Preview - Click download on the question page for the full document.
to enter the command mode (exit insert mode), then type “:wq” and hit enter to save the changes (write) and quit vi. For a more vim commands, see the file “basic_commands_for_vi.pdf” in this tutorial (file is also available in cuLearn) . 1.2 Compile and execute the code Next, compile your “hello.c” source code with the gcc compiler: gcc –Wall hello.c This command instructs gcc to compile the source code file “hello.c” and create an output file with the name a.out. The a.out file is an executable file. Page 1 of 5 Run your program by typing a.out. The program will most likely not execute because it was not found. The OS is searching for the executable file Type ./a.out to run the program. Tips: The OS is using an environment variable called PATH to determine which directories to use. One can add the local directory to the PATH environment variable in order to avoid typing ./ every time one wants to execute a local executable such as a.out. Steps: 1. Show the content of the environment variable PATH. Type from the command line echo $PATH 2. Add the local directory to the PATH environment variable. Type from the command line PATH=$PATH:.; 3. Check that the local path “.” was added. Type from the command line echo $PATH It will show you all the directories that are included when the OS is searching for a program. The file name a.out is a default executable file name that is used by gcc. If you are compiling and executing more than one c file in the same directory the executable a.out will be overwritten. In order to avoid it you can instruct gcc to give the output file a different file name. Compile your “hello.c” again with the following gcc: gcc –Wall –o hello -std=c99 hello.c This command instructs gcc to compile the source code file “hello.c” and output the resulting program (machine code) to file the file “hello” (-o hello).The compiler will enforce the conventions of the C99 standard for the C language (-std=c99) and the compiler will display all the warnings it finds when compiling (-Wall). Recall: If you do not specify the output file name in gcc (by using the –o option), then it will by default use the file name “a.out”. Note, that at first you will compile your programs on the fly with a command line like the one above. Soon you will be introduced to make files (and the make program), which helps to automate the compilation process. However, before automating things, you need to understand what each option in the compilation line above means. 2 Fixing Warning messages Warnings are hints from the compiler that something might not be right. They are not compile-time errors and the compiler will still create an executable machine language program if there are not actual errors (although, you can tell gcc to treat them like errors and not compile your code into machine code if a warning exists). It is always best to fix your code so that you do not have any warnings when you compile. 2.1 Extract the files from the tar file t02.tar (use the command tar –xvf t02.tar). Note that some files will be used in Section 3 below. 2.2 Fix the code in the files 2.2.1 Compile each of the programs tw1.c, tw2.c, t3.c and tw4.c once with the –Wall flag and once without it. Examine the warning and error messages that each compilation option has an output without warning or error messages. 2.2.2 Fix the programs so that no error or warning messages are produced. 2.2.3 Review the code to ensure that you understand what each program does 2.2.4 Execute the programs to ensure correct operation. If the program does not operate correctly, then fix it. 3 Coding a recursive function The purpose of this section is to experiment with recursive functions. In this section of the tutorial you will use the file printTriangles.c. You can see the expected result by executing the file print_triangles_prog . Recursive code consists of three parts: Base Case – this part consists of a set of conditions that terminates the recursive call. Note that the base case may consist of multiple statements. Recursive Step – this part of the recursion determines how the recursive step will be carried out. Action – this part of the recursion determines the side effect that is created by the function. At times the side effect may be combined with the recursive step and at times it may not Note, that the order in which the parts Base Case, Recursive Step and Action appear in a recursive function may be different from functions to function. Computing n! (n factorial) is a classic example of a recursive function. For example, 4!=4*3*2*1, which is 24. Following the parts of the recursion stated above we have the following parts for computing n! Base case – when n is 0 then return 1. Note that 0! == 1 Recursive step – the recursive step computes (n-1)!. This is because if one knows the answer to (n-1)! then all is left to do is to multiply the answer by n in order to determine the answer to n!. Action – The side effect that is created in this part is the multiplication of n by the answer to (n-1)!. Thus the prototype of a function that computes n! is unsigned int factorial(unsigned int num); the pseudo code is as follows: // Base case when n is 0 then return 1; // Action and Recursive steps combined return n * factorial(n-1) Page 3 of 5 3.1 Task 1 In this task you are asked to print a right triangle using ‘*’. The input to the function is the number of rows. For example, if the number of rows is 5 then the output should be * ** *** **** ***** Function prototype (here you can assume that the input is always > 0). void printTriangleRecursive(int numRows); Steps: 1. Identify the three parts (base case, recursive step, and action) 2. Write the pseudo code for each part of the recursion 3. Code the function Hints: Use a for loop or a while loop to print the * in each row using printf function (e.g., printf(“*”);) Use ‘\n’ to skip a line (e.g., printf(“\n”); ) Solution 1. Identify the three parts (base case, recursive step, and action) a. Base case – check if numRows is 0 b. Recursive step – reduce the number of row by one (1) and invoke the recursive call. c. Action – print the stars for the current row 2. Write the pseudo code for each part of the recursion // base case If (numRows == 0) return 0; // recursive call Call the function with numRows-1 // action part Using for loop print numRows * Skip a line 3.2 Task 2 Copy the function printTriangleRecursive () that you just coded and save it as a new function where the function prototype is void printTriangleUpsidedown(int numRows); The new function should print the triangle with the base at the top. For example, if the number of rows is 5 then the output should be ***** **** *** ** * Here, do not rewrite the function code. Instead you need to rearrange the order of some parts of the recursion. Determine which parts you need to reorder. 3.3 Task 3 (optional) This is somewhat more challenging. Here you are required, in addition to printing the triangles, to compute and return the number of ‘*’ that were printed. For example, if numRows is 10 then the parameter numStars should have the value of 55. Note, 1. The number of stars that were printed is returned in the parameter numStars and not as a return value by the function. 2. One cannot assume that numStars was initialized to 0 The function prototype void printTriangleRecursiveCountStars(int numRows, int *numStars); Hints: Set numStars to 0 in the base case part Update numStars during the action part. 3.4 Submission 1. Create a readme file(“README.T02”) that includes: a. a preamble (program author, purpose, list of source files) b. exact compilation command c. launching and operating instructions 2. Create a tar file called “Tutorial02.tar” that has your hello.c, printfTriangles.c and README.T02 files in it. There should be no directories in the tar file. 3. Submit your Tutorial02.tar file to theTutorial2 assignment in cuLearn. End of tutorial section for submission Page 5 of 5 ">