|
-
Nov 19th, 2002, 05:00 AM
#1
Thread Starter
Lively Member
array question
Hi!
how do I send this array to a function to determin wich of
the texts who contains most characters
int main(){
char text[4][20] =
{
"hello",
"hello 2",
"hello 2 u",
"hello 2 u 2"
};
return 0;
}
-
Nov 19th, 2002, 05:13 AM
#2
Addicted Member
You could declare a function:
Code:
int myFunction(char **array)
{
// Do some stuff in here:
if(strcmp(array[0], "hello") == 0)
....
return 9;
}
Or something like that. If the array is constant you could declare it globally so it would be accessible from anywhere - but I expect that it isn;t always the same.
HD
-
Nov 19th, 2002, 05:22 AM
#3
Thread Starter
Lively Member
Ive tryed this now.. but it doesnt work?!
#include <iostream.h>
#include <string.h>
int myFunction(char **array);
int main(){
char text[4][20] =
{
"hello",
"hello 2",
"hello 2 u",
"hello 2 u 2"
};
int a = myFunction(text);
return 0;
}
int myFunction(char **array)
{
// Do some stuff in here:
if(strcmp(array[0], "hello") == 0){
}
return 9;
}
-
Nov 19th, 2002, 05:53 AM
#4
Addicted Member
What do you mean "it doesn't work"? When you say that try to give more details.
Are you using the string class - or just the string manipulation routines (strcmp, strcpy etc)? If you're using strings then the array problem is just 1-dimensional as you can declare an array of strings.
I am assuming the problem is with the conversion from char[4][20] to char **..?
Sorry about that - you need to declare the functions as
Code:
int myFunction(char array[][20])
You have to declare one of the dimensions for the compiler to like it.
HD
-
Nov 19th, 2002, 08:21 AM
#5
Here's correct code:
Code:
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
int maxchars(char ** arr, int length);
int main(){
char text[4][20] =
{
"hello",
"hello 2",
"hello 2 u",
"hello 2 u 2"
};
int a = maxchars(text, 4);
return 0;
}
int maxchars(char (*arr)[20], int length)
{
int m = 0, i, l;
for(i=0; i < length; i++) {
l = strlen(arr[i]);
m = max(m, l);
}
return m;
}
Here's better code:
Code:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
class pred_strlen {
public:
bool operator ()(const string &s1, const string &s2) {
return s1.length() < s2.length();
}
};
int main(){
string text[4] = {
string("hello"),
string("hello 2"),
string("hello 2 u"),
string("hello 2 u 2")
};
int a = max_element(text, text+4,
pred_strlen())->length();
return 0;
}
The code is better because it uses STL algorithms and the C++ string class.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 19th, 2002, 08:38 AM
#6
Addicted Member
I agree - but I wasn't sure that this was allowed - plus I don't know them very well
HD
-
Nov 19th, 2002, 08:56 AM
#7
Monday Morning Lunatic
If the question is as you said above, then the code given is about as correct as you can get in C++.
If your teacher complains, as I've said before, send them here
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 20th, 2002, 03:00 AM
#8
Thread Starter
Lively Member
OK!!
Thanks for your help...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|