
/*Assignment 1
*by Adam Vail
*
*This program takes in a command line argument
*then outputs a list of powers of 2 that are
*less than or equal to the number provided by
*the user.
*/


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

#define ARRAYSIZE 10


/*Prototypes of the functions used in the main method*/

void setPowers(int userinteger, int ar[], int lastindex) ;
int power(int base, int exponent);
void printPowers(int ar[], int firstindex, int lastindex);

/*The main method controls the program. It takes the command
*line argument and makes it into and integer and then calls 
*all three of the prototypes functions to fill and array with
*the appropriate amount of powers of 2 and then outputs those
*numbers based on the number from the user.
*Parameters:
*int argc: the number of white space seperated strings from the 
*		command line
*int *argv[]: an array of pointers that point to the command
*		line entries
*/


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

	int powers[ARRAYSIZE]; /*the array to hold the powers of 2*/
	int userarg;	       /*the user's entry*/
	int lastIndex = 0;     /*the last index of the array that is needed*/
	int k;		       /*counter variable used in the for loop*/

	/*check to make sure only one entry was given on the command line*/
	if( argc != 2){         
		printf("Invalid command line. ");
		printf("Positive integer value required.\n");
		return 0;
	}

	/*convert the command line entry into and integer*/
	userarg = atoi(argv[1]);

	/*make sure that the entry was valid(a positive integer)*/
	if(userarg <= 0){
		printf("Invalid command line. ");
		printf("Positive integer value required.\n");
		return 0;

	}
	
	/*check to see how many positions in the array will be needed*/
	/*to express the powers of 2 equal to or less than the user entry*/
	for(k = 0; k < 10; k++){
		if(power(2,k) <= userarg){
			lastIndex = k;
			
		}
	}

	
	/*if the user number is too big than the highest position needed in*/
	/*is at index 9*/
	if(lastIndex > 9){
		lastIndex = 9;
	}

	/*if the user number is too big than change their number to 512*/
	/*so the there is correct output*/
	if(userarg > 512){
		userarg = 512;
	}

	/*put the powers of 2 into the array*/
	setPowers(userarg, powers, lastIndex);

	printf("Powers of 2 that are less than or equal to %d are\n", userarg);
	
	/*cycle through the array and print out the numbers*/
	printPowers(powers, 0, lastIndex);

	return 0;


}

/*setPowers function places all the correct values into the array.
*It knows how many powers of two that it needs to calculate by
*using the index of the last place in the array that needs to be
*filled with a number.
*Parameters:
*int userinteger: the value that the user gave the program
*int ar[]: the array that holds all the powers of 2
*int lastindex: the last position in the array that should
*		be filled with a number
*/

void setPowers(int userinteger, int ar[], int lastindex){

	int j; /*counter variable*/

	/*cycle through needed array positions storing the*/
	/*powers of 2 in the array*/
	for(j = 0; j <= lastindex; j++){
		ar[j] = power(2,j);
	}

}

/*power is a function that takes a base number and raises
*it to the desired exponent.
*Parameters:
*int base: the number that is to be raised
*int exponent: the power that the base should be raised to
*/


int power(int base, int exponent){
	int result = base;
	int i; /*counter variable*/

	if(exponent == 0){
		return 1;
	}

	/*if an exponent is one return the original number*/
	else if(exponent == 1){
		return base;
	}
	
	/*for all other exponents higher than 1, loop to multiply*/
	/*the base by itself the correct amount of times and return*/
	/*that number*/
	for(i = 0; i < exponent - 1; i++){
		result = result * base;

	}
	return result;
}

/*printPowers is a function that takes the values
*in the array that holds the powers of 2 and prints
*them to the screen along with telling the user
*what power of 2 the outputted number is.
*Parameters:
*int ar[]: the array holding the powers of 2
*int firstindex: where in the array to start outputting numbers
*int lastindex: where in the array the last number that needs to
*		be outputted is
*/

void printPowers(int ar[], int firstindex, int lastindex){
	
	int q;   /*counter variable*/

	/*cycle through the array and print out what power*/
	/*was calculated and then next to it that actual number*/
	for(q = 0; q <= lastindex; q++){
		printf("2^%d = %d\n", q, ar[q]); 
	}

}
