passing arrays & recycling initialized variable

consider this simple code:
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 change_array(int array_1[6])
{
	array_1[4] = 300;

	return;
}

int main()
{

int array_1[6] = {377,11,18,679,4,5};
for(int t(0); t < 6; ++t)
{ cout<<array_1[t]<<"  ";}
cout<<endl;
change_array(array_1);
for(int i(0); i < 6; ++i)
{ cout<<array_1[i]<<"  ";}
	return 0;
}


2 questions. How can I change the values of array1 passed to change_array in a more efficient manner? I tried the initializer list ( array_1 = {blah blah} but of course it didn't work. I was left with changing individual array values.

Second question I failed to learn earlier. If i initialize the counter variable in a for loop can I recycle it and use it again with out initializing. I get an error.

...
 
for (int t(0); t<6; ++t)


is ok

now
 
for (t = 0; t<6;++t)

error
1. What do you mean by a more more efficient? If you want to set the fifth element of the array to 300 what you have is fine.

2. t is only in scope inside the loop. It doesn't exist outside the loop. That's why you can't use t in the second loop without declaring a new variable with that name.
Hi Peter

1. more efficient may not have been the best selection of words. What if I want to set the
fifth through the twentieth elements of the array in the function? (assume the size of array is appropriate)


2.
t is only in scope inside the loop. It doesn't exist outside the loop. That's why you can't use t in the second loop without declaring a new variable with that name.

Thank you, I understand.
Last edited on
What if I want to set the fifth through the twentieth elements of the array in the function?

If you want to set all of them to the same value you can use std::fill.
 
std::fill(array_1 + 4, array_1 + 20, 300);
BUt there is no other less labor intensive way such as an initializer list to load different values fifth through the 20th e.g.? (short of a script to load data from memory to an array like pointer based)
Last edited on
But there is no other less labor intensive way
Don't sweat it. Peter87's example probably translates into half a dozen instructions that take 30ns to run on a 3GHz processor. That's less time than it takes for the light bouncing off the car in front of you to reach your eye.
If you use a std::array instead of a normal array you can assign the array like you can with other types

 
array_1 = {1, 2, 3}; 

but this will modify all values in the array.

To only set some of the values you could create a temp array and copy the elements using std::copy.

1
2
int tmp[] = {2, 3, 5, 7, 11};
std::copy(std::begin(tmp), std::end(tmp), array + 4);

If you find this too wordy you could always wrap it in a function.
I'm sorry that I can't get my head around this. Thank you for your time. I can do this, no prob:

void change_array(int array_1[16])
{
array_1[4] = 300;

return;
}

But what if I wanted to load values (e.g.16 elements ) into this array? I don't think it will take an initializer list in the fx? Or do I do every one by hand?
I get it. The temp array will do it.
Topic archived. No new replies allowed.