Thread: Urgent help on 3d Array

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    5

    Unhappy Urgent help on double-subscript Array

    I'm a newbie to c...i'm having problem with an assignment question.

    The question requires a program to read in staff details from keyboard. The details are staffname and staffnumber.
    Once the last record is read, the program is to prompt for a staffname then search for that staff name using the linear search method. If the name is found the staffname and number are to be displayed.

    I've decided that i will put the staffname and number into a double-subscript array...but i'm not totally sure of how to put the input from the keyboard into the double-suscript array........Plz help .
    Last edited by despoil; 05-17-2002 at 12:59 AM.

  2. #2
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Post some code.
    Alot of people will help you with arrays if you post the effort you made.
    Your question sounds simple enough, lets see what you've tried.

    Please use code tags when you do.
    The world is waiting. I must leave you now.

  3. #3
    Unregistered
    Guest
    You only have a name and number, so I doubt that you'll need a 3D array. Also, newbie assignments aren't likely to use 3D arrays.

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    5
    Ok here is an example of what I'm trying to do

    #include <stdio.h>

    {
    char * details[0][0];

    printf("Enter staffname and staffnumber\n");
    scanf("%s%d", details[0][0] );
    printf("\nDetails were: %s%d", details[0][0] );

    return 0;
    }
    the problem is the array size is determind by number of inputs from the keyboard and i"m not sure on how to do this... however the array should look like this:

    Bob Marley 1
    Dennis Leary 2
    Tom Hanks 3
    Jesse Owen 4
    John Keats 5



    Last edited by despoil; 05-17-2002 at 12:49 AM.

  5. #5
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    Code:
    #define MAXNAMES  1000  // 1000 name max
    #define MAXCHARS  100    // 100 letters per name max
    
    typedef struct  detail {
      char name[MAXCHARS];
      int number;
    } Detail;
    
    typedef Detail EntryTable[MAXNAMES];
    This is the most easy way I could think of storing the names and numbers.
    'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    207
    I had the same problem a few months ago. You just didn't catch the meaning of 1D, 2D, 3D... arrays. Do a search, I think that Salem has given quite a few good answers about it.

    Also, play with stautze's answer, that's as simple as you can get.

    I think you can go for dynamic memory allocation, but that's way too naughty (sp?) for now.

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I wouldn't go beyond using 2D arrays. Even 2D are slower than 1D. 3D gets too confusing and there are far better data structures to represent 3D arrays, like an array of structs that have 3 data members.

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    5

    searching

    Ok this is what my code looks like so far for now
    Code:
    #include <stdio.h>
    
    struct staff {
    	char *staffname;
    	int staffnumber;
    };
    
    typedef struct staff Staff;
    
    int main()
    {
    	char *staffname[50];
    	int staffnumber[50];
    	int searchname;
    	printf("Enter staffname and staffnumber\n");
    	printf("Enter EOF to end input\n");
    	
    	while (!feof(stdin)) {
    		scanf("%s%d", &staffname, staffnumber);
    	}
    
    	printf("Enter searchname\n");
    	scanf("%s", &searchname);
    
    	return 0;
    }
    What i want to know is how to do a linearsearch for a name and if that name is found display to screen that name and its corresponding number. Also I don't fully understand struct the textbook tells me what it is but doesn't show me how to use it...so I'm just going off from what 'stautze' said, which by the way I'm thankfull for.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The basics
    Code:
    #include <stdio.h>
    
    int main()
    {
        char staffname[50][20];
        int staffnumber[50];
        int count = 0;
        int i;
    
        printf("Enter staffname and staffnumber\n");
        printf("Enter EOF to end input\n");
        
        while ( scanf("%s%d", staffname[count], &staffnumber[count]) == 2 ) {
            count++;
        }
    
        for ( i = 0 ; i < count ; i++ ) {
            printf( "%d: %s %d\n", i, staffname[i], staffnumber[i] );
        }
        return 0;
    }

  10. #10
    Registered User
    Join Date
    May 2002
    Posts
    5

    Linear Searching

    This is the code
    Code:
    #include <stdio.h>
    #define size 50
    int main()
    {
        char staffname[size][20];
        int staffnumber[size];
        int count = 0;
        int i;
    	char searchname[20];
    
        printf("Enter staffname and staffnumber\n");
        printf("Enter EOF to end input\n");
        
        while ( scanf("%s%d", staffname[count], &staffnumber[count]) == 2 ) {
            count++;
        }
         printf("Enter search name\n");
         scanf("%s", searchname);
    	
        for ( i = 0 ; i <= count; i++ ){
    	if (staffname[i] == searchname)
    	printf("%s:%d", staffname[i], staffnumber[i]);
    	}
    
    	return 0;
    }
    What's wrong with this...I want to perform a linear search for name and then display that name to screen with it's corresponding number.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > for ( i = 0 ; i <= count; i++ ){
    for ( i = 0 ; i < count; i++ ){

    > if (staffname[i] == searchname)
    if ( strcmp(staffname[i],searchname) == 0 )

    Should be all you need
    Don't forget to include string.h

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    despoil> remember that strcmp() is case sensative If you want to do case insensative, convert all characters to either upper or lower, before doing the compare.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Registered User
    Join Date
    May 2002
    Posts
    5

    Doesn't Work

    The code........THANKS SALEM
    [Code]
    #include <stdio.h>
    #include <string.h>
    #define size 50
    int main()
    {
    char staffname[size][20];
    int staffnumber[size];
    int count = 0;
    int i;
    char searchname[20];

    printf("Enter staffname and staffnumber\n");
    printf("Enter EOF to end input\n");

    while ( scanf("%s%d", staffname[count], &staffnumber[count]) == 2 ) {
    count++;
    }
    printf("Enter search name\n");
    scanf("%s", searchname);


    for ( i = 0 ; i <= count; i++ ){
    for ( i = 0 ; i < count; i++ ){
    if (staffname[i] == searchname)
    if ( strcmp(staffname[i],searchname) == 0 )
    printf("%s:%d", staffname[i], staffnumber[i]);
    }
    }


    return 0;
    }
    It works right up until it asks for the search name...then it just quits without letting you type in the name.

  14. #14
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >It works right up until it asks for the search name...then it just quits without letting you type in the name
    The input buffer still contains the newline character from the previous scanf() call. You'll need to flush it with something like
    >while (getchar() != '\n');
    This goes just before
    >printf("Enter search name\n");

    Also, you still have this in you code:
    >if (staffname[i] == searchname)
    As per Salem's comments, you need to remove it, and let strcmp() do the work for you.

    [EDIT]Sorry Salem, am I butting in here, didn't mean too! [/EDIT]
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Noooooooooooooooo

    > for ( i = 0 ; i <= count; i++ ){
    for ( i = 0 ; i < count; i++ ){

    > if (staffname[i] == searchname)
    if ( strcmp(staffname[i],searchname) == 0 )

    Doesn't mean ADD to the code
    it means REPLACE your code with MINE

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 07:14 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. 3D Array of Bool
    By cybernike in forum C++ Programming
    Replies: 37
    Last Post: 06-28-2007, 11:17 AM
  4. Replies: 1
    Last Post: 01-11-2002, 11:22 PM
  5. 3D Array
    By Alextrons in forum Windows Programming
    Replies: 4
    Last Post: 01-11-2002, 01:39 AM