ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.

Having a problem logging in? Please visit this page to clear all LQ-related cookies.

Introduction to Linux - A Hands on Guide

This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.

Click Here to receive this Complete Guide absolutely free.

Hi guys I'm having a problem that I do not know nor have I found a way to solve. Let's say I have a 10x2 matrix, I create said matrix and fill it one column with zeros and the other with ones, now when I print the matrix I get this:

The first two rows of the second column are being replaced into the last two rows of the first column, now I even checked in visual studio and it works fine there. A friend tried my code and he gets it even worse:
column 1:
0,0,0,0,1,1,1,1,1,1
column 2:
1,1,1,1,1,1,1,1,1,1

As far as I've seen it must be a problem with GCC, unfortunately I need to have this up and running in GCC no matter what. Here I leave the test code. So if you guys have a solution to this I'm all ears.Thanks in Advance.

Code:

#include<stdio.h>
#include<stdlib.h>
int **NxN_Matrix(long rows, long columns);
int main()
{
int **test;
int i,j;
test = NxN_Matrix(10,2);
for(i = 0; i < 2; i++)
{
for(j = 0; j < 10; j++)
{
test[i][j] = i;
}
}
for(i = 0; i < 2; i++)
{
for(j = 0; j < 10; j++)
{
printf("[%d]\n",test[i][j]);
}
printf("\n");
}
return 0;
}
int **NxN_Matrix(long rows, long columns)
{
long i;
int **matrix;
matrix = (int**)calloc(rows,sizeof(int*));
for(i = 0; i < rows; i++)
{
matrix[i] = (int*)calloc(columns, sizeof(int));
}
return (matrix);
}

To debug your code, I changed it a little bit:
This shows that the address assigned to test[0][8] is the same address as test[1][0], so it gets overwritten;

I need more coffee to see where the calculation goes wrong.

Code:

#include<stdio.h>
#include<stdlib.h>
int **NxN_Matrix(long rows, long columns);
int main()
{
int **test;
int i,j;
test = NxN_Matrix(10,2);
for(i = 0; i < 2; i++)
{
for(j = 0; j < 10; j++)
{
test[i][j] = i;
printf("Assigned: test[%d][%d](%p)=%d; Value=%d\n",i,j,&test[i][j],i,test[i][j]);
}
printf("\n");
}
for(i = 0; i < 2; i++)
{
for(j = 0; j < 10; j++)
{
printf("%d x %d = [%d]\n",i,j,test[i][j]);
}
printf("\n");
}
return 0;
}
int **NxN_Matrix(long rows, long columns)
{
long i;
int **matrix;
matrix = (int**)calloc(rows,sizeof(int*));
printf("matrix=%p\n",matrix);
for(i = 0; i < rows; i++)
{
matrix[i] = (int*)calloc(columns, sizeof(int));
printf("%p[%d]=%p\n",matrix,i,matrix[i]);
}
return (matrix);
}

#include<stdio.h>
#include<stdlib.h>
int **NxN_Matrix(int rows, int columns);
int main()
{
int **test;
int i,j;
int r=12, c=3;
test = NxN_Matrix(r,c);
for(i = 0; i < c; i++)
{
for(j = 0; j < r; j++)
{
test[i][j] = i;
}
}
for(i = 0; i < c; i++)
{
for(j = 0; j < r; j++)
{
printf("[%d]\n",test[i][j]);
}
printf("\n");
}
return 0;
}
int **NxN_Matrix(int rows, int columns)
{
int i;
int **matrix;
matrix = (int**)calloc(columns, sizeof(int));
for(i = 0; i < columns; i++)
{
matrix[i] = (int*)calloc(rows, sizeof(int));
}
return (matrix);
}

I've swaped rows and columns in the allocation part, now it works for me. It turns out, that your code works with rows up to a size of 8, but with longer rows you'll get wrong numbers on any position higher than 7.

LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.