Why this program stops working at runtime? - c

It successfully compiles.
But at running on getting values of matrix it crashes stops working.
#include <stdio.h>
void getmat(int mat[100][100],int m,int n);
void matmul(int mat1[100][100],int mat2[100][100],int m1,int n1,int m2,int
n2,int matmul[100][100]);
void printmat(int matmul[100][100],int m,int n);
int main(void)
{
int m1,n1,m2,n2;
printf("Enter the dimensions of matrix1: ");
scanf("%d %d",&m1,&n1);
printf("Enter the dimensions of matrix2: ");
scanf("%d %d",&m2,&n2);
int mat1[m1][n1];
int mat2[m2][n2];
int matmul1[m1][n2];
int matmul2[m2][n1];
printf("For the values of matrix 1\n");
getmat(mat1,m1,n1);
printf("For the values of matrix 2\n");
getmat(mat2,m2,n2);
if(n1==m2)
{
printf("Mat1 x Mat2 is possible.");
matmul(mat1,mat2,m1,n1,m2,n2,matmul1);
printf("Mat1 x Mat2 :\n");
printmat(matmul1,m1,n2);
}
else
printf("Mat1 x Mat2 is not possible.\n");
if(n2==m1)
{
printf("Mat2 x Mat1 is possible.");
matmul(mat2,mat1,m2,n2,m1,n1,matmul2);
printf("Mat2 x Mat1 :\n");
printmat(matmul2,m2,n1);
}
else
printf("Mat2 x Mat1 is not possible.\n");
return 0;
}
void printmat(int matmul[100][100],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%3d ",matmul[i][j]);
}
printf("\n");
}
}
void getmat(int mat[100][100],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("Enter element of %dx%d: ",i+1,j+1);
scanf("%d",&mat[i][j]);
}
}
}
void matmul(int mat1[100][100],int mat2[100][100],int m1,int n1,int m2,int
n2,int matmul[100][100])
{
int i,j,k;
for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)
{
matmul[i][j]=0;
for(k=0;k<m2;k++)
{
matmul[i][j]+=mat1[i][k]*mat2[k][j];
}
}
}
}
Help making any changes or optimizing this code.
Also another way to this.
This error shows up at running a half

Use a Debugger
Also, all your functions are written to take a 100x100 matricies.
But you declare your matricies to have variable sizes:
int mat1[m1][n1];
int mat2[m2][n2];
int matmul1[m1][n2];
int matmul2[m2][n1];
When you pass a 3x3 matrix to a function that is expecting a 100x100 matrix, you will definitely have a bad time.

I see that you are setting up your arrays with variable sizes. You should really set it up as a very large array or use malloc to set up the array properly. When you call the processing routines, pass the arrays as pointers and use the sizes as arguments in order to set up your references. You should note that myarray[i, j] is the equivalent of a single valued array using i*rowsize + j. When you define your array in the subroutines as [100, 100], it will go to [i*100 + j] which is outside the bounds of your actual array.
This causes the program to crash.
Once you actually calculate everything properly, it should work.

Related

scanf function for pointer is not working (for a matrix using pointer to pointer)

this is the program I have written for multiplying two matrices.
#include <stdio.h>
#include <stdlib.h>
void allocate(int **mat,int m,int n)
{
int i;
mat = (int**)malloc(m*sizeof(int*));
for(i=0;i<m;i++)
*(mat+i) = (int*)malloc(n*sizeof(int));
}
void read(int **mat,int m,int n)
{
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
printf("Enter the element in row number %d and column number %d\n",i+1,j+1);
scanf("%d",*(mat+i)+j);
}
}
void multiply(int **mat1,int m,int n,int **mat2,int p,int **prod)
{
int i,j,k;
for(i=0;i<m;i++)
for(j=0;j<p;j++)
{
*(*(prod+i)+j) = 0;
for(k=0;k<n;k++)
*(*(prod+i)+j) += (*(*(mat1+i)+k))*(*(*(mat2+k)+j));
}
}
void PRINT(int **mat,int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",*(*(mat+i)+j));
}
printf("\n\n\n");
}
}
int main()
{
int m,n,p,**mat1,**mat2,**prod;
printf("Enter the number of rows of the first matrix to be multiplied\n");
scanf("%d",&m);
printf("Enter the number of columns of the first matrix to be multiplied\n");
scanf("%d",&n);
printf("Enter the number of columns of the second matrix to be multiplied\n");
scanf("%d",&p);
allocate(mat1,m,n);
allocate(mat2,n,p);
allocate(prod,m,p);
printf("Enter the entries of the first matrix\n");
read(mat1,m,n);
printf("Enter the entries of the second matrix\n");
read(mat2,n,p);
printf("The first input matrix is\n");
PRINT(mat1,m,n);
printf("The second input matrix is\n");
PRINT(mat2,n,p);
multiply(mat1,m,n,mat2,p,prod);
printf("The product matrix is\n");
PRINT(prod,m,p);
return 0;
}
The scanf function used in the read function definition is not working, it just doesn't allow us to give any input and stops unexpectedly. I have used it the same way in another program to find the trace of a matrix and it is fine there.
Please help me finding the error.
Your allocate function receives a copy of the int ** passed as its first argument and, thus, the variables used as this argument in your main function are not modified by it.
To get round this, you could pass the argument as a "pointer to int**" (i.e. int*** mat) - but this starts to get messy (and you'll need to adjust the code inside allocate accordingly). A better way is to redefine the allocate function to return the created pointer, as such:
int** allocate(int m, int n)
{
int i;
int** mat = malloc(m * sizeof(int*));
for (i = 0; i < m; i++)
*(mat + i) = malloc(n * sizeof(int));
return mat;
}
Then, in your main function, modify the calls to allocate as follows:
mat1 = allocate(m, n);
mat2 = allocate(n, p);
prod = allocate(m, p);
You will (of course) need the proper code (at some point) to free the allocated matrices.
Also, see Do I cast the result of malloc?
Your allocation function is very wrong.
Currently, you allocate the local variable mat in allocate and you don't return it nor pas it by pointer, thus, the value is lost at the end of the function (in c, arguments are always passed by value) and you write on invalid data. You have an undefind behavior and as you are lucky, your program segfaults.
Therefore, when you allocate a data, you have to allocate a pointer to this data. Here your data is a int** . That means that your allocate function must take an int*** as an input.
Your program should work with :
void allocate(int ***mat, int m,int n)
{
int i;
*mat = (int**)malloc(m*sizeof(int*));
for(i=0;i<m;i++)
*(*mat+i) = (int*)malloc(n*sizeof(int));
}
Another (maybe cleaner) way to deal with that issue is to return the allocated mat, like below
int** allocate(int m,int n)
{
int i;
int **mat = (int**)malloc(m*sizeof(int*));
for(i=0;i<m;i++)
*(mat+i) = (int*)malloc(n*sizeof(int));
return mat;
}
Also, to debug this kind of issue, valgrind could be really helpful the next time.

Can someone please explain why my program crashes?

I don t get any errors at compilation. The program just crashes when I run it. I tried to print the matrix directly from the generate function and it printed the first line and a bit of the second.
This is my code
void generate(int **a)//*Function to generate a matrix a[i][j]=i+j*
{
int i,j;
for(i=0;i<5;i++){
for(j=0;j<4;j++){
a[i][j]=i+j;
}
}
}
void print(int **a)//*print the resulting matrix from generate function*
{
int i,j;
for(i=0;i<5;i++){
for(j=0;j<4;j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
}
int main()
{
int *a=(int*)malloc(5*4*sizeof(int));//*allocating memory for a matrix of 4 lines and 5 columns.*
generate(&a);
print(&a);
}
1) you are allocating a single dimension memory.
a[i][j]=i+j; //is not valid.
Below is the modified code
#include <stdio.h>
#include <stdlib.h>
void generate(int *a)//*Function to generate a matrix a[i][j]=i+j*
{
int i,j;
for(i=0;i<5;i++){
for(j=0;j<4;j++){
*a=i+j;
a++; //increments to next memory location
}
}
}
void print(int *a)//*print the resulting matrix from generate function*
{
int i,j;
for(i=0;i<5;i++){
for(j=0;j<4;j++){
printf("%d ",*(a++)); //notice another way of accessing
}
printf("\n");
}
}
int main()
{
int *a=(int*)malloc(5*4*sizeof(int));//*allocating memory for a matrix of 4 lines and 5 columns.*
generate(a);
print(a); //passing the pointer
free(a); //Always always practice to free the allocated memory, don't ever do this mistake again
return 0;
}
Two things:
First, int ** denotes a pointer to a pointer that points to an int, not a pointer that points to an array of ints.
Second, when you just pass a pointer to some data structure, e.g. an 4x5 array of integers, then the compiler cannot derive the layout of this data structure. I.e. a statement like a[i][j] would require that the compiler "knows" that each row i consists of 4 columns j, such that it can calculate the "place" to which the value should be stored, i.e. a + (4*i) + j. The compiler simply does not know the nr of columns per row, i.e. 4.
To overcome this while keeping the size of the array at least potentially variable (note that "4" and "5" are still hard coded in the function), you could do the following:
void generate(int *a)//*Function to generate a matrix a[i][j]=i+j*
{
int i,j;
for(i=0;i<5;i++){
for(j=0;j<4;j++){
*(a+(i*4+j)) = i+j;
}
}
}
void print(int *a)//*print the resulting matrix from generate function*
{
int i,j;
for(i=0;i<5;i++){
for(j=0;j<4;j++){
printf("%d ", *(a+(i*4+j)));
}
printf("\n");
}
}
int main()
{
int *a=(int*)malloc(5*4*sizeof(int));//*allocating memory for a matrix of 4 lines and 5 columns.*
generate(a);
print(a);
}

Error while calculating sum of two matrices in c

So I'm new to C and was trying to write a program to add two matrices
program 1
#include <stdio.h>
int main(){
int m,n,o,p,i,j;
int mat1[m][n];
int mat2[m][n];
int result[m][n];
printf("enter the number of rows and columns for matrix ");
scanf("%i%i",&m,&n);
printf("enter elements of matrix one :");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%i",&mat1[i][j]);
}
}
printf("enter the elements of matrix two:");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%i",&mat2[i][j]);
}
}
for(i=0;i<m;i++){
for(j=0;j<n;j++){
result[i][j]=mat1[i][j]+mat2[i][j];
}
}
printf("the sum of the matrices are");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf("%i",result[i][j]);
}
}
return 0;
}
this produced no error
method two
when i used a function to enter the values in two the matrix, it produced the following error
25 24 C:\Users\Hp\my-programs\matrix-entry-function.cpp [Error] invalid types 'int[int]' for array subscript
25 35 C:\Users\Hp\my-programs\matrix-entry-function.cpp [Error] invalid types 'int[int]' for array subscript
code:
#include <stdio.h>
int mat_entry(int m,int n)
{
printf("enter the rows and columns of matrix ");
scanf("%i%i",&m,&n);
int mat[m][n];
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
printf("\nenter the %i'th element %i'th row :",j+1,i+1);
scanf("%i",&mat[i][j]);
}
}
}
int main()
{
int a,b,c,d,e,f,m,n;
int res[m][n];
int mat1=mat_entry(a,b);
int mat2=mat_entry(c,d);
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
res[i][j]=(mat1[i][j]+mat2[i][j]);
}
}
}
Your program 1 happened not to produce error. You are unlucky. You invoked undefined behavior by using values in uninitialized variables having automatic storage duration, which are indeterminate. You must declare mat1, mat2 and result after reading m and n.
int main(){
int m,n,o,p,i,j;
printf("enter the number of rows and columns for matrix ");
scanf("%i%i",&m,&n);
/* move these declaretions after reading number of rows and columns */
int mat1[m][n];
int mat2[m][n];
int result[m][n];
printf("enter elements of matrix one :");
Your program 2 invokes undefined behavior by using return values of functions with no return statements. Also using [] operator with two operands having type int is wrong. E1[E2] is equivalent to (*((E1)+(E2))) (N1570 6.5.2.1 Array subscripting, paragraph 2), so exactly one operand of it must be a pointer (including one converted from arrays).
To return matrixes as return values of functions, consider using structures and allocating memory for elements dinamically.
You have used
int mat1=mat_entry(a,b);
int mat2=mat_entry(c,d);
instead of sending parameters from one function to another, which would work only when the variables you take are pointers, here you have to either use mat1 and mat2 as pointers or you have to make them integer matrices.
There isn't one thing that you are doing wrong my friend.
You are not even using dynamic memory allocation but still trying to declare matrices with values which are not defined yet.Try to run your code,even your first code won't run.
i have corrected your code for you :
#include <stdio.h>
int main(){
int m,n;
printf("enter the number of rows and columns for matrix ");
scanf("%i%i",&m,&n);
int mat1[m][n];
int mat2[m][n];
int result [m][n];
int i,j;
printf("enter elements of matrix one :");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",&mat1[i][j]);
}
}
printf("enter the elements of matrix two:");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",&mat2[i][j]);
}
}
for(i=0;i<m;i++){
for(j=0;j<n;j++){
result[i][j]=mat1[i][j]+mat2[i][j];
}
}
printf("the sum of the matrices are");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf("%d",result[i][j]);
}
}
return 0;
}
I don't think it would be that difficult to put this functionality into a function . Try it out
It is impossible to write a function that makes a matrix with user-supplied dimensions, unless you use dynamic allocation (which you probably don't want to, at this stage).
Luckily, you don't need that! Since all you want is adding matrices, they should have the same dimensions, and you only want to ask the user once what the dimensions are. Afterwards, fill both matrices, and do the "adding" part.
That is:
int main()
{
int a,b,c,d,e,f,m,n;
printf("enter the rows and columns of matrix ");
scanf("%i%i",&m,&n);
int res[m][n];
int mat1[m][n];
int mat2[m][n];
mat_entry(m, n, mat1); // fill matrix 1
mat_entry(m, n, mat2); // fill matrix 2
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
res[i][j]=(mat1[i][j]+mat2[i][j]);
}
}
}
The function mat_entry has no return value (i.e. void), but it receives the matrix to fill by pointer (the details are a bit complicated, but the syntax is straightforward):
void mat_entry(int m,int n, int mat[m][n])
{
...
}
The code inside is the same as you suggested, minus the asking for dimensions:
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
printf("\nenter the %i'th element %i'th row :",j+1,i+1);
scanf("%i",&mat[i][j]);
}
}

Passing arrays into functions, file.exe stopped working

I am a complete beginner in C and I am practicing passing arrays into functions. I wrote a program to take a two dimensional array as input and find sum of the individual columns.
And when I compiled the program I got no errors, but once I run it, I get a dialogue box saying "untitled5.exe stopped working" where untitled5 is the file name.
I got this error quite a few times. I have used both dev c++ and codeblocks to compile my program, so what is the reason for this? Is this a problem with my code or with my compiler or with my laptop?
#include<stdio.h>
void summation (int arr[][5], int size);
int main()
{
int n,arr[n][5],sum,i,j;
printf("enter the number of rows");
scanf("%d",&n);
for (i=0;i<n;i++)
{
for (j=0;j<5;j++)
{
printf("%d,%d th element is",i,j);
scanf("%d",&arr[i][j]);
}
}
summation (arr,5);
return 0;
}
void summation (int arr[][5], int size)
{
int i,j,s=0;
for(j=0;j<5;j++)
{
for (i=0;i<5;i++)
{
s=s+arr[i][j];
}
printf("%d",s);
}
}
In main() you are using i to index the first dimension of the array. In summation() you are using i to index the second dimension of the array. I think that you are going beyond the end of the first dimension inside summation() when main() does not fill up that much of the array (e.g., when you enter 2 for the number of rows).
I think you want
summation (arr,5);
And, inside summation():
for (i=0;i<size;i++)
{
s=s+arr[i][j];
}
#include<stdio.h>
void summation (int arr[][5], int size, int rows);
int main()
{
int n, sum, i, j;
printf("enter the number of rows");
scanf("%d",&n);
int arr[n][5];
for (i=0;i<n;i++)
{
for (j=0;j<5;j++)
{
printf("%d,%d th element is",i,j);
scanf("%d",&arr[i][j]);
}
}
summation (arr, 5, n);
return 0;
}
void summation (int arr[][5], int size, int rows)
{
int i,j,s=0;
for(i=0;i<rows;j++)
{
for (j=0;i<size;i++)
{
s=s+arr[i][j];
}
}
printf("%d",s);
}
So first off I moved your array declaration to after you have initialized n and made it equal to something.
Then your next problem was you were probably going out of bounds in your summation function. You always have 5 columns in your 2darray, but you can have a different amount of rows. Pass the amount of rows, n, into the function summation to make sure you don't go out of bounds.

Intersection of 2 vectors, logical error

I want to write a code (in c) that prints the intersection of two vectors of "N" lenght and cannot print the same number twice. The vectors will be filled with a function, that reads the input ( any number), one by one. And after all, the code needs to print another vector with the intersection between the first two vectors. Until now i wrote the code below, but it has a logical error that i cant solve.
#include <stdlib.h>
#include <stdio.h>
void preencheVetor(int* v, int tamanho){
int i=0;
for(i=0;i<tamanho;i++){
scanf("%d", *(v+i));
printf("\n");
}
}
void interVetor(int* v1, int* v2, int tamanho){
int* v3=malloc(sizeof(int)*tamanho);
int i1=0;
int i2=0;
int i3=0;
int c=0;
for(i1=0;i1<tamanho;i1++){
for(i2=0;i2<tamanho;i2++){
if((v1[i1])==(v2[i2])){
*(v3+c)=v2[i2];
c++;
}
}
}
for(i3=0;i3<tamanho;i3++){
printf("%d", *(v3+i3));
printf("\n");
}
}
int main(){
int n=0;
printf("Vectors Lenght:\n");
scanf("%d", &n);
printf("\n\n");
int v1[n];
int v2[n];
preencheVetor(v1, n);
printf("\n\n");
preencheVetor(v2, n);
printf("\n\n");
interVetor(v1, v2, n);
system("PAUSE");
return 0;
}
The size of vector v3 is c not tamanho. Fix the for(i3...) loop.
Other than that, I think your intersection works OK.
It is very inefficient, of course, if the vectors are not sorted.

Resources