Populating a multi-dimensional array problem

This is a discussion on Populating a multi-dimensional array problem within the C Programming forums, part of the General Programming Boards category; Hello, What I want to do in my program is to populate the first column in a two dimensional array ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    69

    Question Populating a multi-dimensional array problem

    Hello,

    What I want to do in my program is to populate the first column in a two dimensional array with strings. However, if the string is already in the array, I don't want to add it. Here is the loop that I am using:

    Code:
    for (i = 0; i < numCourses; i++) {
            for (k = 0; k <= i; k++) {
                    if (crslist[k][0] != NULL) continue;
                    else crslist[k][0] = courses[i];
            }
        }
    This succeeds in adding every string to the array, but it adds even ones that are already in the array. What I tried to do was use strcmp to check if the crslist[k][0] was the same as courses[i] but I ended up getting a Segmentation Fault one way, and really funky output another way. I'm not allowed to create a new function, or use any memory allocation functions. Any help would be greatly appreciated.

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Please post some more of your code. I'm not entirely sure what you're trying to do.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Based on the code posted so far, I'd guess this is what's wanted:
    Code:
    for (i = 0; i < numCourses; i++) 
    {
      for (k = 0; k <= i; k++) 
      {
        if (strcmp(crslist[k][0], courses[i]) == 0)
          break; /* Already present, ignore this one */
    
        if (crslist[k][0] != NULL) 
          continue;
        crslist[k][0] = courses[i];
      }
    }
    [edit]
    Then again, maybe not. It depends on what crslist and courses are defined as.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    69
    Here's my entire code. Sorry, I don't know why I didn't do this before:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define TOTAL_CLASSES 50
    #define TOTAL_PREREQUISITES 5
    #define NAME_LENGTH 6
    
    main() {
        char *courses[TOTAL_CLASSES];
        char buf[BUFSIZ];
        char *tok;
        char *crslist[TOTAL_CLASSES][TOTAL_PREREQUISITES];
        int position = 0;
        int numCourses = 0;
        int coursesAdded = 0;
        int i;
        int j = 1;
        int k;
        
        // add all courses to array of courses
        while ( fgets(buf, BUFSIZ, stdin) ) {
            for (tok = strtok(buf, " "); tok != NULL; tok = strtok(NULL," ")) {
                    courses[position] = strdup(tok);
                    numCourses++;
                    position++;
            }
        }
        
        // add courses
        for (i = 0; i < numCourses; i++) {
            for (k = 0; k <= i; k++) {
                    if (crslist[k][0] != NULL) {
                       if (strcmp(crslist[k][0],courses[i])) break;
                       else continue;
                    }
                    else crslist[k][0] = courses[i];
            }
        }
        
        printf("%s ", crslist[0][0]);
    }
    With this, I get something in the crslist[0][0] position, but when I try to print crslist[1][0], I get really weird looking characters. Here for more information on the problem, you can go here: http://www.cs.dal.ca/~sedgwick/2132/a3/problem

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Two things so far:
    Code:
    char *crslist[TOTAL_CLASSES][TOTAL_PREREQUISITES] = {0};
    
    
    if (strcmp(crslist[k][0],courses[i]) == 0) break;
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    69
    Thank you so much. It's working good so far. I'm sure I'll be back with more questions, though

    Can I just ask why those bits of code had to be put in there? I'm particularly wondering about the 0 at the end of the crslist line.

    EDIT: Hey, I thought of another question! When I add the strings to the courses array, how can get rid of any newline characters if there's one present? I can't even think of any potential ways to do it.
    Last edited by Zildjian; 09-25-2003 at 07:26 AM.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    Can I just ask why those bits of code had to be put in there? 
    I'm particularly wondering about the 0 at the end of the crslist line.
    The {0} causes the array to be initialised with all elements set to NULL. This is important as you test the first element for NULL within the loop. Without the {0}, the array in uninitialised, and there it isn't NULL, and the "if" statement gets all confused.

    >>how can get rid of any newline characters if there's one present?
    Quick way, straight after fgets(), use this:
    strtok(buf, "\n");
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    22

    Hey Zild

    We happen to be in the same class!! Sedgwick's.
    I'm a total newbie when it comes to c.. i'd been having problem too. Maybe you can help me out if you make any progress.
    Dalguy2004.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multi dimensional array at runtime
    By sawer in forum C++ Programming
    Replies: 21
    Last Post: 05-10-2006, 10:59 PM
  2. Multi Dimensional Array Compare
    By guitarist809 in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2006, 05:03 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. help w/ array problem
    By rhythm313 in forum C++ Programming
    Replies: 3
    Last Post: 11-11-2002, 11:12 PM

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