473,686 Members | 2,941 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading correct matrix using string comparison in C

3 New Member
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 4086
arnaudk
424 Contributor
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 New Member
That did it..Thanks!
Oct 20 '08 #3
MyRedz
17 New Member
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
3396
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 created when the program executes. But the compile keeps reading that it can't find the text file. Karl you wrote the original program from which this one is but a poor copy, can you or anyone else enlighten me as to why yours worked and mine...
15
13263
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 matricies at compile-time? Any help would be appreciated. Hopefully this code comes in useful for someone, let me know if you find it useful, or if you have suggestions on how to improve it. // Public Domain by Christopher Diggins, 2005 ...
2
2106
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 me to use byte value as security matrix. For example as shown below: User id Name Security Matrix
20
5223
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 used various techniques ( loop unrolling, loop jamming...) and tried some matrix libraries : newmat (slow for large matrix) , STL (fast but ..not usefull) , hand coding (brain consuming...), and recently Meschach...
1
2286
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 dimensional array. Cannot seem to figure out how to do like if the read character equals a space or carriage return, don't put it in the array. Any ideas?
21
4639
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 error.
10
7502
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 fine and I was trying to change the elements to int variables a,b,c so that I could say matrix = {a,b,c}; At some point during tweaking all the matrix elements became 0 and I have lost myself.
5
9634
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 *(matrix+(i*columns)+j) ) { min = (matrix+(i*columns)+j);
5
2089
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>
0
8581
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9050
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8766
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6439
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5795
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4307
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4528
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2205
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.