Create a program that already has 5 integers assigned by use. Now, as program runs it will ask user to enter integer, if ineteger is already stored in array it says "you win and your integer was in location X" else "you lose".
#include<iostream>
usingnamespace std;
int main()
{
int a[5];
int elements, key;
bool found = false;
cout<<"Enter the number of element: ";
cin>>elements;
for(int i = 0; i < elements; i++){
cout<<"array["<<i<<"]: ";
cin>>a[i];
}
cout<<"Enter the value to search: ";
cin>>key;
for(int i=0;i<elements;i++){
if(key == a[i]){
found = true;
cout<<"The value is found at index array["<<i<<"]";
}
}
if(!found){
cout<<"Key not found!";
}
return 0;
}
#include<iostream>
usingnamespace std;
int main()
{
int a[5];
int f;
bool found = false;
for(int i = 0; i < 5; i++)
{
cout<<"Enter an integer: ";
cin>>a[i];
}
cout<<"Enter the value to search: ";
cin>>f;
for(int i=0;i<5;i++){
if(f == a[i]){
found = true;
cout<<"The value is found at index array["<<i<<"]";
}
}
if(!found){
cout<<"Key not found!";
}
return 0;
}
Why not you post your question, exactly, so we can advice you on the right track, most of the time I might be wanting this to happen, but people helping me might be giving me an advice on that to happen.
// finalss.cpp : Defines the entry point for the console application.
//
#include<iostream>
usingnamespace std;
int ssrch(int a[], int k);
int main()
{
int arr[99];
int e, k;
char t;
bool found = false;
cout<<"Enter the number of integers: ";
cin>>e;
for(int i = 0; i < e; i++)
{
cout<<"Enter an integer: ";
cin>>arr[i];
}
cout<<"\nEnter the value to search: ";
cin>>k;
for(int i=0;i<e;i++)
{
if(k == arr[i])
{
found = true;
cout<<"You Win - your number was in location["<<i<<"]"<<endl;
}
}
if(!found)
{
cout<<"You lose, want to try again?: ";
}
if (t=='y')
{
ssrch(a[i],k);
}
else
{
cout<<"Exit!";
}
return 0;
}
int ssrch(int a[], int k)
{
int i=0;
while(i< array.length( )) && (k != array[i]))
{
if (array[index] != key)
index++;
}
return (index);
}
}