Initialize ANY one dimensional array to zero.

closed account (S2wCM4Gy)
Say I have the following arrays:

int alpha[20];
int beta[20];
int gamma[4];

I know how to write a function to write an array that I have written down but say a new array is needed and I want to immediately initialize it to zero. I may not be understanding the directions properly so here you go.

"Write the definition of the function setZero that initializes ANY one dimensional array of type int to 0."

Is it saying that any new array that I write should automatically be initialized to zero when compiled without modifying the function? Or is it simply asking for me to initialize the existing one dimensional arrays given within the text?

 
closed account (SECMoG1T)

1
2
3
4
5
6
7
 
 void setZero (int array [])
  {
    for (auto i=std::begin (array); i! =std::end(array), i++)
          *i=0;
   
  }


Similar to that
I think you're being asked to provide a function that can be used to initialise any array of type int to zero.

Using the function to initialise the arrays you already have, or the new ones you create, is a second step.

You will still have to write the code that will call your function each time you create an array, and whatever scheme you come up with will still require programmer co-operation - automatic initialisation will require a magical compiler, or at least one you've created/modified yourself (good luck).

However, for an int array, the compiler does help you, but in a way that doesn't answer your original question , for example:
int array[100] = {0};
will create an array of 100 int all initialised to 0.
Last edited on
Topic archived. No new replies allowed.