Hello, everyone.
I am a first year IT Engineering student as well as a complete newbie to C (and programming in general).
In my introductory course, I was given an assignment, which involves creating a program to get the elements of two 3x3 matrices from the user and display them on the screen. The idea was to make us feel a bit more at ease with two-dimensional arrays.
This was, of course, fairly straightforward - even for someone as green as me. Therefore, I decided to go a bit further than merely what was asked. I extended my program a bit so that it asked the user for the size of the two matrices and then displayed one of them as per the user's preference (If the user enters '1' at the appropriate prompt, it should display the first matrix that was entered, but if he enters '2', it should display the other one).
To accomplish this, (and in the spirit of the original assignment) I decided to use a three-dimensional array like this one: int matrix[matrixNumber][maxRows][maxColumns]
.
The 'matrixNumber' part defines which matrix we're talking about, so that if I wrote something like int a=matrix[0][2][3]
, 'a' would be assigned the value of the element which lies in the second row and third column of the first matrix.
With this basic idea in mind, and having done some internet research on how to go about it, I proceeded to write my code, as follows:
#include<stdio.h>
#define MATRICES 2 /* Defining number of Matrices */
#define MAX_ROWS 10 /* Defining maximum possible rows */
#define MAX_COLUMNS 10 /* Defining maximum possible columns */
int matrix[MATRICES][MAX_ROWS][MAX_COLUMNS]={0}; /* Initializing the 'matrix' array */
int rows = 0;
int columns = 0;
int main(void)
{
int matrixNum; /* Variable to identify the specific matrix to be obtained from the user */
char RunProgram = 'y'; /* Stores the user's choice on rerunning the program */
while(RunProgram == 'y') /*Repeat the loop as long as the user doesn't tell you to stop*/
{
for(matrixNum = 0; matrixNum < MATRICES; matrixNum++)
getMatrix(matrixNum); /* Run the getMatrix function for each matrix */
matrixNum = 0;
printf("Enter Matrix to display(1 or 2): ");
scanf("%d", &matrixNum);
displayMatrix(matrixNum - 1); /* Displays the specified matrix */
fflush(stdin);
printf("\nRun again? (y/n)"); /*Prompts to run again*/
scanf("%c", &RunProgram);
}
return 0;
}
void getMatrix(int number)
{
printf("Taking input for Matrix %d: \n", number + 1);
printf("Enter number of rows needed: ");
scanf("%d", &rows);
printf("Enter number of columns needed: ");
scanf("%d", &columns);
int RowCounter = 0;
int ColumnCounter = 0;
for(RowCounter = 0; RowCounter < rows; RowCounter++)
for(ColumnCounter = 0; ColumnCounter < columns; ColumnCounter++)
{
printf("Enter Element %d x %d: ", (RowCounter + 1), (ColumnCounter + 1));
scanf("%d", &matrix[number][RowCounter][ColumnCounter]);
}
printf("\n");
}
void displayMatrix(int number)
{
int RowCounter;
int ColumnCounter;
printf("\n");
for(RowCounter = 0; RowCounter < rows; RowCounter++)
{
for(ColumnCounter = 0; ColumnCounter < columns; ColumnCounter++)
printf("%d\t", matrix[number][RowCounter][ColumnCounter]);
printf("\n");
}
printf("\n");
}
Unfortunately, this did not work, and my compiler (dev-c++) log shows the following cryptic errors:
matrix2.c:19: error: previous implicit declaration of 'getMatrix' was here
matrix2.c:61: error: conflicting types for 'displayMatrix'
matrix2.c:26: error: previous implicit declaration of 'displayMatrix' was hereExecution terminated
At this point, I'm completely stumped. My questions, in short:
What does this error mean?
More fundamentally, is my code inherently bad? As in, is it a poor way to achieve what I wanted to achieve?
Why is coding so hard? :(