-3

What is the fastest way to set a 2-dim array of double,such as double x[N][N] all to -1?

I tried to use memset, but failed. Any good idea?

r.vengadesh
  • 1,721
  • 3
  • 20
  • 36
HanXu
  • 5,507
  • 6
  • 52
  • 76

7 Answers7

2

Use: std::fill_n from algorithm

std::fill_n(*array, sizeof(array) / sizeof (**array), -1 );

Example:

double array[10][10];
std::fill_n( *array, sizeof(array) / sizeof (**array), -1.0 );

//Display Matrix
for(auto i=0;i<10;i++)
{
    for(auto j=0;j<10;j++)
        cout<<array[i][j]<< " ";    
    cout<<endl;
}
Christian Rau
  • 45,360
  • 10
  • 108
  • 185
P0W
  • 46,614
  • 9
  • 72
  • 119
  • @ShafikYaghmour all the explanation is contained in the linked reference. – rubenvb Jul 29 '13 at 12:09
  • @ShafikYaghmour fastest depends on the implementation, data size and phase of the moon. Only a benchmark can tell. Performance doesn't matter until a profiler says otherwise. – rubenvb Jul 29 '13 at 12:14
  • 1
    @ShafikYaghmour: `memset` doesn't perform the operation in question; since this solution does, it is obviously superior. “Fastest” doesn’t enter into it. – Stephen Canon Jul 29 '13 at 12:17
  • @ShafikYaghmour Since `memset` takes infinite time to solve the problem while this approach doesn't, it is obviously faster than `memset`. – Christian Rau Jul 29 '13 at 12:21
  • @StephenCanon Got it, I misread the answer to a previous thread, rereading that thread I see what you are saying. – Shafik Yaghmour Jul 29 '13 at 12:32
1

Also you can set directly

    double x[4][4] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1} 

if the array index is small.

undur_gongor
  • 15,657
  • 5
  • 63
  • 75
Raju
  • 1,149
  • 1
  • 6
  • 19
  • Look, what if I have array[1000][1000]? It's not a good answer. – ST3 Jul 29 '13 at 12:31
  • 1
    It is even worse. It set the first element to -1 and the rest to 0. – ST3 Jul 29 '13 at 12:40
  • pls go to this link [linkd]http://stackoverflow.com/questions/1065774/c-c-initialization-of-a-normal-array-with-one-default-value[link] – Raju Jul 29 '13 at 12:54
  • It just confirmed that your answer is bad. – ST3 Jul 29 '13 at 12:56
  • hey am just telling you to refer to find a solution, not to prove my answer is optimized.... – Raju Jul 29 '13 at 13:31
  • I know what your answer is wrong and I know how to do that stuff... – ST3 Jul 29 '13 at 14:22
  • And I stated, what your answer is wrong because someone, who is just beginner in programming may think, that it is good and useful, so I help em not to waste time. – ST3 Jul 29 '13 at 14:23
1

A simple loop:

#include <stdio.h>

int main(void)
{
    #define N 5
    double x[N][N];
    size_t i, n = sizeof(x) / sizeof(double);

    for (i = 0; i < n; i++)
        x[0][i] = -1.0;
    for (i = 0; i < n; i++)
        printf("%zu) %f\n", i, x[0][i]);
}
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
1
// create constants
const int rows = 10;
const int columns = 10;

// declare a 2D array
double myArray [rows][columns];

// run a double loop to fill up the array
for (int i = 0; i < rows; i++) 
    for (int k = 0; k < columns; k++)
        myArray[rows][columns] = -1.0;

// print out the results
for (int i = 0; i < rows; i++) {
    for (int k = 0; k < columns; k++)
        cout << myArray[rows][columns];

    cout << endl;
}
Oleksiy
  • 37,477
  • 22
  • 74
  • 122
0

Using std::array and its fill method:

#include <array>
#include <iostream>

int main()
{
  const std::size_t N=4
  std::array<double, N*N> arr; // better to keep the memory 1D and access 2D!
  arr.fill(-1.);
  for(auto element : arr)
    std::cout << element << '\n';
}
rubenvb
  • 74,642
  • 33
  • 187
  • 332
0

Using C++ containers you can use the fill method

array<array<double, 1024>, 1024> matrix;

matrix.fill(-1.0);

if, for some reason, you have to stick with C-style arrays you can initialize the first row manually and then memcpy to the other rows. This works regardless if you have defined it as static array or allocated row by row.

const int rows = 1024;
const int cols = 1024;
double matrix[rows][cols]

for ( int i=0; i<cols; ++i)
{ 
    matrix[0][cols] = -1.0;
}
for ( int r=1; r<rows; ++r)
{
    // use the previous row as source to have it cache friendly for large matrices
    memcpy(&(void*)(matrix[row][0]), &(void*)(matrix[row-1][0]), cols*sizeof(double));
}

But I rather would try to move from C style arrays to the C++ containers than doing that kind of stunt.

ogni42
  • 1,232
  • 7
  • 11
0

memset shouldn't be used here because it is based on void *. So all bytes in are the same. (float) -1 is 0xbf800000 (double 0xbff0000000000000) so not all bytes are the same...

I would use manual filling:

const int m = 1024;
const int n = 1024;
double arr[m][n];
for (size_t i = 0; i < m*n; i++)
    arr[i] = -1;

Matrix is like array in memory, so better to have 1 loop, it slightly faster.

Or you can use this: std::fill_n(arr, m*n, -1);

Not sure which one is faster, but both looks similar. So probably you'll need to make small test to find it out, but as far as I know people usually use one or another. And another thing first one is more C on some compiler it won't work and second is real C++ it and never works on C. So you should choose by the programming language I think :)

ST3
  • 8,826
  • 3
  • 68
  • 92