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 here

Execution terminated

At this point, I'm completely stumped. My questions, in short:

1. What does this error mean?

2. More fundamentally, is my code inherently bad? As in, is it a poor way to achieve what I wanted to achieve?

3. Why is coding so hard? :(

Edited by pravtdelhi

2
Contributors
4
Replies
6
Views
5 Years
Discussion Span
Last Post by pravtdelhi

What does this error mean?

It means you must declare something before you use it. Specifically, getMatrix() and displayMatrix() are defined (and declared) after main(), but you use them in main(). Add a prototype to declare each:

``````void getMatrix(int number);
void displayMatrix(int number);

int main(void)
{
``````

More fundamentally, is my code inherently bad? As in, is it a poor way to achieve what I wanted to achieve?

Inherently? No, but it could be improved. However, those are nitpicks that are best left for later.

Why is coding so hard? :(

Programming is difficult. Welcome to reality.

Thank you very much indeed. It works now. :)

It means you must declare something before you use it. Specifically, getMatrix() and displayMatrix() are defined (and declared) after main(), but you use them in main(). Add a prototype to declare each:

Okay, I think I got that.

Is it generally a better practice to define all the functions you're going to use before you define main() ?

Inherently? No, but it could be improved. However, those are nitpicks that are best left for later.

I'm glad it's all right in essentials.
However, I'm looking to learn and improve, and I'd be very grateful for a hint towards some of the more glaring shortcomings.

Programming is difficult. Welcome to reality.

But the reward is in the challenge, after all. No pain, no gain. :)

Is it generally a better practice to define all the functions you're going to use before you define main() ?

It depends on your prefered style, neither is better than the other. However, for larger projects you'll end up moving the declarations to a header anyway, and the definitions to an implementation file. So the choice of defining before or after main() only applies when you're not modularizing those functions.

Right.