Написал программу простейшей интерполяции. Теперь хочу задать условие "если заданная координата X для поиска значения функции не входит в интервал, то выводить определенный комментарий". Не ищется максимум Код:
#include "conio.h"
#include "stdio.h"
#include "math.h"
#include "malloc.h"
#include "windows.h"
struct point{
double x;
double y;
};
void printstruct(point *p, int count){
for (int i=0; i<count; i++)
{
printf("x = %.2lf\t",p[i].x);
printf("y = %.2lf\n",p[i].y);
}
}
void sortstruct(point *p, int count)
{
for (int j=0; j<count; j++)
{
for (int i=0; i<count-1; i++)
{
if (p[i].x>p[i+1].x)
{
point temp=p[i];
p[i]=p[i+1];
p[i+1]=temp;
}
}
}
}
double findinterval(point *p, int count, double x_arg)
{
int maxx;
point p1, p2;
for (int i=0; i<count; i++)
{
if (x_arg < p[i].x)
{
p1 = p[i-1];
p2 = p[i];
}
}
if (x_arg < p[0].x)
{
printf("Координата не входит в заданный интервал\n");
exit(1);
}
double yy = p1.y + ((p2.y - p1.y)/(p2.x - p1.x))*(x_arg-p1.x);
return yy;
}
double findmax(point *p, int count, double max)
{
max = 0;
for (int i=0; i<count; i++)
{
if (p[i].x > max)
{
max = p[i].x;
}
}
printf ("MaxX = %lf",&max);
return max;
}
int main()
{
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
point *points;
int n;
printf("input count=");
scanf("%d", &n);
points = (point*)malloc(n*sizeof(point));
for (int i = 0; i < n; i++)
{
printf("Input x[%d] = ",i);
scanf("%lf", &points[i].x);
printf("Input y[%d] = ",i);
scanf("%lf", &points[i].y);
}
printstruct(points, n);
sortstruct (points, n);
printstruct (points, n);
findmax (points,n,0);
double x_arg;
printf ("Input x_arg\t ");
scanf("%lf", &x_arg);
printf("%.2lf\n",x_arg);
double y_arg = findinterval(points,n,x_arg);
printf ("y_arg = %.2lf\n", y_arg);
getch();
}
printf ("MaxX = %lf",&max);
Вы здесь хотите отобразить адрес переменой в стеке или значение переменой? – Yaroslav 23 мая '17 в 5:19