LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-27-2008, 03:55 AM   #1
anshumansingh
LQ Newbie
 
Registered: Nov 2008
Posts: 2

Rep: Reputation: 0
segmentation fault


//write a program to compute the sum of even and odd numbers stored in an array of N integers and print the same
#include<stdio.h>
void main()
{
int num[20];
int i,esum=0,odsum=0,N;
printf("enter the size of the array\n");
scanf("%d",&N);
printf("enter the elements one by one\n");
for(i=0;i<N;i++)
scanf("%d",num[i]);
for(i=0;i<N;i++)
{
if((num[i]%2)==0)
esum += num [i];
else
odsum += num[i];
}
printf("sum of even numbers=%d\n",esum);
printf("sum of odd numbers = %d\n",odsum);
}

what is the segmentation error in this programme?
how clear this error in this programme?
 
Old 11-27-2008, 04:19 AM   #2
newtovanilla
Member
 
Registered: Apr 2008
Posts: 267

Rep: Reputation: 30
I got a "segmentation fault" with Linux. I rebooted and it went away.
 
Old 11-27-2008, 11:28 PM   #3
salter
Member
 
Registered: Oct 2008
Posts: 100

Rep: Reputation: 15
It should be
Quote:
scanf("%d",&(num[i]));
instead of
Quote:
scanf("%d",num[i]);
scanf parameters are retrieved by pointers. Using num[i] is actually the value, even though it looks like a pointer (to some).

Last edited by Tinkster; 10-30-2010 at 04:06 PM.
 
Old 11-30-2008, 12:34 AM   #4
anshumansingh
LQ Newbie
 
Registered: Nov 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Smile thanks

thanks budy for ur reply .........
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmentation Fault stefaandk Linux - General 2 09-04-2006 07:10 AM
segmentation fault? ferradura Slackware 3 07-05-2006 05:40 PM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
Segmentation Fault rupesh_pulikool Linux - Software 7 01-07-2005 05:55 AM
Segmentation fault ( C ) wenta0 Programming 4 12-22-2004 05:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration