473,566 Members | 3,307 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

comparing values function

I need to write a function where the third parameter points to an
address whose dereferenced value equals one of the array element's
value, then the the function returns the index of the first matching
array element. Otherwise -1 is returned.
Here's what I have but I am going obviously wrong somewhere:

#include <iostream>
using namespace std;

// This function compares values
void indexvalue (int array [], int size, int *num)
{
for (int count = 0; count < size; count++)
{
if (*num = array [count])
cout << count << endl;
else {
cout << "-1" << endl;
}
}
}

int main ()
{
int four = 4;
int arr [6] = {1, 2, 3,4,5};
int size = 6;

indexvalue (arr, size, &four);

return 0;
}

Sep 9 '07 #1
5 1877
On Sep 9, 11:23 am, Sneaky...@gmail .com wrote:
I need to write a function where the third parameter points to an
address whose dereferenced value equals one of the array element's
value, then the the function returns the index of the first matching
array element. Otherwise -1 is returned.
Here's what I have but I am going obviously wrong somewhere:

#include <iostream>
using namespace std;

// This function compares values
void indexvalue (int array [], int size, int *num)
{
for (int count = 0; count < size; count++)
{
if (*num = array [count])
cout << count << endl;
else {
cout << "-1" << endl;
}
}

}
Return -1 outside for loop.
>
int main ()
{
int four = 4;
int arr [6] = {1, 2, 3,4,5};
int size = 6;

indexvalue (arr, size, &four);

return 0;

}- Hide quoted text -

- Show quoted text -

Sep 9 '07 #2
Sn*******@gmail .com wrote:
I need to write a function where the third parameter points to an
address whose dereferenced value equals one of the array element's
value, then the the function returns the index of the first matching
array element. Otherwise -1 is returned.
Here's what I have but I am going obviously wrong somewhere:

#include <iostream>
using namespace std;

// This function compares values
void indexvalue (int array [], int size, int *num)
{
for (int count = 0; count < size; count++)
{
if (*num = array [count])
You say you are comparing, but you are actually assigning a value here.

Brian
Sep 9 '07 #3
i dont quite understand how to display the index. how should i work w/
if/else statement?

On Sep 9, 2:57 pm, "Default User" <defaultuse...@ yahoo.comwrote:
Sneaky...@gmail .com wrote:
I need to write a function where the third parameter points to an
address whose dereferenced value equals one of the array element's
value, then the the function returns the index of the first matching
array element. Otherwise -1 is returned.
Here's what I have but I am going obviously wrong somewhere:
#include <iostream>
using namespace std;
// This function compares values
void indexvalue (int array [], int size, int *num)
{
for (int count = 0; count < size; count++)
{
if (*num = array [count])

You say you are comparing, but you are actually assigning a value here.

Brian

Sep 9 '07 #4
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html >

I have rearranged the message correctly.
Sn*******@gmail .com wrote:
On Sep 9, 2:57 pm, "Default User" <defaultuse...@ yahoo.comwrote:
Sneaky...@gmail .com wrote:
I need to write a function where the third parameter points to an
address whose dereferenced value equals one of the array element's
value, then the the function returns the index of the first
matching array element. Otherwise -1 is returned.
Here's what I have but I am going obviously wrong somewhere:
#include <iostream>
using namespace std;
// This function compares values
void indexvalue (int array [], int size, int *num)
{
for (int count = 0; count < size; count++)
{
if (*num = array [count])
You say you are comparing, but you are actually assigning a value
here.
i dont quite understand how to display the index. how should i work w/
if/else statement?

if (*num == array [count])
A single = ASSIGNS a value to another, while == COMPARES two values.

You should get the following results with that:

-1
-1
-1
3
-1
-1

To make it match what your problem description says, you'll have to
figure out how to make it return something.


Brian

Sep 9 '07 #5
On Sep 9, 8:23 pm, Sneaky...@gmail .com wrote:
I need to write a function where the third parameter points to an
address whose dereferenced value equals one of the array element's
value, then the the function returns the index of the first matching
array element. Otherwise -1 is returned.
Here's what I have but I am going obviously wrong somewhere:
Many places:-).
#include <iostream>
using namespace std;
// This function compares values
void indexvalue (int array [], int size, int *num)
The usual type for an array in C++ is std::vector. And
arguments which aren't modified should be declared const. And
you would normally not pass an int using a pointer unless you
optionally modify the value in the function. (If you want to
unconditionally modify the value, then you would pass an int&,
and if you don't modify it, you pass simply an int. So your
function header should be:

void
indexvalue( std::vector< int const& array, int num )

Except that if what you wrote is correct, the function should
return the index, so it would be:

size_t
indexvalue( std::vector< int const& array, int num )
{
for (int count = 0; count < size; count++)
{
if (*num = array [count])
cout << count << endl;
else {
cout << "-1" << endl;
}
}
And of course, linear search is built into C++, so you would
normally write:

std::vector< int >::const_iterat or
result
= std::find( array.begin(), array.end(), num ) ;
return result == array.end()
? -1
: result - array.begin() ;

To output (as your function currently does), just change the
second statement to:

std::cout << ( result == array.end()
? -1
: result - array.begin() )
<< std::endl ;
}
--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 10 '07 #6

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

Similar topics

12
885
by: Elijah Bailey | last post by:
I have two char arrays of size k. I want to know which one is bigger (exactly like for instance I compare two ints/longs/etc.). What is the fastest way to do this? k <= 10 usually for my application. I tried bcmp / a loop for comparison, but it seems they are very slow compared to comparing longs...Any ideas? I tried splitting the array...
41
3916
by: Odd-R. | last post by:
I have to lists, A and B, that may, or may not be equal. If they are not identical, I want the output to be three new lists, X,Y and Z where X has all the elements that are in A, but not in B, and Y contains all the elements that are B but not in A. Z will then have the elements that are in both A and B. One way of doing this is of course...
2
3325
by: Manny Chohan | last post by:
Hi, i have two datetime values in format 11/22/04 9:00 AM and 11/22/04 9:30 AM. How can i compare dates .net c# or if there is any other way such as Javascript. Thanks Manny
19
2632
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor = Color.Empty then..... or if mycolor is Color.Empty then .......
2
2054
by: darrel | last post by:
I have two comma delimted strings that I need to compare individual values between the two. I assume the solution is likely to put them into an array? If so, do I need to loop through one, comparing the other, or is there some sort of compare function already in .net? For instance, I have these strings: str1: ,3,6,12,17, str2: ,7,8,12,
2
3373
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function checkInteger(&$value, $checks) { $err = ''; if (!is_numeric($value) || (floatval($value) != intval($value))) { $err .= 'Input must be an integer. ';
25
12999
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void *p, void *q) that will take any two pointers and decide whether they can be compared or not. I really can't think how to do this - any...
18
4093
by: Carramba | last post by:
Hi! is there a better/faster way to compare mantissas of to real number then in following code? #include <stdio.h> #include <stdlib.h> int main(void) { float a,b; int test;
2
2735
by: TamaThps | last post by:
I have to write a program that takes the lines of code from a .cpp as a string into an array. Then I need to compute the ratio of total lines of code to the number of comment lines, and ratio of total number of non comment and non blank lines to the total number of code. This is the void function that I am using to get these ratio's. void...
0
7673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
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...
0
7893
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. ...
1
7645
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7953
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...
1
5485
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...
0
5213
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...
0
3643
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...
0
926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.