Как передать возвращаемое значение в функции getY через указатель - C (СИ)
Формулировка задачи:
Добрый вечер. Подскажите, как передать возвращаемое значение в функции getY через указатель
#include <stdio.h>
#include <conio.h>
#include <locale.h>
#include <math.h>
#include <process.h>
void first(void);
void second(void);
void getSum(int n, double x, double *sum);
double getY(double x);
void main(void) {
setlocale(LC_CTYPE, "rus");
int lab = 0;
while (true) {
system("cls");
//puts("Выберите задание для проверки: 1 или 2.\nВведите 3 для выхода");
puts("Choose task for checking 1 or 2.\nEnter 3 for exit.");
fflush(stdin);
scanf_s("%d", &lab);
switch (lab) {
case 1:
first();
break;
case 2:
second();
break;
case 3:
exit(0);
default:
//puts("Ничего не получится, введите число от 1 до 3 включительно!");
puts("No, enter number from 1 to 3!");
_getch();
}
}
}
void first() {
double a, b, h, sum, x, y;
int n, k, fact, i;
//puts("Задание первое");
puts("Task 1");
//puts("Введите а, b, h и n через пробел, n должно быть целым положительным числом (> 0).");
puts("Enter а, b, h and n divided by spaces, n should be integer positive number (> 0).");
scanf_s("%lf%lf%lf%d", &a, &b, &h, &n);
//a = 0.1;
//b = 1;
//h = 0.1;
//n = 5;
if ((a >= b) || ((a + h) > b))
{
//puts("А должно быть больше b как минимум на 1 h");
puts("А should be more tham b at least at 1 h");
exit(0);
}
if (n <= 0)
{
//puts("N должно быть больше 0.");
puts("N should be greater than 0.");
exit(0);
}
printf_s("a = %.4lf, b = %.4lf, h = %.4lf, n = %d\n", a, b, h, n);
printf_s("S(x) Y(s) |Y(x)-S(x)|\n");
while (a < b)
{
x = a;
//printf_s("%.4lf %.4lf %.4lf\n", a, b, h);
sum = 0;
for (k = 0; k <= n; k++)
{
fact = 1;
for (i = 1; i <= 2 * k; i++)
{
fact *= i;
}
sum += pow(-1., k)*(2 * pow(k, 2.) + 1) / fact *pow(x, 2 * k);
}
y = (1 - pow(x, 2) / 2.) * cos(x) - x / 2 * sin(x);
printf_s("%.4lf %.4lf %.4lf\n", sum, y, fabs(y - sum));
a += h;
}
_getch();
}
void second() {
double a, b, h, sum, x, y;
int n;
//puts("Задание второе");
puts("Task 2");
//puts("Введите а, b, h и n через пробел, n должно быть целым положительным числом (> 0).");
puts("Enter а, b, h and n divided by spaces, n should be integer positive number (> 0).");
scanf_s("%lf%lf%lf%d", &a, &b, &h, &n);
//a = 0,1;
//b = 0,8;
//h = 0.1;
//n = 5;
if ((a >= b) || ((a + h) > b))
{
//puts("А должно быть больше b как минимум на 1 h");
puts("А should be more tham b at least at 1 h");
exit(0);
}
if (n <= 0)
{
//puts("N должно быть больше 0.");
puts("N should be greater than 0.");
exit(0);
}
printf_s("a = %.4lf, b = %.4lf, h = %.4lf, n = %d\n", a, b, h, n);
printf_s("S(x) Y(s) |Y(x)-S(x)|\n");
while (a < b)
{
x = a;
//printf_s("%.4lf %.4lf %.4lf\n", a, b, h);
sum = 0;
getSum(n, x, &sum);
y = getY(x);
printf_s("%.4lf %.4lf %.4lf\n", sum, y, fabs(y - sum));
a += h;
}
_getch();
}
void getSum(int n, double x, double *sum) {
int k, fact, i;
for (k = 0; k <= n; k++)
{
fact = 1;
for (i = 1; i <= 2 * k; i++)
{
fact *= i;
}
*sum += pow(-1., k)*(2 * pow(k, 2.) + 1) / fact*pow(x, 2 * k);
}
}
double getY(double x) {
return (1 - pow(x, 2) / 2.) * cos(x) - x / 2 * sin(x);
}Решение задачи: «Как передать возвращаемое значение в функции getY через указатель»
textual
Листинг программы
#include <stdio.h>
#include <conio.h>
#include <locale.h>
#include <math.h>
#include <process.h>
double *getY(double x);
int main()
{
double *yy;
yy = getY(12.0);
printf("%lf", *yy);
return 0;
}
double *getY(double x)
{
static double y;
y = (1 - pow(x, 2) / 2.) * cos(x) - x / 2 * sin(x);
return &y;
}