User Tools

Site Tools


user:psechris:portfolio:cprogproject5

Project: Exponentiator

A project for CSCS 1320 C/C++ by Paul Sechrist during the Spring 2013.

Objectives

C program must return the result of a first integer argument raised to the power of a second integer argument using only an increment mathematical operation.

Challenge

C program to return the root of two arguments.

Prerequisites

In order to successfully accomplish/perform this project, the listed resources/experiences need to be consulted/achieved:

  • C selection statements
  • C repetition statement

Procedure

I needed to start from the bottom up. I made a counting function, then an addition function using the counting function, then a multiplication using the addition function, then the expo function. I also needed if statements for the special cases such as power of 0 or 1 and base of 0 or 1.

Code

Counter

1
 #include <stdio.h>
 #include <stdlib.h>
 //program that takes an integer and counts 1 increment more
 //n1 stands for number 1
 int main(  )
{
	int n1;
	fscanf(stdin, "%d", &n1);
	n1 = (n1++);
	fprintf(stdout, "%d", n1);
	return(0);
}

Adder

1
 #include <stdio.h>
 #include <stdlib.h>
 
 //program that takes two numbers and adds them together by way of counting
 //n1 = first number, n2 = second number
 //cc1 = counting count 1
 int main(  )
{
 
	int n1;
	int n2;
	int sum;
	int cc1 = 0;
 
	fscanf(stdin, "%d", &n1);
	fscanf(stdin, "%d", &n2);
	sum = n1;
	for(cc1 = 0; cc1 < n2; cc1++)
	{ 
	sum = (sum++);
	}
 
	fprintf(stdout, "%d", sum);
	return(0);
}

Multiplication

1
 #include <stdio.h>
 #include <stdlib.h>
 
 //program that takes two numbers and multiplies them together by way of counting
 //mult1 = first number, mult2 = second number
 //atrack = add tracking number
 //mtrack = multiplier tracking number
 //prod = final answer
 int main(  )
{
 
	int mult1;
	int mult2;
	int prod;
	int atrack = 0;
	int mtrack = 1;
 
	fscanf(stdin, "%d", &mult1);
	fscanf(stdin, "%d", &mult2);
	prod = mult1;
	if (mult1 == 0)
	{
		prod = 0;	
		fprintf(stdout, "%d", prod);
	}
	else if (mult2 == 0)
	{
		prod = 0;
		fprintf(stdout, "%d", prod);
	}
	else if (mult1 == 1)
	{
		prod = mult2;
		fprintf(stdout, "%d", prod);
	}
	else if (mult2 == 1)
	{
		prod = mult1;
		fprintf(stdout, "%d", prod);
	}
	else
	{	
		for(mtrack = 1; mtrack < mult2; mtrack++)
		{ 
			for(atrack = 0; atrack < mult1; atrack++)
			{
				prod = prod++;
			}
		}
		fprintf(stdout, "%d", prod);
	}
 
	return(0);
}

Expo

1
 #include <stdio.h>
 #include <stdlib.h>
 
 //program that takes two numbers and multiplies them together by way of counting
 //base = first number/base number, power = second number/power
 //atrack = add tracking number
 //mtrack = multiplier tracking number
 //etrack = expo track number
 //prod = final product
 //mval is value for mult part of function
 //aval is value for add part of function
 int main(  )
{
 
	int base;
	int power;
	int prod;
	int atrack = 0;
	int mtrack = 1;
	int etrack = 0;
 
	fprintf(stdout, "Enter your base number\n");
	fscanf(stdin, "%d", &base);
	fprintf(stdout, "Enter your power number\n");
	fscanf(stdin, "%d", &power);
	prod = base;
	int mval = base;
	int aval = base;
 
	if (base == 0)
	{
		prod = 0;	
		fprintf(stdout, "Your Answer is: %d", prod);
	}
	else if (power == 0)
	{
		prod = 1;
		fprintf(stdout, "Your Answer is: %d", prod);
	}
	else if (base == 1)
	{
		prod = 1;
		fprintf(stdout, "Your Answer is: %d", prod);
	}
	else if (power == 1)
	{
		prod = base;
		fprintf(stdout, "Your Answer is: %d", prod);
	}
	else
	{	
		for(etrack = 1; etrack < power; etrack++)
		{
			for(mtrack = 1; mtrack < mval; mtrack++)
			{ 
				for(atrack = 0; atrack < aval; atrack++)
				{
					prod = prod++;
				}
			}
		aval = prod;	
		}
		fprintf(stdout, "Your Answer is: %d", prod);
	}
 
	return(0);
}

Execution

G:\CCC\CSCS1320\Joe>expo.exe
Enter your base number
10
Enter your power number
0
Your Answer is: 1
G:\CCC\CSCS1320\Joe>expo.exe
Enter your base number
10
Enter your power number
1
Your Answer is: 10
G:\CCC\CSCS1320\Joe>expo.exe
Enter your base number
10
Enter your power number
2
Your Answer is: 100
G:\CCC\CSCS1320\Joe>expo.exe
Enter your base number
10
Enter your power number
3
Your Answer is: 1000
G:\CCC\CSCS1320\Joe>expo.exe
Enter your base number
10
Enter your power number
4
Your Answer is: 10000
G:\CCC\CSCS1320\Joe>expo.exe
Enter your base number
0
Enter your power number
10
Your Answer is: 0
G:\CCC\CSCS1320\Joe>expo.exe
Enter your base number
1
Enter your power number
10
Your Answer is: 1
G:\CCC\CSCS1320\Joe>

Reflection

This one took me back to the basics of programming, break it down, start small, work big, and go one step at a time.

user/psechris/portfolio/cprogproject5.txt · Last modified: 2013/03/05 00:32 by psechris