Results 1 to 8 of 8

Thread: array question

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2001
    Location
    Sweden, Sthlm
    Posts
    112

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

  2. #2
    Addicted Member HairyDave's Avatar
    Join Date
    Aug 2002
    Location
    Er...I can't remember.
    Posts
    196
    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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2001
    Location
    Sweden, Sthlm
    Posts
    112
    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;
    }

  4. #4
    Addicted Member HairyDave's Avatar
    Join Date
    Aug 2002
    Location
    Er...I can't remember.
    Posts
    196
    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

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  6. #6
    Addicted Member HairyDave's Avatar
    Join Date
    Aug 2002
    Location
    Er...I can't remember.
    Posts
    196
    I agree - but I wasn't sure that this was allowed - plus I don't know them very well

    HD

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Feb 2001
    Location
    Sweden, Sthlm
    Posts
    112
    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
  •  



Click Here to Expand Forum to Full Width