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.
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.
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.
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.
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.
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!
#include<iostream>
usingnamespace 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");
}
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?
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!
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?
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.
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.
#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");
}