1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2013
Posts
1
Rep Power
0

How to add error message using if else

/* Two-dimensional Matrix Operation Program */

#include <stdio.h>
#define MAXRow 20
#define MAXColumn 20

void readMatrix(int x[] [MAXColumn], int, int);

void calculateAddition(int a[] [MAXColumn], int b[][MAXColumn],
int c[][MAXColumn], int, int);

void calculatesubtraction(int a[] [MAXColumn], int b[][MAXColumn],
int c[][MAXColumn], int, int);

void printMatrix(int x[][MAXColumn], int, int);

void main() {
int row, column;
int x;
int a [MAXRow] [MAXColumn], b [MAXRow] [MAXColumn],
c [MAXRow] [MAXColumn];
printf("Program to calculate addition and subtraction of matrices\n\n");
printf("--------------------------------------------------------------\n\n");

printf("Input number of rows\n\n");

scanf("%d", &row);

printf("Input number of columns\n\n");
scanf("%d", &column);

{

printf("\n\nInput Matrix A\n");

printf("\n\nInput Matrix B\n");

}

void calculateAddition(a, b, c, row, column);
printf("Addition of A and B is\n");
printMatrix(c, row, column);

void calculatesubtraction(a, b, c, row, column);
printf("Subtraction of A and B is\n");
printMatrix(c, row, column);

}

void readMatrix(int x[][MAXColumn], int row, int column){
int i,j;
for (i = 0; i < row; i++) {
printf ("Enter data for row %d \n", i+1);
for (j = 0; j < column; j++)
scanf("%d", &x[i][j]);
}
}

int c[][MAXColumn], int row, int column) {
int i,j;
for (i = 0; i < row; i++) {
for (j = 0; j < column; j++)
c[i][j] = a[i][j] + b[i][j];
}
}

void calculatesubtraction(int a[][MAXColumn], int b[][MAXColumn],
int c[][MAXColumn], int row, int column) {
int i,j;
for (i = 0; i < row; i++) {
for (j = 0; j < column; j++)
c[i][j] = a[i][j] - b[i][j];
}
}

void printMatrix(int x[][MAXColumn], int row, int column) {
int i, j;
for (i = 0; i < row; i++) {
for (j = 0; j < column; j++)
printf("%4d", x[i][j]);
printf("\n");
}

getch();
return 0;
}
2. As a newcomer, you need to be aware that the forum software does not by default retain indentation. Therefore, you need to put code samples in between [code] tags, like so:

[code]
code goes here.
[/code]

You can do this automatically with the '#' button at the top of the editing window, or the highlight marker button right next to it.

Now, as it happens, I checked your code and found that it wasn't indented in the first place, which leads me to the next point: you absolutely must indent your code for it to be readable! While the C language does not enforce this, it really is crucial to adopt some sort of indent style; otherwise, trying to match the nesting of blocks becomes a nightmare. Readability is nearly important in modern programs as correctness: there's little point in a working program that cannot be understood when it comes to time update it.

I've also fixed a number of issues with the code:
• I changed the main() function's return type from void to int. While some non-standard compilers allow void main(), strictly speaking only int main() is correct and portable.
• on a related note, you have a return 0; at the end of the function printMatrix(), which is declared as void. I am pretty sure that this was originally meant to be in your main() function.
• You have a pair of braces around a section of code in the main() function, with no loop or conditional header. While this is not wrong, it doesn't accomplish anything.
• in two function calls, you have the keyword void right before the function call:
Code:
void calculateAddition(a, b, c, row, column);
printf("Addition of A and B is\n");
printMatrix(c, row, column);

void calculatesubtraction(a, b, c, row, column);
printf("Subtraction of A and B is\n");
printMatrix(c, row, column);
This will cause the compiler to interpret these as function prototypes, and should have given a redeclaration error.
• You have a call to a function getch(), which is a non-standard function that was part of the <conio.h> header from Turbo C. Modern compilers and their libraries do not have any such function, and even if they did, you would have needed to #include <conio.h> for it to be visible to the program.
• Most crucial of all, you don't explain what the problem you were trying to solve was! We're not mind-readers on this forum; we have no way of knowing just what error checking you want to add to the program, or where. Please give detailed information about what you need help with in the future.

Below, I have copied your code with suitable indentation and corrections, inside of the [highlight] tags for proper readability (to get the code without line numebrs, click on the double-chevron button at the upper right corner of the highlighting window). HTH.
C Code:
/* Two-dimensional Matrix Operation Program */

#include <stdio.h>
#define MAXRow 20
#define MAXColumn 20

void readMatrix(int x[] [MAXColumn], int, int);

void calculateAddition(int a[] [MAXColumn], int b[][MAXColumn],
int c[][MAXColumn], int, int);

void calculatesubtraction(int a[] [MAXColumn], int b[][MAXColumn],
int c[][MAXColumn], int, int);

void printMatrix(int x[][MAXColumn], int, int);

int main()
{
int row, column;
int x;
int a [MAXRow] [MAXColumn], b [MAXRow] [MAXColumn],
c [MAXRow] [MAXColumn];
printf("Program to calculate addition and subtraction of matrices\n\n");
printf("--------------------------------------------------------------\n\n");

printf("Input number of rows\n\n");

scanf("%d", &row);

printf("Input number of columns\n\n");
scanf("%d", &column);

printf("\n\nInput Matrix A\n");

printf("\n\nInput Matrix B\n");

printf("Addition of A and B is\n");
printMatrix(c, row, column);

calculatesubtraction(a, b, c, row, column);
printf("Subtraction of A and B is\n");
printMatrix(c, row, column);

return 0;
}

void readMatrix(int x[][MAXColumn], int row, int column)
{
int i,j;
for (i = 0; i < row; i++)
{
printf ("Enter data for row %d \n", i+1);
for (j = 0; j < column; j++)
scanf("%d", &x[i][j]);
}
}

int c[][MAXColumn], int row, int column)
{
int i,j;
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
c[i][j] = a[i][j] + b[i][j];
}
}

void calculatesubtraction(int a[][MAXColumn], int b[][MAXColumn],
int c[][MAXColumn], int row, int column)
{
int i,j;
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
c[i][j] = a[i][j] - b[i][j];
}
}

void printMatrix(int x[][MAXColumn], int row, int column)
{
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
printf("%4d", x[i][j]);
printf("\n");
}
}
Last edited by Schol-R-LEA; June 4th, 2013 at 08:08 AM.