Matrix problem

B

beginner10

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];

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


}
}
}



}
 
J

Joona I Palaste

beginner10 said:
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>

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];

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 ([email protected]) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"The large yellow ships hung in the sky in exactly the same way that bricks
don't."
- Douglas Adams
 
N

Nick Keighley

beginner10 said:
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];


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
 
J

Jens.Toerring

beginner10 said:
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>

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];


'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
 
J

Joona I Palaste

beginner10 said:
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.
 
F

Flash Gordon

beginner10 said:
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];


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.
 
R

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[j]);
for ( i=0;i<10;i++)
for( j=0;j<10;j++)
fscanf(matb,"%d",&b[j]);
for ( i=0;i<10;i++)
{
for( j=0;j<10;j++)
fprintf(sum,"%4d",a[j]+b[j]);

fprintf(sum,"\n");
}

printf("done");

return 0;
}
 
B

beginner10

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.
 
N

Nick Keighley

beginner10 said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,157
Messages
2,570,879
Members
47,414
Latest member
djangoframe

Latest Threads

Top