Simple Program with Build Errors

Hi everybody,
I'm a beginner trying to write a simple program to transpose a matrix, using CodeBlocks.
I keep getting build errors, such as "expected identifier before numeric constant" and "expected ',' or '---' before numeric constant" for line 5 (#define WIDTH 2).
I have no idea what it means. Yes, I have gone through the tutorials and searched online.
Any help would be greatly appreciated.

Regards, Gerry

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 # include <iostream >
# include <cstdlib>
# include <ctime>
using namespace std
#define WIDTH 2
#define LENGTH 3

void transpose(const int* , int* );
void printMatrix(int*, int , int);

int main()
{

    int OutMatrix[LENGTH][WIDTH];
    int InMatrix[WIDTH][LENGTH];
    std::cout <<" Original matrix " <<"\n";
    printMatrix(*InMatrix,LENGTH,WIDTH);
    transpose(*InMatrix, *OutMatrix);
    std::cout <<" Transposes matrix " <<"\n";
    printMatrix(*OutMatrix,WIDTH,LENGTH);
    return 0;
}
void transpose(const int input[][LENGTH], int output[][WIDTH])
    {
      for (int i=0; i<LENGTH ; ++i)
        {
           for (int j=0; j<WIDTH; ++j)
           {
              output[i][j]= input[j][i];
           }
        }
    }
    
void printMatrix(int *Matrix, WIDTH, LENGTH)
    {
        for (int i=0; i<LENGTH ; ++i)
        {
           for (int j=0; j<WIDTH; ++j)
           {
              std::cout << Matrix[j][i] <<" , ";
           }
           std::cout <<"\n";
        }
    }
You should probably take a look at this

http://www.cplusplus.com/doc/tutorial/preprocessor/

Also to make your life easier and not use preprocessor directives, which may or may not create a whole bunch of bugs depending on how complicated your program is, I would just use const, such that you define your WIDTH and LENGTH as:


1
2
const int WIDTH = 2;
const int LENGTH = 3;


These consts will usually go above your main since you'll want them to be global scope.
A Semicolon is missing at the end of line number 4.

 
using namespace std;
Thanks for your feedback, YFGHNG and akash16
wc bro.
Topic archived. No new replies allowed.