Maybe any Easy question for you

First of all, I don'y know POINTERS and OTHER HIGH LEVEL PROGRAMMING cuz I study C++ in my school.
Secondly, I use Borland C++ Version 3.0

I am trying to create a code that inputs an array size, then inputs the array then print it s corresponding pattern. Example

If we input size 3, then we will input array like this:
1
2
3

then it will print pattern like this:
123
120
100

The Only problem is that my code is giving error that constant expression required in the function that is outside of main(). Please tell the direct solution, I have to submit my project tomorrow and its already night here in India. I have done this code on my own and I'm not asking to rebuild the whole code, just tell me how to make it constant by a method which is not so advanced. Please help.
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
  #include<iostream.h>
#include<conio.h>
void pat(int n)
{
	int i,j,x[n],mat[n][n],c=n;
	cout<<"\nEnter Array: ";
	for(i=0;i<n;i++)
	{
		cin>>x[i];
	}
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			mat[i][j]=0;
		}
	}
	for(i=0;i<n;i++)
	{
		for(j=0;j<(c-i);j++)
		{
			mat[i][j]=x[j];
		}
	}
	cout<<"\n\nThe Required Pattern: ";
	for(i=0;i<n;i++)
	{
		cout<<"\n\n";
		for(j=0;j<n;j++)
		{
			cout<<mat[i][j];
		}
	}
}
void main()
{
	clrscr();
	int z;
	cout<<"\nEnter Size of Array: ";
	cin>>z;
	const int n=z;
	pat(n);
	getch();
}
The Only problem is that my code is giving error that constant expression required in the function that is outside of main().

Well you're probably going to need to learn the following:
First of all, I don'y know POINTERS and OTHER HIGH LEVEL PROGRAMMING

In C++ array sizes must be compile time constants, for example:

1
2
3
4
5
6
7
int intArray[10];  // Okay using a constant (10).

const int ArraySize = 100;
int anotherIntArray[ArraySize]; // Okay using a const qualified value.

int someValue = 11;
int aBadSize[someValue]; // Error: Not a compile time constant. 


The other option is to use dynamic memory new/delete, which means pointers.


And you really need to find a newer compiler, that compiler was probably obsolete before you were born. Download a modern IDE/compiler, like Visual Studio express, or Code::Blocks with the most recent version of gcc.
The Only problem is that my code is giving error that constant expression required in the function that is outside of main(). Please tell the direct solution

The direct solution is to use a constant expression, as your compiler has already told you.


I have done this code on my own and I'm not asking to rebuild the whole code, just tell me how to make it constant by a method which is not so advanced.

What you may do is just make the array(s) large enough that it will serve for any value entered by the user (and perhaps, then, informing the user when the number entered is not appropriate would be a good addition to the code.)
If you delete line 37 and line 43, the program works. what's the problem? could be clearer?
also line 41 is not needed.
1
2
//const int n=z;
	pat(z);
ar2007 wrote:
If you delete line 37 and line 43, the program works.

Your compiler has an extension which allows the use of variable length arrays in C++. Unfortunately, using the extension means the compiler accepts non-conforming programs like this one without complaint. The use of the -pedantic option is recommended for compiling C++ with g++ if you wish to write standard compliant code.
Topic archived. No new replies allowed.