473,385 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Reading correct matrix using string comparison in C

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
3 4060
arnaudk
424 256MB
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
itmfl
3
That did it..Thanks!
Oct 20 '08 #3
MyRedz
17
can u show us your complete coding??
Oct 21 '08 #4

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

Similar topics

3
by: muser | last post by:
With the following code I'm trying to read a text file (infile) and output inaccuracies to the error file (printerfile). The text file is written and stored on disk, while the printerfile has to be...
15
by: christopher diggins | last post by:
Here is some code I wrote for Matrix multiplication for arbitrary dimensionality known at compile-time. I am curious how practical it is. For instance, is it common to know the dimensionality of...
2
by: weetat.yeo | last post by:
Hi all , I need to Security Matrix in my php project. The Security Matrix are Administrator , Engineer, Storeman and Customer. One of my peers said to make php project more robust, he asked...
20
by: Frank-O | last post by:
Hi , Recently I have been commited to the task of "translating" some complex statistical algorithms from Matlab to C++. The goal is to be three times as fast as matlab ( the latest) . I've...
1
by: RishiD | last post by:
Hi, Trying to read an input file, of characters with spaces and carriage returns. Basically it is a 20x20 matrix of characters with spaces. I want to load the information into an two...
21
by: =?UTF-8?B?TWFydGluIFDDtnBwaW5n?= | last post by:
Hello, I´m using a very large 2-dimensional double matrix (45.000x45.000) in my program. At the initialization of the matrix: double matrix = new double I am getting an out of memory...
10
by: bodowpin | last post by:
Hello. I am trying to read a text file that contains 1 2 3 it just looks like that. I was able to read it and assign each number to a matrix element (or array element). It was reading it all...
5
by: Anolethron | last post by:
Wrong one: void minptr (int *matrix, int rows, int columns,int *min){ int i=0,j=0; *min=*matrix; //!!!!!!!!!!!!!!!!! for (i=0; i < rows; i++) { for (j=0; j < columns; j++) { if( *min...
5
by: slizorn | last post by:
hi, well this is the file i have to read into the system... <matrix> rows = 2 cols = 2 1 2 2 4 </matrix>
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.