-1

I'm working on a program that reads linear equations from a file such as those - and solve them using matrices - :

3x+2y-2z=9
-2x+9y+12z=23
4x-7y+9z=45

The file is supposed to contain n equations with n variables , how to get only the numbers and the signs from the above equations to store in 2d dynamic array of Integers

So output will be (some thing like this):

 3  2 -2  9
-2  9 12 23
 4 -7  9 45

Thanks in Advanced

Islams
  • 37
  • 2
  • 11
  • What have you done so far? Maybe you should search for `fscanf` to get started. – 4386427 May 07 '16 at 06:50
  • Please show us what you've tried. Hint: `fscanf` or `fgets` + `sscanf`. – Spikatrix May 07 '16 at 06:51
  • both `fscanf` or `sscanf` does not read the coefficients from the file Ex : fscanf(filePointer, "%d", &coff); Also if i use : fscanf(filePointer, "%s,%d", symbols,&coff) symbol read all the line and coff does not read the coefficients too I think because the equation is sequence of characters without spaces ... so this not work correctly , am i understand some thing wrong ? @CoolGuy – Islams May 07 '16 at 08:00
  • @Islams Use `if(fscanf(filePointer, "%dx%dy%dz=%d", &x, &y, &z, &ans) == 4) { /* Success! */ } else { /* Failed! */ }` – Spikatrix May 07 '16 at 08:10
  • Thank you very much @CoolGuy , – Islams May 07 '16 at 09:16
  • I have 3 question @CoolGuy : (First) why you write x,y,z here "%dx%dy%dz=%d" instead of "%d %d %d =%d" ... (Second) the importance of the "=" operator in the format .... (Third) what can i do to read n equations from the file ? – Islams May 11 '16 at 15:25
  • @Islams First of all you need to keep in mind that `scanf` scans everything given as such, eg: `scanf("abc");` will try to scan exactly `a`, `b` and `c`. Things starting with `%` are called format (or conversion) specifiers. They are special because they tell `scanf` to scan some data and store it in a variable. Eg: `scanf("%d", &myInt);` does not scan a `%` and a `d`, but instead, tries to scan an integer. – Spikatrix May 11 '16 at 16:16
  • @Islams Now, whitespace characters (`\n` [newline], [space], `\t` [tab] etc) also have a special meaning for `scanf`. Read this several times until you get the hang of it: '_A whitespace character in the format string of `scanf` instructs `scanf` to scan and discard any number of whitespace characters, including none, until the first non-whitespace character_'. Here is an example for you: `scanf("a ");` will scan `a` and then, any number of whitespace characters, if any, and stops scanning on encountering a non-whitespace character. – Spikatrix May 11 '16 at 16:21
  • @Islams Another example: `scanf("%d b");` will scan an `int`, then any whitespace, if any, and then a `b`. Hope you got the hang of this now. – Spikatrix May 11 '16 at 16:22
  • @Islams Coming back to your questions, I hope you understood (1) and (2). In case you haven't: 1) because you want to scan an `int`, then `x`, another `int`, then `y` and again one more `int`, and then `z` and `=` and finally, an `int`. `%d %d %d =%d` would scan an `int`, any whitespace, again scan an `int`, any whitespace, and once more scan an `int`, any whitespace, and then, scan in a `=` and then an `int`. In this case, the first `%d` would succeed in scanning the first `int`, whitespace would not consume anything since the next character is `x`, and the next `%d` would fail since `x!=int` – Spikatrix May 11 '16 at 16:28
  • @Islams 2) `=` is nothing special. It tells `scanf` to scan the `=` character. 3) Use a loop? Like `for(int i = 0; i < n; i++){ /* fscanf and other code here */ }` – Spikatrix May 11 '16 at 16:31
  • Ok , your declaration is very good and simple ... Thanks a lot @CoolGuy .. let me clarify only question number (Three) ... in `Linear equation System` the number of lines = the number of unknown variables .. I want to update my file to read `n` in the first line , this mean that eg:(n=5) that I have 5 equations with 5 same variables in each equation to solve it – Islams May 11 '16 at 21:07
  • @CoolGuy after clarification of the format you tell ... What Can I do to read n variables in one line .. and then read n equations will be easy too – Islams May 11 '16 at 21:08
  • @ CoolGuy Could I use array ... but what is the modification that will happen to the format of `fscanf` ... I know you get tired from me :D , As the suppose goal of this program to solve `Linear Equation System` using `MPI` (in Parallel Programming) .. untill now this programme look like a (read file problem) to me – Islams May 11 '16 at 21:13
  • @Islams Assuming the value of `n` is stored in a variable, say, `int n;`, then you can use `for(int i = 0; i < n; i++) { int coeff[n]; int result; for(int j = 0; j < n; j++) { if(fscanf(fp, "%d%*c", &coeff[j]) != 1) { /* Scanning failed */ exit(-1); } } if(fscanf(fp, "%*c%d", &result) != 1) { /* Scanning failed */ exit(-1); } for(int j = 0; j < n; j++) printf("coeff[%d]=%d\n", j, coeff[j]); printf("result=%d\n", result); }` The above code will work only in C99 and above. And it assumes that the linear equation contains only single letter variables. – Spikatrix May 12 '16 at 05:52

1 Answers1

1

Using fscanf, the 'd' modifier handles signed integer, that means it will take care of the input number whether it has + or - in front of it, try following code:

#include <stdio.h>

int main(void) {
    int x, y, z, e;
    FILE *fp = fopen("eq.txt", "r");
    if (!fp)
        return 1;
    while (fscanf(fp, "%dx%dy%dz=%d", &x, &y, &z, &e) == 4) {
        printf("%d %d %d %d\n", x, y, z, e);
    }
    return 0;
}

It outputs for the file you posted:

3 2 -2 9
-2 9 12 23
4 -7 9 45
fluter
  • 11,341
  • 7
  • 44
  • 77
  • Thnaks very much @fluter – Islams May 07 '16 at 09:18
  • I have 3 question @fluter : (First) why you write x,y,z here "%dx%dy%dz=%d" instead of "%d %d %d =%d" ... (Second) the importance of the "=" operator in the format .... (Third) what can i do to read n equations from the file ? – Islams May 11 '16 at 15:08
  • @Islams The format string should match the actual input file, your file does not have spaces, and contains a '=' in between, that's why it is like so. Now it reads the whole file, if you want to read first n, you can do `for (I=0;i – fluter May 11 '16 at 15:43
  • Thanks a lot @ fluter ... let me clarify only question number (Three) ... in `Linear equation System` the number of lines = the number of unknown variables .. I want to update my file to read `n` in the first line , this mean that eg:(n=5) that I have 5 equations with 5 same variables in each equation to solve it ... What Can I do to read n variables in one line .. and then read n equations will be easy too ? thanks in advanced – Islams May 11 '16 at 21:17
  • @Islams in that case, you can read the number `n` first, with `fscanf("%d", &n, fp);` and then read all the n lines after that with a loop through 0 to n. – fluter May 12 '16 at 00:14
  • @fluter You got the arguments of `fscanf` all mixed up. – Spikatrix May 12 '16 at 05:45
  • @CoolGuy my bad, the edit option no longer available for the comment :( – fluter May 12 '16 at 05:50