Display array

Hi ! I need help , I should create an array and the user will enter the values , and display the array and it's values in another function , without using pointers .
so I need help , i don't know what should I write in "displayArray" function

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
using namespace std;

char minu();
void readArray(int a[], int size);
void displayArray(int a[], int size);

int main()
{
	int a[6], size = 0;
	char op;

	cout << "Welcome to the Array Handler!\n\n";
	cout << "Important Note: Maximum array size is 6!!\n\n";
	op = minu();
	while(op != 'q')
	{
		switch (op)
		{
			case '1': readArray(a, size);
				break;

			case '2': displayArray(a, size);
				break;
		}


			op = minu();
	}

	return 0;

}

char minu()
{
	char op;
	cout << "1. Read array" << endl << "2. Display array" << endl; 

	cout << "Enter operation number or 'q' to quit: " ;
	cin >> op;
	return op ;
}

void readArray(int a[], int size)
{
	int i;
	cout << "Enter array size: ";
	cin >> size;
	if (size <=6)
		for (i=0 ;i<size ; i++)
			{
				cout << endl << "a[" << i << "] = ";
				cin >> a[i];
			}
	else
		cout << "sorry ! too big for me" << endl;
}


void displayArray(int a[], int &size)
{


}
The function can look for example the following way

1
2
3
4
void displayArray( const int a[], int size )
{
   for ( int i = 0; i < size; i++ ) std::cout << a[i] << ' ';
}
Last edited on
Last edited on
Pretty sure you need to pass size as reference to readArray. Very sure that it should be const int size for displayArray.

@line 50, you can have the array size 6 (< not <=).

You print it in basically the same way that you read it:
1
2
3
4
5
6
7
8
9
if (size > 6)
{
  cout << "Hey, you entered a large number and I didn't fix it yet!\n";
  return;
}

for (int i = 0; i < size; i++)
  cout << a[i] << " ";
cout << '\n';
now I have no errors, but i can't get an output for case 2
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
using namespace std;

char minu();
void readArray(int a[], int size);
void displayArray(const int a[], int &size);

int main()
{
	int a[6], size = 0;
	char op;

	cout << "Welcome to the Array Handler!\n\n";
	cout << "Important Note: Maximum array size is 6!!\n\n";
	op = minu();
	while(op != 'q')
	{
		switch (op)
		{
			case '1': readArray(a, size);
				break;

			case '2': displayArray(a, size);
				break;
		}


			op = minu();
	}

	
	return 0;

}

char minu()
{
	char op;
	cout << "1. Read array" << endl << "2. Display array" << endl ;
	cout << "Enter operation number or 'q' to quit: " ;
	cin >> op;
	return op ;
}

void readArray(int a[], int size)
{
	int i;
	cout << "Enter array size: ";
	cin >> size;
	if (size <=6)
		for (i=0 ;i<size ; i++)
			{
				cout << endl << "a[" << i << "] = ";
				cin >> a[i];
			}
	else
		cout << "sorry ! too big for me" << endl;
}

void displayArray( const int a[], int &size )
{
   for ( int i = 0; i < size; i++ )
	   cout << a[i] << ' ';
}
Why did you declare the second parameter of function displayArray as a reference?!

I showed you already how the function is defined. Are you unable to copy and paste the code I showed?

One more for very advanced programmers:

1
2
3
4
void displayArray( const int a[], int size )
{
   for ( int i = 0; i < size; i++ ) std::cout << a[i] << ' ';
}


Further if you defined the array as

int a[6];

then you shall use its size.


So instead of the definition

int a[6], size = 0;

shall be

1
2
	const int SIZE = 6;
	int a[SIZE];


And function readArray shall not ask about the array size. It is fixed and equal to 6. So the function shall ask to enter exactly 6 that is SIZE elements of the array.
Otherwise if the size of the array is not known then you shall allocate it in the heap and use the pointer to its first element.
Last edited on
I'm sorry but I used your way and it didn't work , i can't have an output if i choose '2'
and about the size it could be 6 or less , it's not fixed at 6
It is not the problem of the function I showed. It is your problem that you even can not read what other wrote.
Please reread one more my previous message. Function readArray shall be rewritten as I pointed out.

Last edited on
1
2
3
4
5
6
7
8
9
10
void readArray( int a[], int size )
{
	cout << "Enter " << size << " elements of the array:\n ";

	for ( int i = 0 ;i < size ; i++ )
	{
		cout << "a[" << i << "] = ";
		cin >> a[i];
	}
}
Last edited on
nothing wrong about the main function , it's in one of these functions , i did it as you told me i really dont know where is the problem -_- !

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
void readArray(int a[], int size)
{
	int i;
	cout << "Enter array size: ";
	cin >> size;
	if (size <6)
		for (i=0 ;i<size ; i++)
			{
				cout << endl << "a[" << i << "] = ";
				cin >> a[i];
			}
	else
		cout << "sorry ! too big for me" << endl;


}

void displayArray( const int a[], int size )
{
	int i;
 
for ( i = 0; i < size; i++)
cout << a[i] << ' ';

}
The problem is that you enter new size of the array but in main this size is unknown. It is a bad design of the program. I already showed you how function readArray can look.

And why did you declare int i outside the loop?

1
2
3
4
5
6
7
8
void displayArray( const int a[], int size )
{
	int i;
 
for ( i = 0; i < size; i++)
cout << a[i] << ' ';

}


Are you unable simply copy and paste the code I showed?!
Last edited on
well is there any difference if i put it inside the loop or out !!???


anyways Thanks for trying to help =)
Last edited on
Yes there is a big difference. In fact your code strictly speaking is invalid because the scope of the variable exceeds its useful life.
Topic archived. No new replies allowed.