Thread: Array manipulation Question

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    25

    Array manipulation Question

    Ok so i'm writing a program to sort an array using heap sort. I have my main function that gets the information on what the array is, then using a function to sort it. The function is defined as: void maxheapify(int A[], int i, int len)

    So I've figured out that the array doesn't really get modified when I pass the array into the function. But I can't just do A=maxheapify(A, i, len) either because in maxheapify A is an int*... And if I turn the A in main to an int* then I get a whole crap load of errors....

    So anyone have any suggestions? I can post my whole code if need be.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I see no reason why that shouldn't modify the array contents. You must have an error in the implementation.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    So I've figured out that the array doesn't really get modified when I pass the array into the function
    And why not?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    25
    I printed out the array before and after and it comes out the same.... (not sorted)

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by JJJIrish05 View Post
    I printed out the array before and after and it comes out the same.... (not sorted)
    And what about showing the code?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    25
    Code:
    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    
    using namespace std;
    
    
    int operationsCounter=0;
    
    void maxheapify(int A[], int i, int len)
    {
    	int l=2*i;
    	int r=2*i+1;
    	int largest, exchange;
    	if ((l<=len) && (A[l]>A[i]))
    	    {
    		largest=l;
    		operationsCounter++;
    	    }
    	else
    	    {
    		largest=i;
    		operationsCounter++;
    	    }
    	operationsCounter++;
    	if ((r<=len) && (A[r]>A[largest]))
    	    {
    		largest=r;
    		operationsCounter++;
    	    }
    	operationsCounter++;
    	if (largest!=i)
    	{
    		exchange=A[largest];
    		A[largest]=A[i];
    		A[i]=exchange;
    		maxheapify(A, largest, len);
    		operationsCounter+=4;
    	}
    	operationsCounter++;
            //return (A);
    }
    
    int main()
    {
      int exchange, len, option, fakelen;
      cout << "How big of an array do you want to sort? " << endl;
      cin >> len;
      int A[len];
      cout << "To have " << len << " numbers generated randomly please enter 1, to enter the array yourself please enter 2: " << endl;
      cin >> option;
      if (option == 1)
        {
          for (int i=0; i<len; i++)
    	{
    	  A[i]=rand();
    	  operationsCounter++;
    	}
        }
      else if (option == 2)
        {
          cout << "Enter the array: " << endl;
          for (int i=0; i<len; i++)
    	{
    	  cin >> A[i];
    	  operationsCounter++;
    	}
        }
      else
        {
          cout << "You didn't enter 1 or 2.  What a fool. Restart the program if you must." << endl;
        }
      cout << "The array before sorting: ";
      for(int i=0; i<len; i++)
        cout << A[i] << " ";
      cout << endl;
      fakelen=len;
      for(int i=len; i>1; i--)
        {
          fakelen--;
          exchange=A[1];
          A[1]=A[i];
          A[i]=exchange;
          maxheapify(A, 1, fakelen);
        }
      cout << "The array after sorting: ";
      for(int i=0; i<len; i++)
        cout << A[i] << " ";
      cout << endl << "The approximate number of operations for an array of size " << len << " is: " << operationsCounter << endl;
      return 0;
    }

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    cin >> len;
      int A[len];
    How do you succeded to compile this code?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    25
    why? is that not how you declare arrays in c++? It compiles fine and takes in the numbers and prints them out....

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by JJJIrish05 View Post
    why? is that not how you declare arrays in c++? It compiles fine and takes in the numbers and prints them out....
    No, it is not... It is C99, but rest is not C

    Code:
    1>test.cpp(49) : error C2057: expected constant expression
    1>test.cpp(49) : error C2466: cannot allocate an array of constant size 0
    1>test.cpp(49) : error C2133: 'A' : unknown size
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    25
    compiles fine for me using gcc.... so then how am i supposed to declare it?

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    or use std::vector
    or using new
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Registered User
    Join Date
    Mar 2008
    Posts
    25
    why do i have to use one of those? couple sites i've looked at said the way I did is the way to do it.... why shouldn't it work and how exactly do i declare it? int[] A = new int A[len] or something like that?

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Also your algorithm is very strange. You never access the A[0]
    This part of main
    Code:
    exchange=A[1];
          A[1]=A[i];
          A[i]=exchange;
    also has no explanation for me
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #14
    Registered User
    Join Date
    Mar 2008
    Posts
    25
    oh ya.... lol... 1 is supposed to be 0.... let me change that....

  15. #15
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by JJJIrish05 View Post
    why do i have to use one of those? couple sites i've looked at said the way I did is the way to do it.... why shouldn't it work and how exactly do i declare it? int[] A = new int A[len] or something like that?
    Code:
    int* A = new int[len];
    couple sites i've looked at said the way I did is the way to do it
    And what are they? This is not C++, so maybe you looked fro some other langugage - C99 for example
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  2. Array of Structs question
    By WaterNut in forum C++ Programming
    Replies: 10
    Last Post: 07-02-2004, 02:58 PM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. Create Array size Question
    By popohoma in forum C++ Programming
    Replies: 3
    Last Post: 11-04-2002, 03:04 AM