473,657 Members | 2,435 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Linear search

hi guys can some one help me here
i have to accept a number
and preform a linear search for the numbers

and if its not one of the number it has to say its invalid
#include <iostream>
using namespace std;
int searchlist (int[],int,int);
int main()
{
const int Num_Account = 18;
char Account[Num_Account]=
{"5658845, 4520125", "7895122, 8777541",
"8451277, 1302850",
"8080152, 4562555", "5552012, 5050552",
"7825877, 1250255",
"1005231, 6545231", "3852085, 7576651",
"7881200, 4581002" };

cout << "enter Number\n";
cin >> num;

int searchlist(int list [],int num,int value)
{
int index = 0;
int position = -1;
bool found = false;

while (index < num &&!found)
{
if (list == value)
{
found = true;
position = index;
}
index++;
}

return 0;
}

Apr 5 '06 #1
2 3944
* littlegirl:
hi guys can some one help me here
i have to accept a number
and preform a linear search for the numbers

and if its not one of the number it has to say its invalid
#include <iostream>
using namespace std;
int searchlist (int[],int,int);
int main()
{
const int Num_Account = 18;
char Account[Num_Account]=
{"5658845, 4520125", "7895122, 8777541",
"8451277, 1302850",
"8080152, 4562555", "5552012, 5050552",
"7825877, 1250255",
"1005231, 6545231", "3852085, 7576651",
"7881200, 4581002" };

cout << "enter Number\n";
cin >> num;

int searchlist(int list [],int num,int value)
{
int index = 0;
int position = -1;
bool found = false;

while (index < num &&!found)
{
if (list == value)
{
found = true;
position = index;
}
index++;
}

return 0;
}


Try first of all to move the definition of the 'searchlist' function to
/above/ the 'main' function: C++ does not support direct nesting of
function definitions.

When you've moved the definition, remove the declaration of 'searchlist'
that you now have after your 'using' directive, and in general, avoid
declaring functions that you define later on.

In 'main' you need to call 'searchlist', and present the result of that
call.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 5 '06 #2
"Alf P. Steinbach" writes:

<one added point>
i have to accept a number and preform a linear search for the numbers

and if its not one of the number it has to say its invalid
#include <iostream>
using namespace std;
int searchlist (int[],int,int);
int main()
{
const int Num_Account = 18;
char Account[Num_Account]= {"5658845, 4520125", "7895122, 8777541",
"8451277, 1302850",
"8080152, 4562555", "5552012, 5050552",
"7825877, 1250255",
"1005231, 6545231", "3852085, 7576651",
"7881200, 4581002" };

cout << "enter Number\n";
cin >> num;

int searchlist(int list [],int num,int value)
{
int index = 0;
int position = -1;
bool found = false;

while (index < num &&!found)
{
if (list == value)
{
found = true;
position = index;
}
index++;
}
You promised to return an int. Remember? There is no way for the caller to
determine what happened.

An int works fine, but the more modern way is to return a bool, it has
better self-documenting properties than an int.
return 0;
}


Try first of all to move the definition of the 'searchlist' function to
/above/ the 'main' function: C++ does not support direct nesting of
function definitions.

When you've moved the definition, remove the declaration of 'searchlist'
that you now have after your 'using' directive, and in general, avoid
declaring functions that you define later on.

In 'main' you need to call 'searchlist', and present the result of that
call.



Apr 5 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
6051
by: Scott David Daniels | last post by:
I am sorry, but in the Python 2.4 description of "heapify", I find the description of "Transform list x into a heap, in-place, in linear time," unbelievable. I understand the hand-wave that makes dictionary building linear (though I have a hard time with even that). Could somebody tell me what twist of logic makes "heapify" linear, except in the sense that linear is coming to mean "very fast?" Skeptically, -Scott David Daniels...
2
7179
by: leo | last post by:
I am looking for a source code for solving linear programming using simplex method. if any one of you have any code related to it in C,C++ or JAVA then please send it to me immediately.
3
11033
by: sql guy123 | last post by:
This is a real challenge. I hope someone is smart enough to know how to do this. I have a table TABLE1
4
9009
by: mano | last post by:
Hello.... Can anybody give me some idea ... How to write programs on (...(In C++)) Gauss elimination LU Decomposition finding Inverse Thanks
3
10454
by: ntuyen01 | last post by:
Hi All, Does anyone has examples for Linear Regression formula using c#. Or any good third party software create this. Thanks in advance Regards, Ted
1
1670
by: geebanga88 | last post by:
HI i have a method that performs a linear search to find a given vowel. The vowel parameter is a given vowel, while vowel is the array with all the vowels. public static void DisplayFirstAppearance (char vowel, char vowels) { boolean element_found = false; int index; for (index = 0; element_found == false && index < NoVowels; ++index); {
3
5998
by: jivelasquezt | last post by:
Hi all, I'm new to this group so I don't know if this question has been posted before, but does anyone knows about linear/integer programming routines in Python that are available on the web, more specifically of the branch and bound method. Thanks, Jorge Velasquez
3
1849
by: nembo kid | last post by:
I have an issue with a simple function that has to make a linear search for a key into an array. If the key is found in the array, the function it has to return 1 to the caller and pass array index through a out parameter. The issue is that the out parameter is not being updated. If I return the position to the caller (instead to use a out parameter) all is ok.
4
5226
by: Evelien | last post by:
Dear python-users, I am trying to do a non-linear least squares fitting. Maybe trying is not the best word, as I already succeeded in that. At the moment I am using leastSquaresFit from Scientific Python. I know of other least squares routines, such as the one in scipy.optimize and I believe there is also one in numpy. Now here is my question: in my search for a good algorithm, I was not able to find any non-linear least squares fitting...
0
8310
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8826
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6166
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.