If you are interested, read the more complex historical details
Bit Pattern | Integer |
---|---|
0000 | 0 |
0001 | 1 |
0010 | 2 |
0011 | 3 |
0100 | 4 |
0101 | 5 |
0110 | 6 |
0111 | 7 |
1000 | -8 |
1001 | -7 |
1010 | -6 |
1011 | -5 |
1100 | -4 |
1101 | -3 |
1110 | -2 |
1111 | -1 |
File name | Contents | Byte encoding |
---|---|---|
README | text (English) | ASCII |
a.out | machine code | ELF |
banner.jpg | Image | JPEG |
chessboard.bmp | Image | BMP |
main.c | C program | ASCII |
song.mp3 | Sound (music) | MP3 |
ASCII and BMP will be covered in COMP1511.
if
statements? What is the role
of if
statements in programs?
pass_fail.c
that reads in an integer and
prints out "PASS" if the integer is between 50 and 100 inclusive and
fail if it is between 49 and 0, inclusive. It should print out ERROR
if the number is less than 0, more than 100, or if the user does not
enter a number. For example:
./pass_fail Please enter your mark: 42 FAIL ./pass_fail Please enter your mark: 50 PASS ./pass_fail Please enter your mark: 256 ERROR
pass_fail.c
#include <stdio.h>
int main(void) {
int mark;
printf("Please enter your mark: ");
scanf("%d", &mark);
if (mark < 0) {
printf("ERROR\n");
} else if (mark > 100) {
printf("ERROR\n");
} else if (mark >= 50) {
printf("PASS\n");
} else {
printf("FAIL\n");
}
return 0;
}
Another possible solution for pass_fail.c
#include <stdio.h>
int main(void) {
int mark;
printf("Please enter your mark: ");
scanf("%d", &mark);
if (mark < 0 || mark > 100) {
printf("ERROR\n");
} else if (mark < 50) {
printf("FAIL\n");
} else {
printf("PASS\n");
}
return 0;
}
rectangle_area.c
that reads in 2 integers
which are the side-length of a rectangle, and then prints the area of
the rectangle.
For example:
./rectangle_area Please enter rectangle length: 3 Please enter rectangle width: 5 Area = 15 ./rectangle_area Please enter rectangle length: 42 Please enter rectangle width: 42 Area = 1764
rectangle_area.c
// Compute area of a rectangle using ints
// Modified 3/3/2017 by Andrew Taylor (andrewt@unsw.edu.au)
// as a lab example for COMP1511
// Note this program doesn't check whether
// the two scanf calls successfully read a number.
// This is covered later in the course.
#include <stdio.h>
int main(void) {
int length, width, area;
printf("Please enter rectangle length: ");
scanf("%d", &length);
printf("Please enter rectangle width: ");
scanf("%d", &width);
area = length * width;
printf("Area = %d\n", area);
return 0;
}
Your tutor may still choose to cover some of the questions time permitting.
./even_or_odd Please enter a number: 42 EVEN ./even_or_odd Please enter a number: 111 ODD ./even_or_odd Please enter a number: -2 NEGATIVE
even_or_odd.c
#include <stdio.h>
int main(void) {
int x;
printf("Please enter a number: ");
scanf("%d", &x);
if (x < 0) {
printf("NEGATIVE\n");
} else if (x % 2 == 0) {
printf("EVEN\n");
} else {
printf("ODD\n");
}
return 0;
}