find a number from an unorderd array

Page 1 of 1

6 Replies - 1226 Views - Last Post: 31 March 2007 - 05:51 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=25484&amp;s=9ac726aa4beef335ad218f170c8ea1fe&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 freshman_prog

Reputation: 1
• Posts: 23
• Joined: 23-March 07

find a number from an unorderd array

Posted 24 March 2007 - 03:11 AM

how can i find a number from an unorderd array.
Is This A Good Question/Topic? 0

Replies To: find a number from an unorderd array

#2 NickDMax

Reputation: 2254
• Posts: 9,245
• Joined: 18-February 07

Re: find a number from an unorderd array

Posted 24 March 2007 - 10:01 AM

Since the array is unordered the only way to find a particular entry is to go looking for it one by one.

```for(int i=0; i<ArrayMax; i++)
{
if (array[i]==number) {/* found it */ break; }
}

```

This is the big fall back to an unordered array.

BTW: try to name your posts more discriptivly.

This post has been edited by NickDMax: 24 March 2007 - 10:02 AM

#3 freshman_prog

Reputation: 1
• Posts: 23
• Joined: 23-March 07

Re: find a number from an unorderd array

Posted 26 March 2007 - 01:16 PM

Thanks for reply sir but i m still facing problem
plz see my code and make it corect
student 1st year (CS)

#include <iostream>
#include <conio>

int main()

{
int a[5],i,t;
cout<<"enter ur numbers";
for(i=0;i<5;i++)
cin>>a[i];
for(i=0;i<5;i++)
cout<<a[i];
cout<<"enter number which u want to search";
cin>>t;
for(i=0;i<5;i++)
if (a[i]==t)

break;
cout<<"present";

cout<<"absent";
getch();
}

#4 PennyBoki

• system("revolution");

Reputation: 53
• Posts: 2,335
• Joined: 11-December 06

Re: find a number from an unorderd array

Posted 26 March 2007 - 01:23 PM

If you want the position to be printed
```for(i=0;i<5;i++)
if (a[i]==t) cout<<"The number was at  "<<i<<"position";
```

#5 NickDMax

Reputation: 2254
• Posts: 9,245
• Joined: 18-February 07

Re: find a number from an unorderd array

Posted 26 March 2007 - 08:31 PM

```#include <iostream>
#include <conio>
using namespace std;

int main()

{
//You should always use LOTS of comments
//They will also aid the teacher in giving partial-credit if the
//   program does not work.

//Either give variables discriptive names, or list them out
//  and add a comment discribing what they do.
int a[5]; //Array to hold numbers.
int i; //Used to loop though array.
int t; //The number to search for.
int found; //used to tell us if the number is found

//Althoug the brackets are not required in for-loops and if-statments
//  using them will greatly aid in program clarity and can save you from
//  hard to find logic errors.
for(i = 0; i < 5; i++)
{
cin >> a[i];
}

for(i=0;i<5;i++)
{
cout << "a[" << i << "] = " << a[i] << " ";
}

cout<<"\nenter number which you want to search for: ";
cin>>t;

//Here I used a technique I like to call flagging.
//   The found variable (called a flag) will tell us if we actually found
//   the number we were looking for. 0 == false, and non-zero == true
found = 0; //assume we will not find it... (i.e. assume false)
for(i=0; i < 5; i++)
{
if (a[i]==t)
{
found = i+1; //we DID find it!!! (set to true)
//note i+1 is so that if i=0 found will still be >0
break;
}
}

//Now we check the status of our flag and see what we find.
if (found)  //remeber in C++ nonzero is true so this is the same as "if (found != 0)"
{
cout<<"\nNumber found at position: " << found - 1 << "\n";
} else
{
cout<<"That number is not is the array!\n";
}

//Lets not leave the teacher wondering if the program locked up.
cout << "Press any key to continue..." << endl; //last cout should have endl to flush buffer (not really needed but good practice).
getch();  //<---- not the best way to do this, though it works here.
return 0; //Always DEFINE exit point.
}
```

#6 freshman_prog

Reputation: 1
• Posts: 23
• Joined: 23-March 07

Re: find a number from an unorderd array

Posted 31 March 2007 - 04:37 PM

Thankyou very much Sir u r not only solve my problem but also clear my misconceptions.
these days i m studying string having problem with that
like sum of two strings etc if u send me a small program which help me in understanding it i will really thankful to you.

• g+ + -o drink whiskey.cpp

Reputation: 248
• Posts: 13,507
• Joined: 12-July 02

Re: find a number from an unorderd array

Posted 31 March 2007 - 05:51 PM

As an addition to Nick's suggestion, one could also use the boolean data type as opposed to the integer type to indicate success. It can often be more intuitive.