This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
user:psechris:portfolio:cprogproject5 [2013/02/15 15:05] – created psechris | user:psechris:portfolio:cprogproject5 [2013/03/05 05:32] (current) – [Expo] psechris | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ======Project: | ||
+ | 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/ | ||
+ | |||
+ | * 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 ==== | ||
+ | |||
+ | <code c 1> | ||
+ | # | ||
+ | # | ||
+ | // | ||
+ | //n1 stands for number 1 | ||
+ | int main( ) | ||
+ | { | ||
+ | int n1; | ||
+ | fscanf(stdin, | ||
+ | n1 = (n1++); | ||
+ | fprintf(stdout, | ||
+ | return(0); | ||
+ | } | ||
+ | </ | ||
+ | ==== Adder ==== | ||
+ | |||
+ | <code c 1> | ||
+ | # | ||
+ | # | ||
+ | |||
+ | // | ||
+ | //n1 = first number, n2 = second number | ||
+ | //cc1 = counting count 1 | ||
+ | int main( ) | ||
+ | { | ||
+ | |||
+ | int n1; | ||
+ | int n2; | ||
+ | int sum; | ||
+ | int cc1 = 0; | ||
+ | |||
+ | fscanf(stdin, | ||
+ | fscanf(stdin, | ||
+ | sum = n1; | ||
+ | for(cc1 = 0; cc1 < n2; cc1++) | ||
+ | { | ||
+ | sum = (sum++); | ||
+ | } | ||
+ | |||
+ | fprintf(stdout, | ||
+ | return(0); | ||
+ | } | ||
+ | </ | ||
+ | ==== Multiplication ==== | ||
+ | |||
+ | <code c 1> | ||
+ | # | ||
+ | # | ||
+ | |||
+ | // | ||
+ | // | ||
+ | // | ||
+ | // | ||
+ | // | ||
+ | int main( ) | ||
+ | { | ||
+ | |||
+ | int mult1; | ||
+ | int mult2; | ||
+ | int prod; | ||
+ | int atrack = 0; | ||
+ | int mtrack = 1; | ||
+ | |||
+ | fscanf(stdin, | ||
+ | fscanf(stdin, | ||
+ | prod = mult1; | ||
+ | if (mult1 == 0) | ||
+ | { | ||
+ | prod = 0; | ||
+ | fprintf(stdout, | ||
+ | } | ||
+ | else if (mult2 == 0) | ||
+ | { | ||
+ | prod = 0; | ||
+ | fprintf(stdout, | ||
+ | } | ||
+ | else if (mult1 == 1) | ||
+ | { | ||
+ | prod = mult2; | ||
+ | fprintf(stdout, | ||
+ | } | ||
+ | else if (mult2 == 1) | ||
+ | { | ||
+ | prod = mult1; | ||
+ | fprintf(stdout, | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | for(mtrack = 1; mtrack < mult2; mtrack++) | ||
+ | { | ||
+ | for(atrack = 0; atrack < mult1; atrack++) | ||
+ | { | ||
+ | prod = prod++; | ||
+ | } | ||
+ | } | ||
+ | fprintf(stdout, | ||
+ | } | ||
+ | |||
+ | return(0); | ||
+ | } | ||
+ | </ | ||
+ | ==== Expo ==== | ||
+ | |||
+ | <code c 1> | ||
+ | # | ||
+ | # | ||
+ | |||
+ | // | ||
+ | // | ||
+ | // | ||
+ | // | ||
+ | // | ||
+ | // | ||
+ | // | ||
+ | // | ||
+ | int main( ) | ||
+ | { | ||
+ | |||
+ | int base; | ||
+ | int power; | ||
+ | int prod; | ||
+ | int atrack = 0; | ||
+ | int mtrack = 1; | ||
+ | int etrack = 0; | ||
+ | |||
+ | fprintf(stdout, | ||
+ | fscanf(stdin, | ||
+ | fprintf(stdout, | ||
+ | fscanf(stdin, | ||
+ | prod = base; | ||
+ | int mval = base; | ||
+ | int aval = base; | ||
+ | |||
+ | if (base == 0) | ||
+ | { | ||
+ | prod = 0; | ||
+ | fprintf(stdout, | ||
+ | } | ||
+ | else if (power == 0) | ||
+ | { | ||
+ | prod = 1; | ||
+ | fprintf(stdout, | ||
+ | } | ||
+ | else if (base == 1) | ||
+ | { | ||
+ | prod = 1; | ||
+ | fprintf(stdout, | ||
+ | } | ||
+ | else if (power == 1) | ||
+ | { | ||
+ | prod = base; | ||
+ | fprintf(stdout, | ||
+ | } | ||
+ | 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, | ||
+ | } | ||
+ | |||
+ | return(0); | ||
+ | } | ||
+ | </ | ||
+ | =====Execution===== | ||
+ | |||
+ | <cli> | ||
+ | G: | ||
+ | Enter your base number | ||
+ | 10 | ||
+ | Enter your power number | ||
+ | 0 | ||
+ | Your Answer is: 1 | ||
+ | G: | ||
+ | Enter your base number | ||
+ | 10 | ||
+ | Enter your power number | ||
+ | 1 | ||
+ | Your Answer is: 10 | ||
+ | G: | ||
+ | Enter your base number | ||
+ | 10 | ||
+ | Enter your power number | ||
+ | 2 | ||
+ | Your Answer is: 100 | ||
+ | G: | ||
+ | Enter your base number | ||
+ | 10 | ||
+ | Enter your power number | ||
+ | 3 | ||
+ | Your Answer is: 1000 | ||
+ | G: | ||
+ | Enter your base number | ||
+ | 10 | ||
+ | Enter your power number | ||
+ | 4 | ||
+ | Your Answer is: 10000 | ||
+ | G: | ||
+ | Enter your base number | ||
+ | 0 | ||
+ | Enter your power number | ||
+ | 10 | ||
+ | Your Answer is: 0 | ||
+ | G: | ||
+ | Enter your base number | ||
+ | 1 | ||
+ | Enter your power number | ||
+ | 10 | ||
+ | Your Answer is: 1 | ||
+ | G: | ||
+ | </ | ||
+ | =====Reflection===== | ||
+ | This one took me back to the basics of programming, |