Solving 1st degree simultaneous equations using nested loops in c++ -
i've problem requires me write programme finds solution pair of 1st grade simultaneous equations. have test values of x , y exhaustively find integer solution. coefficients, a, b, , c, of both equations in info text file called "input.txt".
the text file has next data:
1 0 99 0 2 -100
below program:
#include <stdio.h>; #define filename "input.txt" int main() { int a, b, c, x, y; file *input; input = fopen(filename,"r"); fscanf(input,"%d%d%d", &a,&b,&c); for(x = -100; x <= 100; x++) for(y = -100; y <= 100; y++) if(a*x+b*y==c){ fscanf(input,"%d%d%d", &a,&b,&c); if(a*x+b*y==c) printf("x=%d, y=%d\n", x, y); } homecoming 0; }
when compile , run program, get
x=99, y=-50 x=100, y=-50
clearly, programme has errors. they?
also, if want print message says solution cannot found in range [-100, 100]
once, how should insert printf function program?
it's been while since i've done c-style file i/o, there couple obvious errors noticed quickly:
you should verify file opened you should verify fscanf reads expected number of items each time you might need spaces between %d format specifiers (not sure on one) you should not read sec set of parameters same variables first set you should read sec set of parameters 1 time (not conditionally in loop)also, why required utilize nested loops? there much improve ways attack problem.
edit: provided output illustration code? how y value -50?
c++ nested-loops
No comments:
Post a Comment