Powers of base 2 in C

This program takes a user input from 1 through 512 (inclusive), and calculates all powers of two that are less than or equal to the value. The program prints out all of these values.

NOTE: Due to my usage of the math library’s log function, it is necessary to compile using -lm under Unix systems for the linker to properly define the log function. Information referenced from: http://c-faq.com/fp/libm.html

/* Assignment1: Powers of 2
 * by Hanly De Los Santos
 * Copyright ©2009, Hanly De Los Santos. All Rights Reserved.
 * http://www.hdelossantos.com
 *
 * This program takes a user input from 1
 * through 512 (inclusive), and calculates all powers of
 * two that are less than or equal to the value. The
 * program prints out all of these values.
 *
 * NOTE: Due to my usage of the math library's log function,
 * it is necessary to compile using -lm under Unix systems
 * for the linker to properly define the log function.
 * Information referenced from: http://c-faq.com/fp/libm.html
 */

/* Load standard I/O library */
#include <stdio.h>
#include <math.h>

/* Define macro */
#define ARRAYSIZE 10

/* Declare function prototypes to define the output of the
 * following functions.
 */
void printPowers(int ar[], int firstindex, int lastindex);
void setPowers(int userinteger, int ar[], int lastindex);

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

	/* This determines whether there are 2 arguments in the command
	 * line input. If there are less than 2 arguments, then a message
	 * is printed prompting the user to enter a positive integer value.
	 */
	if( argc < 2)
		printf("Invalid command line. Positive integer value required.\n");

	/* If there are 2 variables, then the second variable is tested
	 * for the required integer values.
	 */
	else{
		int usrval = atoi(argv[1]);

		/* If the user input value is within the accepted range
		 * 1-512, then the operation is done with the user value.
		 */
		if( usrval >= 1 && usrval <= 512 ){
			int maxexp = ((log(usrval))/log(2));
			setPowers( usrval, powers, maxexp);
			printf("Powers of 2 that are less than or equal to %d are:\n", usrval);
			printPowers(powers, 0, maxexp);
		}

		/* If the value is greater than 512, the operation is done with the
		 * maximum value of 512
		 */
		else if ( usrval > 512 ){
			printf("Powers of 2 that are less than or equal to 512 are:\n", usrval);
			setPowers( 512, powers, 9);
			printPowers(powers, 0, 9);
			printf("argc is equal to: %d", argc);
		}

		/* If a character/negative number is entered instead of an integer, the user is
		 * prompted that a positive integer is required to complete the
		 * operation.
		 */
		else if ( !isdigit(usrval) || usrval <= 0 ){
			printf("Invalid command line. Positive integer value required.\n");
			printf("argc is equal to: %d", argc);
		}
	}

	return 0;
}

/* This function sets the array's values; only the array elements
 * whose values are less than or equal to the userinteger are set.
 * It calls function power() for each value.
 */
void setPowers(int userinteger, int ar[], int lastindex){
	int i;
	for( i = 0; i <= lastindex; i++)
		ar[i] = power( 2, i );
}

/*This function calculates and returns baseexponent utilizing the
 * math library function 'pow'.
 */
int power(int base, int exponent){
	int e;

	e = pow(base, exponent);
	return e;
}

/* This function prints the program's output. For each array
 * element in the range of integers defined by firstindex and
 * lastindex, the function prints one line of the output.
 */
void printPowers(int ar[], int firstindex, int lastindex){
	int p;
	for( p = 0; p <= lastindex; p++)
		printf("2^%d = %d\n", p, ar[p] );
}

Leave a Reply