Hi,

I am writing a C code to add two matrices. I want to take input with a feel of real martice, some thing like

1 2 3
4 5 6
7 8 9

but the Scanf function automatically enter EOL and hence my input looks some thing like this,

1
2
3

4
5
6

7
8
9

any idea how to get this thing straight?

Here is the Code :

// Write a program to add two matrices.

#include <stdio.h>
#include <conio.h>

void main()

{
		int row,column,**ptrA,**ptrB,**ptrC,counter1,counter2;
		clrscr();

		printf("Enter the number of row of the matrices to be processed:");
		scanf("%d",&row);

		printf("\nEnter the number of columns of the matrices to be processed:");
		scanf("%d",&column);

		ptrA=(int**)(calloc(row,sizeof(int *)));
		ptrB=(int**)(calloc(row,sizeof(int *)));
		ptrC=(int**)(calloc(row,sizeof(int *)));

		for (counter1=0;counter1<row;counter1++)
		{
			*(ptrA+counter1)=(int*)(calloc(column,sizeof(int)));
			*(ptrB+counter1)=(int*)(calloc(column,sizeof(int)));
			*(ptrC+counter1)=(int*)(calloc(column,sizeof(int)));
		}

		printf("\n\n\nEnter the values for First Matrice:\n\n\n");

		for (counter1=0;counter1<row;counter1++)
		{
			for (counter2=0;counter2<column;counter2++)
			{
				
			       printf(" \t");

			       scanf("%d",**((ptrA+counter1)+counter2));


			}
			printf("\n");
		}

Replies would be greatly appreciated!

--
Theprofoundgeek

P.s : Not - so - profound after this problem

Edited 7 Years Ago by theprofoundgeek: n/a

for (counter1=0;counter1<row;counter1++)
{
	*(ptrA+counter1)=(int*)(calloc(column,sizeof(int)));
	*(ptrB+counter1)=(int*)(calloc(column,sizeof(int)));
	*(ptrC+counter1)=(int*)(calloc(column,sizeof(int)));
}

(i)
why are you allocating memory for ptrB and ptrC.
you are not using them at all.
(ii)

scanf("%d",**((ptrA+counter1)+counter2));

is wrong, it should be

scanf("%d",&(**((ptrA+counter1)+counter2)));

if you want to allocate memory for two D array.

you can do it simply

int *p;
          int row,col;
          p=(int *)malloc(row*col*sizeof(int));

to read the element into array
you could do it simply

for(i=0;i<row;i++)
              for(j=0;j<col;j++)
                 scanf("%d",(p+i*row+j));

and to print the array

for(i=0;i<row;i++)
         {
             for(j=0;j<col;j++)
               printf("%d",*(p+i*row+j));
           printf("\n");
        }

this is very simplest way of readind elements into matrices.
the 2D veiw is only for our convenience, in memory every thing is contiguous.

C programs do not need all those typcasts. Only C++ requires them.

Why are you using all those difficult-to-follow pointer arithmetic? It would be much simpler and clearer to just use normal array indices via [] operator.

ptrB= calloc(row,sizeof(int *));
		ptrC= calloc(row,sizeof(int *));

		for (counter1=0;counter1<row;counter1++)
		{
			ptrA[counter1] = calloc(column,sizeof(int));
			ptrB[counter1] = calloc(column,sizeof(int));
			ptrC[counter1] = calloc(column,sizeof(int));
		}
		for (counter1=0;counter1<row;counter1++)
		{
			for (counter2=0;counter2<column;counter2++)
			{		
			       printf(" \t");
			       scanf("%d", &ptrA[counter1][counter2]);
			}

>>any idea how to get this thing straight?
Yes -- do not type <Enter> after each number

Edited 7 Years Ago by Ancient Dragon: n/a

C programs do not need all those typcasts. Only C++ requires them.

Why are you using all those difficult-to-follow pointer arithmetic? It would be much simpler and clearer to just use normal array indices via [] operator.

ptrB= calloc(row,sizeof(int *));
		ptrC= calloc(row,sizeof(int *));

		for (counter1=0;counter1<row;counter1++)
		{
			ptrA[counter1] = calloc(column,sizeof(int));
			ptrB[counter1] = calloc(column,sizeof(int));
			ptrC[counter1] = calloc(column,sizeof(int));
		}
		for (counter1=0;counter1<row;counter1++)
		{
			for (counter2=0;counter2<column;counter2++)
			{		
			       printf(" \t");
			       scanf("%d", &ptrA[counter1][counter2]);
			}

>>any idea how to get this thing straight?
Yes -- do not type <Enter> after each number

C programs do not need all those typcasts. Only C++ requires them.

i have come across this lines often in the forum.

but is it not a better idea/or method to use explicit type casting.

Gaiety:
i have come across this lines often in the forum.

but is it not a better idea/or method to use explicit type casting

Yeah, Gaiety, its always preferable to type cast the data type then convert it in every instance of accessing the data. (At least this is what I have been told up to now!)

Yeah, Gaiety, its always preferable to type cast the data type then convert it in every instance of accessing the data. (At least this is what I have been told up to now!)

Casting the return of dynamic memory allocation functions should not be made a habit of, in C. If the standard header file stdlib.h is included, there's not need of casting and it can avoid subtle errors.

Casting the return of dynamic memory allocation functions should not be made a habit of, in C. If the standard header file stdlib.h is included, there's not need of casting and it can avoid subtle errors.

Understood! Thanks Aia! :)

Casting the return of dynamic memory allocation functions should not be made a habit of, in C. If the standard header file stdlib.h is included, there's not need of casting and it can avoid subtle errors.

i dont understand how not casting return value can avoid errors, please just brief about that.

This article has been dead for over six months. Start a new discussion instead.