Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Не корректно выводит значения(выделены цветом).Все значения переменных рассчитаны верно. Спасибо

+2 votes
asked Mar 10, 2023 by Rail4icK (150 points)

#include<stdio.h>
#include<math.h>

 main()
{ int n=0;
 float al=-5/18, bt=0, gm=5/54, dt=-1/3, eps=0.00001;
 float x0=1.8,y0=1;
 float x,x1,z1;
 float y,y1,z2;
 x=x0;
 y=y0;
printf("n++    x        x1   fabs(x1-x)   y        y1   fabs(y1-y)\n");


do
{

  x1=x+(al*(x*x-4) + bt*(x+3*y-8));  // MPI   должно вывести  х=2, у=2

//x1=x+((-1/(2*x))*(x*x-4)+ (0*(x+3*y-8))); //Oбщий метод        должно вывести  х=2, у=2

//x1=x-((x*x-4)/3);//Метод Ньютона    работает корректно

z1=x;

y1=y+(gm*(x*x-4) + dt*(x+3*y-8));  // MPI    должно вывести  х=2, у=2

//y1=y+((1/(6*x))*(x*x-4)-(1/3)*(x+3*y-8)); //Oбщий метод    должно вывести  х=2, у=2

//y1=y-((x*x+3*x+9*y-28)/(6*x));//Метод Ньютона работает корректно 

z2=y;

printf("%d   %.4f   %.4f   %.4f    %.4f   %.4f   %.4f\n",n++,x,x1,fabs(x1-x),y,y1,fabs(y1-y));
x=x1;
y=y1;
}
while(fabs(z1-x)>eps || fabs(z2-y)>eps);
}

1 Answer

+1 vote
answered Mar 11, 2023 by Peter Minarik (86,160 points)
edited Mar 11, 2023 by Peter Minarik

The problem is that there is a difference between an integral number and a floating point number.

So all your variables al, bt, gm, dt do not have the value you think they have. You can easily check this by printing their values.

You'd assume dt has the value -0.333333, but the actual value is 0. The reason for this is that there is a difference between integral numbers and floating point numbers.

If you do an integer division, the result is and integer number. If yo do a float division, the result is a float number. Since the expression 1 / 3 is a division where both operands are integers the result is also an integer 1 / 3 = 0.

If you want to do a floating point division, use floating point numeric literals.

float al = -5.0f / 18.0f, bt = 0, gm = 5.0f / 54.0f, dt = -1.0f / 3.0f, ...

The f suffix tells the compiler the number is a float, not a double. But having the decimal dot is a must, otherwise the compiler will definitely think you have integral numbers there.

commented Mar 12, 2023 by Rail4icK (150 points)
edited Mar 12, 2023 by Rail4icK
Спасибо, заработало!
commented Mar 12, 2023 by Peter Minarik (86,160 points)
I don't speak Russian, so I rely on translators (I suppose you do the same with English text)...

So what the translator says is that something still does not work (общий вид). Could you give more details on what is printed (incorrectly) and what should be the correct output? This would allow me to give you more help as I don't see what else is broken right now.
commented Mar 12, 2023 by Rail4icK (150 points)
Я нашел ошибку, большое спасибо!
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...