Solution - Getting Started with C

Primes

Solution

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

static bool primes[100];

void mark_multiples(unsigned int number)
{
  for(unsigned int i = 2*number; i < 100; i += number)
    primes[i] = false;
}

void compute_primes(void)
{
  for(unsigned int i = 0; i < 100; i++)
    primes[i] = true;

  for(unsigned int i = 2; i < 100; i++)
    mark_multiples(i);
}

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

  unsigned int counter = 0;
  for(unsigned int i = 2; i < 100; i++)
  {
    if (primes[i])
    {
      if ((++counter % 3) == 0)
        printf("%d\n", i);
      else
        printf("%d\t", i);
    }
  }

  printf("\n");

  return EXIT_SUCCESS;
}

Sorting Floats

Finding the smallest element

Solution

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

float data[50] = {18042893.83, 8469308.86, 16816927.77, 17146369.15, 19577477.93, 4242383.35, 7198853.86, 16497604.92, 5965166.49, 11896414.21, 10252023.62, 13504900.27, 7833686.90, 11025200.59, 20448977.63, 19675139.26, 13651805.40, 15403834.26, 3040891.72, 13034557.36, 350052.11, 5215953.68, 2947025.67, 17269564.29, 3364657.82, 8610215.30, 2787228.62, 2336651.23, 21451740.67, 4687031.35, 11015139.29, 18019798.02, 13156340.22, 6357230.58, 13691330.69, 11258981.67, 10599613.93, 20890184.56, 6281750.11, 16564780.42, 11311762.29, 16533773.73, 8594844.21, 19145449.19, 6084137.84, 7568985.37, 17345751.98, 19735943.24, 1497983.15, 20386643.70};

unsigned int minIndex(unsigned int start)
{
  unsigned int result = start;
  for(; start < 50; start++)
  {
    if (data[start] < data[result])
      result = start;
  }

  return result;
}

int main(int argc, char *argv[])
{
  printf("%d %f\n", minIndex(5), data[minIndex(5)]);
  printf("%d %f\n", minIndex(21), data[minIndex(21)]);
  printf("%d %f\n", minIndex(45), data[minIndex(45)]);
  printf("%d %f\n", minIndex(78), data[minIndex(78)]);
  return EXIT_SUCCESS;
}

Sorting the Array

Solution

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

float data[50] = {18042893.83, 8469308.86, 16816927.77, 17146369.15, 19577477.93, 4242383.35, -7198853.86, 16497604.92, 5965166.49, 11896414.21, 10252023.62, 13504900.27, 7833686.90, 11025200.59, 20448977.63, 19675139.26, 13651805.40, 15403834.26, 3040891.72, 13034557.36, 350052.11, 5215953.68, 2947025.67, 17269564.29, 3364657.82, 8610215.30, 2787228.62, 2336651.23, 21451740.67, 4687031.35, 11015139.29, 18019798.02, 13156340.22, 6357230.58, 13691330.69, 11258981.67, 10599613.93, 20890184.56, 6281750.11, 16564780.42, 11311762.29, 16533773.73, 8594844.21, 19145449.19, 6084137.84, 7568985.37, 17345751.98, 19735943.24, 1497983.15, 20386643.70};

unsigned int minIndex(unsigned int start)
{
  unsigned int result = start;
  for(; start < 50; start++)
  {
    if (data[start] < data[result])
      result = start;
  }

  return result;
}

void sort(void)
{
  for(unsigned int start = 0; start < 50; start++)
  {
    unsigned int idx = minIndex(start);
    float tmp = data[start];
    data[start] = data[idx];
    data[idx] = tmp;
  }
}

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

  for(unsigned int i = 0; i < 50; i++)
    printf("%e\n", data[i]);

  return EXIT_SUCCESS;
}

Manipulating Integers

The Sign of Integers

Solution

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

_Bool isNegative(int v)
{
  return (v < 0);
}

int main(int argc, char *argv[])
{
  printf("0: %d\n", isNegative(0));
  printf("-1: %d\n", isNegative(-1));
  printf("-8: %d\n", isNegative(-8));
  printf("1: %d\n", isNegative(1));
  printf("8: %d\n", isNegative(8));
  return EXIT_SUCCESS;
}

The Sign of Integers using Arithmetic

Solution

bool isNegative(int v)
{
  return (v >> 31) & 1;
}

Explanation: signed integers are encoded using two's complement (while this is not specified in the C standards, it is the case on all modern architectures). In two's complement, the most significant bit stores the sign (0: positive, 1: negative). The idea here is to retrieve this bit by right shifting the value so that the new least significant bit is this bit sign (so if the integer is represented with 32 bits, we have to right shift it by 31 bits). The bit-wise and (& 1) ensure that we return only 0 (positive) or 1 (negative).