2D array procession

I have written 8x8 matrix with randomly generated numbers in a range[-30..45]
but I don`t know how to get a diagonal matrix with the same range of numbers and all other elements turning into 0. Also, can anybody show me how to transform this array into a vector?
Thank you, I appreciate it.
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>
#include<stdlib.h>
#include<time.h>
using namespace std;

int main()
{
   int array[8][8];
   srand(time(NULL));


   for(int i=0;i<8;i++)
   {
      for(int j=0;j<8;j++)
      {
      array[i][j]=rand()%76-30;
      cout<<array[i][j]<<"\t";
      }
      cout<<endl;
   }
   return 0;
}
closed account (j3Rz8vqX)
Header:
#include<vector>

Standard constructor:
std::vector<int> myIntegerVector;
http://www.cplusplus.com/reference/vector/vector/vector/

Look for the modifiers in the below:
http://www.cplusplus.com/reference/vector/vector/
Should be: assign, push_back, pop_back, insert, erase, swap, clear, and two others that are restricted to c++11.

What you'd probably want would be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    //Construct a vector of vectors, type int; named my2dVector.
    //Construct it with a default size of 8, each containing a vector size 8 of 0's.
    std::vector<std::vector<int>> my2dVector(8,std::vector<int>(8,0));
    //Arguments(First,Second):: First is the number of elements to be included, Second is the value to be included for each element.

    //Draw the vector with std::cout
    for(int i=0;i<8;i++)
    {
        for(int j=0;j<8;j++)
        {
            cout<<my2dVector[i][j];
        }
        cout<<endl;
    }

Edit: Added comments.
Last edited on
Topic archived. No new replies allowed.