LAB 2: Character and integer literals, number systems, array and character arrays
- Read pdf posted on course web, if you are not so familiar with the different number bases. It also shows the conversions you should know for this course.
- Visit website cleavebooks.co.uk/scol/calnumba.htm to play with
different bases of integer representations. Enter a valid number in any base and you will see the representations in all the other bases. For this course, we focus on Binary (base 2), Octal (base 8) and Hexadecimal (base 16) representations. The [] beside each base lists the valid symbols for that base. For example, valid symbols for base 8 are [0,1,2,3,4,5,6,7] whereas valid symbols for base 16 are [09,A,a,B,a,C,c,D,d,E,e,F,f]. Recall that for a base X, there are X valid symbols, which are [0,1,,X-1].
Entering 38 in base 8 or 4H in base 16 and observe how the calculator refuses to work for you. Enter 37 in base 8, or 31 in base 10 and calculate it. Convince yourself that results in binary, octal and hexadecimal do have decimal value 31. Note that this calculator does not accept negative input numbers.
- To see how a 32 bits binary representation looks like, and also how negative numbers are represented in binary, please download, compile and run the Java program java. (Recall that you can compile the program in terminal by issuing javac Binary.java, and then run the program by issuing java Binary) The program first demonstrates that for an integer number (stored internally in binary), there are four ways (Decimal, Binary, Hex and Octal) in Java to denote the integer number in code. (In C there are three ways
Decimal, Hex and Octal as there is no Binary literal in standard C.)
This program then converts integer input from stdin, which is assumed to be a decimal integer literal, into its 32 bits binary representation, which is the way integers are stored internally in memory. You can enter negative numbers (especially -1, -2, -3) to examine how negative number is represented using 2s complement. If you know 2s complement, convince yourself with the flip then +1 rule of 2s complement. The program also shows the Oct and Hex values, using printf with specific conversion specifications %d, %o and %x(%X). Enter 2147483647 which is the max value of 32 bits unsigned integer 231-1, observing how it is represented. Stop the program by entering -10000.
- Download, compile and run the C program c. Similar to the Java program above, this program reads in a decimal integer literal, and then outputs the integer value in Decimal, Octal, and Hex, using printf with specific conversion specifications %d, %o and %x(%X) respectively. Observe that sizeof is used to get the int size in your system. Usually you should get 4. Also observe how a while true loop is implemented and how scanf is used to read in the integer from stdin. Stop the program by entering -10000.
Displaying binary representation, however, is not directly supported in printf. In the next lab you will see an enhanced version of this program, which uses bitwise operations to display binary in C. Bitwise operations will be covered soon in class.
No submission for problem 0
1. Problem A Character literals
1.1 Specification
Write an ANSI-C program that reads input from the Standard input, and then outputs the processed information on the Standard output.
Each user input contains an integer, followed by a blank and then a character. If the character does represent a digit, e.g., 3, (how to check?), then the program outputs the sum of the entered integer and the numerical value that this character represents (how to get the numerical value of a digit character such as 3?). If the character does not represent a digit, but represents an alphabet letter, e.g., A, d, then the program outputs that the character is a letter. If the character is not a digit or letter, outputs that the character is others.
The program continues until the input integer is -10000 (and is followed by a blank and any one character).
1.2 Implementation
- name your program c
- keep on reading and processing input, until an integer -10000 is read.
- use scanf (%d %c, ..) to read inputs.
- define a Boolean function isDigit(char c) to determine if c represents a digit. We mentioned in class that ANSI-C does not have a type `boolean, instead ANSI-C uses 0 to represent false, and uses non-zero integer to represent true. So, as a general practice in C, your function should return a non-zero integer number (usually 1) if c is a digit and return
0 otherwise.
Note that you should NOT use library functions here (calling function is slower). Moreover, you should NOT write code like if (c==0 || c==1 || c==|| c==9) .
Instead, use the shorter (one line) idiom discussed in class to examine if c is a digit char.
Also for portability concerns, should NOT use a particular integer number in the idiom;
- Note that in getting the numerical value of a digit character such as 3, you should NOT
use if (c==0)elseif(c==1) elseif (c==..).elseif(c==9) .
Instead, use the one-line idiom that we discussed in class. Also for portability concerns, should NOT use a particular integer number in the idiom;
- define a Boolean function isLetter(char c) to determine if c represents an alphabet letter. Again, should NOT use library functions in this function, and should NOT use 50 if-else statements . Use the one line idiom instead.
- define a Boolean function isOperator(char c) to determine if c represents one of the five arithmetic operators (what are they?). Here probably no idiom can be applied.
- put the definition (implementation) of function isDigit()isLetter()and isOperator after your main function. Call the three functions in main
1.3 Sample Inputs/Outputs: (ONE blank line between each interaction/iteration):
red 338 % gcc lab2A.c -o lab2a
red 339 % lab2a
Enter an integer and a character separated by blank: 12 c
Character c represent a letter
Enter an integer and a character separated by blank: 12 9
Character 9 represents a digit. Sum of 12 and 9 is 21
Enter an integer and a character separated by blank: 100 8
Character 8 represents a digit. Sum of 100 and 8 is 108
Enter an integer and a character separated by blank: 120 !
Character ! represents others
Enter an integer and a character separated by blank: 12 K
Character K represent a letter
Enter an integer and a character separated by blank: 120 +
Character + represents an operator
Enter an integer and a character separated by blank: 154 %
Character + represents an operator
Enter an integer and a character separated by blank: 124 #
Character + represents others
Enter an integer and a character separated by blank: -10000 a red 340 %
Submit your program by issuing submit 2031A lab2 lab2A.c
2. Problem B Character literals
2.1 Specification
Write an ANSI-C program that uses getchar to read from the Standard input, and outputs (duplicates) the characters to the Standard output. For each input character that is a lower-case letter (how to check?), converts it into the corresponding upper case letter in the output (how to convert?). If the input character is a digit, convert it to if it is less than 5, else convert it to + The program continues to read and output until EOF is entered.
2.2 Implementation
You might want to start with the copy.c program presented in textbook (Ch 1) and slides.
Observe that, as mentioned in class, although the copy program calls putchar or printf in every iteration of the loop (after every getchar reading), the output is not displayed in every iteration. Rather, is displayed only after a whole line is read in. This is related to the buffer used by the system. Specifically, instead of executing putchar or printf for every getchar reading , the system buffers (stores) the chars that are read in, and executes putchar or printf only after a new line character
or EOF is read in.
- name your program lab2B.c
- use getchar() and a loop to read characters.
- use putchar() or printf()to print the input characters on the standard output.
- In checking and converting lower case characters, do NOT use any C library functions (e.g, islower(), toupper()). Do your own checking and conversion. Moreover, you should NOT use
if (c==..|| c==.. ||c==.. || c==.. c== Instead, use the shorter
idiom discussed in class. Also for portability concerns, should NOT use integer numbers such as 32.slower
2.3 Sample Inputs/Outputs (from Standard input):
red 308 % gcc Wall lab2B.c red 309 % a.out
You can ignore the warning messages.
Hello The World
HELLO THE WORLD
How Old Are You?
HOW OLD ARE YOU?
I am 27, and my id is yu35X6. THANKs!
I AM -+, AND MY ID IS YU-+X+. THANKS!
^D (press Ctrl and D) red 310 %
2.4 Sample Inputs/Outputs (from redirected input file):
Using your favorite text editor, create a text file my_input.txt that contains hEllo
How Are You!
I Am Good and THAnKs!
I am year 3, my office is Las2015C on 4700 Keele st.
See you later..
red 311 % a.out < my_input.txt
HELLO
HOW ARE YOU!
I AM GOOD AND THANKS!
I AM YEAR -, MY OFFICE IS LAS+C ON -+ KEELE ST.
SEE YOU LATER.. red 312 %
Submit your program by issuing submit 2031A lab2 lab2B.c
3. Problem C Char conversion, Integer Array
3.1 Specification
Write an ANSI-C program that takes input from Standard in, and outputs the occurrence count of digits 0-9 in the inputs.
3.2 Implementation
- name your program c
- use getchar to read from the standard input. After reading a digit character, the program updates the corresponding counter for the digit. The program continues to read until EOF is entered. Then outputs the occurrence count of each digit in the input. Also output the occurrence of other (non-digit) characters in the input.
- do NOT use 10 individual counters for the digit characters. Instead, use an integer array store the counters. That is, maintains an array of 10 counters.
- when a character is read in, if it is a digit character, then how to find the corresponding counter? In class (and slides) we showed a program that uses a bunch of if-else statements,
i.e., if (c==0) else if (c==1) else if (c==..) else Here, do
NOT use this approach, instead, use the shorter idiom discussed in class, which takes advantage of the index of the character to figure out the corresponding counter in the array.
Sample Inputs/Outputs: (download dont copy/paste the input file input2C.txt)
red 368 % gcc -Wall lab2C.c
red 369 % a.out
YorkU LAS EECS
^D (press Ctrl and D)
0: 0
1: 0
2: 0
3: 0
4: 0
5: 0
6: 0
7: 0
8: 0
9: 0 This is the occurrence count of other (non-digit) characters
X: 15 red 370 % a.out EECS2031A FW2019-21
LAS1006A-C
^D (press Ctrl and D)
0: 4
1: 4
2: 3
3: 1
4: 0
5: 0
6: 1
7: 0
8: 0
9: 1 X: 17 red 371 % a.out EECS3421 this is good 3 address 500 yu264076
423Dk
^D (press Ctrl and D)
0: 3
1: 1
2: 3
3: 3
4: 3
5: 1
6: 2
7: 1
8: 0
9: 0 X: 34 red 372 % a.out < input2C.txt
0: 4
1: 7
2: 4
3: 4
4: 5
5: 2
6: 2
7: 5
8: 0
9: 0 X: 211 red 373 % Submit your program by issuing submit 2031A lab2 lab2C.c
4. Problem D Array and Character array (Strings)
Useful information
An array is a fundamental and most important data structure built into C. A thorough understanding of arrays and their use is necessary to develop effective applications. We will continue to learn array in the course.
| h | e | l | l | o |

![[Solved] EECS2031 Lab2-Character and integer literals, number systems, array and character arrays d](https://assignmentchef.com/wp-content/uploads/2022/08/downloadzip.jpg)
