473,387 Members | 1,548 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,387 software developers and data experts.

Matrix problem

Hello!

Can anyone help me? This code doesnt work. Where is the problem?
Inside the files mata and matb there is (should be) 10*10 matrix. It
should count a sum to new matrix. It does not work. Where is the problem?
#include <stdio.h>

main()
{
int first_matrix[10][10] ;
int second_matrix[10][10];
int sum_matrix=0;
int y, x;
int a, b;
printf("Matrix sum is saved in sum.usr file! %d\n", sum_matrix);

FILE *mata; mata = fopen("mata.txt", "w");
FILE *matb; matb = fopen("matb.txt", "w");

FILE *sum; sum = fopen("sum.usr", "w");

for(y = 0; y < 1; y++) {
for(x = 0; x < 1; x++) {
sum_matrix = first_matrix[y][x] + second_matrix[a][b];

printf("%d ", sum_matrix);
if(x == 4) {
}
}
}

}
Nov 14 '05 #1
8 1695
beginner10 <pe************@yahoo.com> scribbled the following:
Hello! Can anyone help me? This code doesnt work. Where is the problem?
Inside the files mata and matb there is (should be) 10*10 matrix. It
should count a sum to new matrix. It does not work. Where is the problem?
#include <stdio.h> main()
int main(void) is the recommended style.
{
int first_matrix[10][10] ;
int second_matrix[10][10];
int sum_matrix=0;
int y, x;
int a, b;
printf("Matrix sum is saved in sum.usr file! %d\n", sum_matrix); FILE *mata; mata = fopen("mata.txt", "w");
FILE *matb; matb = fopen("matb.txt", "w"); FILE *sum; sum = fopen("sum.usr", "w");
Do you have a C99 compiler? If not, then don't mix declarations and
executable code.
for(y = 0; y < 1; y++) {
for(x = 0; x < 1; x++) {
Look closely at these for loops. Especially look at the number following
"y <" and "x <". It's not exactly ten, is it?

sum_matrix = first_matrix[y][x] + second_matrix[a][b]; printf("%d ", sum_matrix);
if(x == 4) {

What is this supposed to do? What's so special about 4? Anyway, the
way your for loops go, x will never ever get to 4 in the first place.
}
}
}
}


--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"The large yellow ships hung in the sky in exactly the same way that bricks
don't."
- Douglas Adams
Nov 14 '05 #2
beginner10 wrote:
Can anyone help me? This code doesnt work. Where is the problem?
Inside the files mata and matb there is (should be) 10*10 matrix. It
should count a sum to new matrix. It does not work. Where is the problem?

I'm not entirly clear what you are trying to do so my comments may not
be entirely germane. What does "count a sum to new matrix" mean?

#include <stdio.h>

main()
int main (void)
{
int first_matrix[10][10] ;
int second_matrix[10][10];
int sum_matrix=0;
int y, x;
int a, b;
printf("Matrix sum is saved in sum.usr file! %d\n", sum_matrix);
I'm not sure if you can mix declarations and statements in C, you
certainly can't in C90
FILE *mata; mata = fopen("mata.txt", "w");
FILE *matb; matb = fopen("matb.txt", "w");
FILE *matb; matb = fopen("matb.txt", "r");
If you intend to read from these files

FILE *sum; sum = fopen("sum.usr", "w");
at some point you should have read the two matrices

for(y = 0; y < 1; y++) {
ITYM y < 10
for(x = 0; x < 1; x++) {
ditto
sum_matrix = first_matrix[y][x] + second_matrix[a][b];
a and b are uninitialised, try:-

sum_matrix = first_matrix[y][x] + second_matrix[x][y];

first_matrix[] and second_matrix[] are uninitialised. I assume you
meant to read them from the files...
printf("%d ", sum_matrix);
printf ("%d\n", sum_matrix);
otherwise the sum may not be printed.
if(x == 4) {
}
what is this meant to do?

}
}
}


hope that is some help.
--
Nick Keighley

As far as the laws of mathematics refer to reality,
they are not certain; and as far as they are certain,
they do not refer to reality.
Albert Einstein

Nov 14 '05 #3
beginner10 <pe************@yahoo.com> wrote:
Can anyone help me? This code doesnt work. Where is the problem?
Inside the files mata and matb there is (should be) 10*10 matrix. It
should count a sum to new matrix. It does not work. Where is the problem? #include <stdio.h> main()
int main( void )
{
int first_matrix[10][10] ;
int second_matrix[10][10]; int sum_matrix=0;
int y, x;
int a, b;
printf("Matrix sum is saved in sum.usr file! %d\n", sum_matrix);
Printing out 0 at the end looks a bit useless...
FILE *mata; mata = fopen("mata.txt", "w");
FILE *matb; matb = fopen("matb.txt", "w");
1) Unless you have a C99 compliant compiler all variables must be
defined before the first "executable" statement.
2) Why do you open the file in write mode when you want to read from
them? Opening in write mode will actually delete the contents of
the files.
3) After opening the files you should test if you succeeded. On
faliure the return value of fopen() will be NULL.
4) You never read anything from that files, so both your matrices are
uninitialized. Just opening the files won't put the date from the
files into the matrices.
FILE *sum; sum = fopen("sum.usr", "w"); for(y = 0; y < 1; y++) {
for(x = 0; x < 1; x++) {
Are you sure you don't want to iterate while 'x' and 'y' are less than
10?
sum_matrix = first_matrix[y][x] + second_matrix[a][b];
'a' and 'b' are uninitialized variables - they can have some random
values far outside the size of the array.
printf("%d ", sum_matrix);
You wrote that you want to print to the file "sum.usr", don't you?
if(x == 4) {
What's that good for?
}
}
}
Your main() function is missing a return statememt - main() must
return an int.
}


What exactly are you trying to do? Sum two matrices? Or what does
"It should count a sum to new matrix" is supposed to mean?

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #4
beginner10 <pe************@yahoo.com> scribbled the following:
Hello! Can anyone help me? This code doesnt work. Where is the problem?
Inside the files mata and matb there is (should be) 10*10 matrix. It
should count a sum to new matrix. It does not work. Where is the problem?


This code has so many stupid mistakes that I think this is a homework
problem. The code apparently comes from an exercise where the point is
to find all the errors in the presented program and fix them.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"We sorcerers don't like to eat our words, so to say."
- Sparrowhawk
Nov 14 '05 #5
beginner10 wrote:
Hello!

Can anyone help me? This code doesnt work. Where is the problem?
Inside the files mata and matb there is (should be) 10*10 matrix. It
should count a sum to new matrix. It does not work. Where is the problem?
#include <stdio.h>

main()
Main returns an int, tell the compiler this explicitly. It is also
better to tell the compiler explicitly it does not take any parameters.

int main(voi)
{
int first_matrix[10][10] ;
int second_matrix[10][10];
int sum_matrix=0;
int y, x;
int a, b;
printf("Matrix sum is saved in sum.usr file! %d\n", sum_matrix);

FILE *mata; mata = fopen("mata.txt", "w");
In C90 you have to put all your variable definitions *before* other
statements. Also, you can do things like:
FILE *mata = fopen("mata.txt", "w");
to declare and initialise a variable in one operation.
Also, you do realise you have opened the file for WRITEING and truncated
it to 0 length if it existed, don't you? Are you sure you did not meen
to open it for reading? */
FILE *matb; matb = fopen("matb.txt", "w");

FILE *sum; sum = fopen("sum.usr", "w");

for(y = 0; y < 1; y++) {
for(x = 0; x < 1; x++) {
Those loops are going to be very short. Did you mean 10 rather than 1?
An exelent example of why you should have used #defines for the array
dimensions rather than using magic numbers everywhere.
sum_matrix = first_matrix[y][x] + second_matrix[a][b];
It might have helped if you had actually read the data from somewhere
(mata.txt and matb.txt?) in to first_matrix and second_matrix first. As
it is you are operating on unititialised data which, I believe, gives
you undefined behavious.
printf("%d ", sum_matrix);
if(x == 4) {
x will never be 4, see comments above about your for loops.


}
}
}

}


I hope you have plenty of time to finish this assignment.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #6
raj
if u have a 10*10 matrix in each mata and matb file and if u want to
write the sum of those 2 matrices to sum.usr ..( i hope , this is your
problem ) then the following simple code will do
#include<stdio.h>
int main()
{
int a[10][10];
int b[10][10];
int i,j;
FILE *mata,*matb,*sum ;
if( (mata = fopen("mata.txt", "r"))==NULL)
{
printf("\nerror opening the file mata");
exit(1);
}
if( (matb = fopen("matb.txt", "r"))==NULL)
{
printf("\nerror opening the file matb");
exit(1);
}
if( (sum = fopen("sum.usr", "w"))==NULL)
{
printf("\nerror opening the file sum");
exit(1);
}
for ( i=0;i<10;i++)
for( j=0;j<10;j++)
fscanf(mata,"%d",&a[i][j]);
for ( i=0;i<10;i++)
for( j=0;j<10;j++)
fscanf(matb,"%d",&b[i][j]);
for ( i=0;i<10;i++)
{
for( j=0;j<10;j++)
fprintf(sum,"%4d",a[i][j]+b[i][j]);

fprintf(sum,"\n");
}

printf("done");

return 0;
}

Nov 14 '05 #7
Still i have a problem. The row changhe is too far away. How can i change
that?

sum,"%d", --> sum,"%d ", This did not help me.

Nov 14 '05 #8
beginner10 wrote:
Still i have a problem. The row changhe is too far away. How can i change that?

sum,"%d", --> sum,"%d ", This did not help me.


I don't understand. What does "the row change is too far away" mean?
Could you give more context, at the very least the whole line where
your proposed change is. And explain what you want the code to do.
--
Nick Keighley

Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
4
by: Yudan Yi | last post by:
I have a problem to copy (assign) a matrix to another matrix. Curreny, I know copy the number using loops, while it will take some time, I wonder if there have faster method. The following code...
3
by: robix | last post by:
Hi again. I'm now asking your help because of a smal problem i'm getting with my multiplication matrix code. I'll try to give you as many details as possible. My matrix structure: typedef...
16
by: Martin Jørgensen | last post by:
Hi, I've made a program from numerical recipes. Looks like I'm not allowed to distribute the source code from numerical recipes but it shouldn't even be necessary to do that. My problem is...
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...
6
by: lancered | last post by:
Hi dear all, I am using Python2.4.2+NumPy1.0.1 to deal with a parameter estimation problem with the least square methods. During the calculations, I use NumPy package to deal with matrix...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
14
by: James Stroud | last post by:
Hello All, I'm using numpy to calculate determinants of matrices that look like this (13x13):
18
by: Hypnotik | last post by:
Hello everyone. I'm writing a program which uses a class called matrix. I have written all of the different functions, constructor, etc. When I run the program I receive "Constructor", which I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.