Hey,
Basically for this question, I am reading elements of a matrix from a text file. Now, the matrix is an upper triangular matrix, meaning the elements under the main diagnol are zero.
eg
1 2 3 4
0 5 6 7
0 0 9 1
0 0 0 2
Heres the problem. In the text fle, the matrix is stored in "efficient form," meaning the anything below the main diagnol (the zeros) are not listed.
Now I am trying the read in the efficient matrix text file and display it on the screen WITH the zeros added in. (It is a 6 x 6 matrix in the txt file btw) I have my algorithm...
i = the zero counter (intialized to 0)
j = the integer element counter (intialized to 6)
-print the number of zeros in the i counter (i = 0)
-increment the i counter (i = 1)
-print the number of elements in the j counter from the text file (j = 6)
-decrement the j counter (j =5)
- new line
-print the number of zeros in the i counter (i = 1...print 1 zero)
-increment the i counter (i = 2)
-print the number of elements in the j counter from the text file ( j = 5... print the next 5 elements)
-decrement the j counter (j = 4)
- new line
....and so on (the i and j variables dont match my code exactly...)
I think my code is correct except for the part of reading in the text file in the element[] array (in bold below). Im not too sure on how to do that. If you could help me out with that, Id really apprecieate it!
Thanks
Code:#include<stdio.h> #include <stdlib.h> viewM1(); main() { int choice; printf("\nThis program allows you to multiply two matricies in a NON-EFFICIENT format\n\n"); printf("Please choose from one of the following options:"); printf("\n1 - See Matrix #1"); printf("\n2 - See Matrix #2"); printf("\n3 - Multiply Matrix #1 and Matrix #2"); printf("\n4 - Exit\n"); printf("\nOption Number: "); scanf("%d", &choice); if (choice == 1) viewM1(); return 0; } viewM1() { int elements[22], i, j, k, l, counter = 0, a = 0; FILE *file_ptr; file_ptr = fopen("matrixA.txt", "r"); if (file_ptr == NULL) { printf("Sorry! The File could not be opened!\n"); exit(1); } else { while (!feof(file_ptr)) fscanf(file_ptr, "%d", &elements[a]); a = a + 1; } for (i = 0; i <= 5; i++) { for (j = 0; j < i; j++) { printf("0"); for (k = 6; k > 0; k--) { for (l = 0; l <= k; l++) { printf("%d", elements[counter]); counter++; } } } printf("\n"); } fclose(file_ptr); return 0; }



LinkBack URL
About LinkBacks


