473,804 Members | 3,461 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 4102
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
3404
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
13283
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
2118
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
5251
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
2292
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
4658
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
7516
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
9641
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
2096
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
9705
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
9576
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10074
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9138
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6847
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
5515
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
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4291
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2983
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.