代写LAB 3 Arrays and Strings, Relational and Logic operators, Type conversion, Bitwise operations代写Jav
- 首页 >> Database作业LAB 3 Arrays and Strings, Relational and Logic operators, Type conversion, Bitwise operations
Released: July 2nd (Tue) Due: July 9th (Tue), 11:59 pm Total mark: 100pt
Problem 1 scanf, arithmetic and logic operators (20 pts)
Specification
Write an ANSI-C program that reads an integer from standard input, which represents a year, month and day, and then determines how many days has elapsed in the year.
Implementation
• name your program lab3Leap.c
• keep on reading a (4 digit) integer of year, followed by month and day, until a negative year is entered (followed by any month and day).
• define a ‘Boolean’ function int isLeap(int year) which determines if year
represents a leap year. A year is a leap year if the year is divisible by 4 but not by 100, or otherwise, is divisible by 400.
• implement function int countDays(int month, int day, int isLeap)
where month and day represent the current month and day of a year, and isLeap
indicates whether the year is a leap year. The function calculateshow many days have
elapsed since the start of the year, including currentday. There are 31 days in Jan, Mar,
May, July, Aug, Oct and Dec, and there are 30 days in April, June, Sep, and Nov. There are 29 days in Feb if the year is a leap year, and 28 days in Feb if the year is not a leap year.
• call the functions in main, and produce output as shown below. (The two functions do not produce output). Note that if ayear is leap year then the output ends with [leap year].
• put the definition (implementation) of your functions after your main function.
Sample Inputs/Outputs:
red 364 % gcc lab3Leap.c -o leap red 365 % leap
Enter date ('YYYY MM DD'): 2010 1 1
1 days of year 2010 have elapsed
Enter date ('YYYY MM DD'): 2011 8 8
220 days of year 2011 have elapsed
Enter date ('YYYY MM DD'): 2012 8 8
221 days of year 2012 have elapsed [leap year]
Enter date ('YYYY MM DD'): 2400 8 8
221 days of year 2400 have elapsed [leap year]
Enter date ('YYYY MM DD'): 2010 10 1
274 days of year 2010 have elapsed
Enter date ('YYYY MM DD'): 2012 10 1
275 days of year 2012 have elapsed [leap year]
Enter date ('YYYY MM DD'): 2100 11 4
308 days of year 2100 have elapsed
Enter date ('YYYY MM DD'): 2032 11 4
309 days of year 2032 have elapsed [leap year]
Enter date ('YYYY MM DD'): 2031 2 12
43 days of year 2031 have elapsed
Enter date ('YYYY MM DD'): 2032 2 12
43 days of year 2032 have elapsed [leap year]
Enter date ('YYYY MM DD'): -1 5 4
red 366 %
Submit your program oneClass: lab3Leap.c
Problem 2 bits as Boolean flags (20 pts)
Specification
In our lectures, we mentioned that one usefulness of the bitwise operator is to use bits as
Boolean flags. Here is an example. Recall that in lab 2 we have the problem of counting the
occurrence of digits in user inputs. We used an array of 10 integers where each integer element is a counter. Now consider a simplified version of the problem: there is no need to count the
number of occurrences of each digit, instead, we just need to record whether each digit has
appeared in the input or not (no matter how many times they appear). For example, for input
EECS2031, 2019-23FW, LAS0006, we need to record that 0,1,2,3, 6, and 9 appeared in the inputs, but 4,5,7, and 8 did not. One way to do this is to maintain an array of 10 (short) integers, where each integer element is used as a Boolean flag: 0 for False (absent) and 1 for
True (present). Now imagine in the old days when memory was very limited, and thus instead of 10 integers, which takes 160~320 bits, you can only afford to use one integer (16~32 bits) to do the job. Is it possible?
Here the bitwise operations come to the rescue. The idea is that, since we only need a
True/False info for each digit, 1 bit is enough for each digit, so we need only a total of 10 bits to record. Thus, an integer or even a short integer is enough. Specifically, we declare a short int variable named flags, which usually has 16 bits. Then we designate 10 bits in flags as
Boolean flags digits 0~9. For example, we designate the rightmost bit (denoted bit-0) as the
Boolean flag for digit 0, designate the next bit (denoted bit-1) as the Boolean flag for digits 1,
and soon. flags is initially set to 0. Then after reading in the first digit, say, 2, we use bitwise
operation to “turn on” (set to 1) bit-2 of flags. So flags’ bit representation becomes
00000000 00000100. Later when reading in another 2, you can somehow check if bit-2 is on and turns it on if not,or alternatively, simply use the same operation to turn on bit-2 of flags, although bit-2 is already on. After reading all inputs EECS2031A, FW2019-22, LAS1006A, which contains digit 0,1,2,3,6 and 9, the internal representation of flags becomes
0000010 01001111. That is, bit 0,1,2,3,6 and 9 are on. Finally, we can use bitwise operations to examine the lower 10 bits of flags one by one, determining which are 1 and which are 0.
Implementation
Download the partially implemented file lab3flags.c. Similar to lab2C.c, this program keeps on reading inputs using getchar until end of file is entered. It then outputs if each digits is present in the inputs or not.
• Observe that by putting getchar in the loop header, we just need to call getchar once.
(But a parenthesis is needed due to operator precedence which will be covered in lecture 4).
• Complete the loop body, using one of the idioms mentioned on previous page, so that flags is updated properly after reading a digit char.
• Complete the output part, by checking the rightmost 9 bits one by one. Hint: There are at least 2 approaches to check whether a particular bit is 1 or 0. One of the idioms mentioned earlier can do this, and another approach is hinted in the printBinary function.
• For your convenience,a function printBinary() is defined and used to output the binary representation of flags, both before and after user inputs. (This is the same function given in lab3bit.c)
It is interesting to observe that function printBinary () itself uses bitwise operations to generate artificial ‘0’ or ‘1’ , printing the binary representation of an int. It would be
interesting to trace the code. This may help you complete the output part.
Sample inputs/outputs (download input2C.txt from lab2) red 369 % a.out
flags: 00000000 00000000
YorkU LAS C
^D (press Ctrl and D)
flags: 00000000 00000000
0: No
1: No
2: No
3: No
4: No
5: No
6: No
7: No
8: No
9: No
red 370 % a.out
flags: 00000000 00000000
EECS2031M FW2019-22 LAS1006A
^D (press Ctrl and D)
flags: 00000010 01001111
0: Yes
1: Yes
2: Yes
3: Yes
4: No
5: No
6: Yes
7: No
8: No
9: Yes
red 371 % a.out
flags: 00000000 00000000
EECS3421 this is good 3
address 500 yu264067
429Dk
^D (press Ctrl and D)
flags: 00000010 11111111
0: Yes
1: Yes
2: Yes
3: Yes
4: Yes
5: Yes
6: Yes
7: Yes
8: No
9: Yes
red 372 % a.out < input2C.txt
flags: 00000000 00000000
flags: 00000000 11111111
0: Yes
1: Yes
2: Yes
3: Yes
4: Yes
5: Yes
6: Yes
7: Yes
8: No
9: No
red 373 %
Submit your program oneClass: lab3flags.c
Problem 3 Bitwise operation (20 pt)
Specification
A digital image is typically stored in computer by means of its pixel color values, as well as some formatting information. Each pixel color value consists of 3 values of 0 ~ 255, representing red (R), green (G) and blue (B).
As mentioned in class, Java’s BufferedImageclass has a method int getRGB(int x,int y),
which allows you to retrieve the RGB color value of animage at pixel position (x,y). How could the method return 3 values at a time? Instead of returning an int array or an object, the ‘trick’ is to return an integer (32 bits) that packs the 3 values into it. Since each value is 0~255 thus 8 bits are enough to represent it, a 32 bits integer has sufficient bits. They are packed in such away
that, counting from the right most bit, B values occupies the first 8 bits (bit 0~7), G occupies the next 8 bits, and R occupies the next 8 bits. This is shown below. (The left-most 8 bits is packed with some other information about the image, called Alpha, which we are not interested here.)
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
|
|
|
|
|
|
|
|
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
Suppose a pixel has R value 18 (which is binary 00010010), G value 7 (which is binary 00000111) and B value 8 (which is binary 00001000), and Alpha has value 100, then the integer is packed as
0 |
0 |
1 |
0 |
1 |
0 |
1 |
0 |
0 |
0 |
0 |
1 |
0 |
0 |
1 |
0 |
0 |
0 |
0 |
0 |
0 |
1 |
1 |
1 |
0 |
0 |
0 |
0 |
1 |
0 |
0 |
0 |
100 for Alpha 18 7 8
Implementation
In this exercise, you are going to use bitwise operations to pack input R,G and B values into an integer, and then use bitwise operations again to unpack the packed integer to retrieve the R, G and B values.
Download and complete lab3RGB .c. This C program keeps on reading input from the stdin.
Each input contains 3 integers representing the R, G and B value respectively, and then outputs the 3 values with their binary representations (implemented for you). The binary
representations are generated by calling function void printBinary(int val), which is defined for you in another program binaryFunction.c.
Next is the packing part that you should implement. This packs the 3 input values, as well as Alpha value which is assumed to be 100, into integer variable rgb_pack.
Then the value of rgb_pack and its binary representation is displayed (implemented for you).
Next you should unpack the R, G and B value from the packed integer rgb_pack. Note that you should not just use the original R,G and B value, as this is considered a “hoax” .
After that, the unpacked R,G and B value and their Binary, Octal and Hex representations are displayed (implemented for you).
The program terminates when you enter a negative number for either R, G or B value.
Hint: Packing might be a little easier than unpacking. Considering shifting R,G,B values to the
proper positions and then somehow merge them into one integer (using bitwise operators). For unpacking, you can either do shifting + masking, or, masking + shifting, or, shifting only. Shifting + masking means you first shift the useful bits to the proper positions, and then turnoff (set to 0) the unwanted bits while keeping the values of the useful bits. What you want to end up with, for example for R value, is a binary representation of the following, which has decimal value 18.
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
1 |
0 |
0 |
1 |
0 |
Masking + shifting means you first use & to turnoff some unrelated bits and keep the values of the good bits, and then do a shifting to move the useful bits to the proper position. When doing
shifting, the rule of thumb is to avoid right shifting on signed integers. Explore different approaches for unpacking.
Finally, it is interesting to observe that function printBinary () itself uses bitwise operations to generate artificial ‘0’ or ‘1’, printing the binary representation of the int.
Since printBinary () is defined in another file, how to use a function that is defined in another file? Don’tdo include<binaryFunction.c>, instead, declare the function
printBinary () in the main program lab3RGB.c (how?), and then compile the files together, as shown below. (We will talk more about multiple files next week).