1. ## arrays

hello, i have a question on arrays....i think. I have a file and it contains a list of 5 first names and 5 second names. I need to print out on screen every possible combination of names on screen so i was thinking the following. Do i first read in the file, then store all names in an array and all second names in another array and somehow work through these logically? Im quite new to this and im not sure if an array is the way to go to achieve this task? Im not searching for a full answer to the solution just a suggestion to point me in the right direction and what im thinking is correct

Any input is apprecited, thank you

2. You could do it with two arrays and two for loops, one nested inside the other.

3. Why don't you store the first names and second names in two structs, then you can access any one you like using the dot notation

--dave

4. can you give an example of the two structs? i don't think it makes sense.

5. Originally Posted by droseman
Why don't you store the first names and second names in two structs, then you can access any one you like using the dot notation

--dave
If you are storing the first name in one struct (containing just a character string?) and the last in another, isn't that the same as using two arrays?

6. There are certain algorithms that just beg or even require, you to use a linked list. [The very slick and advanced Algorithm X, aka Dancing Links, is one such. There are others.]

Aside from those types of algorithms, I'd suggest *always* using an array. They're easier and faster to program, easier to debug, and faster running.

It's sort of like recursion. It's a great tool for some algorithms, but unless the program is made more clear, or with less code, or both, (like depth first search), forget about it. Use iterative loops with function calls, instead.

We had a recent experience with an assignment of transgalactic2, that required a recursive solution to a simple problem. The answer was just a brain buster, requiring several hours to figure it out, just because it had to be fully recursive, and involved recursive calls, inside of other recursive calls, all in the same function. What a headache!

Imo arrays are the meat and potatoes, and fish too, for programming. Instant access by using an index, plus it provides a pointer right to it's own base - hey, you can't beat that.

7. If you are storing the first name in one struct (containing just a character string?) and the last in another, isn't that the same as using two arrays?
I had in mind a struct of arrays

Code:
```struct names
{
firstname1[5];
firstname2[5];
firstname3[5];
};```
and the same for the second names.

As I don't know much about linked lists I can't really comment on that but it sounds like a good approach

--dave

I have managed to do as follows which prints out all 7 usernames and passwords from each file on the screen however i cannot figure out how to put them into an array or print out every possible combination of both. The 2 files just contain a list of 7 words each

Code:
```

printf ("Enter path to Username File\n");

printf ("Enter path to Password File\n");

char line [7];
char lines [7];

while (fgets(line, sizeof line, Usernamefileinput) !=NULL)
{

fputs (line, stdout);

}

while (fgets(lines, sizeof lines, Passwordfileinput) !=NULL)
{

fputs (lines, stdout);

}```

thanks

9. I would approach it like this:

read your names into a names1[][] and names2[][] arrays that you have declared, make sure your arrays have the length (number of rows), and breadth (number of columns or char's), for each name in the files.

ideally, you'd malloc the size you needed, but malloc isn't something you've covered yet, so just make it big enough.

then use a while (not EOF) loop to read in the names data, into your names1[][] array.

Code:
```i = 0
while((fscanf("%s", names1[i])) != EOF)
i++;```
fgets() is a better alternative. I didn't think you'd been introduced to it yet.

When you first try to open a file, failure will bring back NULL. When you want to test for the end of a file, use EOF, rather than NULL.

Anyway, repeat the above actions with names2[][], but use j as your counter, instead of i.

Now you have what you need for your combinations, with the help of two nested for loops

Code:
```for(k = 0; k < i; k++)
{
//print a name1[k] here
for(m = 0; m < j; m++)
print a name here from names2[m]
}```

10. thank you for your help. I understand the looping part which will do exactly what im hoping! however im struggling with the reading part. Whenever i read each name and print it out it works fine, however when i try to put it in my array rather than print it out i start incurring lots of problems. It keeps telling me i cant put them into an array. Im wondering if ive defined my array round as ive used

int array [7];

for 7 items to be placed in the array?

11. You can't assign to an array. You can assign to one particular slot of an array.

12. Ah rite well i was trying to assign it to a part of the array like below

Code:
```int i;
int NameArray [5];  // holds 5 names

while (fgets(line, sizeof line, Usernamefileinput) !=NULL)
{

for (i = 0; NameArray [i]; i++)
{
NameArray[1] = line;
}

}```
I was expecting this to get a name stored in 'line' then assign 'line' to the next available type in my array but it keeps throwing up mismatch errors. Is 'line' not a string in which i can assign to my array? or is it because my array is defined as an 'int'?

thank you for your patience everyone

13. 'line' is indeed not a string. It is an int. Arrays are always (whatever)[] -- and since a string is char[], you need a char[][].

14. sorry i think im asking silly questions but i dnt think i understood the last 1.
So i should define by array as

char Name [5][20]; // Five entries with twenty chars long?

would i have to convert line to a string before i can attempt to assign it to my array?

15. Originally Posted by pastitprogram
sorry i think im asking silly questions but i dnt think i understood the last 1.
So i should define by array as

char Name [5][20]; // Five entries with twenty chars long?

would i have to convert line to a string before i can attempt to assign it to my array?
Each row in the array is a char string because it has an end of string char '\0' at the last char position.

This is what I was suggesting:

Code:
```#include <stdio.h>

int main(void)  {
FILE *inf;
int i;

void printIt(char names[][20], int i);

char names[20][20] = {{ '\0' }};  //fills the array with end of string markers

if((inf = fopen("text.txt", "rt")) == NULL)
return 1;

i = 0;
while((fscanf(inf, "%s", names[i])) != EOF)
i++;

printIt(names, i);

i = getchar();
return 0;
}
void printIt(char names[][20], int NamesNum)  {
int i;

for(i = 0; i < NamesNum; i++)
printf("%s\n", names[i]);
}
/* Where text.txt file had the contents:
Ann
Barbara
Charles
David
Edward
Frank
*/```
Passing around 2D arrays and getting rows to print as a string, can be a little dodgy.

Now I'd add the array names2[][], and do basically the same thing. Load it from the file into your names2 array, and then call printit() to be sure it's loaded right.

Now you're *finally* ready for your two for loops, as I showed earlier, (also suggested by DaveH).

Did you say the first and last names were listed in the same file?

If so, then i and j in the two for loops, can reference the very same array, and you don't need a second array, at all.

Code:
```for(i etc. )   //handles first names
{
for(j etc. )  //handles last names
{
printf( "first name: %s,  last name: %s", names[i], names[j]);
}
}```