Thread: can i dynamicaly check the size of an array?

  1. #1
    Registered User matheo917's Avatar
    Join Date
    Sep 2001

    can i dynamicaly check the size of an array?

    i am currently working on the program that uses recursion...summarizing it ---> have to enter 10 integers into an array and using recursion and and binary search (divide into 2 smaller problems)...
    the only thing that gets me stuck is how can I dynamicaly check the size of array as we go deeper and deeper into each recursive call...????? the reason for this is that i need a base (stopper) case that will return a value when an array will have only 1 member...???

    i will include the code for this function...
    i have commented the problems....

    int MaxArray(int Array[], int First, int Last)
    int Largest;

    int Mid = (Array[Last]) / 2; // finding midpoint

    if (Last == 1) // won't work..... NOT (Last)
    Largest = Array[Last]; // need to have a different base case
    // not totally different (just the tiny bit different)

    MaxArray(Array, First, Mid); // One half including midpoint
    MaxArray(Array, (Mid + 1), Last); //Second half
    // recursive call...
    // i have to send the results to another function to compare
    // integers in term of size......and return the biggest one.


    i have done the same problem in various easier ways, this time i need to do it this way...

    i would appreciate any suggestions and any help...


  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Unless you use a sentinel value to mark the end of the array there is no way to dynamically find the size. If you need to to pass on information about the size from the function that created the array, where you can do sizeof(array/array[0]) if necessary, you'll have to send it in separate parameter.

  3. #3
    Here are some suggestions:

    Don't pass the array about, make it global if you can to reduce stack usage.

    Your Mid function should look at the difference between first and last e.g.

    Mid = First + (Last - First ) / 2;

    Your function should return when the difference between First and Last is <= 1;

    Have your function return the maximum of the two values that it is currently checking, this way when your recursion unrolls, the maximum value will float to the top.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c++ a very simple program
    By amjad in forum C++ Programming
    Replies: 1
    Last Post: 05-27-2009, 12:59 PM
  2. Little Array Difficulty
    By G4B3 in forum C Programming
    Replies: 16
    Last Post: 03-19-2008, 12:59 AM
  3. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  4. Changing a Structures Members array size
    By Xei in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2002, 07:45 PM
  5. Class member array size with constuctor initalizer
    By rafe in forum C++ Programming
    Replies: 2
    Last Post: 10-14-2002, 10:09 AM