Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Help some help on Arrays C++, Plz

I need some help on this code...Im a beginner and I have a little trouble on Array...

I try to get this work...but it still not working.. can u anyone help me out?
Thx

Here is the program what I have so far...

#include <iostream>
#include <string>

/* Program to find location of an element within an array */

using namespace std;

/* Find the location of a particular string. Return the index
of the first instance of the string within the array if found,
the length of the array otherwise.
*/

bool index(string list, int length, string target) {

for (int i = 1; i <= length; i++) {
if (list[i] == target)
break;
}

return i;
}

int main() {

string words1[] = {"Albino", "bats", "catch", "drosophilia"};

cout << index(words1, 4, "bats") << endl;

return 0;
}
[994 byte] By [leoyous] at [2007-11-11 9:56:07]
# 1 Re: Help some help on Arrays C++, Plz
ok here is your errors :

1) bool type can contains "true" or "false" only so u must not use the function "index" type bool but int to be able to know the number .

2) the function argument must be an array; change "string list" to "string list[]" .

More hints:

3) your search method will ignore first element as the array is zero based, you must begein with int i=0; and to have real result make this : return i+1;

4) avoid specify the length in your function by using sizeof() function ; "i<sizeof(list)"
and hence no need to the second argument.
Amahdy at 2007-11-11 20:59:50 >
# 2 Re: Help some help on Arrays C++, Plz
sizoef(lis) is dangerous as it returns the total size of the array in bytes. What you need is actually sizeof(list)/sizeof(string).
Secondly, don't pass strings by value. It's inefficient and dangerous. Always pass strings by reference, either as const or non-const (if the callee modifies the string).
Danny at 2007-11-11 21:00:50 >
# 3 Re: Help some help on Arrays C++, Plz
and better also make it array af pointers :
char *list[]

about sizeof() because I'm sure it's string no problem will occure .. different from int's and so on.
Amahdy at 2007-11-11 21:01:53 >
# 4 Re: Help some help on Arrays C++, Plz
Since strings are a dynamic size and all other types are static (until you create objects or arrays) sizeof cant be used.

if you say sizeof(myarray); and myarray contains "abc","defg" the result will be 9 one byte for each letter and one for each \0 and how would you use 9 in the calculation?
if you could be sure that the memory was mapped like it is when you declare it with string whatever[] = { "abc",defg" }; you could simply loob though all the bytes counting the \0's and use that but since you could dynamicly add strings to the array with new or malloc (may God forbid) that wouldnt be a very good way of doing it.

I'm sorry to be "correcting" you (especially if I'm wrong) as I actually think you'r a better coder than me.
ungamed at 2007-11-11 21:02:54 >
# 5 Re: Help some help on Arrays C++, Plz
thx for all reply...but I still cant fix it..have to use 1 and bool type function..

here what i got now

#include <iostream>
#include <string>

/* Program to find location of an element within an array */

using namespace std;

bool index(string list[], int length, string target) {

for (int i = 1; i <= length; i++) {
if (list[i] == target)
break;
}

return index;
}
int main() {
// You are guaranteed that there is no bug in the code for main()
string words1[] = {"Albino", "bats", "catch", "drosophilia"};

cout << index(words1, 4, "bats") << endl;

return 0;
}
leoyous at 2007-11-11 21:03:47 >
# 6 Re: Help some help on Arrays C++, Plz
Maybe you should try this:
// Find location of an element within an array; returns -1 if not found
int index(const string list[], int length, const string & target)
{
for( int i = 0; i < length; i++)
{
if( list[i] == target) return i;
}

return -1;
}
I hope this helps.
Viorel at 2007-11-11 21:04:57 >
# 7 Re: Help some help on Arrays C++, Plz
Since strings are a dynamic size and all other types are static (until you create objects or arrays) sizeof cant be used.

I'm sorry to be "correcting" you (especially if I'm wrong) as I actually think you'r a better coder than me.
There's no need to aplologize. This forum is open for expressing views, and correcting mistakes.

And I'm afraid I have to correct you here;)
First, let's distinguis between string objects, i.e., instances of the std::string class and char arrays. sizeof(string) is always a fixed value because it reports the size of the string object, not the size of the dynamic char array it contains.
As for sizeof (charptr), this will only report the size of the pointer (typically 4 bytes), not the length of the string, *unless* charptr is an array. In other words:
char array[10];
sizeof(array); //10
char *ptr;
ptr=new char[1000];
sizeof(ptr); //always 4.
And I certainly wouldn't recommend using pointers to pointers. Quite the contrary: use vector<string> and thus be rid of all the pointer and sizeof mess.
Danny at 2007-11-11 21:05:51 >
# 8 Re: Help some help on Arrays C++, Plz
thx for all reply...but I still cant fix it..have to use 1 and bool type function..

Do u mean u must use "bool" and start i by 1 ??

I think you need some changes in the original code in this case :

#include <iostream>
#include <string>

using namespace std;

bool index(string& list, string target) { //you can use your "string list" but I agree with danny in this .
if (list == target) return true;
else return false;
}

int main() {

string words1[] = {"Albino", "bats", "catch", "drosophilia", "c++"};

for (int i=1; i<=sizeof(words1); i++)
{
if(index(words1[i-1], "c++"))
{cout <<i<< endl; break;}
}

return 0;
}
Amahdy at 2007-11-11 21:06:49 >