Hi again,
1 more problem now....
I changed the location of where the files are scanned into the main function so that I can pass the arrays on the matrix multiply function when need be. I also added in a 2D array which stores the enitre matrix (matrixA2D and matrixB2D - so that I can matrix multiply later on)
When I enter option 1 to view Matrix 1, it goes to the 5th row of the array and abruptly stops and I get an error. I took out the operation where I fill in the elements for the 2D array, and then it works. Im not sure what the problem is though. Tracing through the code, it seems like what I have wrote will fill in the elements for the 2D array... Any thoughts?
Code:
#include<stdio.h>
#include <stdlib.h>
void viewM1(int [21]);
void viewM2(int [21]);
void main()
{
FILE *file_ptrA;
FILE *file_ptrB;
int choice;
int elementsA[21], elementsB[21], counter = 0, a = 0, b = 0;
file_ptrA = fopen("matrixA.txt", "r");
file_ptrB = fopen("matrixB.txt", "r");
while (!feof(file_ptrA))
{
fscanf(file_ptrA, "%d", &elementsA[a]);
a = a + 1;
}
while (!feof(file_ptrB))
{
fscanf(file_ptrB, "%d", &elementsB[b]);
b= b + 1;
}
fclose(file_ptrA);
fclose(file_ptrB);
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(elementsA);
else if (choice == 2)
viewM2(elementsB);
}
void viewM1(int matrixA[])
{
int i, j, counter = 0, a= 0, matrixA2D[6][6];
for (i = 0; i <= 5; i++)
{
for (j = 0; j < i; j++)
{
printf("%4d ", 0);
matrixA2D[i][j] = 0;
}
for (; j <= 5; j++)
{
printf("%4d ", matrixA[counter]);
matrixA2D[i][counter] = matrixA[counter];
counter++;
}
printf("\n");
}
}
void viewM2(int matrixB[])
{
int i, j, counter = 0, a = 0, matrixB2D[6][6];
for (i = 0; i <= 5; i++)
{
for (j = 0; j < i; j++)
{
printf("%4d ", 0);
matrixB2D[i][j] = 0;
}
for (; j<=5; j++)
{
printf("%4d ", matrixB[counter]);
matrixB2D[i][counter] = matrixB[counter];
counter++;
}
printf("\n");
}
}