PRACTICE

Practice program 1.

Write a function that shows the binary representation of a number. Use a sample program to iterate from 0 to 8, to show each value in its binary form.

#include <stdio.h>

void
showbin(int num)
{
   /* show the binary values for the "first" 8 bits */

   int shift;
   int mask;

   /* 10101010 */
   /* 76543210 */

   for (shift = 7; shift >= 0; shift--) {
      mask = 1 << shift;

      if (num & mask) {
         putchar('1');
      }
      else {
         putchar('0');
      }
   }

   /* put the last newline */

   putchar('\n');
}

int
main()
{
   int i;

   for (i = 0; i <= 8; i++) {
      printf("%d\n", i);
      showbin(i);
   }

   return 0;
}

Practice program 2.

Write a function that adds two vectors, where each vector has an 𝓍 and 𝓎 component. To add two vectors, simply add the 𝓍 values together, and the 𝓎 values together. That is, vector 2,2 plus vector 1,3 equals vector 4,5. Use a sample program to test this function.

#include <stdio.h>

typedef struct {
   float x;
   float y;
} vector_t;

vector_t
add_vector(vector_t a, vector_t b)
{
   vector_t result;

   result.x = a.x + b.x;
   result.y = a.y + b.y;

   return result;
}

int
main()
{
   vector_t a, b, result;

   a.x = 2.0;
   a.y = 2.0;

   b.x = 1.0;
   b.y = 3.0;

   /* add two vectors */

   result = add_vector(a, b);

   printf("%f,%f plus %f,%f is %f,%f\n", a.x, a.y, b.x, b.y,
          result.x, result.x);

   return 0;
}

Practice program 3.

Write a function that multiplies two complex numbers. In mathematics, a complex number is represented as 𝓍+𝔦𝓎, where 𝔦 is the square root of -1. For two complex numbers 𝓍1+𝔦𝓎1 and 𝓍2+𝔦𝓎2, the product is:

(𝓍1𝓍2 − 𝓎1𝓎2) + 𝔦(𝓍1𝓎2 + 𝓍2𝓎1)
#include <stdio.h>

typedef struct {
   float real;
   float imag;
} complex_t;

complex_t
mult_complex(complex_t a, complex_t b)
{
   complex_t result;

   result.real = (a.real * b.real) - (a.imag * b.imag);
   result.imag = (a.real * b.imag) + (a.imag * b.real);

   return result;
}

int
main()
{
   complex_t a, b, result;

   a.real = 2.0;
   a.imag = 2.0;

   b.real = 1.0;
   b.imag = 3.0;

   /* multiply two complex numbers */

   result = mult_complex(a, b);

   printf("%f+i%f times %f+i%f is %f+i%f\n", a.real, a.imag, b.real, b.imag,
          result.real, result.imag);

   return 0;
}

Practice program 4.

Write a sample program that shuffles a deck of cards. Let's keep this simple, and use a special deck of only ten cards. Each card is just a value from zero to nine (or zero to ninety, if you want to multiply each by ten to get a more obvious number to work with). This is a neat example of how to shuffle a deck of cards through sorting. Hint: use a structure that has a cardvalue and shuffle element, and assign a random number to each shuffle value. Sort on the shuffle.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>                      /* for time() */

typedef struct {
   int cardvalue;
   int shuffle;
} card_t;

int
compare_card(const void *a, const void *b)
{
   card_t card1, card2;

   card1 = *(card_t *) a;
   card2 = *(card_t *) b;

   return (card1.shuffle - card2.shuffle);
}

int
main()
{
   card_t *deck;
   int i;
   time_t seconds;

   /* allocate the deck */

   deck = malloc(10 * sizeof(card_t));

   if (deck == NULL) {
      fprintf(stderr, "could not allocate array\n");
      return 1;
   }

   /* assign values to each card in the deck */
   /* show the deck */

   /*
      time(&seconds);
      srand((unsigned int) seconds);
    */

   srand((unsigned int) time(NULL));

   puts("before shuffling:");

   for (i = 0; i < 10; i++) {
      deck[i].cardvalue = i * 10;
      deck[i].shuffle = (int) random();

      printf("card %d is %d\n", i, deck[i].cardvalue);
   }

   /* shuffle the deck */

   qsort(deck, 10, sizeof(card_t), *compare_card);

   /* show the deck */

   puts("after shuffling:");

   for (i = 0; i < 10; i++) {
      printf("card %d is %d\n", i, deck[i].cardvalue);
   }

   return 0;
}

This also uses the time function, which returns the number of seconds since the system's epoch. The epoch may be different for different systems, so be careful about re-using this number across both DOS and Linux. But usually you'll use it once to get the system's time, and maybe use it again to check how many seconds have elapsed since a prior event. The time_t variable type is just a type definition for a suitably large variable, since the number of seconds is a pretty big number.

You can use the number of seconds as a seed for the srand function. I've commented out another way to use it. The time function will return the number of seconds in the variable that was passed to it using & (to reference the variable's address instead of its value). But if you pass a NULL value, it will just return the number of seconds as the return value.