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,950 questions

51,892 answers

573 users

How to declare dynamic 1D array and use it as 2D array using new in C++

1 Answer

0 votes
#include <iostream>
#include <iomanip>

constexpr int ROW = 3;
constexpr int COL = 4;

int main()
{
    int *matrix = new int[ROW * COL];

    srand(time(NULL));
    for(int i = 0; i < ROW; ++i) {
        for(int j = 0; j < COL; ++j) {
            matrix[j * ROW + i] = rand() % 100;
            std::cout << std::setw(2) << matrix[j * ROW + i] << " ";
        }
        std::cout << "\n";
    }

    delete [] matrix;
 
    return 0;
}
     
     
     
     
/*
run:
   
24 44 18 32 
56 50  1 86 
15 69 84 34
      
*/

 



answered May 12, 2021 by avibootz

Related questions

1 answer 124 views
1 answer 120 views
2 answers 194 views
1 answer 156 views
156 views asked Jan 8, 2017 by avibootz
1 answer 128 views
128 views asked Jan 8, 2017 by avibootz
1 answer 80 views
80 views asked Dec 3, 2024 by avibootz
...