Week1 Program+Dev+Process+Presentation
Week1 Program+Dev+Process+Presentation
Incorrect implementation
Implementation phase
Working Program
Problem-Solving Phase
4
𝒙
𝒙𝒚
𝒚
Algorithm for 𝑥 𝑦 (2/8)
6
Find repetition
Set𝑛 = 𝑥 = 3, 𝑦 = 4
𝑛 = 𝑚𝑢𝑙𝑡𝑖𝑝𝑙𝑦 𝑥 𝑏𝑦 𝑛
𝑛 = 𝑚𝑢𝑙𝑡𝑖𝑝𝑙𝑦 𝑥 𝑏𝑦 𝑛
𝑛 = 𝑚𝑢𝑙𝑡𝑖𝑝𝑙𝑦 𝑥 𝑏𝑦 𝑛
𝑦
𝑛 = 𝑥 is 𝑟𝑒𝑠𝑢𝑙𝑡
Algorithm for 𝑥 𝑦 (7/8)
11
endwhile
𝑛 is answer
Algorithm for 𝑥 𝑦 (8/8)
12
#include <stdio.h>
int main(void) {
int x, y, i, n;
printf("Enter two integers: ");
scanf("%d %d", &x, &y);
n = x;
i = 1;
while(i < y) {
n = n * x;
i += 1;
}
printf("%d raised to power of %d is: %d\n", x, y, n);
return 0;
}
Coding the Algorithm – Even better
15
#include <stdio.h>
int main(void) {
printf("Enter base and power: ");
int base, power;
scanf("%d %d", &base, &power);
int result = exponent(base, power);
printf("%d ^ %d is: %d\n", base, power, result);
return 0;
}
debug code
Note: There are 2 major bugs in previous
program!!!