Write a C program whose main function will. Initialize an array with 4 different
ID: 3577846 • Letter: W
Question
Write a C program whose main function will. Initialize an array with 4 different integer values. Use a “while” or a “do-while” loop in the main function to pass the values of the integers, one at a time, to another function called “Third_Power”. Use a prototype to declare the new function “Third_Power”. The “Third_Power” function must accept the value of each integer, raise it to a power of 3, and then pass the new value back to the main function. The main function must print these values with a blank line between them using this print statement: printf(“The third power of %d is %d ”, x, y)
Explanation / Answer
#include <stdio.h>
int Third_Power(int x){
int y = x * x * x;
return y;
}
int main(){
int a[4] = {2,5,7,8};
int i,y;
for(i=0; i<4; i++){
y = Third_Power(a[i]);
printf("The third power of %d is %d ", a[i], y);
}
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
The third power of 2 is 8
The third power of 5 is 125
The third power of 7 is 343
The third power of 8 is 512
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.