Some assistance with arrays

I have a very simple program here that prints out the values contained within the array. My text book uses this within a function, so I tried building a simple main function that calls show_the_world, and it should print 1-5 onto the screen. It also talks about setting your array base to a constant type, as to prevent error, and for ease of editing for later use. I just want to get this working for my own understanding, but clearly I am mixing something up. I tried setting size_of_array as a global constant, but obviously that's wrong. Anything to point me in the right direction will be of great assistance.

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
 #include <iostream> 

using namespace std; 
void show_the_world(const int a[], int size_of_a);

const int size_of_a = 5; 

int main()
{ 
	
	show_the_world; 

	
}

void show_the_world(const int a[], int size_of_a)
{
	
	
	cout << "The array contains the following values : \n";
	for (int i = 0; i < size_of_a; i++)
	{
		cout << a[i];
		cout << endl;
	}
	
} 

Last edited on
so where is the const int a[]?

const int a[]; a[i] is not allowed to change.

like this:

#include <iostream>

using namespace std;
void show_the_world();

const int size_of_a = 5;
const int a[]={1,2,3,4,5};
int main()
{
a[0] = 6;// it's wrong, to change const object is error, your should comment it.

show_the_world();

return 0;
}

void show_the_world()
{
cout << "The array contains the following values : \n";
for (int i = 0; i < size_of_a; i++)
{
cout << a[i];
cout << endl;
}
}
I'm not sure I follow. What do you mean by "a[i] is not allowed to change?" And I didn't realize I had to have the indexes declared as constants too. It almost seems a little inconvenient to have a[]={1,2,3,4,5}, when I thought we could have something like a[size_of_array], where I could change the size whenever I need? Or will that just print garbage?
like code int a[size_of_array]; // you should use const number in the [], so please write "#define size_of_array 5" before int a[size_of_array];

And int a[]={1,2,3,4,5}; is right. Complie will count size with first declared and assigned.

The last "void show_the_world(const int a[], int size_of_a);" , you declared const int a[], this arrary a is const, so you can not change a[], any element in a[].
closed account (48T7M4Gy)
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
#include <iostream> 

using namespace std;

void show_the_world( int[], int );

int main()
{ 
    const int size_of_a = 5; 
    int zz[] = { 5, 67, 98, 2, 25 };
    
	show_the_world( zz, size_of_a );
	
	return 0;
}

void show_the_world( int a[], int sz )
{
        int limit = sz;
    
	cout << "The array contains the following values:" << endl;
	for (int i = 0; i < limit; i++)
	    cout << a[i] << endl;
	    
	cout << "Thank you " << endl;
	return;
} 


The array contains the following values:
5
67
98
2
25
Thank you 
 
Exit code: 0 (normal program termination)
Last edited on
Topic archived. No new replies allowed.