#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#define USAGE(err) printf("USAGE: fibo [number]\nError: %s", err)
// Typedef heap and stack strings
typedef char* string;
// Define functions
int fibo_index(int index);
int fibo(int** output, int limit);
int intLength(int input);
// Main function
int main(int argc, const char** args)
{
// Application expects 2 arguments
if(argc != 2)
{
// Build an error message
const char errorMsg[] = "Got %d arguments";
char error[strlen(errorMsg) - 2 + intLength(argc - 1) + 1];
sprintf(error, errorMsg, argc - 1);
// Output usage
USAGE(error);
}
else
{
int input;
if(sscanf(args[1], "%d", &input))
{
int* range;
if(fibo(&range, input))
{
printf("The fibonacci range:\n");
int index;
for(index = 0; index < input; index++)
{
printf("%d, ", range[index]);
}
free(range);
}
else
{
printf("Couldn't use the input to make a fibonacci range");
}
}
else
{
// Build an error message
const char errorMsg[] = "Input was not a valid number: %s";
char error[strlen(errorMsg) - 2 + strlen(args[1]) + 1];
sprintf(error, errorMsg, args[1]);
// Print error message
USAGE(error);
}
}
printf("\n");
}
// Returns the specified index from the fibonacci range
int fibo_index(int index)
{
}
// Returns the fibonacci range until the specified limit
int fibo(int** output, int limit)
{
// Must be 1 or more
if(limit < 1) return 0;
int* range = (int*)malloc(sizeof(int) * limit);
assert(range);
int i;
// Calculate the range
for(i = 0; i < limit; i++)
{
int value;
if(i == 0) value = 0;
if(i == 1) value = 1;
else value = range[i - 2] + range[i - 1];
range[i] = value;
}
*output = range;
return 1;
}
// Count the digits by counting the number of times dividing by 10 is necessary
int intLength(int input)
{
// 1 digit
if(input < 10) return 1;
else return 1 + intLength(input / 10);
}