4 Replies - 348 Views - Last Post: 29 January 2009 - 11:43 AM Rate Topic: -----

#1 ngxcrunner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 13-October 08

help with an error

Posted 29 January 2009 - 10:56 AM

hi i need some help with my code i dont why it keeps coming up with one error. its suppose to search through array and find a number and output which part of the array it is.

#include<iostream>
using namespace std;

int search (int a[], int &x, int &n){

	bool ser = false;
	int i=0;
	while (i<n){
		if(a[i]==x){
			cout<<i<<endl;
			ser=true;
				return i;}
		i++;}
	if(ser==false){
		cout<<"there is no such value\n";
		return -1;}}


void main(){
	
	int a[5]={1,2,3,4,5};
	search (a[5], int b, int c);
	
}

maybe someone can help figure out the problem

Is This A Good Question/Topic? 0
  • +

Replies To: help with an error

#2 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: help with an error

Posted 29 January 2009 - 11:02 AM

Why make it so complicated?

int search (int a[], int x){
int i;
int max = sizeof(a) / sizeof(int);  
  for (i = 0; i < max; i++)
	 if(a[i]==x)
	   return i;
  }
  cout<<"there is no such value\n";
  return -1;
}




search (a[5], int b, int c);
should be
int found = search (a, b );

This post has been edited by Gloin: 29 January 2009 - 11:06 AM

Was This Post Helpful? 0
  • +
  • -

#3 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: help with an error

Posted 29 January 2009 - 11:10 AM

Didn't notice at first but you have to assign some value(s) to b (and c)
Was This Post Helpful? 0
  • +
  • -

#4 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: help with an error

Posted 29 January 2009 - 11:27 AM

'Gloin' pointed you in the right direction ...

This may help clarify some ...

Shalom,
David
http://developers-he...index.php/topic,46.0.html
http://developers-he.../index.p...opic,106.0.html

#include<iostream>
using namespace std;

int search(int a[], int n, int x)
{
    for(int i=0; i<n; ++i)
        if( a[i] == x ) return i;
        
    // else ... if reach here ...
    return -1;
}


int main()
{

    int a[]={1,2,3,4,5,6,7,8,9};
    int size = sizeof a / sizeof a[0];
    
    int test = 0;
    int index = search(a, size, test);
    if (index >=0 ) cout << test << " found at index " << index << endl;
    else cout << "There is no such value as " << test << " in the array." << endl;

    test = 1;
    index = search(a, size, test);
    if (index >=0 ) cout << test << " found at index " << index << endl;
    else cout << "There is no such value as " << test << " in the array." << endl;
    
    test = 9;
    index = search(a, size, test);
    if (index >=0 ) cout << test << " found at index " << index << endl;
    else cout << "There is no such value as " << test << " in the array." << endl;
    
    test = 10;
    index = search(a, size, test);
    if (index >=0 ) cout << test << " found at index " << index << endl;
    else cout << "There is no such value as " << test << " in the array." << endl;
    
    cout << "Press 'Enter' to continue ... " << flush;
    cin.get();
}

Was This Post Helpful? 0
  • +
  • -

#5 ngxcrunner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 13-October 08

Re: help with an error

Posted 29 January 2009 - 11:43 AM

thank you all for posting so far i am going to try this as soon as i can right after class and practice
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1