Thread: Need help for programming

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    4

    Need help for programming

    Hello every one:
    I am doing a small assignment but I am stuck on it. I have no idea at all, please give me a hint.

    Question:
    Implement the List ADT using an array representation with explicit length. You must implement the nine operations given in the class notes (namely: Length, GetKth, Find, FindNext, IsEmpty, Insert, Delete, DeleteKth, and MakeEmpty), using the names they were given (you may not just use array indexing for GetKth, nor a built-in length property/method for Length) and the parameters they were given.
    Try to make the code as generic as possible, given the language you are using. This can be as simple as using typedef to give code that can be compiled for multiple different types, to void pointers, to making it a list of Objects, to using a templated class.

    Nick

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    5
    what specific aspect are you stuck on? and please don't say the whole thing.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    4
    Using Java, I want to do like this:
    int Length(array A[])
    {
    int length = 0;
    for(int i = 0; i < something ; i++)
    length++;
    return length;
    }

    but I don't know how to determinte "the end of array" without using bulid-in length function.

    if for C, what is the sentinel for the end of array? Is it Null?

    int Length(array A[])
    {
    int length = 0;
    while(A[length] != NULL)
    length++;

    return length;
    }

    Is that right?

  4. #4
    Registered User *pointer's Avatar
    Join Date
    Oct 2001
    Posts
    74
    >if for C, what is the sentinel for the end of array? Is it Null?

    In C NULL is a defined integer, in this case 0. The end of an array is determined by a null character '\0' if it's a string, otherwise you have to know the size of the array and use that size as your condition. Or initialize the array initially to all '\0' and you can save the last element as the sentinel.

    >Using Java, I want to do like this:
    int Length(array A[])
    {
    int length = 0;
    for(int i = 0; i < something ; i++)
    length++;
    return length;
    }
    Code:
    Array.length;
    If you don't want to use the length operator for an array, which is a bad idea since length is there to aid the programmer, you can do exactly the same thing. Though keep in mind that in C, unlike Java, you can't declare and initialize your counter inside the for loop, you have to declare all variables at the beginning of the block.

    >but I don't know how to determinte "the end of array" without using bulid-in length function.

    When you declare an array you have to give a size for it, that size is the length of the array, so use that number - 1 and you have the last element in your array.
    pointer = NULL

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well, in C you'd best study pointers, as they allow a multitude of complex operations to be built with a little effort. Most of the Library functions provided to us by past programmers were built using pointers! Here's an approximation of a pointers being used to count the length of a string.


    char hello[] = "Hello";
    ...


    int length(char *pnt)
    {
    int count=0;

    while(pnt){count++;}

    return count;
    }


    This I have not tested but am pretty sure it will work!

    As you can see, pointers can be very powerful, simple, and elegant tools!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    4
    the above loop expression should read

    while(*pnt){count++;}

    not

    while(pnt){count++;}

    you need to use the indirection operator otherwise you are using the address pointed to by pnt not its value.

Popular pages Recent additions subscribe to a feed