How to find the position in array. please help ASAP.

Pages: 12
Sep 21, 2011 at 12:29pm
Here's my problem sirs. I need a code for this output please help me. Also include some brief explanations thank you very much!

SAMPLE OUTPUT:

Enter a number (8) variable

input 8
12 20 4 33 55 10 5 33

enter number to search

33

number 33 is present is present at position 8
number 33 is present is present at position 4

not found---> not found

Last edited on Sep 21, 2011 at 1:18pm
Sep 21, 2011 at 12:31pm
What did you do so far? What do you know about C++?
Sep 21, 2011 at 12:36pm
I am currently taking up IT in the Philippines And it was my first time to encounter arrays. Since I don't have someone to ask help, I decided if you sirs can help me figure these things out. I just have basic knowledge in c++. like if and loops etc. and now we're into arrays.

I am stuck with this.

#include<iostream>;
using namespace std;
int main()
{
int x, a[5], i;
cout<<"Please Enter a Number: ";
cin>>x;
cout<<"Input "<<x<<" number "
for(i=0;i<5;i++)
cin>>a[i];
cout<<"The Numbers are ";
for(i=0;i<5;i++)
cout<<" "<<a[i];
system("PAUSE");
}

So now, I need to look for the place of a certain number. Just like my problem above.

cout<<"
Last edited on Sep 21, 2011 at 12:54pm
Sep 21, 2011 at 12:54pm
Arrays are basically "fields" of data, for example int a[5]; declares an array "a" that has 5 integers in it. The array would look a bit like this:


Array a
index: 0   1   2    3    4
value: 10 15 25  45  120


Note that the values are completely arbitrary here - they behave like any other int. You can access each value of the array like this:

1
2
a[0] = 20; // sets the first element of the array to 20
a[1] = a[4]; //sets the second element of the array to the 5th element of the array 


I hope that makes it a bit clearer, if you're still not sure, ask.
Sep 21, 2011 at 1:02pm
Please sir, can you submit me your sample code to my problem? I will try to understand it. I'm in dilemma right now. I'm totally confused by this. BTW your explanation above was clear, I somehow got the logic. Thanks for the help.
Sep 21, 2011 at 1:26pm
So, what you are trying to do is to find the index of a number inside the array?

This is actually pretty simple, you already know how to iterate over an array with a for loop, right?
Basically, you just have to compare the value of a[i] to your search value in each iteration, and break out of the loop when they are equal. I assume you already know if and break? After that, just check the value of i - if it's <5 (in this case), i is the index of the element you were looking for. If i is 5, the value you were looking for is not in the array.



PS: use [ code ] yourcodegoeshere [ /code] tags (without the spaces). This enables syntax highlighting and line counting.
Sep 21, 2011 at 1:54pm
Exactly. How am I going to compare the values? In our class with the fundamentals, breaks were taught with switch statement. Please sir, give me a sample code that i can use as a model. I really owe you a lot sir. Thank You very much.

Sep 21, 2011 at 2:03pm
lol, you are learning a fundamentals of programming and you haven't learn basic structuring of code, and you are expecting us to do it. Real Smart there.

algorithm would be more like:


say "Please Enter 5 values:"
start a loop for the length of the array
get each value and place in the array at said position.
end the loop

Say "Which number would you like to find?"
start a loop for the length of the array
compare each iteration of the array to, usually is an if(myarray[index] == inputnumber)
On that if, if true I would use my break there...
end the loop.

If I was tracking my index, when I hit my break, the index would be the found position.
remember arrays are 0 indexed.

so how do you tell the user if I didn't find it?

You should know the basic c++ constructs to get this far.
Sep 21, 2011 at 2:03pm
We have a "no code solutions" policy for homeworks questions (well, it's not a strict rule, but I and many others here adhere to it).

You do know if statements, yes?

1
2
3
4
if(condition_is_true)
{
   do_something();
}


Now what if we insert for the condition
 
if(array[i] == search_value)

?

break isn't only used for switches - it is also used to break out of loops:

1
2
3
4
5
6
7
8
9
int i;
for(i=0; i<5; ++i)
{
   if(i==3)
   {
        break;
    }
}
cout<<i; //prints 3 


That's enough to solve your problem - just give a long (or short) and hard (or soft) thought.
Sep 21, 2011 at 2:07pm
It is simple: Don't know arrays? Study them: http://www.cplusplus.com/doc/tutorial/arrays/ . Now you can solve the problem yourself! Happy reading.
Sep 21, 2011 at 2:18pm
Sir Azagaros sorry for being ignorant it's my fault, and thank you as well for giving me spirit in striving to study more. Sir hanst99 I now have the concept, and I will try to do it as what you gave me. My gratitude to you sirs!
Sep 21, 2011 at 2:21pm
And you might not want to call me sir - I'm neither a knight or some other kind of noble.
Sep 21, 2011 at 2:31pm
This is not tested:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream>
using namespace std;
int main()
{
int x, y, a[100], i; // more numbers to enter
cout<<"Please Enter a Number: ";
cin>>x;
//cout<<"Input "<<x<<" number "
if((x < 1) || (x > 100)) // Check whether the number is valid
cout<<"Invalid Number" << endl;
else
{
for(i=0;i<x;i++) // x is the number of values the user wants to enter
cin>>a[i];
// The following is not necessary: the user knows the values
//cout<<"The Numbers are ";
//for(i=0;i<5;i++)
//cout<<" "<<a[i];
cout<<"Please enter the number to find: ";
cin>>y;
for(i=0;i<x;i++) // we iterate through the entered numbers to find the index of y
{
if(a[i] == y)
{
cout<<"The number is at position " << i << endl;
break;
}
}
system("PAUSE");
}
Sep 21, 2011 at 2:33pm
So then you are one of those rumored people who keep handing out ready written code to the homework folks instead of getting them to solve their problems themselves?
Sep 21, 2011 at 2:54pm
Sorry if you feel uncomfortable with that. I was used to that. But I really appreciate everything, I am currently working on it. I hope i will get it. Thanks!
Sep 21, 2011 at 3:27pm
People really should start getting banned for handing out homework solutions. It's despicable.
Sep 21, 2011 at 7:12pm
hanst99 wrote:
So then you are one of those rumored people who keep handing out ready written code to the homework folks instead of getting them to solve their problems themselves?
computer says ... yes!

1
2
3
bool efford_shown = easier ? done : said;
if(efford_shown)
  op = code;


packetpirate wrote:
People really should start getting banned for handing out homework solutions. It's despicable.
sure and you decide when it's just help and when it's 'handing out homework solution'.

a helping hand must be cut off when it's too helpfull...
Sep 21, 2011 at 7:47pm
Are you high? I don't HAVE to define what's help and what's handing out answers. You blatantly GAVE HIM the full solution to his problem so that he doesn't even have to do the assignment or understand it. Don't try to justify your stupidity with more stupidity.
Sep 21, 2011 at 8:32pm
I agree in the fact that "handing out homework solutions" doesn't require a definition outside of what is understood in plain English. It is quite simple: If your code can be copied/pasted, compiled and run and presented AS IS (or with simple cosmetic changes) as the solution to the homework, you did the homework for this person.

I personally dislike the practice of handing out homework because these people will bug me in 10 years with their crappy code and complete incompetence because they learned nothing when they should have.

Besides that, though, it is really up to you (coder777 or anyone else out there giving homework solutions) if you don't mind being every lazy @ss n00b's personal assistant for free. Really. Your time, not mine.

Finally, the name calling is out place, I would say.

@ OP: I'm not cataloging you as one of these. I am speaking in general.
Last edited on Sep 21, 2011 at 8:33pm
Sep 22, 2011 at 3:08am
Done! Need for approval. Thanks!

#include<iostream>
using namespace std;
int main()
{
int n,x,i,a[100],flag=0;
cout<<"Enter a Number ";
cin>>n;
cout<<"Enter "<<n<<" numbers";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"Enter a Number to be search ";
cin>>x;
for(i=0;i<n;i++)
if(a[i]==x)
{
cout<<"Number "<<x<<" is present in position"<<i+1<<endl;
flag==1;
}
if(flag==0)
{
cout<<"Search is unsuccessful"<<endl;
}
}
system("PAUSE");
}

Please highlight the wrong part thanks!

Not Tested.
Pages: 12