arrays

This is a discussion on arrays within the C Programming forums, part of the General Programming Boards category; hello, i have a question on arrays....i think. I have a file and it contains a list of 5 first ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    99

    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. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    You could do it with two arrays and two for loops, one nested inside the other.

  3. #3
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83
    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. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    can you give an example of the two structs? i don't think it makes sense.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Quote Originally Posted by droseman View Post
    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. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #7
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83
    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

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    99
    thank you for the replies, however i am still a little stuck as to how to go about this task.
    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:
    
    	FILE * Usernamefileinput ;
    FILE * passwordfileinput ;
    
    printf ("Enter path to Username File\n");
    		scanf ( "%s", &usernamePath);
    		
    		
    		printf ("Enter path to Password File\n");
    		scanf ( "%s", &passwordPath);
    		
    
    	
        Usernamefileinput = fopen (usernamePath, "r+");
     Passwordfileinput = fopen (passwordPath, "r+");
    	
    	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. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I would approach it like this:

    Open your names file

    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. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    99
    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. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can't assign to an array. You can assign to one particular slot of an array.

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    99
    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. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    '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. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    99
    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. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by pastitprogram View Post
    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]);   
       }
    }
    Last edited by Adak; 02-04-2009 at 06:10 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 07:59 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM

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