1. (a) data
(b) stack
(c) stack
(d) stack
(e) stack
(f) data
(g) heap
(h) stack
(i) data
(j) stack
# NOTE: Should show work for the following!
2. (a) get(11, 2) = 0
(b) set(1, 3) = 9
(c) clear(23, 3) = 23
(d) toggle(23, 3) = 31
(e) get(set(45, 4) >> 3, 1) = 1
3a:
int sum = 0;
for(int i = 0; i < 10; ++i) sum += a[i];
3b:
char *s = (char *)malloc(sizeof(char) * 5);
3c:
if(c >= 'a' && c <= 'z') printf("uppercase\n");
else if(c >= '0' && c <= '9') printf("digit\n");
else printf("neither\n");
4a:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int *a = NULL, cap = 0, len = 0;
int tmp;
while(scanf("%d", &tmp) == 1) {
if(len >= cap - 1) {
cap = (cap == 0) ? 8 : cap * 2;
a = (int *)realloc(a, cap * sizeof(int));
}
a[len++] = tmp;
}
return 0;
}
4b:
#include <stdio.h>
int main(int argc, char *argv[])
{
unsigned int x;
scanf("%u", &x);
int count = 0;
for(unsigned int m = 1; m; m <<= 1) {
if((m & x)) count += 1;
}
printf("%u\n", 1 << count);
return 0;
}