FALL 2017 CSCI 304 LAB1 (Due on Sep-19, 11:59:59pm) Objectives: Debugger Standard I/O Arithmetic statements Conditional structures Looping structures File I/O Strings Pointers Functions Structures Important data structure related to pointers, Linked List operations REMINDERS: Ø Every lab requires a README file. This file should include the following: o Student name(s) - to avoid the 10% deduction (as explained in the course syllabus) o Effort distribution for each contributor (assumed 100% if you are not working with a partner) o Total amount of time to complete the entire lab o Short description of any concerns, interesting problems or discoveries encountered, or comments in general about the contents of the lab o Directions on how to run the solution(s), as needed Ø You should aim to always hand an assignment in on time. If you are late (even by a minute or heaven forbid, less than a minute late), you will receive 50% of your earned points for the designated grade as long as the assignment is submitted by 11:59pm the following day, based on the due date listed on the syllabus and confirmed by the instructor. If you are more than 24 hours late, you will receive a zero for the assignment and your assignment will not be graded at all. Ø Any lab submitted that does not compile and run WILL RECEIVE AN AUTOMATIC GRADE OF ZERO. No exceptions will be made for this rule - to achieve even a single point on a lab your code must minimally compile and execute without crashing. Ø You are welcome to do more than what is required by the assignment as long as it is clear what you are doing and it does not interfere with the mandatory requirements. Ø You are responsible for making sure that your lab submits correctly. Ø Be sure that you are aware of the Coding Style file information posted on my Day by Day website. GRADING CRITERIA (approximate percentages listed) Ø (20%) The code and algorithm are well commented. A code block should be included in the main program including the programmer name(s) as well as explaining the nature of the problem and an overall method of the solution (what you are doing, not how). A short comment should be included for each logical or syntactic block of statements Ø (20%) The program should be appropriate to the assignment, well- structured and easy to understand without complicated and confusing flow of control. Ø (60%) The results are correct, verifiable, and well- formatted. The program correctly performs as assigned.
LAB DESCRIPTION PART 1 (15%) Debugger Questions. Mandatory file name à lab1p1.txt Type in the following program with the name gdbtest.c Make sure your program compiles and runs correctly. Compile the program using the GDB (the GNU debugger) option: gcc g o gdbtest gdbtest.c If you would like, create a program text file once your program compiles correctly with the line numbers (as shown in class) or use the list command. Run the program in the GDB environment by typing: gdb gdbtest You should see the GDB prompt which looks like this: (gdb) Choose 5 different debugger commands and test them out on your program. Write a small paragraph for each command and specify why you used it and what it did. Order the commands in such a way that someone could follow your directions to walk through them consecutively to learn about GDB. #include <stdio.h> #include <stdlib.h> int main (void) int i = 2; while (i < 256) i = square(i); printf ("%d\n", i); return (EXIT_SUCCESS); int square (int x) int y = x * x; return (x * x); PART 2 (20%). Mandatory filename à lab1p2q1.c lab1p2q2.c Q1: Output Practice (10%): In the introductory program we include the library <stdio.h>, which contains the function printf. This function is used to produce output in the form of characters that are printed in the terminal. In its simplest form, the function is called with a string as argument: printf("hello World!\n"); That is great, but we want our programs to output more than just the fixed strings that the programmer writes in the program. To print the contents of variables, we add format specifiers to the string and add the variables we want to print as arguments: int number; char letter; printf("%d is an integer and %c is a character\n", number, letter); Different types of variables have different specifiers, all starting with a percentage sign. Common specifiers are %d for integers, %f for floats, %c for characters and %s for strings. To output a percentage sign, we use %%. Write a function that outputs: a) The string: One half is 50% b) two integers and their difference. c) two floats and the result of dividing one with the other Write a main function that calls your other functions. The output has to be as follows:
$./lab1p2q1 One half is 50% The difference between 10 and 3 is 7 1.000000 / 3.000000 is 0.333333 $ //Notice this newline Q2: Input Practice (10%): For input we use the function scanf, also from the library <stdio.h>. The scanf function takes a format string followed by references to where the input should be stored. Example that reads an integer to a variable: int number; scanf("%d", &number); Notice that the & character in front of the variable name. It means that the variable is passed as reference to scanf. It allows scanf to update the value of the variable. If & is not there, the program would likely crash at that point. When reading a string, the & sign can be omitted: char my_variable[100]; scanf("%s", my_variable); Write functions that: a) asks for two integers and outputs them and their sum. b) asks for two floats and outputs their product. c) asks for a word and prints it twice on the same row. Write a main function that calls your other functions. The output has to be as follows: $./lab1p2q2 Give two integers: 12 5 You entered 12 and 5, their sum is: 17 Give two floats: 3.14 2 You entered 3.140000 and 2.000000, their product is: 6.280000 Give a word: Yey! Yey! Yey! $ //Notice this newline PART 3. SIMPLE CIPHER (25%) Mandatory filename à lab1p3.c PROBLEM: Ø You have a message that you want to encode. The scheme to encode the message is to reverse each word in the phrase where words are separated by one or more non- alphabetic characters; and keep each non- alphabetic character in their original position. Thus, the output string and the input string should be the same length.
Ø You will need to use the underscore character _ instead of a blank for the input in order to read it in as a single text value. However, when you create your encoded output, be sure to convert the underscore to a blank. Ø In addition, output all alphabetic characters as upper case letters. INPUT/OUTPUT: Ø Input file name: lab1p3in Ø Output file name: lab1p3out Ø Example Input à Output: War_eagle à RAW ELGAE Reading_records_of_variable_length? à GNIDAER SDROCER FO ELBAIRAV HTGNEL? Have_fun._Doing_this_lab_:) à EVAH NUF. GNIOD SIHT BAL :) Words%end*with^non- alpha characters!!! à SDROW%DNE*HTIW^NON- AHPLA SRETCARAHC!!! CONSTRAINTS: The length of the input message to be encoded will not exceed 100 characters, including the new line character (after all, texting has a character limit so why can t we?) Do not use subscripts for this lab; use pointers to manipulate the location of each character in the array(s) PART 4. LINKED LIST (40%) Mandatory filename à lab1p4.c PROBLEM: Write the following structs: Ø Create the structure Node that contains the following members: o data: a char pointer (char*). o next: a pointer to the structure Node. Ø Create the structure Linkedlist that contains the following members: o head: a pointer to structure Node. Write a program that does the following in the main function: Ø Your program should read arguments. Ø Declare a linked list LL. Don t forget to initialize the head of the Linked List. Ø Insert the arguments that start with an uppercase letter at the beginning of the Linked List in the order they arrived in. Use insertatbeginning. Ø Insert the arguments that start with a lowercase letter at the end of the Linked List in the order they arrived in. Use insertattheend. Ø Before inserting, check if the word exist in the Linked List using findnode. if it exist, delete it using deletenode. Ø Print the linked list data using displaylinkedlist. Write the following functions: Ø Write the function insertatbeginning that takes a pointer to structure Linked List LL and a string ele. Allocates a node using malloc or calloc, then inserts it into the beginning of the linked list. As for the data, get the size of ele using strlen, allocate a string, and then copy the string to it using strcpy. o void insertatbeginning(struct Linkedlist* LL, char ele[]);
Ø Write the function insertattheend that takes a pointer to structure Linked List LL and a string ele. Allocates a node using malloc or calloc, then inserts it at the end of the linked list. As for the data, get the size of ele using strlen, allocate a string, and then copy the string to it using strcpy. o void insertattheend(struct Linkedlist* LL, char ele[]); Ø Write the function deletenode that takes a pointer to structure Linked List LL and a string ele. Then removes the node with the sting in its data from the linked list and frees its space. Don t forget to free the string s space too. o void deletenode(struct Linkedlist* LL, char ele[]); Ø Write the function findnode that takes a structure Linked List LL and string ele. Then return 1 if ele exist. Otherwise, return 0. o int findnode(struct Linkedlist LL, char ele[]); Ø Write the function displaylinkedlist that takes a structure Linked List LL. Then prints all the linked list s nodes data separated by commas. o void displaylinkedlist(struct Linkedlist LL); Sample Runs: $./lab1p4 ERROR: The program must read at least an argument. $./lab1p4 my name is Marwan and my car is White The list:- White, Marwan, name, and, car CONSTRAINTS: The input string will be split by space as sample runs. LAB SUBMISSION You should submit all your lab assignments electronically using the submit command. The format of submit command is as follows: ~zpeng/bin/submit cs304 labname files- or- directory- to- submit where, the cs304 is different per course section, labname is the lab you are working on (lab1, lab2, etc.) and files- or- directory- to- submit is a list of the file(s) or directory that make up the lab. The labname for this lab is: Be sure to submit the following files: lab1 lab1p1.txt lab1p2q1.c lab1p2q2.c lab1p3.c lab1p4.c README For example, for this lab, the submit command should be: ~zpeng/bin/submit cs304 lab1 lab1p1.txt lab1p2q1.c lab1p2q2.c lab1p3.c lab1p4.c README And Please Make Sure: all the files are in current directory where you type submit command. NOTE: All of the files in a lab MUST be submitted using one command. If you use two submit commands, the second one erases the files from the first submission. Do not press the enter key in the middle of your submit command line; let it wrap if necessary. Your programs MUST be submitted in source code form. Make sure that you submit the *.c (and *.h files when necessary). Do NOT submit the object files (*.o) and/or the executable. It is YOUR responsibility to make sure your code can compile and run on CS department server me.cs.wm.edu.