how can i find a number from an unorderd array.
find a number from an unorderd arrayc++
Page 1 of 1
6 Replies - 1012 Views - Last Post: 31 March 2007 - 05:51 PM
Replies To: find a number from an unorderd array
#2
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.
This is the big fall back to an unordered array.
BTW: try to name your posts more discriptivly.
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
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();
}
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();
}
#4
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
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.
}
#6
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
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
#7
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.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote





|