Array Fill (display array problem)

Hello.

main.cpp

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

using namespace std;

int Random(int low, int high){
	// Random numbers with range e.g. random between 5 and 10.
	int randNumber = rand() % high + low;
	return randNumber;
}

void RandomArrayFill(int* array, int size){
	cout << "Creating array and filling it with random numbers..." << endl;
	for(int i=0; i<size; i++)
		array[i] = Random(0,100);
}
void PrintArray(int* array, int size)
{
	// If the array is of size 0 then call it a null array.
	if(size == 0)
	{
		cout << "NULL Array" << endl;
	}
	else
	{
		// Size not 0 then loop through each element and print it.
		cout << "Array = {";
		for(int i=0; i<size; i++){
			for(int j=1;j<size; j++)
				cout << ",";
			cout << array[i];
		}
		cout << "}" << endl;
	}
}


int main(){

	srand((unsigned) time(0));

	cout << "Enter the size of an array to create: ";
	int size;
	cin >> size;

	// Allocating memory
	int* array = new int[size];

	RandomArrayFill(array, size);
	PrintArray(array, size);

	// Deleting memory
	delete [] array;
	return 0;
}

Output

Enter the size of an array to create: 5
Creating array and filling it with random numbers...
Array = {,,,,96,,,,58,,,,9,,,,19,,,,6}
Press any key to continue . . .



What I want is to display the array as follows :

Array = {96,58,9,19,6}


I would like some help on this one.Not a smart guy and understanding loops is fairly complex for me :)
Thank you.
Step through your code and you should see the problem. When you enter your for loop, you immediately enter the next for loop; there is no "delay" which you seem to expect.
@kid :

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

using namespace std;

int Random(int low, int high){
	// Random numbers with range e.g. random between 5 and 10.
	int randNumber = rand() % high + low;
	return randNumber;
}

void RandomArrayFill(int* array, int size){
	cout << "Creating array and filling it with random numbers..." << endl;
	for(int i=0; i<size; i++)
		array[i] = Random(0,100);
}
void PrintArray(int* array, int size)
{
	// If the array is of size 0 then call it a null array.
	if(size == 0)
	{
		cout << "NULL Array" << endl;
	}
	else
	{
		// Size not 0 then loop through each element and print it.
		cout << "Array = {";
		for(int i=0; i<size; i++)
		{
		//	for(int j=1;j<size; j++)
			//	cout << ",";
			cout << array[i]<< " , ";
		}
		cout << "}" << endl;
	}
}


int main(){

	srand((unsigned) time(0));

	cout << "Enter the size of an array to create: ";
	int size;
	cin >> size;

	// Allocating memory
	int* array = new int[size];

	RandomArrayFill(array, size);
	PrintArray(array, size);

	// Deleting memory
	delete[] array;
	return 0;
}
That's what i tried. Thank for your help. But if I would like to display it exactly like this :
Array = {96,58,9,19,6}
and not like this
Array = {96,58,9,19,6,}
what should I do ?
This example
1
2
3
4
5
for(int i=0; i<size; i++)
{
	cout << array[i]<< " , ";
}
cout << "}" << endl;

it ends with the coma, and I do not want that.
For every number that it prints i want to be followed by a coma but to stop before the last number. I can't figure it out.
Last edited on
@kid : it is here !

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

using namespace std;

int Random(int low, int high){
	// Random numbers with range e.g. random between 5 and 10.
	int randNumber = rand() % high + low;
	return randNumber;
}

void RandomArrayFill(int* array, int size){
	cout << "Creating array and filling it with random numbers..." << endl;
	for(int i=0; i<size; i++)
		array[i] = Random(0,100);
}
void PrintArray(int* array, int size)
{
	// If the array is of size 0 then call it a null array.
	if(size == 0)
	{
		cout << "NULL Array" << endl;
	}
	else
	{
		// Size not 0 then loop through each element and print it.
		cout << "Array = {";
		for(int i=0; i<size-1; i++)
		{

			cout << array[i]<< " , ";
		}
		cout<< array[size-1] ;
		cout << "}" << endl;
	}
}


int main(){

	srand((unsigned) time(0));

	cout << "Enter the size of an array to create: ";
	int size;
	cin >> size;

	// Allocating memory
	int* array = new int[size];

	RandomArrayFill(array, size);
	PrintArray(array, size);

	// Deleting memory
	delete [] array;
	system ("pause");
	return 0;
}
Thank you for helping.This is the new output :

Array = {96,58,9,19,6,6}

This line cout<< array[size-1] ; will just fill the Array with the last number.
I am thinking at this one for more than 3 hours...
Topic archived. No new replies allowed.