C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 11th, 2005, 12:39 AM
bsnbiophys bsnbiophys is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 1 bsnbiophys User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 m 34 sec
Reputation Power: 0
Question segmentation fault c

Im just learning how to use arrays in c and for this particular assignment I am supposed to use a linear search using recursion and i keep getting a segmentation fault:
#define SIZE 100
int linearSearch( const int array[], int key,int size);

int main()
{int a[SIZE], x, searchKey, element;
for(x=0; x<SIZE; x++){a[x]=2*x;}
printf("enter integer search key:\n");
scanf("%d", &searchKey);
element = linearSearch(a,searchKey,SIZE);
if (element!= -1) printf("found value in element %d\n", element);
else printf("value not found\n");
}
int linearSearch(const int array[], int key,int size)
{int n;
if(n<size && array[n]==key) return n;
return -1;
}

The program allows the user to put in a search key and then gives a segmentation fault

Reply With Quote
  #2  
Old April 11th, 2005, 11:04 PM
BoolBooB BoolBooB is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 36 BoolBooB User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 35 m 42 sec
Reputation Power: 4
First off, put your code inside a code tag (the # sign) to make it more readable.

Second off, n inside of linearSearch is not initialzed. You'll get whatever happens to be on the stack at the time of the call to linearSearch, ie. garbage - you need to initialize this before using it.

Third off, linearSearch is not recursive. To be recursive it must call itself, either directly or inderectly. You need to figure recursion out on your own or you wont understand it. I struggled with it for a long, long time before it made sense and then it just kinda clicked. Look at some examples in your book, and rememer that you need to know what condition is going to end the recursion and make sure you check for it!

Why recursively? This must be a class and you have to do this recursively, right? It would be a whole lot simpler to do iteratively.

Code:
int linearSearch(const int array[], int key,int size)
{
	static int n = 0;
	if (n<size && array[n]==key) 
	{
		return n;
	}
	else if (n < size)
	{
		++n;
		linearSearch(array, key, size);
	}
	else
	{
		return -1;
	}
}


Should get you in the ballpark, though I haven't coded it to see if it works or not! Also, this is only one solution. There are other ways to approach this. Dang it, now you wont learn recursion on your own.

PS This will only work once, as n will not get reset to 0.

Reply With Quote
  #3  
Old April 12th, 2005, 07:30 PM
draghu draghu is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 1 draghu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 44 sec
Reputation Power: 0
Thumbs up linearsearch

Use this linearSearch function and your program will work.



int linearSearch(const int array[], int key,int size)
{int n=0;
for(n=0;n<size;n++)
{
if(array[n] == key)
return n;
}
return -1;
}
~

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > segmentation fault c


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT