CS1101C Lab 2

The deadline for this lab is Friday 12 September 2008, 23:59:59 hours.

For all of these questions, you are not allowed to use the following keywords in your solution: while, do, for. No credit will be given if this rule is violated.

Question 1 (Integer Checking)

The name of your C program file must be called lab2q1.c, files with any other name will not be marked.

Write a simple program that asks the user to enter an integer. If the integer is between 32 to 126 inclusive, it is a valid integer; else it is an invalid integer.

Sample Runs

Assuming that the name of the executable file is lab2q1, sample run(s) of the program are shown below. User input is denoted in bold.

$ ./lab2q1

Enter an integer (32-126): 56

You have entered a valid integer.

$ ./lab2q1

Enter an integer (32-126): 31

You have entered an invalid integer.

$ ./lab2q1

Enter an integer (32-126): 310

You have entered an invalid integer.

$

Question 2 (Characters)

The name of your C program file must be called lab2q2.c, files with any other name will not be marked.

Write a program that asks the user to enter the ASCII value (from 32 to 126 inclusive, display error message for invalid integers) of a character. Display the actual character and determine the results of each of the following functions on the character: isupper, islower, isalpha, isdigit, isalnum, ispunct, isspace. If the function returns true, print the return value.

Assuming that the name of the executable file is lab2q2, sample run(s) of the program are shown below. User input is denoted in bold.

Sample Runs

$ ./lab2q2

Enter an integer (32-126): 127

You have entered an invalid integer.

$ ./lab2q2

Enter an integer (32-126): 68

Character with ASCII value 68 is: "D"
    isupper is true, returns 1
    isalpha is true, returns 1
    isalnum is true, returns 1

$ ./lab2q2

Enter an integer (32-126): 33

Character with ASCII value 33 is: "!"
    ispunct is true, returns 16

$ ./lab2q2

Enter an integer (32-126): 55

Character with ASCII value 55 is: "7"
    isdigit is true, returns 4
    isalnum is true, returns 4

$

Question 3 (Leap Year)

The name of your C program file must be called lab2q3.c, files with any other name will not be marked.

A year that is not divisible by four is not a leap year.
A year that is divisible by four and not divisible by 100 is a leap year.
A year that is divisible by 400 is a leap year.

Write a program that determines if the year entered by the user is a leap year.

Assuming that the name of the executable file is lab2q3, sample run(s) of the program are shown below. User input is denoted in bold.

Sample Runs

$ ./lab2q3

Enter year: 1987

1987 is not a leap year.

$ ./lab2q3

Enter year: 2008

2008 is a leap year.

$ ./lab2q3

Enter year: 2100

2100 is not a leap year.

$ ./lab2q3

Enter year: 2000

2000 is a leap year.

$

Note and Ponder

  1. Assume that all user input is valid unless otherwise stated.

  2. If your program does not work as you expect (logical errors), use extra printf statements to print out all the values of your variables to aid in your debugging.

  3. Most importantly, have lots of fun programming!


This document, index.html, has been accessed 30 times since 25-Jun-24 11:57:13 +08. This is the 2nd time it has been accessed today.

A total of 23 different hosts have accessed this document in the last 444 days; your host, nsrp-source.comp.nus.edu.sg, has accessed it 6 times.

If you're interested, complete statistics for this document are also available, including breakdowns by top-level domain, host name, and date.