Reading correct matrix using string comparison in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • itmfl
    New Member
    • Oct 2008
    • 3

    Reading correct matrix using string comparison in C

    We are writing a program that multiplies two matrices of size n x m and m x n together. The matrices are stored in a file. The user provides the filename in the command line prompt. The file is formatted like so:
    /beginning/

    matrix1
    3 2
    1 6
    2 4
    3 5

    matrix2
    2 3
    2 8 9
    8 2 1


    /end/

    matrix1 and matrix2 can be in any order. We are supposed to use string comparison to make sure that you are reading in the correct contents of the matrices. The first two numbers under the corresponding matrices are the dimensions.

    I only need help with the string comparison. As of now I am only able to enter the file into an array. Here is what I have so far.

    [code=c]

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

    #define MaxFilename 256
    #define MaxColumnandRow s 1000

    int main(int argc,char *argv[]){

    char inputFile1[MaxFilename];
    char outputFile1[MaxFilename];
    char assignedValues[MaxColumnandRow s][MaxColumnandRow s];
    int a,b,n,m,j,i,k;
    int matrix1[20][20];
    int matrix2[20][20];
    int matrix3[20][20];

    FILE *input1;
    FILE *output1;


    //User supplies input information

    printf("Which file contains the information for the matrices?\n");
    scanf("%s",&inp utFile1);

    input1 = fopen(inputFile 1,"r");

    if (input1 == 0)
    {
    printf("File Not Found\n");
    exit(0);
    }

    //User supplies output information

    printf("What output file will store the sorted items?\n");
    scanf("%s",&out putFile1); //writes the matrix numbers to the file outputFile

    output1=fopen(o utputFile1,"r+" );


    //Read and Assign input info
    for ( i=0; i<=10; i++)
    {
    for (j=0;j<=10;j++)
    {
    fscanf(input1," %c",&assignedVa lues[i][j]);
    }
    }
    //printf("\n");
    //printf("%c\n",a ssignedValues[0][6]);
    //a=(int)assigned Values[0][6];
    //printf("%d\n");

    if (a==49)
    {
    for ( i=0; i<=10; i++)
    {
    for (j=0;j<=10;j++)
    {
    fcanf(input1,"% d",&matrix1[i][j];
    printf("\nmatri x1\n");
    }
    }
    row=matrix1[1][0];
    column=matrix1[1][1];


    }
    else if (a==50)
    {
    for ( i=0; i<=20; i++)
    {
    for (j=0;j<=10;j++)
    {
    fcanf(input1,"% d",&matrix2[i][j];
    printf("\nmatri x1\n");
    }
    }
    row=matrix1[1][1];
    column=matrix1[1][0];
    printf("\nmatri x2\n");

    }

    else
    {
    printf("\nerror \n");

    }


    }

    [/code]

    I only need help with the string comparison part. Please let me know if any more info is needed.

    Thanks.
  • arnaudk
    Contributor
    • Sep 2007
    • 425

    #2
    Why don't you use fgets() to read an entire line into a string. Then you can use strcmp() to compare the two strings until you have a match for "matrix1" or "matrix2". When you have a match, you know what to expect on the next line and can fscanf() the values of the dimension into appropriate variables and then read in the matrix - you'll know how many lines you have to read and what they will look like from the dimension variables. When that's done, repeat the fgets() again until you reach the next "matrix[n]", etc.

    Comment

    • itmfl
      New Member
      • Oct 2008
      • 3

      #3
      That did it..Thanks!

      Comment

      • MyRedz
        New Member
        • Jan 2007
        • 17

        #4
        can u show us your complete coding??

        Comment

        Working...