Answered step by step
Verified Expert Solution
Question
1 Approved Answer
How do I Write a C program called powers that prints a table of powers. It will take two arguments. The first is the base
How do I Write a C program called powers that prints a table of powers. It will take two arguments. The first is the base and the second is the exponent of the largest power to print.
Sample runs:
calvin 21:15:48 > ./powers 7 20
-- --------------------
i 7^i
-- --------------------
0 1
1 7
2 49
3 343
4 2401
5 16807
6 117649
7 823543
8 5764801
9 40353607
10 282475249
11 1977326743
12 13841287201
13 96889010407
14 678223072849
15 4747561509943
16 33232930569601
17 232630513987207
18 1628413597910449
19 11398895185373143
20 79792266297612001
-- --------------------
calvin 21:15:49 > ./power 2 10
bash: ./power: No such file or directory
calvin 21:16:43 > ./powers 2 10
-- --------------------
i 2^i
-- --------------------
0 1
1 2
2 4
3 8
4 16
5 32
6 64
7 128
8 256
9 512
10 1024
-- --------------------
calvin 21:16:49 > ./powers 10 18
-- --------------------
i 10^i
-- --------------------
0 1
1 10
2 100
3 1000
4 10000
5 100000
6 1000000
7 10000000
8 100000000
9 1000000000
10 10000000000
11 100000000000
12 1000000000000
13 10000000000000
14 100000000000000
15 1000000000000000
16 10000000000000000
17 100000000000000000
18 1000000000000000000
-- --------------------
calvin 21:16:57 > ./powers 7 20
-- --------------------
i 7^i
-- --------------------
0 1
1 7
2 49
3 343
4 2401
5 16807
6 117649
7 823543
8 5764801
9 40353607
10 282475249
11 1977326743
12 13841287201
13 96889010407
14 678223072849
15 4747561509943
16 33232930569601
17 232630513987207
18 1628413597910449
19 11398895185373143
20 79792266297612001
-- --------------------
calvin 21:17:11 > ./powers 7 0
-- --------------------
i 7^i
-- --------------------
0 1
-- --------------------
calvin 21:17:31 > ./powers 7
usage: powers base max-exponent
calvin 21:17:35 > ./powers
usage: powers base max-exponent
Your program must include a function
long power(int base, int exp) {
...
}
that computes base raised to the exponent power. For example power(2, 4) = 16. The function must compute and return the power. It must use repeated multiplication to compute the power. In particular, it may not use the functions pow, log, or exp or their variations.
The power function will compute the power NOT PRINT IT! The main function will do all of the printing.
Your program must produce exactly the same format as the sample runs.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started