6 Replies - 1072 Views - Last Post: 31 March 2007 - 05:51 PM Rate Topic: -----

#1 freshman_prog  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2246
  • View blog
  • Posts: 9,236
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 freshman_prog  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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
waiting 4 ur reply
Naushad Qamar
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();
}
Was This Post Helpful? 0
  • +
  • -

#4 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • 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";

Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2246
  • View blog
  • Posts: 9,236
  • 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
	//The comments will help you understand your programs
	//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
	
	cout<<"Please enter your numbers: ";
	
	//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.
}

Was This Post Helpful? 0
  • +
  • -

#6 freshman_prog  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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.
your wellwisher , NAUSHAD QAMAR
Was This Post Helpful? 0
  • +
  • -

#7 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1