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. -
-
#include <stdlib.h>
-
#include <stdio.h>
-
#include <math.h>
-
#include <string.h>
-
-
#define MaxFilename 256
-
#define MaxColumnandRows 1000
-
-
int main(int argc,char *argv[]){
-
-
char inputFile1[MaxFilename];
-
char outputFile1[MaxFilename];
-
char assignedValues[MaxColumnandRows][MaxColumnandRows];
-
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",&inputFile1);
-
-
input1 = fopen(inputFile1,"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",&outputFile1); //writes the matrix numbers to the file outputFile
-
-
output1=fopen(outputFile1,"r+");
-
-
-
//Read and Assign input info
-
for ( i=0; i<=10; i++)
-
{
-
for (j=0;j<=10;j++)
-
{
-
fscanf(input1,"%c",&assignedValues[i][j]);
-
}
-
}
-
//printf("\n");
-
//printf("%c\n",assignedValues[0][6]);
-
//a=(int)assignedValues[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("\nmatrix1\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("\nmatrix1\n");
-
}
-
}
-
row=matrix1[1][1];
-
column=matrix1[1][0];
-
printf("\nmatrix2\n");
-
-
}
-
-
else
-
{
-
printf("\nerror\n");
-
-
}
-
-
-
}
-
-
I only need help with the string comparison part. Please let me know if any more info is needed.
Thanks.
3 4060
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.
can u show us your complete coding??
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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>
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |