Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4064006
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:47:59+00:00 2026-05-20T15:47:59+00:00

I just found out this code from a tutorial for matrix addition in c++

  • 0

I just found out this code from a tutorial for matrix addition in c++ by reading the values from a file-

I wanted to ask what does #define does here? What is so special in it? And how is it different from separately declaring M and N as int or char in main?

code

#include <iostream>
#include <fstream>
using namespace std;

#define M 4
#define N 5

void matrixSum (int P[M][N], int Q[M][N], int R[M][N]);
void matrixSum (int P[M][N], int Q[M][N], int R[M][N]) {
 for (int i=0; i<M; i++)    // compute C[][]
  for (int j=0; j<N; j++) 
    R[i][j] = P[i][j] + Q[i][j];
}

int main () {

 ifstream f;
 int A[M][N];
 int B[M][N];
 int C[M][N];

    f.open("values");   // open file

    for (int i=0; i<M; i++)     // read A[][]
      for (int j=0; j<N; j++) 
        f >> A[i][j];   

    for (int i=0; i<M; i++)     // read B[][]
      for (int j=0; j<N; j++) 
        f >> B[i][j];

    matrixSum (A,B,C);      // call to function

    for (int i=0; i<M; i++) {   // print C[][]
      for (int j=0; j<N; j++) 
        cout << C[i][j] << " ";
      cout << endl;
    }
    f.close();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T15:47:59+00:00Added an answer on May 20, 2026 at 3:47 pm

    It is a preprocessor directive of the form

    #define identifier token-sequence
    

    The preprocessor runs before the compiler transforms your code for use in the compiler. The order is as follows:

    • Trigraph replacement
    • Line splicing
    • Macro definition and expansion

    So with the #define you can have character manipulation (macro substitution).

    Whenever M is seen 4 will be substituted.

    The compiler will then see

     void matrixSum (int P[4][5], int Q[4][5], int R[4][5]);  // ..etc
    

    The other way would be to use the const qualifier on a global variable.

    In C, it would be

    // Some fileA.c  
    const int M; // initialize
    
    // Some fileB.c
    const int M = 4; // defined
    

    So I would say try to avoid when possible because macros are a form of text substitution, they do not obey scope and type rules.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From this tutorial http://www.brighthub.com/internet/web-development/articles/11010.aspx I found the code below. Is there a way to
Just found this out, so i am answering my own question :) Use a
I just found this in some old code, and I'm not sure what it
I just found out that by converting PNG32 to PNG8 via Photoshop will fix
I just found out that my program is losing 5% execution speed when it
I've just found out about Stack Overflow and I'm just checking if there are
I’ve just found out that the execution plan performance between the following two select
Thanks to a Q&A on stackoverflow. I just found out how to determine the
I have just installed Eclipse 3.4 and found out that there is not a
Well, I'm trying to follow the tutorial from http://rapidandroid.org/wiki/Graphing . But I found a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.