anyone C++ people on?

Discussion in 'OT Technology' started by D1G1T4L, Mar 1, 2005.

  1. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    any C++ people on?

    i have a question about passing two-dim. arrays

    this is what i got

    Code:
    #include<iostream>
    #include<cctype>
    #include<string>
    #include<iomanip>
    using namespace std;
    //function prototypes
    void selectionSort(char * const array, const int size);
    void swap(char * const element1Ptr, char * const element2Ptr);
    int main()
    {
    //user input max length
    const int INPUT = 512;
    //user input
    char sentence[INPUT];
    //will contain the words
    char storage[INPUT][INPUT];
    //stores value of storage
    int sValue[INPUT] = {-1};
    //counts total input
    int counter = 0;
    //tokenizer pointer
    char *tokenPtr;
    //gets user input
    cout << "Enter a sentence (Up to 1024 characters): ";
    cin.getline(sentence, 1024, '\n');
    tokenPtr = strtok(sentence, " ");
    while(tokenPtr != NULL)
    {
    strcpy(storage[counter], tokenPtr);
    tokenPtr = strtok(NULL, " "); // next token
    counter++;
    }
    //sort the array
    selectionSort(storage[counter], counter);
    //displays only words with more than 0 occurrences
    cout << "\n\nWorld Length ======> Occurrences\n\n";
    //counter that goes through all the input
    /*int counter2 = 0;
    while(counter2 < counter )
    {
    cout << storage[counter2] << "\n";
    counter2++;
    }
    */
    return 0;
    }
    //selection sort function
    void selectionSort(char * const array, const int size)
    {
    /*
    int smallest; //index of smallest array
    //loop over size - 1 elements
    for(int i=0; i < size-1; i++)
    {
    smallest = i; //first index of remaining array
    //loop to find index of smallest element
    for(int index = i + 1; index < size; index++)
    if(strcmp(array[smallest],  array[index]))
    smallest = index;
    swap(&array[i], &array[smallest]);
    }
    */
    //	int counter2 = 0;
    //	while(counter2 < size )
    //	{
    cout << array[0] << "\n";
    //		counter2++;
    //	}
    }
    //swap values at memory location to which element1Ptr 
    // and element2Ptr point
    void swap(char * const element1Ptr, char * const element2Ptr)
    {
    int hold = *element1Ptr;
    *element1Ptr = *element2Ptr;
    *element2Ptr = hold;
    }
    

    all i am trying to do right now is to pass the multi dimensional array and print out the first value

    but when it happens, it just prints out garbage

    why doesnt it print hello as it suppose to be



    as you can see selectionSort(storage[counter]. counter); is not commented out

    and neither is cout << array[0] << "\n"; that prints the first value of storage

    but the only problem is it prints garbage, i dont know why because if i do the same thing cout << storage[0] << "\n"; before passing to function, it prints out fine, so something happens when i pass to the function? or i dont do it right?

    someone help please
     
    Last edited: Mar 1, 2005
  2. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area

    first of all cout << *storage[0] << "\n"
    is not gonna work for 2 reasons
    1st) storage should be array[0] since thats the name you are passing to the function
    2nd) when i use *storage[0], it just tells me illegal indirection and wont let me compile


    also what u just said

    thats what i exactly have right now..... and it prints out garbage

    unless i do the same thing in main, it just doesnt work once i pass that array to function and try to do the same thing
     
  3. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area

    in the main()?

    or in the selectionSort()?

    you werent even printing out the proper array in selectionSort() function, so i dont see how it works for you?
     
  4. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    Your problem is that you're not passing a multidimensional array to selectionsort. You've defined selectionsort to take a char pointer, so all you can give it is a simple char array.
    If you want to pass your storage as a 2-dimensional array, selection sort needs to look like:

    void selectionsort(char ** const array, int size)
    {
    ...
    }

    then you can call it with just the array name:

    selectionsort(storage, counter);
     
  5. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    whoa, hm interesting, so if you want to pass a 2 dimensional array, you have to use ** ??? i am confused, cause i cant find this anywhere in my book
     
  6. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area

    i just did it your way and it says selectionSort cannonc convert parameter 1 from char[512][512] to char **const
     
  7. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    ok i got it to work, the problem was that i was passing storage[counter] to selectionSort, counter is 1 greater than my last index, so i had to pass it as storage[counter-1]
     
  8. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    ok, turns out you have to be specific about the array bounds when you're passing a multidimensional array to a function. It makes sense because the compiler wouldn't know where one element ends and where the next one starts without the array bounds.

    So, selection sort should look like...

    void selectionsort(char array[512][512], int count)
    {
    ...
    }

    if you pass storage[counter] or storage[counter-1], you're only passing one element from your 2-d array. If you really want to sort an array of strings, you need to pass the whole array.
     
  9. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area

    is there a way i can rewrite
    void selectionsort(char array[512][512], int count)
    {
    ...
    }

    using pointers?

    for example
    void selectionSort(const char* array[], const int size)

    or something?
     
  10. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    you could use selectionSort(const char* array[], const int size) if you get rid of your 2-D array and make it a 1-D array of char pointers, then dynamically allocate each string as you need it.

    char *storage[512];

    storage[counter] = new char[512];
     
  11. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    basically how can i pass the whole array using pointers?
     
  12. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    If you use the dynamically allocated method, you'd pass it the same way.

    selectionsort(storage, counter);
     
  13. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    Even if you left selectionsort as...
    void selectionSort(char array[512][512], const int size)
    You're still passing a pointer to the entire array when you call the function using...
    selectionsort(storage, counter);
    The array isn't being copied to the stack. You need to specify the boundaries of the 2-D array so the compiler knows where each element begins. When you declare a 2-D array like char storage[512][512], the data is still one contiguous block memory as if you declared a 1-D array like char storage[262144]. storage[1] of the 2-D version would be equivalent to &storage[512] of the 1-D version. The compiler has to know the array bounds to correctly compute the addresses of each element when you are indexing the array only by it's 1st dimension.
     
  14. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    oh cool ok if i want to use 2d array do i specify my selectionSort as

    void selectionSort(char array[512][512], int count)
    {
    ...
    }

    and how do i pass to it? selectionSort(storage, counter);
     
  15. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    You pass just the array name, without any [] index.
     
  16. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    ok cool thx
     
  17. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    1 more question since my array size is

    [512] by [512]

    and lets say i store "hello" into array[0][]

    if lets say i print out char by char h, e, l, l, o, will the next element be \0 aka NULL or will it be some garbage value?
     
  18. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    If you used strcpy to store "hello" into array[0], then there will be a NULL immediately after the 'o'. After the NULL, it's probably garbage.
     
  19. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    hey skinjob can you help me and explain why my sorting function is not working? i think my swap is not working properly... the program runs fine but it doesnt sort

    Code:
    #include<iostream>
    #include<cctype>
    #include<string>
    #include<iomanip>
    using namespace std;
    
    //function prototypes
    void selectionSort(char array[512][512], const int size);
    void swap(char * element1Ptr, char * element2Ptr);
    
    int main()
    {
    	//user input max length
    	const int INPUT = 512;
    
    	//user input
    	char sentence[INPUT];
    
    	//will contain the words
    	char storage[INPUT][INPUT];
    	
    	//stores value of storage
    	int sValue[INPUT] = {-1};
    
    	//counts total input
    	int counter = 0;
    	
    	//tokenizer pointer
    	char *tokenPtr;
    
    
    	//gets user input
    	cout << "Enter a sentence (Up to 1024 characters): ";
    	cin.getline(sentence, 1024, '\n');
    	
    	tokenPtr = strtok(sentence, " ");
    
    	while(tokenPtr != NULL)
    	{
    	
    		 strcpy(storage[counter], tokenPtr);
    		 tokenPtr = strtok(NULL, " "); // next token
    		 counter++;
    	}
    
    
    	//sort the array
    	selectionSort(storage, counter);
    
    	//displays only words with more than 0 occurrences
    	cout << "\n\n ======> \n\n";
    
    	//counter that goes through all the input
    	/*int counter2 = 0;
    	while(counter2 < counter )
    	{
    		
    		cout << storage[counter2] << "\n";
    		counter2++;
    
    	}
    	*/
    		
    	return 0;
    }
    //selection sort function
    void selectionSort(char array[512][512], const int size)
    {
    	
    	int smallest; //index of smallest array
    
    	//loop over size - 1 elements
    	for(int i=0; i < size-1; i++)
    	{
    		smallest = i; //first index of remaining array
    
    	//loop to find index of smallest element
    	for(int index = i + 1; index < size; index++)
    
    		if(strcmp(array[smallest],  array[index]))
    			smallest = index;
    	
    	swap(array[i], array[smallest]);
    	}
    
    
    	int counter2 = 0;
    	while(counter2 < size )
    	{
    		
    		cout << array[counter2] << "\n";
    		counter2++;
    
    	}
    }
    
    //swap values at memory location to which element1Ptr 
    // and element2Ptr point
    void swap(char * element1Ptr, char * element2Ptr)
    {
    	char *hold = element1Ptr;
    	element1Ptr = element2Ptr;
    	element2Ptr = hold;
    }
    
     
  20. cobra050

    cobra050 Gold Finger

    Joined:
    Sep 5, 2003
    Messages:
    12,663
    Likes Received:
    0
    in swap, try doing a strcpy
     
  21. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    the swap functions seems to swap the values but they dont stay the same order when i print them out

    here is the output

     
  22. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    here is also the print out their addresses,

     
  23. cobra050

    cobra050 Gold Finger

    Joined:
    Sep 5, 2003
    Messages:
    12,663
    Likes Received:
    0
    try this:
    void swap(char ** element1Ptr, char ** element2Ptr)
    {
    char *hold = *element1Ptr;
    *element1Ptr = *element2Ptr;
    *element2Ptr = hold;
    }

    and call it like this: swap(&array,& array[smallest]);
     
  24. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area



    that doesnt work, says none of the 2 overloads can convert parameter 1 from type char(*_w64)[512]


    plus why isnt my version working since it seems to change the elements
     
  25. cobra050

    cobra050 Gold Finger

    Joined:
    Sep 5, 2003
    Messages:
    12,663
    Likes Received:
    0
    i think yours isn't working because it isn't modifying the pointer values. for example

    this does not work
    swap(int x, int y)
    {
    int hold = y;
    y=x;
    x=hold;
    }

    this does
    swap(int *x, int *y)
    {
    int hold = *y;
    *y=*x;
    *x=hold;
    }

    same thing for pointers. i'm not sure why my example didnt' work...

    try strcpy!
     

Share This Page