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
Related
(QW) Write a well documented modular C program to compute SGPA of 2 students of particular semester containing 3 courses, suitably using structures.
Getting garbage value as output scanning of elements goes perfectly but when it comes to printing it displays random number as output, Is the sgpa calculator part of the code correct?
//reaplced courses with 3 and no of students with 2
#include<stdio.h>
struct studentlist
{
int Rno;
char name[20];
int course_marks[8];
}S[2];
int main()
{
int sgpa[2];
printf("ENTER DETAILS\n");
for(int i=0;i<2;i++)
{
printf("enter details of student %d\n",i);
scanf("%d %s",&S[i].Rno,S[i].name);
for(int j=0;j<3;j++)
{
printf("enter marks coursenumber %d \n",j);
scanf("%d",S[i].course_marks[j]);
}
}
//printing details
for(int i=0;i<2;i++)
{
printf("details of student %d are \n",i);
printf("%d %s",S[i].Rno,S[i].name);
for(int j=0;j<3;j++)
{
printf("coursewise marks for studenet %d are\n",j);
printf("%d\n",S[i].course_marks[j]);
}
}
printf(" \n\n ");
//sgpa calculation
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
sgpa[j]=0;
sgpa[j]+=S[i].course_marks;
}
}
//printing
for(int i=0;i<2;i++)
{
printf("%d",sgpa[i]);
}
}
Your usage of S[j].course_marks is wrong.
It is an array and it will be converted to a pointer in expressions here.
It seems the lines
scanf("%d",S[j].course_marks);
printf("%d\n",S[i].course_marks);
sgpa[j]+=S[i].course_marks;
should be
scanf("%d",&S[i].course_marks[j]);
printf("%d\n",S[i].course_marks[j]);
sgpa[j]+=S[i].course_marks[j];
Also the loop
for(int j=0;j<3;j++)
{
sgpa[j]=0;
sgpa[j]+=S[i].course_marks;
}
is bad because it accesses spga[2] while spga has only 2 elements spga[0] and spga[1].
Also it looks weird even if it weren't out-of-range access and
sgpa[j]=0;
sgpa[j]+=S[i].course_marks;
is equivalent to
sgpa[j]=S[i].course_marks;
You should rethink the algorithm here.
#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.
I made this simple program that asks the user to input the number of columns the matrix called arp is going to have because, that way when the program asks the user to input a number so it can find matches on the numbers stored at the array without comparing all 10 columns allocated on memory with array to pointer type.
The problem here comes when the user inputs into the columns size definition 2. All works fine before the last function of p3() does its job, then it doesn't even return to main to execute the infinite loop defined there. I have tried removing the pointers and didn't work; I also tried removing other parts of the code but still nothing...
Update: Tried removing the function to find elements felmnt() and still the same.
Here is The Buggy Code:
#include <stdio.h>
#include <stdlib.h>
int loop = 1;
void keepalive(void)
{
int ckr = 0;
fflush(stdin);
printf("\n\n ******[s]<< CONTINUE | EXIT >>[n]******\n");
while(printf(" > ") && (ckr = getchar()) != 's' && ckr != 'n') fflush(stdin);
getchar();
if(ckr == 'n') loop = 0;
system("CLS");
}
void felmnt(int *colu, int (*arp)[10])
{
int nius=0, colmts, x, i, ejct;
do
{ // loop to let the user find more elements inside matrix
colmts=0;
printf("\n Insert The Number To Find\n > ");
scanf("%d", &nius);
for(x=0; x<*colu; x++) // search of element inside matrix
{
for(i=0; i<=9; i++)
if(nius == arp[i][x])
colmts++;
}
if(colmts>1) // results printing
{
printf("\n %d Elements Found", colmts);
}else if(colmts)
{
printf("\n 1 Element Found");
}else
{
printf("\n Not Found");
}
printf("\n TRY AGAIN? s/n\n > ");
ejct=getchar();
getchar();
}while(ejct=='s');
}
void mat(int *colu, int (*arp)[10])
{
int ci, cn, tst=0;
for(ci=0; ci<*colu; ci++)
{
for(cn=0; cn<10; cn++)
{
printf("\n Input The Number [%d][%d]\n > ", ci+1, cn+1);
scanf(" %d", &arp[cn][ci]);
}
}
printf(" Numbers Inside Matrix> ");
for(ci = 0; ci<*colu; ci++)
{
for(cn=0; cn<10; cn++) printf(" %d", arp[cn][ci]);
}
}
void p3(void)
{ // >>>>main<<<<
int colu=0;
while(loop)
{
printf("\n Input The Quantity Of Columns To Use\n > ");
scanf("%d", &colu);
int arp[10][colu];
mat(&colu, arp);
felmnt(&colu, arp);
keepalive();
}
}
int main(void)
{
int ck = 0, ndx;
while(1)
{ // infinite loop
p3();
fflush(stdin);
printf("\nPause !!!");
getchar();
}
return 0;
}
Please Note that this problem is for homework.
With the following code, I am inputting the data on the Kattis website for this problem. The code compiles, however, I'm getting a segmentation fault after the 'Number of Partygoers #:' printf, and I'm not sure why. Please help!
In Kattis, the description of the problem is basically: There are a number of party goers, you realize someone isn't supposed to be here because you sent all of the invitations to couples. Each party goer has a code on their invitation that matches another's. Find the odd man out.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int N,i,n,j,a,count=0,num=1;
int guest[1000],cas[1000];
scanf("%d",&N);
printf("Initial N: %dn",N);
while(N!=0)
{
printf("While #: %dn",N);
scanf("%d",&n);
printf("Number of Partygoers: %dn",n);
for(i=0;i<n;i++)
{
scanf("%d",guest[i]);
printf("Goer's Id: %dn",i);
guest[i]=cas[i];
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(cas[i]==guest[j])
{
printf("Is Equaln");
if(count==2)
{
break;
i++;
j=0;
printf("Count Equaln");
}
count++;
j++;
}
else
{
printf("Case #%d: %dn",num,cas[i]);
}
}
}
N--;
num++;
}
return 0;
}
scanf is expecting a pointer and you are passing a value.
Change
scanf("%d",guest[i]);
to
scanf("%d",&guest[i]);
There is another problem, you get a value with scanf but then you overwrite this value with an uninitialized value (garbage):
for(i=0;i<n;i++){
scanf("%d",&guest[i]);
printf("Goer's Id: %d\n",i);
guest[i]=cas[i]; /* cas[i] is used uninitialized */
}
Do you mean cas[i]=guest[i];?
I can't understand where is the mistake. Help me correct it please. The output is coming as all the elements of resultant matrix being zero.
#include<stdio.h>
#include<conio.h>
int main()
{
int a[5][5],b[5][5],c[5][5],i=0,j=0,row1,col1,row2,col2,row3,col3,s=0,k=0,l=0;
printf("Enter no. of rows and no. of columns of first matrix:\n");
scanf("%d %d",&row1,&col1);
printf("Enter no. of rows and no. of columns of second matrix:\n");
scanf("%d %d",&row2,&col2);
if(col1==row2)
{
row3=row1;
col3=col2;
}
else
{
printf("Not possible!");
exit(1);
}
printf("Enter elements of first matrix:\n");
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter elements of second matrix:\n");
for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{
scanf("%d",&b[i][j]);
}
}
i=0;
j=0;
for(k=0;k<row3;k++)
{
for(l=0;l<col3;l++)
{
while(i<row3 || j<col3)
{
//printf("Hi");
s=s+a[i][j++]*b[i++][j];
//printf("%d\n",s);
}
}
printf("%d\n",s);
c[k][l]=s;
s=0;
}
printf("Sum matrix is:\n");
for(k=0;k<row3;k++)
{
for(l=0;l<col3;l++)
{
printf("%d ",c[k][l]);
}
printf("\n");
}
getch();
}
I have included comments of printing in the while loop so as to debug but it's not helping.
You are setting the result outside of your column loop, so you only set one result per row. Change the code to this by moving those 3 lines inside the brace:
for(k=0;k<row3;k++)
{
for(l=0;l<col3;l++)
{
i = 0; j = 0;
while(i<row3 || j<col3)
{
//printf("Hi");
s=s+a[k][j++]*b[i++][l];
//printf("%d\n",s);
}
// THIS CODE HAS MOVED:
printf("%d\n",s);
c[k][l]=s;
s=0;
}
}
Also, your addition needs to use the k and l indices, so that you move along a row of a[][] given by k and a column of b[][] given by l:
s=s+a[k][j++]*b[i++][l];
You forgot to initialize i and j inside the loop where you add the two matrices.
According to your code add.
i=0; j=0 inside the double for loop for addition.
Hope this helps