# Thread: Populating a multi-dimensional array problem

1. ## 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. Please post some more of your code. I'm not entirely sure what you're trying to do.

3. 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];
}
}```

Then again, maybe not. It depends on what crslist and courses are defined as.

4. 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 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++;
}
}

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. Two things so far:
Code:
```char *crslist[TOTAL_CLASSES][TOTAL_PREREQUISITES] = {0};

if (strcmp(crslist[k][0],courses[i]) == 0) break;```

6. 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.

7. 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");

8. ## 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.