473,790 Members | 2,534 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

segmentation error ....!

segmentation error !!!!
hi guys ,
i wrote this program to multiply two matrices (just the basic code
without checkin 4 the condition n==p )
"
#include<stdio. h>
main()
{
int a[10][10],b[10][10],c[10][10];
int m,n,p,q,i,j,k;
printf("Enter the size of the first array A:");
scanf("%d %d",&m,&n);

printf("Enter the size of the second array A:");
scanf("%d %d",&p,&q);

printf("\nEnter the value of the Matrix A:\n");
for(i=0;i<m;i++ )
for(j=0;j<n;j++ )
{
printf("\nEnter the value:");
scanf("%d",a[j]);
}
printf("\nEnter the value of the Matrix B:\n");

for(i=0;i<p;i++ )
for(j=0;j<q;j++ )
{
printf("\nEnter the value:");
scanf("%d",b[j]);
}

/* MULTIPLICATION */

for(i=0;i<m;i++ )
for(j=0;j<q;j++ )
{
c[j]=0;
for(k=0;k<n;k++ )
c[j]+=(a[k]*b[k][j]);
}

for(i=0;i<m;i++ )
{
printf("\n");
for(j=0;j<q;j++ )
printf("%d \t",c[j]);
}
}

"
i compiled this code using gcc compiler.... no errors showed up ...
then i executed ...this is what i got ....
"
Enter the size of the first array A:2
2
Enter the size of the second array A:2
2

Enter the value of the Matrix A:

Enter the value:2
Segmentation fault

"
can u guys please tell me what this segementation error is ..?
and y it was thrown up suddenly in my program ,,?
i've never encountered this error before ...

Feb 20 '07 #1
4 1951
In article <11************ **********@m58g 2000cwm.googleg roups.com>,
John Doe <ba***********@ gmail.comwrote:
>int a[10][10],b[10][10],c[10][10];
[...]
>scanf("%d",a[j]);
a is a two-dimensional array, so you want something like &a[i][j].
You have the same problem here:
>scanf("%d",b[j]);
and here:
>c[j]=0;
and here (twice):
>c[j]+=(a[k]*b[k][j]);
and here:
>printf("%d \t",c[j]);
-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Feb 20 '07 #2
On Feb 20, 8:51 pm, rich...@cogsci. ed.ac.uk (Richard Tobin) wrote:
In article <1171985776.676 322.268...@m58g 2000cwm.googleg roups.com>,

John Doe <bala.mailb...@ gmail.comwrote:
int a[10][10],b[10][10],c[10][10];
[...]
scanf("%d",a[j]);

a is a two-dimensional array, so you want something like &a[i][j].
You have the same problem here:
scanf("%d",b[j]);

and here:
c[j]=0;

and here (twice):
c[j]+=(a[k]*b[k][j]);

and here:
printf("%d \t",c[j]);

-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
thanks ..
i got the problem solved ...

Feb 20 '07 #3
John Doe said:
segmentation error !!!!
hi guys ,
i wrote this program to multiply two matrices (just the basic code
without checkin 4 the condition n==p )
"
#include<stdio. h>
main()
{
int a[10][10],b[10][10],c[10][10];
int m,n,p,q,i,j,k;
printf("Enter the size of the first array A:");
scanf("%d %d",&m,&n);
Check that the read succeeded. In this case, you are asking scanf to
populate two objects, so it will return 2 if successful.
printf("Enter the size of the second array A:");
scanf("%d %d",&p,&q);
Check that the read succeeded. In this case, you are asking scanf to
populate two objects, so it will return 2 if successful.

Now would be a good point at which to make sure that none of i, j, m,
and n exceed 10. (At first, I thought this was probably causing your
problem, but then I discovered that your test data comprises 2, 2, 2,
2, so let's move on.)
printf("\nEnter the value of the Matrix A:\n");
for(i=0;i<m;i++ )
for(j=0;j<n;j++ )
{
printf("\nEnter the value:");
scanf("%d",a[j]);
Two points here. Firstly, a[j] is an array, so it evaluates to &a[j][0],
which is fine (it's the address of an int, which is what's required),
but probably not what you want. It's more likely that you want either
&a[i][j] or perhaps &a[j][i].

Secondly, check that the read succeeded. In this case, you are asking
scanf to populate one object, so it will return 1 if successful.
}
printf("\nEnter the value of the Matrix B:\n");

for(i=0;i<p;i++ )
for(j=0;j<q;j++ )
{
printf("\nEnter the value:");
scanf("%d",b[j]);
Two points here. Firstly, b[j] is an array, so it evaluates to &b[j][0],
which is fine (it's the address of an int, which is what's required),
but probably not what you want. It's more likely that you want either
&b[i][j] or perhaps &b[j][i].

Secondly, check that the read succeeded. In this case, you are asking
scanf to populate one object, so it will return 1 if successful.
}

/* MULTIPLICATION */

for(i=0;i<m;i++ )
for(j=0;j<q;j++ )
{
c[j]=0;
See below for a discussion of the above line.

for(k=0;k<n;k++ )
c[j]+=(a[k]*b[k][j]);
If either j or k exceeds 9, you're in trouble. And see below.
}

for(i=0;i<m;i++ )
{
printf("\n");
for(j=0;j<q;j++ )
printf("%d \t",c[j]);
See below.
}
}

"
i compiled this code using gcc compiler.... no errors showed up ...
So did I, and I got:

foo.c:3: warning: return-type defaults to `int'
foo.c:3: warning: function declaration isn't a prototype
foo.c: In function `main':
foo.c:34: incompatible types in assignment
foo.c:36: invalid operands to binary *
foo.c:43: warning: int format, pointer arg (arg 2)
foo.c:45: warning: control reaches end of non-void function
make: *** [foo.o] Error 1

The first couple are no big deal, although it would be good to address
them:

int main(void)

In my (indent-fixed) version of the source, line 34 is:

c[j] = 0;

And this may well be your blow-up. Here, you're trying to write an int
value to an object that is an array of 10 ints. You can't assign to
arrays in C, only to their members.

In each case, I fixed c[j] to read c[i][j]. The "invalid operands to
binary *" error was caused by a[k], so I fixed it to read a[j][k]
(check the logic in case that's not what you meant).

The program then runs to completion, but with very strange results. If I
were you, I would define your arrays like this:

int a[10][10] = {0};
int b[10][10] = {0};
int c[10][10] = {0};

This will ensure that they start off with all member values equal to 0.
With that correction, and a puts("") at the end, the output was
reasonable.
"
can u guys please tell me what this segementation error is ..?
It normally means you (in the general sense!) have done something stupid
with arrays or pointers.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 20 '07 #4
In article <11************ **********@m58g 2000cwm.googleg roups.com"John Doe" <ba***********@ gmail.comwrites :
....
int a[10][10],b[10][10],c[10][10];
....
printf("\nEnter the value of the Matrix A:\n");
for(i=0;i<m;i++ )
for(j=0;j<n;j++ )
{
printf("\nEnter the value:");
scanf("%d",a[j]);
This is a strange way to initialise a 2-dimensional array... (Where is
'i' used?) More of this kind of errors do occur.
for(i=0;i<m;i++ )
for(j=0;j<q;j++ )
{
c[j]=0;
for(k=0;k<n;k++ )
c[j]+=(a[k]*b[k][j]);
}
This is also not understandable. Where is 'i' used?
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Feb 20 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2962
by: Justin Tang | last post by:
Hi I am wondering about the segmentation fault in PHP. Namely, I'm running PHP version 4.6.9 on my server right now and when I try to process a large piece of text via textarea(3k+), the resulting POST data I get seems to be result from a segementation fault error. The data I receive becomes truncated followed by the variable name, equal sign, then the entire span of the data. Code:
3
8044
by: Anks | last post by:
i am unable to find why following code is giving segmentation fault.... way to produce seg fault: run the program... give input 12345678....enter any key except 'x'.... again give 12345678 as input...then segmentation fault happens... please somebody enlighten me... #include <stdio.h> #include<stdlib.h>
21
8310
by: user | last post by:
I just finish writing LAB2 with no errors when compiling, but once i run it, i get "segmentation fault".. i don't know what is wrong, can anyoen tell me what it means and how i can fix it? thx!
6
2316
by: damian birchler | last post by:
If I run the following I get a segmentation fault: #define NAMELEN 15 #define NPERS 10 typedef struct pers { char name; int money; } pers_t;
5
2998
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I understood that a segmentation fault can occur whenever I declare a pointer and I leave it un-initialized. So I thought the problem here is with the (const char *)s in the stuct flightData (please note that I get the same fault declaring as char * the...
18
26124
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)? Thanks.
2
2243
by: zl2k | last post by:
hi, all I am using a 2 dimensioanl array implemented by vector<vector<long> >. When the row number grows to 8 and I am trying to insert a new row, I got the segmentation error. However, if I reserve the row number to 16 (in my case the size of row will always less than 16) at the very begining, then everything works well. (The column number could be quite large.) What is the problem? The vector can automatically grow as needed, right?...
3
5188
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection from 10g release2 PHP is configured with the following parameters './configure' '--prefix=/opt/oracle/php' '--with-apxs=/opt/oracle/apache/bin/apxs' '--with-config-file-path=/opt/oracle/apache/conf' '--enable-safe-mode' '--enable-session'...
3
4206
by: Dhieraj | last post by:
While compiling a C++ code I am getting the following error : CC -c -I/opt/iona/artix/2.0/include -I/opt/iona/asp/6.0/include -I/opt/ar/api63/include -I//var/tmp/vidya/aotscommon/include -I//var/tmp/coe/coesup2.0b17/include -I/opt/oracle/product/9.2.0.5.0/rdbms/demo -I/opt/oracle/product/9.2.0.5.0/rdbms/public -I/opt/oracle/product/9.2.0.5.0/plsql/public -I/opt/oracle/product/9.2.0.5.0/network/public ...
0
1812
by: ollii | last post by:
Hello evryboody, i created client and srever program that they can both communicate together by TCP and UDP, but when i want to send message to server from client i get error on the server i get error "Segmentation Fault" This is Code for server: /* server_it.c: DST iterative echo server to be linked with DST_sock */ #include <stdio.h>
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10412
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9986
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9021
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3703
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.