Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,988 questions

51,933 answers

573 users

How to zero a 2D array in C++

1 Answer

0 votes
#include <iostream>
#include <cstring>

#define ROWS 2
#define COLS 3
   
void print_array2d(int array2D[][COLS], int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%2i", array2D[i][j]);
        }
        printf("\n");
    }
}
    
int main() {
    int array2D[ROWS][COLS] = {{5, 3, 8}, {9, 2, 4}};
  
    print_array2d(array2D, ROWS, COLS);
    
    std::memset(array2D, 0, sizeof(array2D[0][0]) * ROWS * COLS);
    
    print_array2d(array2D, ROWS, COLS);
}
    
   
   
    
/*
run:
     
 5 3 8
 9 2 4
 0 0 0
 0 0 0
   
*/

 



answered Jan 10, 2025 by avibootz
...