Reputation: 31
So have been trying and figuring out why my code doesn't want to work with me. Cannot find the mistake here.. It should let me enter all the details and afterwards display everything from arrays. But different things i have tried and still end up in the segmentation fault. Im relativly new in programming so criticism is allowed.
#include <stdio.h>
#include <math.h>
int main (void)
{
int j, n;
int *A,*B, *H;
double const e = 2.71828183;
int x[15];
float y[15];
printf("\n");
printf(" sqrt(x)*sin(1/x)\n");
printf("y=-------------\n");
printf(" (x+e^x) \n\n");
int i=0;
while((*A)>*(B));
{
printf("Enter A :\n");
scanf("%d", A);
printf("Enter B :\n");
scanf("%d", B);
if((*A)>(*B))
{
printf("enter sometthing that.....\n");
}
i++;
}
do
{
printf("Enter H:\n");
scanf("%d", H);
if((*H)<15)
{
printf("\n");
}
while((*H)>15)
{
printf("H is wrong, try again\n");
}
j++;
}
while((*H)>15);
do
{
x[i+1]=(x[i]+(i+1)*(*H));
y[i]=(sqrt(x[i])*sin(1/x[i]))/(x[i]+(pow(e,x[i])));
i++;
n=i-1;
}
while (x[i]<=(*B));
for(i=0;i<=n;i++)
printf("X | Y\n");
{
printf("%d | %f\n", x[i], y[i]);
}
return 0;
}
Upvotes: 0
Views: 63
Reputation: 22264
These pointers are uninitialised so they point to random memory locations.
int *A,*B, *H;
Here you are dereferencing uninitialised pointers (undefined behaviour -> segmentation fault)
while((*A)>*(B));
I would recommend declaring variables like :
int A=1, B=0, H;
modify the while loop (based on initial values of A, B it will execute at least once) (or use do while
):
while(A > B)
and scan like this :
scanf("%d", &A);
Upvotes: 5