comparing values function

S

SneakyElf

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;
}
 
Y

Yogish Baliga

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 -
 
D

Default User

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
 
S

SneakyElf

i dont quite understand how to display the index. how should i work w/
if/else statement?

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
 
D

Default User

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.


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
 
J

James Kanze

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 said:
{
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_iterator
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 ;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top