Exercises


(1)Write a C program that does the following:

The session should look something like:

Enter an integer:
You entered the integer 34

 

(2)Write a C program that asks the user for an integer input, and then decides if the input is positive, negative or zero.

Here's a sample session:

UNIX prompt >> a.out
Enter an integer: 10
Input is positive

UNIX prompt >> a.out
Enter an integer: -10
Input is negative

UNIX prompt >> a.out
Enter an integer: 0
Input is zero

 


For/While Loops


(3)Write a C program that goes through a FOR loop 3 times, asking the user for an integer input each time through the loop and deciding whether the input was positive, negative, or zero.

Here's what a sample session would look like:

UNIX prompt >> a.out

Enter an integer: 211
Input is positive

Enter an integer: 0
Input is zero

Enter an integer: -12
Input is negative

 

(4)Repeat (3) but this time use a WHILE loop that executes until the input is zero.

 

Sample Output:

UNIX prompt >>a.out

Enter an integer: 1
Input is positive

Enter an integer: 2
Input is positive

Enter an integer: -9
Input is negative

Enter an integer: -0
Input is zero

EXITING...

 


Functions


(5)Consider the following program:

#include void main( ) { int a = 2; printf("Value of 'a' in main( ) before calling MyFunction( ) is %d\n", a); MyFunction(a); printf("Value of 'a' in main( ) after calling MyFunction( ) is %d\n", a); } void MyFunction(int a) { a = 9; printf("Value of 'a' inside MyFunction( ) is %d\n", a); }

The program has the following output:

UNIX prompt >> a.out

Value of 'a' in main( ) before calling MyFunction( ) is 2
Value of 'a' inside MyFunction( ) is 9
Value of 'a' in main( ) after calling MyFunction( ) is 2

Why is the value of 'a' different in MyFunction( ), and unchanged in main( )?

 


Pointers

(6)Consider the following variable declarations:

int *a;

int **b;

int ***c;

Fill in the following table with the appropriate data types:

 

 

(7)Convert the code in (5) to pass by reference. Verify that the value of "a" permanently changes after the function timestwo().

 


Harder Exercises

(8)Write a program in C to implement a simple calculator, which does the following operations:

Add, Subtract, Multiply, Divide, Sin, Cos, Tan.

Note:
Sin, Cos, Tan are defined in the header file math.h
Therefore, in the beginning of the program, just below

#include
you'd also need to include
#include

Compile the program as follows:
gcc –lm calculator.c

 

Sample Outputs:

UNIX prompt >> a.out

VALID OPERATIONS:
(1) Add
(2) Subtract
(3) Multiply
(4) Divide
(5) Sin
(6) Cos
(7) Tan

Select the operation number you wish to perform (1 - 7): 5

Enter the operand: 0.7854
You entered 0.785400
Answer = 0.707108

 

Run it once more

 

UNIX prompt >> a.out

VALID OPERATIONS:
(1) Add
(2) Subtract
(3) Multiply
(4) Divide
(5) Sin
(6) Cos
(7) Tan

Select the operation number you wish to perform (1 - 7): 5

Enter the two operands: 2 4
You entered 2.000000 and 4.000000
Answer = 6.000000

 



(9)Digital data is transmitted over a transmission channel as 8-bit sequences of zero's and one's (known as bytes). The transmitted data may look something like this:

1 0 0 1 0 1 0 1

1 0 1 1 1 0 1 1

0 1 0 0 1 0 0 0

1 1 1 0 0 1 1 1

The first seven digits (bits) of each byte contain the information that is to be transmitted (called the message part of the byte). The last bit of each byte is called the check bit; it's used as a crude check to determine if the data was transmitted correctly, or there was an error during transmission.

error --> a zero transmitted as one, or a one transmitted as a zero.

Here's how the check bit is used to detect transmission errors:
If there is an even number of one's in the message part of the byte, the check_bit should be 0
If there is an odd number of one's in the message part of the byte, the check_bit should be 1

 

Your job:
Implement an error-detector in C.

Your program should add up all the ones in the message part (first seven bits) of an incoming byte, determine if the number of one's is even or odd, and see if the check bit correctly indicates this. Print a message on the screen to let the user know if the transmission was good or had an error.

 

Sample session

UNIX prompt >> a.out
Enter a sequence (length 8) of zero's and one's: 1 0 0 1 0 1 0 1
Transmission successful!

UNIX prompt >> a.out
Enter a sequence (length 8) of zero's and one's: 1 0 1 1 1 0 1 1
Transmission successful!

UNIX prompt >> a.out
Enter a sequence (length 8) of zero's and one's: 0 1 0 0 1 0 0 1
Transmission Error!

UNIX prompt >> a.out
Enter a sequence (length 8) of zero's and one's: 1 1 1 0 0 1 1 0
Transmission Error!

UNIX prompt >> a.out
Enter a sequence (length 8) of zero's and one's: 1 1 1 0 0 1 1 1
Transmission successful!

 


(10)Repeat (9); this time the program should keep asking the user for input (Hint: use a loop) until the user enters all zeros, that is, the input is: 0 0 0 0 0 0 0 0

Would you use a FOR loop or a WHILE loop? Why?

Sample output:

UNIX prompt >> a.out

Enter a sequence (length 8) of zero's and one's: 1 1 1 1 1 1 1 1
Transmission successful!

Enter a sequence (length 8) of zero's and one's: 1 1 0 0 0 0 0 1
Transmission Error!

Enter a sequence (length 8) of zero's and one's: 1 0 1 0 1 0 1 0
Transmission successful!

Enter a sequence (length 8) of zero's and one's: 1 0 0 1 0 0 0 1
Transmission Error!

Enter a sequence (length 8) of zero's and one's: 0 0 0 0 0 0 0 0
Transmission successful!

EXITING...

 


(11) Create a link list by adding Cases at the end of the list. This part should be very similar to LinkListExample1.c
The program should now ask the user for a Case Number, and remove that Case from the link-list.

 


Coming Soon: Even more Exercises & Answers to Exercises