I'm a college student learning how to program in C. We recently got an assignment to transpose a list of names. We have to read in from a file a list of names and print them out and write to a file with the names going downwards.

I'm not asking for anyone to do my homework so please don't tell me that you won't because I know that. I simply need some pointers.

I'm going to use a 2 dimensional array as well as a double for loop for the printing.

Does anyone have any pointers on how to do this? Other than that I have nothing else. Thank you in advance.

you don't need a two dimensional array. Read a name from the file, then loop through the names that you have loaded until you find one that is less than this one. insert the name in the list before this entry.

when you are complete read the list from the start printing it as you go.

I understand what your saying but sadly, apparently we have to use a 2 dimensional array. Thank you for the input

A "list of names" by definition is a 2D array.
Each row is one name
Each column is one letter in each name.

So with this definition, I would have to run a double for loop to start printing letter by letter? In other words, I'd have to start printing the first letter of every name and then move onto the second and so on and so on?

Here is what I have so far.

#include <string.h>
#include <stdio.h>

char transpose_text(char names[20][60])
{
    int i, j;
    for (i=0;i<17;i++)
         for(j=0;j<60;j++)
             printf("%s", names[i][j]);
     
     
}

void main (void)
{
     FILE *pnames, *trans;
     char names[20][60];
     int i; /* first for loop variable */
     int j; /* second for loop variable */
     
     pnames = fopen(file directory, "r");
     trans = fopen(file directory, "w");
     
     while (fscanf (pnames, "%d", &trans) != EOF)
     {
          fgets(names, 60, pnames);
          printf("%s", names);
          fprintf(trans, "%s \n", names);
     }
     
     transpose_text(names[20][60]);
     
     getche();
}

Kudos for using fgets
Bad for using void main() -- see this
Bad for using getche() -- see this
And one more bad for "Here is what I have so far" with no additional information. Is what you posted working?
-- If so, say so, mark the thread solved, and thank the people that helped.
-- If not, explain why not. In detail.

Sorry, no what I have doesn't work. It reads from the file fine but I have yet to solve transposing the names. I would think that given the definition you stated I could write the loop to go through the list and get the first letter of each name then go to the next until it reaches the end of the list. After, I would move on to the second letter and so on? Am I on the right track

Yes. But you might want to do a sanity check on what you have. Did you in fact read the file and store it correctly? All you checked was reading the file.

What if your names file doesn't open? Why would you still want to read the file?

Why are you writing what you read to the transpose file? Isn't it supposed to contain the transposed names? For that matter, why do you even have the transpose file open yet?

What are you doing with trans in line 24? Why are you reading an integer into it? What are you doing with that integer?

This article has been dead for over six months. Start a new discussion instead.