Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Lab week 3. 

In this week’s lecture we looked at chars.  Consider the following variable declaration;

int a = 5;

int b = 10;

int c;

char d = ‘d’;

char e = ‘e’;

char f;

Exercise 1.

What would be the result of the following operations (record your predictions on this sheet in the appropriate space :

Operation

Predicted Result

Actual Result

c = a + d

 

 

f = d + e

 

 

c = e – d

 

 

f = b + e

 

 

Once you have made your predictions write a C program to display the actual results and compare the two.  If your predicted answers do not match the actual answers make you understand why and if you are unsure ask.

Exercise 2.

In previous exercises we’ve used getchar() to prevent VS from closing the output window but what the function really does is read in a character from the keyboard.  We can store the character retrieved by placing it in a char variable like so :

char input;

input = getchar();

The char entered by the user is now stored in the variable input.

Write a simple program that reads in 3 characters from the keyboard, and outputs to the screen on one line all three characters and the sum of their ASCII values.

Exercise 3. 

Write a program that use a char array (see lecture notes) to store 5 characters inputted from the keyboard, the program should then output the five characters on one line.

After this the program should prompt the user to enter a number between 1 and 5 and a new character.  The program should replace the old character at that position with the new character entered and output the new set of characters.

EG.

If the user initially inputs a,b,c,d,e

Then enters 3 and z, the program should output a,b,z,d,e.