Thread: Transposing Data HELP!!!

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    8

    Transposing Data HELP!!!

    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?

    Code:
    1. #include <string.h>
    2. #include <stdio.h>
    3. char transpose_text(char names[20][60])
    4. {
    5. int i, j;
    6. for (i=0;i<17;i++)
    7. for(j=0;j<60;j++)
    8. printf("%s", names[i][j]);
    9. }
    10. void main (void)
    11. {
    12. FILE *pnames, *trans;
    13. char names[20][60];
    14. int i; /* first for loop variable */
    15. int j; /* second for loop variable */
    16. pnames = fopen(file directory, "r");
    17. trans = fopen(file directory, "w");
    18. while (fscanf (pnames, "%d", &trans) != EOF)
    19. {
    20. fgets(names, 60, pnames);
    21. printf("%s", names);
    22. fprintf(trans, "%s \n", names);
    23. }
    24. transpose_text(names[20][60]);
    25. getche();
    26. }
    I currently have this but it doesn't transpose the names yet. I'm working on that. Any help?

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    What do you mean by transpose exactly? The letters? By how much?

    If you mean transposing the characters by a predefined value, then you can take advantage of the fact that ascii letters have a numeric representation. i.e adding 1 to 'a' gives 'b' and so on. Not all ascii characters are printable though, so you need to figure out how to handle that, and decide if it matters.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    8
    I mean transposing like rows to columns. In other words, we have to print the names not in rows but in columns.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Then you should probably switch your loops, that is, for each j print all i's.

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    8
    On line 9 I already tried switching the i with j and vice versa but the program crashes shortly after that.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You can't just switch i and j since you do not have 60 rows. I don't know what you are doing in line 24 btw, both pname and trans are file pointers.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Compile with your warnings turned all the way up. Here's what I get when I compile your program:
    Code:
    $ gcc -Wall -g -std=c99  transpose.c   -o transpose
    transpose.c: In function ‘transpose_text’:
    transpose.c:8: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
    transpose.c: At top level:
    transpose.c:10: warning: return type of ‘main’ is not ‘int’
    transpose.c: In function ‘main’:
    transpose.c:16: error: ‘file’ undeclared (first use in this function)
    transpose.c:16: error: (Each undeclared identifier is reported only once
    transpose.c:16: error: for each function it appears in.)
    transpose.c:16: error: expected ‘)’ before ‘directory’
    transpose.c:16: error: too few arguments to function ‘fopen’
    transpose.c:17: error: expected ‘)’ before ‘directory’
    transpose.c:17: error: too few arguments to function ‘fopen’
    transpose.c:18: warning: format ‘%d’ expects type ‘int *’, but argument 3 has type ‘struct FILE **’
    transpose.c:20: warning: passing argument 1 of ‘fgets’ from incompatible pointer type
    /usr/include/stdio.h:604: note: expected ‘char * restrict’ but argument is of type ‘char (*)[60]’
    transpose.c:21: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[60]’
    transpose.c:22: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘char (*)[60]’
    transpose.c:24: warning: passing argument 1 of ‘transpose_text’ makes pointer from integer without a cast
    transpose.c:3: note: expected ‘char (*)[60]’ but argument is of type ‘char’
    transpose.c:25: warning: implicit declaration of function ‘getche’
    transpose.c:15: warning: unused variable ‘j’
    transpose.c:14: warning: unused variable ‘i’
    make: *** [transpose] Error 1
    To clean this up:

    • Line 8: %s is for whole strings, i.e. char *. You're using names[j][i] which is just a char. Read the printf docs, you want %c.
    • Line 10: It's int main(void) and return an int at the end, usually 0. Read this: FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com.
    • Line 16-17: Read up on how to use fopen. Also, check the return value and if fopen failed, print an error (look into perror) and exit your program immediately.
    • Line 18: Read some examples on scanf. You need an int variable to store a %d result from scanf. Make sure to pass the address of that int to scanf.
    • Line 20: names is effectively a char **. You just want a char *, so use a counter variable, say i, to count how many names so far. Then do something like fscanf(names[i], sizeof(names[i]), pnames);. The sizeof trick allows you to avoid magic numbers, so you can change the maximum name length in one place only.
    • Line 21-22: Again, use a counter variable i and print names[i].
    • Line 24: names[20][60] doesn't pass the whole array. It tries to pass the 61st "column" of the 21st "row", but you only have 20 rows and 60 columns. Remember, arrays in C start at 0 and end at size-1. Just pass 'names' without the [ ].
    • Line 25: getche is not a standard function. Try getchar instead.
    • Line 14-15: remove any variables you don't need
    • Why does your transpose function stop at 17 instead of 20? This is another good reason to avoid magic numbers and use a constant.
    • After you transpose your array, you have to remember that the null characters that end a name are in each column. It might help if you set the entire names array to all null characters, and in your transpose function, simply print nothing or a space for null characters.

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    52
    Quote Originally Posted by lscamaro View Post
    On line 9 I already tried switching the i with j and vice versa but the program crashes shortly after that.
    What happens when j = 25 and i = 1?

    Your printf will be:
    printf("%s", names[25][1]);

    That will be out of bounds of array names. switch your for loops not i and j.
    Last edited by mnd22; 01-25-2012 at 04:00 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Transposing a matrix made from a 1d array
    By brown in forum C Programming
    Replies: 4
    Last Post: 10-06-2009, 02:05 PM
  2. Transposing matrix
    By 2112 in forum C Programming
    Replies: 16
    Last Post: 02-08-2008, 05:29 PM
  3. Replies: 21
    Last Post: 11-03-2007, 02:56 PM
  4. transposing matrix from file to file
    By inky in forum C Programming
    Replies: 4
    Last Post: 07-19-2005, 08:25 PM
  5. Bit transposing
    By silverwatch in forum C++ Programming
    Replies: 1
    Last Post: 11-04-2003, 04:14 AM