Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |
 User Name: Password: Remember me

New Free Tools on Dev Shed!
We're Excited to announce that Dev Shed now has 70 free tools on the site. To learn more, click here!

 Dev Shed Forums Sponsor:
#1
June 4th, 2013, 08:22 AM
 Muax_07
Registered User

Join Date: Jun 2013
Posts: 1
Time spent in forums: 3 m 5 sec
Reputation 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]);
}
}

void calculateAddition(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 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
June 4th, 2013, 09:05 AM
 Schol-R-LEA
Commie Mutant Traitor

Join Date: Jun 2004
Location: Norcross, GA (again)
Posts: 1,798
Time spent in forums: 1 Month 2 Weeks 3 Days 1 h 39 m 19 sec
Reputation Power: 1569
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:
 Original - 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");    readMatrix(a, row, column);     printf("\n\nInput Matrix B\n");    readMatrix(b, row, column);     calculateAddition(a, b, c, row, column);    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]);    }}  void calculateAddition(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 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");    }}
```
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
#define KINSEY (rand() % 7) λ Scheme is the Red Pill
Scheme in ShortUnderstanding the C/C++ Preprocessor
Taming PythonA Highly Opinionated Review of Programming Languages for the Novice, v1.1

FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. \$4500 obo. tverres@et.ins.gov

Last edited by Schol-R-LEA : June 4th, 2013 at 09:08 AM.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > How to add error message using if else

## Developer Shed Advertisers and Affiliates

 Thread Tools Search this Thread Search this Thread: Advanced Search Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off
 View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox Forum Jump Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

 Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |