Appfs Exercise 1: stream input from stdin.
More...
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <assert.h>
Go to the source code of this file.
|
#define | MAX_NUMS ((INT_MAX >> 5) + 1) |
| Maximum number of 32 bit ints we need to store bits. More...
|
|
|
int | main (void) |
| Program to read binary positive 32 bit integers from stdin, sort the numbers and print them sorted. More...
|
|
- Author
- Thorsten Koch
- Date
- 18Oct2014
gcc -O3 -Wall -std=c99 -o ex1b2 ex1b2.c
Use stdin for input
Definition in file ex1b2.c.
#define MAX_NUMS ((INT_MAX >> 5) + 1) |
Actually, the numbers are not sorted. There is an array with one bit for each possible number. Upon reading the respective bit is set for the number recognized. At the end the array is scanned in order and the numbers present are printed.
Definition at line 25 of file ex1b2.c.
29 static unsigned int have_num[
MAX_NUMS];
36 assert(
sizeof(
int) == 4);
38 memset(have_num, 0,
sizeof(have_num));
42 while(fread(&n,
sizeof(n), 1, stdin))
51 unsigned int msk = 1 << (n & 31);
64 fprintf(stderr,
"Total numbers read = %d\n", total_nums);
68 for(
int k = 0; k < 31; k++)
69 if (have_num[i] & (1 << k))
70 printf(
"%d\n", (i << 5) + k);
#define MAX_NUMS
Maximum number of 32 bit ints we need to store bits.