Dynamic Matrix. Random numbers and size problem.

create a dynamic matrix and I can now complete the problem I have is that when you see the results on the screen when creating a 4x4 matrix in the positions [] [3] of each row you can see 0's. Sometimes when creating a 3x4 matrix or one that is not square I can see that the largest number is not between 0 and 10 but it is much bigger. I hope you can help me. I would appreciate if you have any link regarding the spacing of writing when programming. Thank you.

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
39
40
41
42
43
44
45
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <time.h>
using namespace std;
void Dimensionar2D(vector<vector<int> > &v,int &,int &);
void Rellenar2D(vector<vector<int> > &,int &,int &);
void Observar2D(vector<vector<int> > &);
int BuscarM2D(vector<vector<int> > &);
int main(int argc, char *argv[]) {
	vector<vector<int> > matriz;
	int F=0,C=0;
	Dimensionar2D(matriz,F,C);
	Rellenar2D(matriz,F,C);
	Observar2D(matriz);
	cout<<"El mayor es: "<<BuscarM2D(matriz)<<endl;
	return 0;
}
void Dimensionar2D(vector<vector<int> > &v,int &a,int &b){
while(a<2 || a>13 && b<2 || b>13){
	cout<<"Filas: "<<flush;cin>>a;
	cout<<"Columnas: "<<flush;cin>>b;
	cout<<endl;
}}
void Rellenar2D(vector<vector<int> >&v,int &a,int &b){
vector<int> temp;
srand (time(NULL));
for(int x=0;x<a;x++){
		for(int y=0;y<b;y++){
		temp.push_back(rand()%10);}
	v.push_back(temp);temp.clear();}
}
void Observar2D(vector<vector<int> > &v){
for(int x=0;x<v.size();x++){
	for(int y=0;y<v[y].size();y++){cout<<v[x][y]<<" "<<flush;}
cout<<endl;}
}
int BuscarM2D(vector<vector<int> > &v){
	int actual=0,mayor=0;
	for(int x=0;x<v.size();x++){
		for(int y=0;y<v[y].size()-1;y++){actual=v[x][y];
		if(actual>mayor){mayor=actual;}}}
	return(mayor);
}
Is this literally just initializing a 4x4 matrix with random values?

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>


int main()
{
	std::vector<int> v;
        std::generate_n(std::back_inserter(v), 4*4, []() { return rand() % 10; });
	for(auto& i : v)
		std::cout << i << ' ';
}


Ideally would use something better than rand()
Last edited on
Thanks Solved.
Topic archived. No new replies allowed.