Home Books by K. N. King Short courses Recommended books Recommended links FAQ

### Chapter 26

2. [was #2]

```void int_printf(const char *format, ...)
{
va_list ap;
const char *p;
int digit, i, power, temp;

va_start(ap, format);

for (p = format; *p != '\0'; p++) {
if (*p != '%') {
putchar(*p);
continue;
}

if (*++p == 'd') {
i = va_arg(ap, int);
if (i < 0) {
i = -i;
putchar('-');
}

temp = i;
power = 1;
while (temp > 9) {
temp /= 10;
power *= 10;
}

do {
digit = i / power;
putchar(digit + '0');
i -= digit * power;
power /= 10;
} while (i > 0);
}
}

va_end(ap);
}
```

7. [was #4] The statement converts the string that `p` points to into a long integer, storing the result in `value`. `p` is left pointing to the first character not included in the conversion. The base used for the conversion is 10.

9. [was #6]

```double rand_double(void)
{
return (double) rand() / (RAND_MAX + 1);
}
```

#### Answers to Selected Programming Projects

1. [was #8]

(a)

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

int main(void)
{
int count = 1000;

while (count-- > 0)
printf("%d", rand() & 1);
printf("\n");
return 0;
}
```

(b) For generating numbers in the range 0 to `N` `-` `1`, the formula `rand()` `/` `(RAND_MAX` `/` `N` `+` `1)` often gives better results than `rand()` `%` `N`. For example, if `N` is 2 and `RAND_MAX` is 32767, the formula works out to `rand()` `/` 16384, which yields 0 if the return value of `rand` is less than 16384 and 1 if it's greater than or equal to 16384.

3. [was #10; modified]

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

#define N 1000

int compare_ints(const void *p, const void *q);

int main(void)
{
int a[N], i;
clock_t start_clock;

for (i = 0; i < N; i++)
a[i] = N - i;

start_clock = clock();
qsort(a, N, sizeof(a[0]), compare_ints);

printf("Time used to sort %d integers: %g sec.\n", N,
(clock() - start_clock) / (double) CLOCKS_PER_SEC);

return 0;
}

int compare_ints(const void *p, const void *q)
{
return *(int *)p - *(int *)q;
}
```

4. [was #12]

```#include <stdio.h>
#include <time.h>

int main(void)
{
struct tm t;
int n;

/* initialize unused members */
t.tm_sec = t.tm_min = t.tm_hour = 0;
t.tm_isdst = -1;

printf("Enter month (1-12): ");
scanf("%d", &t.tm_mon);
t.tm_mon--;

printf("Enter day (1-31): ");
scanf("%d", &t.tm_mday);

printf("Enter year: ");
scanf("%d", &t.tm_year);
t.tm_year -= 1900;

printf("Enter number of days in future: ");
scanf("%d", &n);

t.tm_mday += n;
mktime(&t);
printf("\nFuture date: %d/%d/%d\n",
t.tm_mon + 1, t.tm_mday, t.tm_year + 1900);

return 0;
}
```

6. [was #14]

(a)

```#include <stdio.h>
#include <time.h>

int main(void)
{
time_t current = time(NULL);
struct tm *ptr;
char date_time[37];

ptr = localtime(&current);
strftime(date_time, sizeof(date_time), "%A, %B %d, %Y  %I:%M", ptr);
printf("%s%c\n", date_time, ptr->tm_hour <= 11 ? 'a' : 'p');

return 0;
}
```

(b)

```#include <stdio.h>
#include <time.h>

int main(void)
{
time_t current = time(NULL);
char date_time[22];

strftime(date_time, sizeof(date_time), "%a, %d %b %y  %H:%M",
localtime(&current));
puts(date_time);

return 0;
}
```

(c)

```#include <stdio.h>
#include <time.h>

int main(void)
{
time_t current = time(NULL);
struct tm *ptr;
char date[9], time[12];

ptr = localtime(&current);
strftime(date, sizeof(date), "%m/%d/%y", ptr);
strftime(time, sizeof(time), "%I:%M:%S %p", ptr);

/* print date and time, suppressing leading zero in hours */
printf("%s  %s\n", date, time[0] == '0' ? &time[1] : time);

return 0;
}
```