• No results found

EC312 Lab 1 Arrays and Strings

N/A
N/A
Protected

Academic year: 2021

Share "EC312 Lab 1 Arrays and Strings"

Copied!
6
0
0

Loading.... (view fulltext now)

Full text

(1)

EC312 Lab 1 – Arrays and Strings

Part 1: Initial Set-Up

There are three C programs that you will use for this lab placed them in the ec310code directory. The programs you will use today are named sx3a.c, sx3b.c and sx3c.c and these three programs are sitting in the ec310code directory:

We need to copy these files to the work directory. To copy them from the ec310code directory to the work directory,

carefully enter the following at the home directory prompt:

cp ec310code/sx3a.c work

Now carefully enter the following two lines at the home directory prompt

cp ec310code/sx3b.c work cp ec310code/sx3c.c work

You should have copies of sx3a.c , sx3b.c and sx3c.c in your work directory.

Verify that you have sx3a.c , sx3b.c and sx3c.c in your work directory by changing to the work directory and then listing the files in the work directory:

cd work ls

If you do not have sx3a.c , sx3b.c and sx3c.c in in your work directory STOP and ask your instructor or lab tech for help. Otherwise, proceed to Part 2.

Part 2: Fun with Navy

You should now be in the work directory:

(2)

#include<stdio.h> #include<string.h> int main( ) { char school[ 5 ] ; school[0] = ‘N’ ; school[1] = ‘a’ ; school[2] = ‘v’ ; school[3] = ‘y’ ; school[4] = 0 ; printf( "%s\n" , school ); }

Save your program ( Control-o ) and exit nano ( Control-x) and then compile your program using:

gcc –o sx3a.exe sx3a.c

and then run your program:

./sx3a.exe

to confirm it executes as expected.

If your program is not working STOP and ask your instructor for help. Otherwise, proceed to Part 3.

Part 3: Wa?

Now, using nano change the line char school[5] ; to read

char school[10] ; and rerun your program.

Question 1: Did your modification have any effect on your program’s output? Explain why or why not.

Now, we would like to modify our program so that it prints out: Wavy instead of Navy. Change one line of code in your program to accomplish this. Do not use strcpy for this question. Verify your solution works.

Question 2: What change did you make to your program?

Now change the line school[2] = ‘v’ ; to read school[2] = ‘\0’ ;

Note: ‘\0’ is the null character; 0x00 on the ASCII table. This is equivalent to one byte of zeros in binary: 0000 0000.

Question 3: What output is produced? Why is this output produced?

(3)

Now, change the line school[2] = ‘\0’ ;

back to its original form school[2] = ‘v’ ;

change the array declaration line char school[10] ;

to read instead char school[2] ;

So, we are telling C that we intend that our character string only have two characters (and remember, one of them should be the NULL character).

Run the program.

Question 4: What was printed out? How many bytes are needed for the string “Navy”? How many bytes are actually allocated

for the string named school now?

Question 5: Why did the program compile without error? How were you able to assign values to school[2] through school[5]

now that they are out of bounds?

If you feel very confident in your answer to Question 5, go on to Part 4. Otherwise, STOP and ask your instructor for help.

Part 4: Out of Bounds

Examine the program sx3b.c using nano. The C program is shown below: #include <stdio.h> #include <string.h> int main() { int count[3] = {11 , 12 , 13}; int j; for (j = 1 ; j <= 3 ; j = j + 1) {

printf("The next number is %d\n" , count[j] ); }

}

Compile and run the program.

gcc –o sx3b.exe sx3b.c ./sx3b.exe

Question 6: What was printed out? Explain.

Question 7: Without changing the array count, how would you fix the program so that it prints out:

The next number is 11 The next number is 12 The next number is 13

Make this fix to your code and have your instructor verify that your program works properly. After you have Question 7 signed off, proceed to the next part.

(4)

Part 5: Joys of strcpy

Examine the program sx3c.c using nano. The C program is shown below: #include <stdio.h>

#include <string.h> int main()

{

char slogan[16] = "Cyber 2 is fun!" ; printf("\n%s\n\n" , slogan );

}

Compile and run the program.

gcc –o sx3c.exe sx3c.c ./sx3c.exe

Question 8: Fill in the “original memory diagram” on your answer sheet to show how slogan would be stored in memory.

(Don’t forget the Null!) You can just write the character in each box; i.e. no need to use your ASCII table. Carefully add the following two lines to your program above, right before the closing brace.

strcpy( slogan , "EC312" ); printf("%s\n" , slogan );

Compile and run the program.

Question 9: State what was printed out, and fill out the “After strcpy” memory diagram on your answer sheet based on what

should be stored in memory at that location after the strcpy( slogan , "EC312" ); was executed. Your friend is confused. Looking at the length of the two strings:

C y b e r 2 i s f u n ! - - - E C 3 1 2

Question 10: Why didn’t the second item print as EC312 2 is fun!? In other words, what happened to the 2 is

fun! that finished the string Cyber 2 is fun!, since those last few characters were not overwritten? Explain your answer by referring to your memory diagram from question 9.

Finally, in order to see that 2 is fun! still exists in memory, carefully add the following line of code to your program, right before the closing brace: printf(“%s\n\n”, slogan + 6);

Then recompile and run the program on last time.

Question 11: What was printed on your screen? Explain the result, referring to the memory diagram from question 9.

(5)

EC312 Lab 1 Answer Sheet

Name: Question 1: Question 2: Question 3: Question 4: Question 5: Question 6: Question 7: ____________________________ Instructor/ Lab Tech

(6)

Questions 8 and 9:

Question 10:

References

Related documents