Foundation of Computer Sci Lab
Foundation of Computer Sci Lab COT 3002L
Popular in Course
Popular in OTHER
This 18 page Class Notes was uploaded by Dr. Kristopher Pacocha on Monday October 12, 2015. The Class Notes belongs to COT 3002L at Florida Atlantic University taught by Staff in Fall. Since its upload, it has received 59 views. For similar materials see /class/221655/cot-3002l-florida-atlantic-university in OTHER at Florida Atlantic University.
Reviews for Foundation of Computer Sci Lab
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 10/12/15
Session 9 Student Name Other Identification Arrays and Strings In this laboratory session you will 1 Learn how to declare and manipulate arrays 2 Learn how to declare strings 3 Experiment with the string processing functions strlen strcat strc py and st rcm p Your instructor will tell you which of the proposed experiments you are to perform In preparation for this laboratory session you should read Sections 52 and 71 of Computer Science An Overview 128 Session 9 OneDimensional Arrays A one dimensional homogeneous array is declared in a C program by identifying the type associated with the components of the array followed by the array s name followed by the number of components in the array within brackets For example an array named grades with five components each of type char would be declared by char grades5 and an array named temperature consisting of 24 components of type int would be declared by int temperature24 Later in the program individual components of an array are referenced by the array name followed by the index of the particular component within brackets lndices start at 0 that is the first component has index 0 the second has index 1 and so on Thus the declaration char grades5 declares grades to be an array of length 5 whose entries are referenced by grades0 gradesi grades2 grades3 and grades4 In particular the statements grades0 39B39 gradesi 39C39 assign the characters B and C to the first two components of the array g rades and temperature23 temperature9 temperature12 assigns the sum of the tenth and thirteenth components of temperature to the 24th component The components of a one dimensional array can be initialized at the time the array is declared by following the declaration with an symbol and a list of the Values to be assigned within braces Thus char grades5 39B39 39C39 39A39 39C39 39D39 not only establishes an array named grades but also assigns the Values B C A C and D to the array s components Experiment 9 Step 1 Execute the following program CP09E01 and record the results include ltiostreamhgt void mainvoid int numbersS i num i 0 while i lt 5 cout ltlt quotGive me an integernquot cin gtgt num numbersi num cout ltlt quotThe entry in numbersquot ltlt i ltlt quot is quot cout ltlt numbersi ltlt quotnquot i Session 9 129 Step 2 Change the program from Step 1 so that it uses a for statement instead of a whi Ie statement Summarize your work below Experiment 92 Step 1 Execute the following program CP09E02 and record the results include ltiostreamhgt void mainvoid int i float fatgrams6 fatgrams0 126 fatgramsi 320 fatgrams2 20 fatgrams3 112 fatgrams4 05 fatgrams5 399 for i 0 i lt 6 i cout ltlt fatgramsi ltlt endl 130 Session 9 Step 2 Alter the program in Step 1 so that the elements in the array are displayed in reverse order Summarize your work below Step 3 Alter the program in Step 1 so that the array is initialized when it is declared rather than by a sequence of assignment statements Summarize your work below Step 4 Alter the phrase for i 0 i lt 6 i Session 9 in the program in Step 1 to read for i 0 i lt 7 i Execute the modi ed program and record the results Explain what is wrong Experiment 93 Step 1 Execute the following program CP09E03 and record the results include ltiostreamhgt void fun int x int y int z void mainvoid int i 5 int arrayA3 1 2 3 int arrayB3 8 9 10 cout ltlt quotInitial value of i quot ltlt i ltlt endl cout ltlt quotInitial value of arrayA0 quot ltlt arrayA0 ltlt endl cout ltlt quotInitial value of arrayB0 quot ltlt arrayB0 ltlt endl funi arrayA0 arrayB cout ltlt quotFinal value of i quot ltlt i ltlt endl cout ltlt quotFinal value of arrayA0 quot ltlt arrayA0 ltlt endl cout ltlt quotFinal value of arrayB0 quot ltlt arrayB0 ltlt endl void fun int x int y int z x 20 v21 z022 132 Session 9 Step 2 Note that the parameters passed to the function fun are X the first component of arrayA and the entire array arrayB Assignments are made to each of these parameters within the function Which of these assignments affect the parameter s value in the function main Step 3 State a general rule regarding when assignments made to parameters in a function affect the parameter s value in the environment that called the function Enumerated Data Types To increase program readability C provides for enumerated data types Such types are merely a means by which descriptive names can be assigned an order and used in place of a sequence of integers An enumerated data type is declared by a statement beginning with the keyword enum followed first by an identifier and then by a list of descriptive names enclosed in braces Thus enum weekdays Monday Tuesday Wednesday Thursday Friday establishes an order among the workdays of the week A variable of this type can then be declared by using weekdays as the type within a variable declaration statement Thus weekdays day declares day to be a variable of type weekdays Variables of enumerated data types can be used as array indices For example if hoursworked is declared by the statement float hoursworked5 then the expression hoursworkedTuesday provides a more descriptive reference to the second entry in the array than hoursworked1 Session 9 133 Experiment 94 Step 1 Execute the following program CP09E04 and record the results include ltiostreamhgt enum cointype nicke dime quarter void mainvoid cointype coin int holdings3 3 6 2 int total 0 for coin nickel coin lt quarter coin switch coin case nickel total total 5 holdingscoin break case dime total total 10 holdingscoin break case quarter total total 25 holdingscoin b cout ltlt quotTotal holdings are quot ltlt total ltlt quot centsnquot Step 2 Explain the results obtained in Step 1 134 Session 9 Mu ItiDi mensional Arrays Multi dimensional arrays are declared by merely extending the syntax of one dimensional array declarations to include the length of the array in each of the additional dimensions Thus int x34 declares x to be a two dimensional array of integers with 3 rows and 4 columns whereas char v345 declares y to be a three dimensional array of characters with 3 rows 4 columns and 5 planes Similarly entries in a multi dimensional array are referenced by identifying each index of the entry in question The expression x20 refers to the entry in the third row and first column of the array X Remember indices begin at 0 Entire sections of multi dimensional arrays can also be referenced For example x2 refers to the entire third row of the two dimensional array x Thus if the declaration char ist1020 is interpreted as establishing a list of 10 names each of length no more than 20 then the expression ist4 refers to the fifth name in the list Experiment 95 Step 1 The following program CP09E05 prints the contents of the array blockofint row by row Modify the program so that it prints the array column by column that is the modified program should print the first column of blockofint followed by the second column followed by the third Summarize your work below include ltiostreamhgt void mainvoid int blockofint43123456789101112 for int row 0 row lt 4 row for int column 0 column lt 3 column cout ltlt blockofintrowcolumn ltlt quot quot cout ltlt quotnquot Session 9 135 Step 2 Modify the original program in Step 1 so that you can change the entries in the array blockofi nt from the keyboard Summarize your work below Experiment 96 Step 1 Execute the following program CP09E06 and record the results include ltiostreamhgt void printrowint y3 void mainvoid int board23 i 2 3 4 5 6 for int x 0 x lt 2 x cout ltlt boardxO ltlt quot quot ltlt boardxi cout ltlt quot quot ltlt boardx2 ltlt endl printrowboardi printrowboard0 void printrowint y3 cout ltlt yO ltlt quot quot ltlt yi ltlt quot quot ltlt y2 cout ltlt quotnquot i 36 Session 9 Step 2 Explain the results obtained in Step 1 In particular explain how a multi dimensional array is initialized and how the rows of the array board are passed to the function pri ntrow Strings A string in a C program is expressed as zero or more characters within quotation marks Here are some examples quotMicrosoft Borlandquot a The structure of a string is that of a one dimensional array of type character In C a string is terminated by the end of string marker known as the null character This special character is represented by 0 a backslash and a zero An array containing the string quotCookiequot must have seven components six to store the characters and one more to hold the end of string marker In turn there is an important difference between a character and a string containing only one character The expressions c and 39c do not represent the same thing The string c consists of two elements the character 39c and the end of string marker the character 39c consists of only one element Thus the declaration char name8 39W39 39i39 39I39 39I39 39i39 39a39 39m39 declares the Variable name to be an array consisting of eight components of which only the first seven are initialized whereas char name8 quotWilliamquot declares the Variable name as an array containing the letters of the name William Session 9 137 followed by an end of string marker The standard C library contains several functions for performing string manipulations These include strlen for nding the length of a string strcat for concatenating two strings strcmp for comparing two strings and strcpy for copying a string The prototypes for these functions are contained in the file stri ngh Experiment 97 Step 1 Execute the following program CP09E07 and record the results This program contains an error include ltiostreamhgt void mainvoid int i char tabloid50 quotEnquirerquot char numbersS i 2 3 4 5 for i 0 i lt 8 i cout ltlt tabloidi cout ltlt quotnquot for i 0 i lt 5 i cout ltlt numbersi cout ltlt quotnquot cout ltlt tabloid ltlt endl cout ltlt numbers ltlt endl Step 2 Explain the results obtained in Step 1 Nhat is wrong on the last line 138 Session 9 Experiment 98 Step 1 Execute the following program CP09E08 and record the results include ltiostreamhgt include ltstringhgt void mainvoid char stringA20 quotNutcrackerquot char stringB20 quotSleeping Beautyquot char stringC20 quotSwan Lakequot cout ltlt quotLength of stringA quot ltlt strlenstringA ltlt endl cout ltlt quotLength of stringB quot ltlt strlenstringB ltlt endl cout ltlt quotLength of stringC quot ltlt strlenstringC ltlt endl Step 2 Based on the results from Step 1 does the length of a string as reported by strlen include the end of string marker Explain Session 9 139 Experiment 99 Step 1 Execute the following program CP09E09 and record the results include ltiostreamhgt include ltstringhgt void mainvoid char stringA20 quotNutcrackerquot char stringB20 quotSleeping Beautyquot strcpystringB stringA cout ltlt quotstringA quot ltlt stringA ltlt endl cout ltlt quotstringB quot ltlt stringB ltlt endl cout ltlt stringB8 ltlt stringB9 ltlt stringB10 cout ltlt stringB11 ltlt stringB12 ltlt stringB13 Step 2 What are the actions of the function strc py Step 3 Replace the statement strcpystringB stringA in the program in Step 1 with the simple assignment statement stringB stringA Try to compile and execute the program Then replace the assignment statement with the statement stringB quotSwan Lakequot 140 Session 9 and again try to compile and execute the program What can you conclude about assigning strings with the assignment statement as opposed to the strcpy function Experiment 910 Step 1 Execute the following program CP09E10A and record the results include ltiostreamhgt include ltstringhgt void mainvoid int x y 2 char stringA20 quotNutcrackerquot char stringB20 quotSleeping Beautyquot x strcmpstringA stringB y strcmpstringB stringA z strcmpstringA stringA cout ltlt quotx quot ltlt x ltlt endl cout ltlt quoty quot ltlt y ltlt endl cout ltlt quot2 quot ltlt 2 ltlt endl Step 2 What can you conclude about the actions of the function strcmp Session 9 Step 3 Try to compile and execute the following program CP09E10B What can you conclude about comparing strings with such operators as and lt include ltiostreamhgt include ltstringhgt void mainvoid int x y 2 char stringA20 quotNutcrackerquot char stringB20 quotSleeping Beautyquot if stringA lt stringB WRONG cout ltlt quotStringA is less than or equal to stringBnquot else cout ltlt quotStringA is greater than stringBnquot Step 4 Correct the program in Step 3 using the strcmp function Summarize your work below 142 Session 9 Experiment 911 Step 1 Execute the following programCP09E11 and record the results include ltiostreamhgt include ltstringhgt void mainvoid char stringA30 quotNutcrackerquot char stringB30 quotSleeping Beautyquot strcatstringB stringA cout ltlt quotstringA quot ltlt stringA ltlt endl cout ltlt quotstringB quot ltlt stringB ltlt endl Step 2 What can you conclude about the actions of the function strcat Session 9 143 PostLaboratory Problems 91 92 95 96 97 98 99 910 91 N Write a program that takes a string given by the user and prints it in reverse order Write a program that inputs ten integers from the user and stores them in a one dimensional array Once these numbers are stored find their sum and their average Display your results Write a program that counts the number of times a given letter appears in a given string Write a program that removes all the vowels from a given string But don39t just leave white space in place of the vowels actually push the word together For example the string puppy should become pppy Write your own version of the function strcmp Write a program that retrieves the first row and the first column of a two dimensional table from the user and then fills in the remaining entries to form a multiplication table That is the value in each position should be the product of the value at the top of its column and the value at the left of its row For example 0 4 5 6 l 4 5 6 2 8 i 0 i 2 3 l 2 l 5 l 8 Write a program that computes the matrix product of matrices Write a program that applies the quick sort algorithm see Chapter Four of Computer Science An Overview to sort lists of names Write a program for playing tic tac toe also known as naughts and crosses Write a program that converts an English sentence into Pig Latin Pig Latin is the language obtained by moving the first letter of each word in English to the end of the word and adding the letters a and y For example the word cat would be HatcayH in Pig Latin Write a recursive function that reports whether its parameter a string is a palindrome A palindrome is a string that is the same when read forward as when read backward such as deed madam and abcba Describe the manner in which a list of names is stored by the following program segment Then complete the program so that it sorts the list using the insertion sort algorithm see Chapter Four of Computer Science An Overview include ltiostreamhgt char ist830 void getistvoid int i for i 0 i lt 8 i cout ltlt quotEnter a namenquot cin gtgt isti 144 Session 9 913 Write a program that determines which of two strings precedes the other in alphabetical order It should then report its findings in sentence form For example when given the strings quotdiamondquot and quotgoldquot it should report diamond is less than gold or when given the strings quoti 23quot and quot85quot it should report 123 is less than 85 Explain this latter statement 914 What is wrong with the following program Does it do what you expect include ltiostreamhgt enum ordinals first second third void mainvoid ordinals array3 first second third int x for x 0 x lt 4 x cout ltlt arrayx ltlt endl
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'