One dimensional arrays

I'm confused on constant arrays. const array[5]={0} what is the purpose of having a constants when that array would be changed later on. Can someone give an exmaple where I would have to use a constant. For instance, I want to enter in the scores of 5 students, I will use an array of 5 elements. I know I won't use const on this matter, but where would I use a const.

Second question. My book did not provide me an ample text regarding two arrays I as parameters. So I have this void printarray (int list []; int lists []; int size list, int size y]

For the second question, I just need an example of two parameters being used as arrays
fahmankhan75 wrote:
I'm confused on constant arrays. const array[5]={0} what is the purpose of having a constants when that array would be changed later on.

Then don't use const arrays where you need to change the values. const arrays maybe used where you're sure you won't change them during program run.

fahmankhan75 wrote:
Second question. My book did not provide me an ample text regarding two arrays I as parameters.
1
2
3
4
5
void printArrays(int rollArr[], char gradeArr[], int size)
{
   for(int I = 0; I < size; I++)
      cout << rollArr[I] << "\t" << gradeArr[I] << endl;
}
First, forgive my low English skill, please.


If you want to improve the legibility of your code, you can use reserved word like "#define" or "const". And few programmers like more "const" than "#define".

example:
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 <conio.h>
#include <iostream>
using namespace std;


//#define LEFT 75
//#define RIGHT 77
//#define UP 72
//#define DOWN 80

const int LEFT = 75;
const int RIGHT = 77;
const int UP = 72;
const int DOWN = 80;

int main()
{
	int c;

	while(1)
	{
		c = getch();
		if(c == 224)
		{
			c = getch();
			switch(c)
			{
			case LEFT:
				cout << "left" << endl;
				break;
			case RIGHT:
				cout << "right" << endl;
				break;
			case UP:
				cout << "up" << endl;
				break;
			case DOWN:
				cout << "down" << endl;
				break;
			}
		}
	}

	return 0;
}



Or otherwise const used in this case.
1
2
3
4
5
6
7
8
9
10
class AAA
{
	int a[100000];
	int b[50000];
	int c[300000];
};

void Function(AAA ob)
{
}


1) AAA is very big object.
2) And function parameter is operating by copy.
3) You only want to read a AAA's data.
3) But 1) and 2), create a very big another AAA data.
4) So you decide to change parameter type.

1
2
3
void Function(AAA& ob)
{
}


5) But this code is, If you change ob's data in Function, together changed the original AAA's data(not in Function's).

6) Maybe you think "Why so play the foolish action? I will never change ob.". But If your program is very big, you can forget that. Or otherwise If you work other programmers, they can't read your think.

So,
1
2
3
void Function(const AAA& ob)
{
}


7) use const.

In addition, const is to the commonly used.
Last edited on
thanks guys!!!
Topic archived. No new replies allowed.