Fscanf not working properly
Program reads 4 values from a file (x1, y1, x2, y2) and calculates the slope-intercept form of their line.
The program simply will not do what I want, though, as the slope always comes up as zero.
Code:
/*Brendan Keeler - Oct. 5, 2009 - The purpose of this program is to find the slope-intercept form of a given line.*/
#include <stdio.h>
float slope(x1, y1,x2, y2){
float s=0;
if ((x2 - x1)== 0){
printf("Not expressible in slope-intercept form");
}
else{
s = (y2 - y1)/(x2-x1);
}
return s;
}
float y_int(x1, y1, slope){
float b;
b = -slope*(x1)+y1;
return b;
}
void disp_slope_int(float slope, float y_int){
printf("Slope-intercept form is:\n\n y = %fx + %f\n", slope, y_int);
}
main(){
FILE *fin;
float x1, y1, x2, y2, m, b;
int i = 0;
float numbers[4];
fin = fopen("input.txt", "r");
while(fscanf(fin, "%d", &numbers[i])) {
i++;
}
x1 = numbers[1];
y1 = numbers[2];
x2 = numbers[3];
y2 = numbers[4];
m = slope(x1,y1, x2, y2);
b = y_int(x1,y1,m);
disp_slope_int(m,b);
fclose(fin);
}