Processing Array Data

I need to process an array of integers such that every three consecutive integers above a certain number, say 5 a "+" is displayed.

Ex: (5)

4
6 +
7 +
8 +
4
5
7 +
7 +
7 +
6 +
4
9
3

Admittedly this is for a school project. I did the rest of the work (reading in file data, stepping through the array), I just need a little help. Thanks if you can help.
Ive wrote a little test program that does what you want,

you might need to change 'printf' for 'cout' (not knowing which one that you use)

hope this helps.
Shredded

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
#include	<stdio.h>

int	main(int argc,char** argv)
{
	int	testArray[] = {4,6,7,8,4,5,7,7,7,6,4,9,3};

	int	testValue = 5;
		
	int	numberOfValues = 13;

	bool*	isAbove = new bool[numberOfValues];	

	for (int i=0;i<numberOfValues;i++)
	{
		isAbove[i] = false;
	}

	for (int i=0;i < (numberOfValues - 2);i++)
	{
		if (testArray[i] > testValue &&
			testArray[i+1] > testValue &&
			testArray[i+2] > testValue)
		{
			isAbove[i] = true;
			isAbove[i+1] = true;
			isAbove[i+2] = true;
		}
	}

	printf("\n");

	for (int i=0;i<numberOfValues;i++)
	{
		if (isAbove[i] == true)
		{
			printf("%d +\n",testArray[i]);
		}
		else
		{
			printf("%d\n",testArray[i]);
		}
	}
}
That should just a simple for loop to go through every array index, test for a number greater than 5, and display the number plus the symbol next to it.

1
2
3
4
5
6
7
8
9
10
11
12
13
for (int i = 0; i < sizeof(array) - 1; i++)
{
   if (array[i] > 5 && array[i+1] > 5 && array[i+2] > 5)
   {
      cout << array[i]   << " +" << endl
           << array[i+1] << " +" << endl
           << array[i+2] << " +" << endl;
      i += 2;
   }

   else
      cout << array[i] << endl;
}

this is a pretty simple solution in C++ instead of C. Hope it helps any. It's rather a rather self-explanatory algorithm.
Last edited on
That algorithm is not what the OP requested. It won't work properly for a sequence of 4 numbers greater than 5, for instance. It also tries to access out of bounds values.
Topic archived. No new replies allowed.