#include<stdio.h>
int main()
{
setbuf(stdout,NULL);
int p,q,r,s,a[p][q],b[r][s],i,j,k,u,v,res[u][v],sum=0;
printf("Enter the number of rows and columns of the 1st matrix: ");
scanf ("%d%d",&p,&q);
printf("Enter the number of rows and columns of the 2nd matrix: ");
scanf ("%d%d",&r,&s);
printf("Enter the elements of matrix1: ");
u=p;
v=s;
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of matrix2: ");
for(i=0;i<r;i++)
{
for(j=0;j<s;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<p;i++)
{
for(j=0;j<s;j++)
{
for(k=0;k<r;k++)
{
sum+=a[i][k]*b[k][j];
}
res[i][j]=sum;
sum=0;
}
}
printf("The resultant matrix is: ");
for(i=0;i<p;i++)
{
for(j=0;j<s;j++)
{
printf("%d\t",res[i][j]);
}
printf("\n");
}
return 0;
}
**I'm trying to write a program to perform Matrix Multiplication. The code isn't getting executed...its just terminating and I really cant find the error. When I tried running it online I got ''Bus error(Code Dumped)error 135"...but in my system the programs just terminating without an error. Please help me find the mistake or concept I'm missing here.. **
In the code
int p,q,r,s,a[p][q],b[r][s],i,j,k,u,v,res[u][v],sum=0;
you are using the values of p, q, r, s, u and v uninitialized. As they have automatic storage (local scope) and the type int can have trap representation, and the variables u and v never have their address taken, it'll invoke undefined behaviour. Even for the other variables other than u and v, the values will be indeterminate, resulting in practically invalid code.
To resolve the problem, define the VLAs after you scan the values into the respective variables to be used as array dimension.
Related
The code below shows segmentation error.
The error prevails
Tried many changes but of no use
#include<conio.h>
#include<stdio.h>
void main()
{
static int a[100][100],b[100][100],c[100][100]={0};
int m,n,p,i,j,k;
printf("Enter no pf rows and colums for matrix A");
scanf("%d%d",&m,&n);
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("enter no.");
scanf("%d",&a[i][j]);
}
}
printf("Enter no. of column for matrix b");
scanf("%d",&p);
for(i=0;i<=n-1;i++)
{
for(j=0;j<=p-1;j++)
{
printf("enter no.");
scanf("%d",&b[i][j]);
}
}
for(i=0;i<=m-1;i++)
{
for(j=0;i<=p-1;j++)
{
c[i][j]=0;
for(k=0;k<=n-1;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf(" The resultant matrix is");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=p-1;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}
Tried it on turbo c++
Error 139
Segmentation error
Error 139 Segmentation error
this is because
for(j=0;i<=p-1;j++)
must be
for(j=0;j<=p-1;j++)
currently j is increased without end so c[i][j]=0; and other accesses are out of the array with an undefined behavior (your segmentation fault)
Other remarks :
to do for(j=0; j < p; j++) is a better choice compatible with for instance size_t with is the right type for an index
I strongly encourage you to check if your scanf success, currently you do not know if valid numbers was enter, for instance
if (scanf("%d%d",&m,&n)!= 2)
fprintf(stderr, "invalid numbers");
else {
also check when necessary the enter values are > 0, this must be the case for the number of rows and columns
I typed the following code to sort the components of an int array. It does not show any error but does stops working abruptly. The error is generally after entering 7-8 inputs which shows that program.exe has stopped working. Does it has anything related to the code ?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,a[n],i,j,temp;
printf("Enter number of inputs.\n");
scanf("%d",&n);
printf("Enter inputs\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("Numbers in descending order are:\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
return 0;
}
The problem is here:
int n, a[n], i, j, temp;
Declarations are done sequentially. If you write this in a slighly more readably (but equivalent form) you'd have this:
int n;
int a[n]; // here the variable n has not yet been initialized
// it contains an indeterminate value, and therefore the a array
// will have an indeterminate size and the program will have
// so called "undefined behaviour " (google that)
int i;
...
You should write the beginning of your program like this:
int main()
{
int n,i,j,temp;
printf("Enter number of inputs.\n");
scanf("%d",&n);
int a[n]; // now n has a determinate value
printf("Enter inputs\n");
Disclaimer: no error checking is done for brevity.
Always compile with warnings enabled and listen to them. Many of them are actually errors. Especially the warning variable 'somevar' is uninitialized when used here is always an error.
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]);
}
}
I'm writing a program that requires me to do a union of two arrays. Here is my code so far.
I get Segmentation fault as an error after I enter set A.
#include <stdio.h>
void Union(int a[], int b[], int set1, int set2)
{
int u[20], i, j, unionIndex=0,trigger;
for(i=0; i<set1; i++)
{
u[unionIndex] = a[i];
unionIndex++;
}
for(i=0; i<set2; i++)
{
trigger=0;
for(j =0; j<set1; j++)
{
if(b[i] == u[j])
{
trigger =1;
break;
}
}
if(trigger =0)
{
u[unionIndex]=b[i];
unionIndex++;
}
}
for(i=0;i<unionIndex;unionIndex++)
{
printf(" %d",u[i]);
}
}
int main(void) {
int N=0;
int M=0;
int i;
int j;
printf("Please enter the number of elements in set A: ");
scanf("%d",N );
int a[N];
printf("Enter the numbers in set: ");
for(i=0;i<N;i++)
{
scanf("%d",&a[i]);
}
printf("Please enter the number of elements in set B: ");
scanf("%d",M );
int b[M];
printf("Enter the numbers in set: ");
for(j=0;i<M;i++)
{
scanf("%d",&b[i]);
}
Union(a,b,N,M);
return 0;
}
I'm pretty sure the issue has something to do with arrays because the program will compile but i get the error right after the user enters set A. I'm a beginner at C but I know a lot more about Java, so I'm thinking this has something to do with memory allocation. I'm not really sure how to solve the issue, so if you could point me in the right direction that would be helpful.
You need to pass the address of the variable to scanf()
Change
printf("Please enter the number of elements in set A: ");
scanf("%d",N );
to
printf("Please enter the number of elements in set A: ");
scanf("%d", &N);
Same goes for other place
printf("Please enter the number of elements in set B: ");
scanf("%d", &M);
There is another possible mistake
Its here
for(j =0; j<set1; j++)
{
if(b[i] == u[j])
In this set1 is equal to N, so j will go from 0 to N-1. And array u[] has only 20 elements. There is a possibility of array access out of bound if some user enter value more then 20 for N.
The problem, as I see it is in
scanf("%d",N );
and
scanf("%d",M );
It invokes undefined behavior as scanf() needs the argument to a format specifier to be a pointer to the type.
Just to clarify, you're essentially passing the address as 0 (value of the variable), which is not a valid addres, anyway.
You need to pass the address there, like
scanf("%d", &N );
and
scanf("%d", &M );
That said, in your Union() function, you're using a user-defined value to limit the for loop, against a constant value 20. In case the user input is more than 20, you'll be overrunning the memory which invokes undefined behavior.
The reason you're getting the segmentation fault is because of how you're calling scanf when reading in N and M. The %d format specifier for scanf expects an int *, i.e. the address of an int, but you're passing in an int. This is undefined behavior.
So you can fix them like this:
scanf("%d",&N );
....
scanf("%d",&M );
Some addtional bugs:
When looping to read in the values for b:
for(j=0;i<M;i++)
{
scanf("%d",&b[i]);
}
You have the wrong loop indexes:
for(j=0;j<M;j++)
{
scanf("%d",&b[j]);
}
When checking trigger:
if(trigger =0)
This is an assignment, not a comparison:
if(trigger == 0)
When looping to print out u:
for(i=0;i<unionIndex;unionIndex++)
You're incrementing the wrong variable:
for(i=0;i<unionIndex;i++)
Finally, u need to have a length of at least set1 + set2, otherwise you risk writing off the end of the array:
int u[set1+set2];
Fix those and you should get the desired results.
I'm trying to make the letters of a matrix sort alphabetically and then be written out in a single string.For instance you type ten words,which are then stored in an array,and every letter has its place in the matrix then,right?But after I've written the words I want to bunch all the letters of all words together and then type all the letters out in alphabetical order.This is what I have so far:
#include <stdio.h>
#include <conio.h>
int main(void){
int i, j, k, f, n, m;
//was trying out various things,that's why I have so many useless ints up there
char word[10][15],temp;
for(i=0;i<=9;i++)
{
printf("Type in wword number %d: ", i+1);
gets(word[i]);
}
for(k=i-1;k>=0;k--)
{
for(m=0;m<k;m++)
if(word[k][f] > word[m][n])
{
temp=word[k][f];
word[k][f]=word[m][n];
word[m][n]=temp;
}
}
printf("Letters alphabetically sorted: ");
for(i=0;i<=9;i++){
for(j=0;j<=14;j++){
printf("%d",word[i][j]);
}
}
printf("\n");
getch();
}
I'm still in the process of learning about matrixes and I've gotten pretty familiar with arrays by now.But the sorting thing is confusing me,this was my attempt but it doesn't work.It lets you write all the words,and then it crashes.
What am I doing wrong here?And how do I correct it?
In your code here:
temp=word[k][f];
word[k][f]=word[m][n];
word[m][n]=temp;
the variables n and f are used uninitialised. That will most likely be the cause of the crash.
f,n are uninitialized. It has garbage and is the reason for crashing at this point.
for(k=i-1;k>=0;k--)
{
for(m=0;m<k;m++)
if(word[k][f] > word[m][n]) // f,n are uninitialized and are error prone
I think this will work..Please excute and tell me..
void main()
{
char word[10][15],temp,sorted_word[15];
int i,j,ii,k,l=0;
for(i=0;i<=9;i++)
{
printf("Type in wword number %d: ", i+1);
gets(word[i]);
}
for(i=0;i<=9;i++)
{
for(j=0;word[i][j]!='\0';j++)
{
ii=i;
for(k=j+1;1;k++)
{
if(ii==9 && word[ii][k]=='\0')
break;
if(word[ii][k]=='\0')
{
ii++;
k=0;
}
if(word[i][j]>word[ii][k])
{
temp=word[i][j];
word[i][j]=word[ii][k];
word[ii][k]=temp;
}
}
sorted_word[l++]=word[i][j];
}
}
sorted_word[l]='\0';
printf("%s",sorted_word);
getch();
}
here the
for(i=0;i<=9;i++)
{ printf("type in wword %d: ",i+1);
gets(word[i]);
}
gets (word[1]);
stores the value from word[1] onwards but where as the character array starts from
word[0].
may be this is not the full solution for u problem
this issue may help u in solving your doubt.