Array Assistance Please

Good Evening,
I am having problems with the code below, what I need to do is have the array show 15 lines of value (alpha[0] through alpha[14], then the next set of 15 and so on, on its own line. When I run the code it loops indefinitely. Any help that can be offered is greatly appreciated, I have put the entire program after the code I need help with.

Verbatim directions: Output the value of the alpha so that 15 components per line are printed.

Thank you in advance,

1
2
3
4
5
for (counter = 0; counter < 50; counter +15)//h
	{
		cout << alpha[counter] << alpha[counter + 1] << alpha[counter + 2] << alpha[counter + 3] << alpha[counter + 4] << alpha[counter + 5] << alpha[counter + 6] << alpha[counter + 7] << alpha[counter + 8] << alpha[counter + 9]
		<< alpha[counter + 10] << alpha[counter + 11] << alpha[counter + 12] << alpha[counter + 13] << alpha[counter + 14] << endl;//h
	}

This is the code in its entirety
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
#include <iostream>

using namespace std;

const int SIZE = 50;
static int alpha[SIZE]; //a
int counter = 0;
int main()
{
	for (counter = 0; counter < SIZE; counter++)//b
	{
		alpha[counter] = -1;//b
	}
		
	cout << alpha[0] << endl;//c
	alpha[24] = 62;//d
	alpha[9] = 3 * alpha[49] + 10;//e

	for (counter = 0; counter < SIZE; counter++)//f
	{
		if (alpha[counter] % 2 == 0 || alpha[counter] % 3 == 0) //f
			cout << alpha[counter] << endl;//f
	}

	cout << alpha[49] << endl;//g

	for (counter = 0; counter < 50; counter +15)//h
	{
		cout << alpha[counter] << alpha[counter + 1] << alpha[counter + 2] << alpha[counter + 3] << alpha[counter + 4] << alpha[counter + 5] << alpha[counter + 6] << alpha[counter + 7] << alpha[counter + 8] << alpha[counter + 9]
		<< alpha[counter + 10] << alpha[counter + 11] << alpha[counter + 12] << alpha[counter + 13] << alpha[counter + 14] << endl;//h
	}
	
	for (counter = 0; counter < SIZE; counter ++)//i
		if (alpha[counter] % 2 == 0)
		{
			alpha[counter] = alpha[counter] + 1;//i
		}

	for (int diffArray[SIZE], counter = 0; counter < SIZE; counter++)//j
	{
		diffArray[counter] = alpha[counter] - alpha[counter - 1];//j
	}
	system("pause");
	return 0;
}
Last edited on
The point of arrays is to not have to repeat yourself 15 times :)

Try replacing lines 27-30 with this, let me know if that helps:
1
2
3
4
5
6
for (int i = 0; i < 50; i++)
{
	cout << alpha[i];
	if (i % 15 == 14)
		cout << '\n'; // New line every 15 elements
}


PS: Avoid global variables like what you have on lines 5-7. Move those declarations into main().

PPS: The reason it loops indefinitely is because your for loop had (counter = 0; counter < 50; counter +15) "counter + 15" doesn't do anything. You'd need to assign it. But this is not the right approach, because you'd still be going out of bounds of your array unless 50 is divisible by 15 (it's not).
Last edited on
Also, careful here
1
2
3
4
	for (int diffArray[SIZE], counter = 0; counter < SIZE; counter++)//j
	{
		diffArray[counter] = alpha[counter] - alpha[counter - 1];//j
	}

counter = 0 at the start, so you're accessing alpha[-1]. You probably want to start at counter = 1.
Last edited on
Thank y'all very much, that solved the problem!
Topic archived. No new replies allowed.