0
현재 빌드되고 실행됩니다. 하지만 내 문제는 포인터 함수를 호출하려고 할 때 내 주요 함수에서 첫 번째 print 문을 출력하지 않는다는 것입니다. 내 최종 목표는 1-36의 f() 값 표처럼 인쇄하도록하는 것입니다. 그 두 줄의 코드에서 &과 *를 사용하는 것은 숙제의 일부입니다.숙제, 포인터 및 함수 호출에 붙어
#include <stdio.h>
#include <math.h>
double pointer (double *bi);
double f(double x);
double trapz(int n, double a, double b);
double simpsons(int m, double c, double d);
int main()
{
int g, h, xii=0;
double e[35];
for (xii=1; xii>36; xii++){
printf("%d,%f", xii, pointer(&e));
}
double x0 = 1, xn = 36, x1 = 1, xm =36;
int n1 = 35, n2 = 100, n3 = 10000, n4 = 35, n5 = 1000, n6 = 10000;
printf("For n = %d, the integral is %f\n", n1, trapz(n1, x0, xn));
printf("For n = %d, the integral is %f\n", n2, trapz(n2, x0, xn));
printf("For n = %d, the integral is %f\n", n3, trapz(n3, x0, xn));
printf("for n = %d, the integral is %f\n", n4, simpsons(n4, x1, xm));
printf("for n = %d, the integral is %f\n", n5, simpsons(n5, x1, xm));
printf("for n = %d, the integral is %f\n", n6, simpsons(n6, x1, xm));
printf("Enter the amount of intervals 'n' for the Trapezoidal rule and I'll give you an answer!\n");
scanf("%d", &g);
printf("\nYour answer is: %f\n", trapz(g, x0, xn));
printf("Enter the amount of intervals 'n' for the Simpson's rule and I'll give you an answer!\n");
scanf("%d", &h);
printf("\nYour answer is: %f\n", simpsons(h, x1, xm));
return 0;
}
double pointer (double *bi){
int xi=0;
for (xi=1; xi>36; xi++){
*bi=f(xi);
}
return 0;
}
double f(double x)/*function that calculates f(x)*/
{
double y;
y = 1000 * sin(5*x)/(x*x+7.68);
return y;
}
double trapz(int n, double a, double b)
{
int i;
double dx, x, sum;
dx = (b-a)/n;
sum=f(a)+f(b);
for (i=1; i<n; i++){
x=a+dx*i;
sum+=2*f(x);
}
sum*=dx/2;
return sum;
}
double simpsons(int m, double c, double d)
{
int j;
double dz, z, sum2;
dz = (d-c)/m;
sum2 = f(c)+f(d);
for (j=1; j<m; j++){
z=c+dz*j;
sum2 += 2*(j+j%2)*f(z);
}
sum2 *= dz/3;
return sum2;
}
갱신 : 나는 지금 인쇄되는 포인터 기능을 편집 할 수 있지만 올바른 번호 컴파일러 경고에
#include <stdio.h>
#include <math.h>
double pointer(double *bi, int ci);
double f(double x);
double trapz(int n, double a, double b);
double simpsons(int m, double c, double d);
int main()
{
int g, h, xii=0;
double e[35];
for (xii=1; xii<36; xii++){
printf("\t%d \t\t %f\n", xii, pointer(&e, xii));/*calls function and prints xii,f(xi). 1-36 for xi, and 1-36 for f(xi)*/
}
double x0 = 1, xn = 36, x1 = 1, xm =36;
int n1 = 35, n2 = 100, n3 = 10000, n4 = 35, n5 = 1000, n6 = 10000;
printf("For n = %d, the integral is %f\n", n1, trapz(n1, x0, xn));
printf("For n = %d, the integral is %f\n", n2, trapz(n2, x0, xn));
printf("For n = %d, the integral is %f\n", n3, trapz(n3, x0, xn));
printf("for n = %d, the integral is %f\n", n4, simpsons(n4, x1, xm));
printf("for n = %d, the integral is %f\n", n5, simpsons(n5, x1, xm));
printf("for n = %d, the integral is %f\n", n6, simpsons(n6, x1, xm));
printf("Enter the amount of intervals 'n' for the Trapezoidal rule and I'll give you an answer!\n");
scanf("%d", &g);
printf("\nYour answer is: %f\n", trapz(g, x0, xn));
printf("Enter the amount of intervals 'n' for the Simpson's rule and I'll give you an answer!\n");
scanf("%d", &h);
printf("\nYour answer is: %f\n", simpsons(h, x1, xm));
return 0;
}
double pointer(double *bi, int ci){
double ans;
*bi = (1000 * sin(5*ci))/(ci*ci+7.68);
ans = *bi;
return ans;
/*function is supposed to return f(xi)*/
}
double f(double x)/*function that calculates f(x)*/
{
double y;
y = 1000 * sin(5*x)/(x*x+7.68);
return y;
}
double trapz(int n, double a, double b)
{
int i;
double dx, x, sum;
dx = (b-a)/n;
sum=f(a)+f(b);
for (i=1; i<n; i++){
x=a+dx*i;
sum+=2*f(x);
}
sum*=dx/2;
return sum;
}
double simpsons(int m, double c, double d)
{
int j;
double dz, z, sum2;
dz = (d-c)/m;
sum2 = f(c)+f(d);
for (j=1; j<m; j++){
z=c+dz*j;
sum2 += 2*(j+j%2)*f(z);
}
sum2 *= dz/3;
return sum2;
}
, 변수'e'가 이미있는' double *'이므로,'pointer (& e)'를'pointer (e)'로 변경해야합니다. 또한,'pointer' 함수에서'xi = 1'에서'xi> 36'으로가는 것은 완전히 잘못된 것입니다 ... – WhatsUp
@WhatsUp은 가깝지만'e'는 * 배열입니다. 첫 번째 요소에 대한 포인터를 감쇠시킬 수 있습니다. 나는. 일반'e'를 사용하는 것은'& e [0]'와 같습니다. 그 유형은'double *'이 될 것입니다. '& e' 표현식은 완전히 다른 것입니다. * array *에 대한 포인터입니다.이 배열은'double (*) [35]'타입을가집니다. '& e [0]'과'& e' (모두 다행스럽게도!)가 같은 위치를 가리키고 있기 때문에 아마 문제의 근원은 아닙니다. –
문제 해결 방법은 [프로그램 디버깅 방법을 배우십시오] (https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)해야합니다. –