Can anyone help troubleshoot my C code? #include <stdio.h> #include <math.h> #de
ID: 2292533 • Letter: C
Question
Can anyone help troubleshoot my C code?
#include <stdio.h>
#include <math.h>
#define MAXSIZE 10 // Maximum size of 1-D array
void arrayin(double X[], int n) {
for (int i = 0;i<n;++i){
scanf("%lf",&X[i]);
}
}
void arraypower(double X[], int n, double Z[],float p){
for (int i = 0;i<n;++i){
Z[i] = pow(X[i],p);
}
}
void printarray(int n, double Z[]){
for (int i = 0;i<n;++i){
printf(" %f ",Z[i]);
}
int main (void)
{
double X[MAXSIZE]; // Array holds user’s input values
int n; // Actual array size
printf("Enter size of array: ");
scanf("%d", &n);
// actual code:
printf("Enter Array Values: ");
arrayin(X,n);
float p;
printf("Enter Power: ");
scanf("%f", &p);
double Z[n];
arraypower(X,n,Z,p);
printarray(n,Z);
}
}
Explanation / Answer
#include <stdio.h>
#include <math.h>
#define MAXSIZE 10 // Maximum size of 1-D array
void arrayin(double X[], int n) {
int i;
for (i = 0; i < n; ++i){
scanf("%lf",&X[i]);
}
}
void arraypower(double X[], int n, double Z[],float p){
int i;
for (i = 0;i < n; ++i){
Z[i] = pow(X[i],p);
}
}
void printarray(int n, double Z[]){
int i;
for (i = 0; i < n; ++i){
printf("%0.2lf ",Z[i]);
}
printf(" ");
}
int main ()
{
double X[MAXSIZE]; // Array holds user’s input values
int n; // Actual array size
printf("Enter size of array(less than 10): ");
scanf("%d", &n);
// actual code:
printf("Enter Array Values: ");
arrayin(X,n);
float p;
printf("Enter Power: ");
scanf("%f", &p);
double Z[n];
arraypower(X,n,Z,p);
printf("The elements of the Power Array are ::: ");
printarray(n,Z);
return 0;
}
//NOTE :: While using gcc we have to specify the math library exclusively at command line using -lm option
// else it gives an error saying pow is undefined
/****************** OUTPUT OF PROGRAM *******************
>> gcc -lm scr_c_program.c
>> ./a.out
Enter size of array(less than 10): 4
Enter Array Values: 10 20 30 40
Enter Power: 3
The elements of the Power Array are ::: 1000.00 8000.00 27000.00 64000.00
********************************************************/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.