array math

I'm trying to make this function add 4 to the array until the array hits the flag then the adding stops. I'm so close, yet so far..

I have tried so many different ways. What am i doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;  

void add_4(int array[], int flag = 12)
	{
		int i = 0;
		while (array[i] < flag)
		{	
		cout <<array[i] + 4 <<endl;
		array[i]++;
		}
	}
	int main ()
	{			
		int values []	= {1,2,3,4,5,6,7,8,9,10,11,12};
		add_4 (values);
		//for (int i=0; i<12; ++i)

		cout << values << endl;

		system ("pause");
		return 0;
	
	}
Last edited on
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
#include <iostream>
using namespace std;  

void add_4(int array[], int aFlag)
	{
		int i = 0;
		while (i < aFlag)
		{	
		 array[i] += 4;
		 cout << array[i] << endl;
		i++;
		}
	}
	int main ()
	{			
		int values []	= {1,2,3,4,5,6,7,8,9,10,11,12};
		add_4 (values, 12);
		
		
		system ("pause");
		return 0;
	
	}
Last edited on
Thanks kemort.

Why won't this stop at 12?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;  

void add_4(int array[], int flag)
	{
	        flag = 12;
		int i = 0;
		while (i < flag)
		{	
		 array[i] += 4;
		 cout << array[i] << endl;
		i++;
		}
	}
	int main ()
	{			
		int values []	= {1,2,3,4,5,6,7,8,9,10,11,12};
		add_4 (values, 12);
		
		
		system ("pause");
		return 0;
	
	}
closed account (48T7M4Gy)
line 8: while(i <12) looks like it stops when i gets to 11, I'd say. But don't forget since it starts at array[0] 12 elements are printed out.

If you find the parameter passing confusing you could change the names to make it clear

1
2
3
4
5
6
7
8
9
10
11
void add_4(int array[], int aFlag)
	{
	       aFlag = 12; // this is questionable, why have it??
		int i = 0;
		while (i < aFlag)
		{	
		 array[i] += 4;
		 cout << array[i] << endl;
		i++;
		}
	}

:)
Last edited on
I am missing something...

I want this function to add 4 to the array until the array hits the flag then stop adding.

The output should look like this:

5
6
7
8
9
10
11

then stop

Ah, now i see what this function is doing.
I think I need to add another if statement.

I'm not sure how to implement it but it would be something like

if (array[i] = flag-1)
break;

I'm still trying to get the logic in my head..



closed account (48T7M4Gy)
I'm still trying ... my head

That's the spirit!
Cheers :)
Topic archived. No new replies allowed.