By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,324 Members | 537 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,324 IT Pros & Developers. It's quick & easy.

Reading correct matrix using string comparison in C

P: 3
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include <string.h>
  6.  
  7. #define MaxFilename 256
  8. #define MaxColumnandRows 1000
  9.  
  10. int main(int argc,char *argv[]){
  11.  
  12. char inputFile1[MaxFilename];
  13. char outputFile1[MaxFilename];
  14. char assignedValues[MaxColumnandRows][MaxColumnandRows];
  15. int a,b,n,m,j,i,k;
  16. int matrix1[20][20];
  17. int matrix2[20][20];
  18. int matrix3[20][20];
  19.  
  20. FILE *input1;
  21. FILE *output1;
  22.  
  23.  
  24. //User supplies input information
  25.  
  26.     printf("Which file contains the information for the matrices?\n");
  27.       scanf("%s",&inputFile1);
  28.  
  29.     input1 = fopen(inputFile1,"r");
  30.  
  31.       if (input1 == 0)
  32.          {
  33.           printf("File Not Found\n");
  34.           exit(0);
  35.          }
  36.  
  37. //User supplies output information
  38.  
  39.     printf("What output file will store the sorted items?\n");
  40.       scanf("%s",&outputFile1); //writes the matrix numbers to the file outputFile
  41.  
  42.       output1=fopen(outputFile1,"r+");
  43.  
  44.  
  45.  //Read and Assign input info
  46. for ( i=0; i<=10; i++)
  47.     {
  48.     for (j=0;j<=10;j++)
  49.         {
  50.           fscanf(input1,"%c",&assignedValues[i][j]); 
  51.          }
  52.      }
  53.      //printf("\n");
  54.      //printf("%c\n",assignedValues[0][6]);
  55.      //a=(int)assignedValues[0][6];
  56.      //printf("%d\n");
  57.  
  58.      if (a==49)
  59.          {
  60.              for ( i=0; i<=10; i++)
  61.                 {
  62.                     for (j=0;j<=10;j++)
  63.                     {
  64.                          fcanf(input1,"%d",&matrix1[i][j];
  65.                          printf("\nmatrix1\n");
  66.                      }
  67.                  }
  68.              row=matrix1[1][0];
  69.              column=matrix1[1][1];
  70.  
  71.  
  72.          }
  73.      else if (a==50)
  74.          {
  75.              for ( i=0; i<=20; i++)
  76.                 {
  77.                     for (j=0;j<=10;j++)
  78.                     {
  79.                          fcanf(input1,"%d",&matrix2[i][j];
  80.                          printf("\nmatrix1\n");
  81.                      }
  82.                  }
  83.              row=matrix1[1][1];
  84.              column=matrix1[1][0];
  85.              printf("\nmatrix2\n");
  86.  
  87.          }
  88.  
  89.      else
  90.          {
  91.              printf("\nerror\n");
  92.  
  93.          }
  94.  
  95.  
  96. }
  97.  
  98.  
I only need help with the string comparison part. Please let me know if any more info is needed.

Thanks.
Oct 19 '08 #1
Share this Question
Share on Google+
3 Replies

100+
P: 424
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.
Oct 20 '08 #2

P: 3
That did it..Thanks!
Oct 20 '08 #3

P: 17
can u show us your complete coding??
Oct 21 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.