Array manipulation Question

This is a discussion on Array manipulation Question within the C++ Programming forums, part of the General Programming Boards category; Ok so i'm writing a program to sort an array using heap sort. I have my main function that gets ...

  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,893
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    So I've figured out that the array doesn't really get modified when I pass the array into the function
    And why not?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    cin >> len;
      int A[len];
    How do you succeded to compile this code?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    or use std::vector
    or using new
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Page 1 of 2 12 LastLast
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, 06: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, 12:01 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  5. Create Array size Question
    By popohoma in forum C++ Programming
    Replies: 3
    Last Post: 11-04-2002, 02:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21