just a question this time

This is a discussion on just a question this time within the C Programming forums, part of the General Programming Boards category; heres my prob i've got an assignment for college that utilises a static data structure, specificaly a table structure it ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    54

    Question just a question this time

    heres my prob

    i've got an assignment for college that utilises a static data structure, specificaly a table structure
    it will have a name, a credit limit and a refNumber(unique no.)

    in my text book (which is written for pascal ) it says that when i delete a section of my table i should move the other data up to sort of fill in the gaps, but if its a static data structure, shouldn't the gaps stay and be filled when new data is added??

    it also has a primitive operation called resetTable? which will reset the position back to the start of the table, i can understand why this is required at all

    heres my structure for the table

    struct table
    {
    int key;//unique number
    char data[20];//name
    int creditLimit;//as it says
    };


    int main(void)
    {
    table dataTable[MAX];//max is defined as 10
    ...
    .
    .
    .
    .
    .
    .
    .
    .
    }and so on

    does that look correct

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Well, there's two ways to do this....
    1. Keep track of the number of entries using a variable.
    2. Use a table string terminator (yes, I just made that phrase up).

    I'm gonna look at option 1, as the second option involves occupying an entire structure. Our table should, having some data in it, look something like this...

    Table has 4 elements...
    1: TableNode 0
    2: TableNode 1
    3: TableNode 2
    4: TableNode 3
    5: Don't care
    6: Don't care
    ...

    numElements = 4

    And if we remove TableNode 1, then...
    1: TableNode 0
    2: TableNode 2
    3: TableNode 3
    4: Don't care
    5: Don't care
    ...

    numElements = 3



    If you do it this way, then there is the nice benefit that emptying your table simply means setting numElements = 0
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you "remove" a middle element of an array, you bump everything below it up one element, so that you can run through the array without having to wonder if you're going to hit a "deleted" value.

    Let's say you remove array[3]. Ok, then all you do is:


    for(x=4;x<array_count;x++) array[x-1]=array[x];

    Now you just decrement count.

    array_count--;

    You now can display your array easily:
    for(x=0;x<array_count;x++) showElement( array[x] );

    Sure, you could tag each empty element as "unused", but then you'd have to change how you display/sort/etc them.
    Code:
    for(x=0;x<array_size;x++)
    {
        if( array[x] != empty )
        {
            do_whatever( array[x] );
        }
    }
    One extra check for each time you want to access any array element now, instead of just checking to see if the element you're accessing is higher than the total count or what not.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    >> int main(void) <<

    First of all, you should be doing 'int main()' for your definition of main.

    >> table dataTable[MAX];//max is defined as 10 <<

    Secondly, your declaration is a little off. It should be:
    Code:
    struct table dataTable[MAX];
    But besides that, your code looks fine.

    --Garfield
    1978 Silver Anniversary Corvette

  5. #5
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Arg!!! quzah and QuestionC got to it before me. Sorry for the clutter.
    1978 Silver Anniversary Corvette

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Hehe. We all raced to reply apparently!

    >> int main(void) <<

    First of all, you should be doing 'int main()' for your definition of main.
    No. This is wrong. If you fail to provide the values, your compiler will default values for you. It's always better to indicate what you want than to rely on your compiler to provide defaults.

    Additionally, this is not Java, so "myfunction(void)" isn't considered wrong. (I believe Java dislikes you to use the keyword "void" in method parameters.)

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Unless you use #pragma, the program will start with main and I don't see the reason for parameter passing in main.

    --Garfield
    1978 Silver Anniversary Corvette

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    54
    thanx for the quick reply guys

    thats what i thought was the correct way, but wont that approach not make the table a static structure anymore, i get confused with the static/dynamic scenario

    on the int main(void) flame, hehe blame my c teacher

    on the struct correction, do you really need to place struct in front of it, i have a working program using a vector structure and i aint used struct at all except for setting up the structure itself

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Parameter passing in main just to make it explicit that you don't want anything. I'm sure there's somerule out there deciding what is and isn't legal for main declaration, but for all purposes,
    int main (void)
    is just documenting that you're not wanting to use any command line arguments. And whether one thinks it makes sense or not, it certainly is legal.
    Callou collei we'll code the way
    Of prime numbers and pings!

  10. #10
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Yeah, I'm pretty sure you need the 'struct' keyword. If you just wanted to use 'table' as the specifier, then your struct would have to be set up like this:
    Code:
    struct {
    /* struct members */
    } table;
    Then you can just write:
    Code:
    table tblVar;
    Whatever you choose.

    --Garfield
    1978 Silver Anniversary Corvette

  11. #11
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    >> it certainly is legal. <<

    I respect your answer, but just because something is legal, doesn't make it good practice. And not just in programming.

    --Garfield
    1978 Silver Anniversary Corvette

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    I'm pretty sure it will make a static dtat structure actually. It's static because it occupies a constant amount of memory. It's not really a well defined term, but the implementation that you seem to describe follows what we suggested.
    Callou collei we'll code the way
    Of prime numbers and pings!

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    >>> it certainly is legal. <<
    >
    >I respect your answer, but just because something is legal,
    > doesn't make it good practice. And not just in programming.
    >
    >--Garfield

    I fail to see why you are so hung up on this. In your opinion, why
    is 'int main()' better than 'int main( void )'? Additionally, explain
    then why this:

    int myfunction()

    is better than:

    int myfunction(void)

    It is always better to implicitly state what you mean, rather than
    to have it left up to un-documented compiler defaults. (When I
    say 'un-documented', I mean to say, that by glancing at the code
    you know for a fact that '(void)' means it is guarinteed to have
    no arguments.

    If you say 'int main()', what defaults does it use? I can venture a
    guess and say it's the same thing as 'void', but what if it isn't?
    What if instead, it defaults to:

    int main( int argc, char *argv[] )

    Not that it really should make any difference, if you aren't using
    variables named that, but still, why is it "wrong" to be specific?

    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    >> why is it "wrong" to be specific? <<

    I didn't say it was wrong, now you're twisting my words. I said that it is not always good practice. I guess I can see your argument here.

    --Garfield
    1978 Silver Anniversary Corvette

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers
    By Big_0_72 in forum C Programming
    Replies: 3
    Last Post: 10-28-2008, 08:51 PM
  2. need help in time zone
    By Gong in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2007, 04:44 AM
  3. It's a question of time ...
    By Mandy_C in forum C Programming
    Replies: 4
    Last Post: 04-09-2006, 03:18 PM
  4. time question.
    By InvariantLoop in forum C Programming
    Replies: 5
    Last Post: 02-01-2005, 02:00 PM
  5. inputting time in separate compilation
    By sameintheend01 in forum C++ Programming
    Replies: 6
    Last Post: 03-13-2003, 04:33 AM

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