MODERNIZED PROGRAM

INTRODUCTION TO COMPUTING SYSTEM COURSE

LC3 & C PROGRAMMING LAB

REQUIREMENTS

Present at lab room on time.

Create a subdirectory studentname_code in the D:/Clabs directory and save all your files inside it.

You can create further subdirectory labn , n =1..5 is the lab index.

Try to do all questions step by step.

Show all your results to TA at the end of the lab.

LAB 0:

EDITING, COMPILING, AND SIMULATINGLC3 PROGRAMS

Exercise 1

Write and simulate a program using the LC-3 Assembly language:

-Input 1 lowercase ASCII letter (between ‘a’ and ‘z’)

-Change lowercase ASCII letter to uppercase ASCII letter.

-Output them on screen.

Exercise 2

Write and simulate a program using the LC-3 Assembly language:

-Input 1 characters (between ‘A’ and ‘Z’)and 1 positive number(between 0 and 9).

-Output them on screen.

Exercise 3

Write and simulate a program using the LC-3 Assembly language:

-Input 3 bits A, B and C.

-

-Output the result on screen.

Exercise 4

Write and simulate a program using the LC-3 Assembly language:

-Input 2 unsigned integers(between 0 and 9).

-Compute the summation between these numbers.

-Output the result on screen.

Exercise 5

Write and simulate a program using the LC-3 Assembly language:

-Input 2 unsigned integers(between 0 and 9).

-Multiply two positive numbers.

-Output the result on screen.

Exercise 6

Write and simulate a program using the LC-3 Assembly language:

-Input 2 unsigned integers (between 0 and 99).

-Compute the summation between these numbers.

-Output the result on screen.

Exercise 7

Write and simulate a program using the LC-3 Assembly language:

-Input 2 unsigned integers (between 0 and 99).

-Compare these numbers.

-Output the result on screen.(Ex: “The result is: 54 > 37”)

LAB 1:

EDITING, COMPILING AND RUNNING C PROGRAMS

Exercise 1

Open the sample program intro.cin the C editor window by means of running the executing file: TC.exe.You must do the lab in the DOS OS with the blue screen.

Explain the purpose of this C text.

Remove the closing symbol } from the C text of the program intro.c and compile it again. Explain the result. Now restore the removed symbol and replace identifier main with main_program. Can you compile and run the program now? At what stage do things go wrong and why?

Restore the correct name of the main function. Modify the program so that it works in centimeters rather than in inches. Now make it take the radius value in centimeters but display the result in square inches. One inch is 2.54 centimeters.

Modify the program so that it can do the inverse process, area is the input and radius is the output. You must check all cases of the input (in centimeters).

Exercise2

Compile the program sqrt.c. Note the error message you receive.

This links in the math library in which the sqrt function is defined. How can you link the math library to your program? Run the program and input a few different numbers. What happens when the input number is negative? Try to treat this situation.

Exercise3

Compile and run the following programs. See how much of the program text you understand and check the output result with some inputs.

define.c

overflow.c

sizes.c

anatomy.c

Explain the purpose of these programs.

LAB 2: BASIC OPERATORS AND INPUT/OUTPUT

Exercise1

Compile and run the program floatari.c. Modify it to get results with more decimal digits than you get from the original version.

Modify the program prepost.c to eliminate all operators ++ and -- while keeping the results unchanged. What is the point of having ++ and -- in the language?

Compile and run the program relation.c. Replace b - a == b – cwith a = b – ceverywhere in the program, run it again and explain what happens.

Correct mistakes in the following programs (we call these files the bug files):

  • intarit1.bug.c
  • intarit2.bug.c

Exercise2

Have a look at the program putchar.c. Can you achieve the same result any easier? Try your method and make sure that the output stays the same. Why do we need function putchar at all?

Modify the program puts.c so that it contains only one call to an I/O function instead of 13 separate calls to puts. Your program should still feature the same 13 separate strings, and its output should coincide with the output of the original program in every detail. Now achieve the same result by calling just one I/O function and passing it just one parameter.

Exercise3

Compile and run the program scanf.c. Do you understand the role of function scanf? Can you read two integer numbers by one call to scanf? What about one integer and one real number in one go? Check whether your method actually works.

Find out what happens when you enter a letter while scanf expects a number.

Write a program that reads and displays one integer number. Denote this number with x, so that your program contains the statement

scanf("%i",&x);

Now replace this statement with the following one:

x = scanf("%i",&x);

Does it make any difference? What do you think is the reason? Try reading two numbers at a time:

z = scanf("%i%i",&x,&y);

Check what happens if you enter letters instead of numbers. Can you explain the results?

Exercise4

Have a look at the program getchar.c. Compile and run it. Do you understand its behaviour? Guess what would happen if you entered 3 characters each time you were asked for one. Check whether your guess is correct. Can you imagine any reason for echoing and buffering the characters that come from the keyboard? Will the program behave any differently if the standard input is redirected so that the characters come from a file?

Exercise5

Correct mistakes in the following programs:

printf.bug.c

scanf.bug.c

stdio.bug.c

Exercise6

Write a program that requests three floating point numbers, and prints their sum and product to three decimal places.

LAB 3: CONDITIONALS AND LOOPS

Exercise1

Compile and run the program conditio.c. Modify it to eliminate redundant variables abs and max.

Modify the program ifelse.c to achieve the same effect with a conditional expression instead of the if...else statement. Is it true that you can always replace if...else by a a conditional expression?

Compile and run the program switch.c. Do you understand it? What will be the effect of swapping the the 3 lines that start with case1: with the 3 lines that start with case3:? Check whether your guess is correct. Now swap one of these fragments with the two lines that start with default: and explain the effect caused by this modification.

Correct mistakes in the following programs:

if1.bug.c

if2.bug.c

Exercise2

Have a look at the program while.c. Do you understand what it does? Compile and run the program to see whether it behaves as expected. Replace the while loop with a do...while loop without affecting the program's behaviour. Now use a for loop instead. Which version is the best? Why does C support three different loop statements instead of just one?

Modify the program to perform the same action repeatedly, as long as numbers are entered. How would you make this program to stop once you have started it? Now change the program so that it stops when 0 is entered.

Exercise3

Have a look at the program nestfor.c. Remove the part that prints 200 X's. Does the part that prints out the heading really have to be placed inside the main loop? Can you move it away from that loop correctly?

Modify the program to print out only the lower triangle of the multiplication table: after all, multiplication is a commutative operation!

Compile and run the program continue.c. What is the role of continue in it? Replace one of the continue statements with a break statement and explain how this affects the program's behaviour.

Modify the program to get rid of all the continue statements. Can continue be eliminated from any C loop? Why does the language support this statement?

Exercise4

Correct mistakes in the following programs:

for1.bug.c

for2.bug.c

for3.bug.c

while1.bug.c

while2.bug.c

Try to understand the program bottles.c. Re-write it to make it more comprehensible.

Exercise5

Have a look at the program mcducks.c. Can you predict its behaviour? Compile and run the program.

Modify the program to take orders repeatedly and to calculate the correct change for each order.

Add a facility for cancelling the whole order and starting from scratch.

Add a facility for cancelling an item: use lower-case letters for entering items and upper-case letters for cancelling them. Do not forget to display an error message when there is an attempt to cancel an item that has not been entered (or, say, when an item that has only been requested twice is cancelled three times). Display an itemised receipt for each order.

Exercise6

Write a program to print out all the prime numbers up to 1000. Reminder: prime numbers are integers greater than one with no factors other than themselves and one.

HINT: use the % operator to determine whether one number is a factor of another.

LAB 4: ARRAYS AND POINTERS

Exercise1

Compile and run the program analyse.c. Is the text array essentially needed here? Try to achieve the same results without any array whatsoever. Modify the program so that it outputs the line entered by the user in the reversed order of characters before displaying the statistics. Do you need an array now?

Modify the program array4.c to read the values of matrices A and B at run-time instead of defining them in the program. Suggest a few sensible pairs of sample matrices to test your program. Is it true that A.B and B.A are the same for any A and B?

Now modify the program to deal with 7x7 rather than 3x3 matrices. Use loops to input and output the elements. Can you make the size of the matrices a run-time parameter?

Exercise2

Have a look at the program bubble.c. Do you understand how it works? Compile and run the program.

The number of swaps is not a very accurate measure of performance, as much time is inevitably spent comparing elements that do not require swapping. Modify the program to count the number of iterations of the do...while loop and display this number together with the number of swaps. Now compare these performance characteristics for the following two nearly-sorted input sequences:

10 1 2 3 4 5 6 7 8 9

and

2 3 4 5 6 7 8 9 10 1

Can you explain the difference?

Modify the program to implement the "shaker-sorting": within each iteration of the outer do...while loop perform two swapping loops in opposite directions. The first of those loops moves the "bubbles" upwards while the second one moves the "heavies" downwards. Does this approach enhance the performance? Try sorting the above two sample sequences again.

Exercise3

Compile and run programs pointer1.c and pointer2.c. Do you understand the C code and the results you get? Explain the difference between prefix operators * and . Can **p be a valid C expression? Can &v be a valid expression? What about *&x and &*x?

As you can see from pointer2.c, p[i] and *(p+i) mean the same thing, provided i is an integer and p is a pointer. Bearing in mind that + is a commutative operator, will i[p] have the same effect?

Exercise4

Have a look at the program strings1.c. Can you predict its output? Check whether your guess is correct. Now replace strings "David" and "Hamill" with "Vyacheslav" and "Muchnick", respectively (do not change anything else in the program). Compile and run the program again. Why is the first name displayed in such a strange form now? Why doesn't the same thing happen to the last name?

The program bubble2.c bubble-sorts strings. Modify it to read up to 10 strings of up to 80 characters from the input and to shaker-sort rather than bubble-sort them (see Exercise 2 above).

Swapping strings takes time proportional to their length. It is much more efficient to swap pointers instead, without moving the contents. Modify the program to implement this idea (you will need an additional array of pointers).

Exercise5

Correct mistakes in the following programs:

array.bug.c

pointer.bug.c

string.bug.c

Exercise6

Write a program that reads character strings from the keyboard (standard input) and prints them in reverse.

LAB 5: FUNCTIONS

Exercise1

Compile and run the program funct5.c. Why are the usual break statements missing from the switch statement in function pins? What will happen if you replace return 0 with break? Try running this version of the program with IC type numbers 0 and 5. Can you explain its behaviour?

Now re-write function pins in a form that works according to the original specification but has only one return statement.

Remove identifier tn from the declarationint pins(int tn); in the very beginning of the program and check whether it makes any difference. Now modify the definition of function pins in the same way. Why doesn't it work?

Try replacing the declaration int pins(int tn); with the following alternative versions and explain the effect in each case:

  • void pins(int);
  • int pins(void);
  • int pins();

Exercise2

Have a look at the program funct6.c and try to predict its output. Run the program to see whether you were right. Can you see where the problem is?

What is the role of the two return statements in this program? Will it make any difference if you remove them?

Re-write the program to make it work. Do it in two different ways: by using a function that returns a value and by using a function that actually modifies variable x.

Compile and run the program array5.c (you have to compile it with the -lm option, as it uses the mathematical library module). Will this program still work if you replace [3] with [] in the parameter declarations in functions print_vec and length? Can you make these functions work with 5-vectors instead of 3-vectors? Now define universal versions of these functions that are capable of working with arbitrary N-vectors.

Exercise3

Have a look at the program bisect.c. Do you understand how it works? Try finding a root of some other function.

It is very inconvenient to re-define function f each time when you need a root of a different function. Moreover, you may need to find roots of more than one function in the same program. Therefore, it is a good idea to make the function in question a parameter to function bisect. Try to implement this idea. Bear in mind that functions proper can not be passed as parameters in C, but pointers to functions can.

Exercise4

Compile and run the program static.c. Explain why variables a and b get different values despite their apparently symmetric treatment in the program.

Define a parameterless function that returns string "one" when it is called for the first time, string "two" when it is called for the second time etc until "ten"; all subsequent calls should result in the string "many". Your main program should call this function 15 times and display the results.

Exercise5

Write a function that computes and returns the factorial of a positive integer n, which is the product of all the positive integers from 1 to n. Build the function into a program that reads integers from the keyboard (standard input) and prints their factorial.