Department of Engineering

Exercises 1

• ```#include <stdio.h>
#include <stdlib.h>

int odd(int number){
/* return 0 if number is even, otherwise return 1 */
if ( (number/2)*2 == number)
return 0;
else
return 1;
}

int main(){
int i;
i = 7;
printf("odd(%d) = %d\n",i,odd(i));
}
```
• ```#include <stdio.h>
#include <stdlib.h>

void binary(unsigned int number){
/* print decimal `number' in binary */
unsigned int power_of_2;
power_of_2=1;
/* Find the greatest power of 2 which isn't more
than the number
*/
while (power_of_2<= number)
if (power_of_2*2>number)
break;
else
power_of_2=power_of_2*2;

/* Now print out the digits */
while(power_of_2>0){
if( number/power_of_2 == 1){
printf("1");
number = number - power_of_2;
}
else
printf("0");
power_of_2=power_of_2/2;
}
printf("\n");
}

int main(){
unsigned int i;
i=187;
printf("%d in binary is ",i);
binary(i);
}
```
• ```#include <stdio.h>
#include <stdlib.h>
void base(unsigned int number, unsigned int base){
/* Print 'number' to a specified base */
unsigned int power_of_base;
power_of_base=1;
/* Find the greatest power of 'base' which isn't more
than the number
*/
while (power_of_base<= number){
if (power_of_base*base>number)
break;
else
power_of_base=power_of_base*base;
}

/* Now print out the digits */
while(power_of_base>0){
printf("%1d", number/power_of_base);
number = number - power_of_base * (number/power_of_base);
power_of_base=power_of_base/base;
}
printf("\n");
}

int main(){
base(87,2);
base(100,8);
}
```
• ```#include <stdio.h>
#include <stdlib.h>
#define PRIME 1    /* Create aliases for 0 and 1 */
#define NONPRIME 0

int numbers[1000];

void mark_multiples(int num){

/* Set all elements which represent multiples of num to NONPRIME */
int multiple = num *2;
while (multiple < 1000){
numbers[multiple] = NONPRIME;
multiple = multiple + num;
}
}

int get_next_prime(int num){
/* find the next prime number after `num' */
break;
}
}

main(){
int i;
int next_prime;

/* Set all the elements to PRIME.*/
for(i=0;i<1000;i++){
numbers[i] = PRIME;
}

/* 0 and 1 aren't prime, so set numbers[0] and numbers[1] to false */
numbers[0] = NONPRIME;
numbers[1] = NONPRIME;

next_prime = 2;
do{
mark_multiples(next_prime);
next_prime = get_next_prime(next_prime);
}while(next_prime < 1000);

/* Print out the indices of elements which are still set to PRIME */
for(i=0;i<1000;i++)
if (numbers[i] == PRIME)
printf(" %d  ",i);

exit(0);
}
```

Exercises 2

• ```int mystrcmp(const char *s1, const char *s2){
while(*s1++=*s2++)
;
}
```

• ```int ccase;
if (skew >= 0){
ccase = copy_right + function;
}
else{
bptr = bptr + chunk_bytes;
ccase = copy_left + function;
}
```
• ```char *strchr(const char* str, int c)
{
while(*str !='\0'){
if (*str == c)
return str;
else
str++;
}
return NULL;
}
```
• ```#include <stdio.h>
#include <stdlib.h>

char * get_string(char str[])
{
printf("Input a string: ");
return gets(str);
}

main(){
int degrees;
char scale;
int return_value;
char string[1023];
while(1){
printf("Please type in a string like 20C or 15F\n");
printf("Use control-C to quit\n");
get_string(string);
return_value = sscanf(string,"%d%c",&degrees, &scale);
if (return_value != 2){
printf("There's a mistake in your input. Try again.\n");
continue;
}
if (( scale == 'f')|| (scale == 'F'))
printf("%s is %dC\n",string,((degrees-32)*5)/9);
else
if (( scale == 'c')|| (scale == 'C'))
printf("%s is %dF\n",string, (degrees*9)/5+32);
else{
printf("Unable to determine whether you typed C or F\n");
printf("Try again.\n");
}
}
}
```

Exercises 3

• ```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/* This version of the primes program takes an optional
argument from the command line. Because it uses sqrt()
it needs the maths library.
*/

#define PRIME 1     /* Create aliases for 0 and 1 */
#define NONPRIME 0
#define DEFAULT_RANGE 1000
int maxprime;
int *numbers;
int range;

void usage(void){
printf("usage: prime [max]\n");
}

/* Set all elements which represent multiples of num to NONPRIME */
void mark_multiples(int num){
int multiple = num;
while (multiple+num <= range){
multiple = multiple+num;
numbers[multiple]= NONPRIME;
};
}

/* find the next prime in the range after `num' */
int get_next_prime(int num){
break;
}
}

main(int argc, char *argv[]){
int i;
int next_prime;

/* If more than 1 arg has been given , flag an error */
if (argc > 2){
usage();
exit(1);
}

/* If one arg has been given, try to read it as an integer
(sscanf returns the number of successfully scanned items)
*/
if (argc == 2){
if (sscanf(argv[1],"%d",&range) != 1)
range = DEFAULT_RANGE;
}
else
range = DEFAULT_RANGE;

maxprime = sqrt (range);

/* Instead of a fixed size array, malloc some space */
numbers = (int*) malloc( (range+1)* sizeof (int));

/* Set all the elements to PRIME.*/
for (i=0;i<range;i++)
numbers[i]= PRIME;

/* 0 and 1 aren't prime, so set numbers[0] and numbers[1] to false */
numbers[0] = NONPRIME;
numbers[1] = NONPRIME;

next_prime = 2;
do{
mark_multiples(next_prime);
next_prime = get_next_prime(next_prime);
} while(next_prime <= maxprime);

/* Print out the indices of elements which are still set to PRIME */
for (i=0;i<range;i++)
if (numbers[i]== PRIME)
printf("%d\n",i);

exit(0);
}
```
• ```#include <stdio.h>
#include <stdlib.h>
#define NUMBERCOUNT 10 /* the number of numbers */
int main()
{
/* Read 10 numbers from a file called 'data' */
int numbers[NUMBERCOUNT];
FILE *fp;
int i, return_value;
float total;
char line[100];

fp = fopen("data","r");
if (fp == NULL){
fprintf(stderr,"Cannot open 'data' for reading. Bye.\n");
exit(1);
}
/* Read the numbers in */
for(i=0;i<NUMBERCOUNT;i++){
if (fgets(line,100,fp) == NULL){
fprintf(stderr,"End of file reached too early. Bye.\n");
exit(1);
}

return_value = sscanf(line,"%d", numbers+i);
if (return_value !=1){
fprintf(stderr,"Cannot parse line %d of 'data'. Bye.\n",i+1);
exit(1);
}
}
fclose(fp);

/* Now calculate the total */
total=0.0;
for(i=0;i<NUMBERCOUNT;i++){
total=total+numbers[i];
}
printf("The total is %.3f. The average is %.3f\n",
total,total/NUMBERCOUNT);
}
```
• ```#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define UIDCOUNT 10
#define MAXUIDLENGTH 20

main()
{
/* Sort the first 10 uids in the password file */
char uids[UIDCOUNT][MAXUIDLENGTH];
char *cptr;
FILE *fp;
int i, return_value;
float total;
char line[100];

fp = fopen("/etc/passwd","r");
if (fp == NULL){
fprintf(stderr,"Cannot open '/etc/passwd' for reading. Bye.\n");
exit(1);
}

/* Read each of the first ten lines into the 'line' array.
Replace the first ':' by a '\0'. Copy the resulting
truncated string into the uids array
*/
for(i=0;i<UIDCOUNT;i++){
if (fgets(line,100,fp) == NULL){
fprintf(stderr,"End of file reached too early. Bye.\n");
exit(1);
}

cptr = strchr(line,':');
if (cptr == NULL){
fprintf(stderr,"Strange line in '/etc/passwd'. Bye.\n");
exit(1);
}

*cptr = '\0';
strncpy(uids[i],line,MAXUIDLENGTH);
}

/* See the qsort man page for an explanation of the following.
Note that strcmp doesn't precisely match the man page's
requirements, so you may get a warning message on compiling
*/
qsort((void*) uids, (size_t) UIDCOUNT, MAXUIDLENGTH, strcmp);

/* Print the sorted list */
for(i=0;i<UIDCOUNT;i++){
printf("%s\n", uids[i]);
}
}
```
• ```#include <stdio.h>
#include <stdlib.h>
#define TABLE_SIZE 50
#define MAX_STR_LEN 64
#define EMPTY  (-1)

typedef struct _entry {
int value;
struct _entry *next;
char str[20];
} Entry;

char str[MAX_STR_LEN];

/* Create an array of elements of type Entry */
Entry *table[TABLE_SIZE];

int process(char *str){
int value = 1;
while (*str){
value = value * (*str);
str++;
}
return value;
}

char * get_string(char str[])
{
printf("Input a string\n");
return gets(str);
}

int hashfn(char *str){
int total = 0;
int i;
while (i = *str++)
total += i;
}

void set_table_values(void){
/* set all the entries in the table to NULL
*/
int i;
for (i =0;i<TABLE_SIZE;i++)
table[i] = NULL;
}

void set_entry(Entry *entry, char *str){
strcpy(entry->str,str);
entry->value = process(str);
}

Entry *create_an_entry(void){
Entry *entry;
entry = (Entry*) malloc(sizeof (Entry));
return entry;
}

main(){
int bucket;
int value;
set_table_values();

/* Use get_string repeatedly. For each string:-
use the hash function to find the string's entry
in the table.
*/
while(get_string(str)){
if (! strcmp(str,"end")){
printf("Program ended\n");
exit(0);
}

bucket = hashfn(str);
value = find_entry(&(table[bucket]), str);
printf("Value of <%s> is %d\n",str,value);
}
}
```
• © Cambridge University, Engineering Department, Trumpington Street, Cambridge CB2 1PZ, UK (map)
Tel: +44 1223 332600, Fax: +44 1223 332662