Creating a multidimensional array in an infinite loop???

Dear Experts,
from the example given below, I would like to Generate a matrix who stores a generated array for each iteration. I have an understanding of inputting single elements to a matrix but how can I input an array to a matrix. let's say I would like to input and array of 4 elements that should be stored as a single row of the generated matrix and do this for an infinite while{true} loop.

Best Regards
Ali

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <unistd.h>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <stdlib.h>
#include <stdio.h>
#include <vector>

void printArray(int arr[], int size) {
	for ( int i = 0; i < size; i++ ) {
		cout << arr[i] << ' ';
	}
	cout << endl;
}

int main(){
	srand(time(NULL));
	int c1= (rand()%10)+4;
	int c2= (rand()%10)+4;
	int c3= (rand()%10)+4;
	int c4= (rand()%10)+4;

	cout <<"Generated Values are:"<<c1 <<","<<c2<<","<<c3<<","<<c4<<endl;

    std::cout <<endl<< "put these value into an array" << std::endl;
    int arr[]{c1,c2,c3,c4};

    int size=sizeof(arr)/sizeof(arr[0]);    
    printArray(arr,size);

    std::cout <<endl<< "Let's creat a matrix now" << std::endl;
    std::vector<int>::iterator itr; // to display matrix elements
    vector<vector<double> >matrix;

  }

I would like to Generate a matrix who stores a generated array for each iteration.
Why not think of it as working with an array/vector, the storing them in a collection?

The code then looks like:
1
2
3
4
5
6
7
int main() {
	srand(time(NULL));
	std::vector<std::vector<int>> collection;

	std::vector<int> n = { (rand()%10)+4, (rand()%10)+4, (rand()%10)+4, (rand()%10)+4 };
	collection.push_back(n);
}
Hi kbw, thanks for your response, your method gives me this error

error: in C++98 ‘n’ must be initialized by constructor, not by ‘{...}’

error: could not convert ‘{((rand() % 10) + 4), ((rand() % 10) + 4), ((rand() % 10) + 4), ((rand() % 10) + 4)}’ from ‘<brace-enclosed initializer list>’ to ‘std::vector<int>’
Last edited on
how about using a list?? can it save multiple arrays
A std::vector is a list. A std::list is something else. Poor naming that cannot be changed ;p
well my problem is that I want something in the form of a table(e.g 1000 x 4 ), which can accessible and changeable in every iteration.
well my problem is that I want something in the form of a table(e.g 1000 x 4 ), which can accessible and changeable in every iteration.
That's what I posted, right?
Topic archived. No new replies allowed.