Click here to Skip to main content
16,022,060 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
// Codigo no imprime asteriscos, envio pequeño fragmento de dos funciones:
The code does not print asterisks, I am sending a small snippet of two functions.

What I have tried:

C++
void llenar_matriz_numero(int num, char c, vector<vector<char>> &matriz)
{
	int numero_escogido=num;
	char asterisco=c;
	
	switch (numero_escogido) {
	case 1:
		matriz[0][0]='1';
		matriz[0][1]=asterisco;
		break; 
	case 2:
		matriz[0][0]='1';
		matriz[0][1]=asterisco;
		matriz[1][0]='2';
		matriz[1][1]=asterisco;
		matriz[1][2]=asterisco;

void imprimir_matriz(int num, char c,FILAS,COLUMNAS,vector<vector<char>> &matriz)
{
		int i,j;
		
		llenar_matriz_numero(num,c,matriz);
		for(i=0;i<=FILAS;i++){
			for(j=0;j<=COLUMNAS;j++){
				
				cout<< matriz[i][j] <<' ';
			}
			
		}
		cout<<endl;
	}
		break;
Posted
Updated 26-Sep-24 14:12pm
v2

As a side note
Quote:
void llenar_matriz_numero(int num, char c, vector<vector<char>> &matriz)
{
int numero_escogido=num;
char asterisco=c;
You choose a good name for the variable, but, unfortunately, that's not enough. You have to call the function properly, e.g.

C++
llenar_matriz_numero(2, '*', m); 
 
Share this answer
 
I don't have a definitive solution for you but I have suggestions and tips. Firstly, I agree with everything merano99 wrote. Here are my additions :

When I see somewhat complex types I prefer to add a type definition or a using statement. I also prefer not have "using namespace" statements but that's just me. Anyway, I would add these statements :
C++
using vchar = std::vector< char >;
using vvchar = std::vector< vchar >;
Then using those types will simply the code a bit which makes it easier to read.

Here is one way you can initialize the matrix :
C++
void InitializeMatrix( int rows, int cols, vvchar & matrix )
{
    // initialize the matrix with rows X columns of zeros

    matrix.reserve( rows );
    for( int y = 0; y < rows; ++y )
    {
        vchar oneRow( cols, 0 );
        matrix.push_back( oneRow );
    }
}
Here it is as a template function so the matrix can be of (nearly) any type :
C++
template< typename T >
void InitializeMatrix( int rows
                     , int cols
                     , std::vector< std::vector< T > > & matrix
                     , T & initial
                     )
{
    // initialize the matrix with rows X columns of default objects

    matrix.reserve( rows );
    for( int y = 0; y < rows; ++y )
    {
        std::vector< T > oneRow( cols, initial );
        matrix.push_back( oneRow );
    }
}

// a test of the template function

using vvint = std::vector< std::vector< int > >;

void TestInitializeMatrix()
{
    vvint matrix;
    int initial = 0;
    InitializeMatrix< int >( 6, 5, matrix, initial );
    trace( "element at %d,%d is %d\n", 3, 2, matrix[3][2] );
}
 
Share this answer
 
1. This is an English-language forum. I have translated the Spanish parts for you, but we would appreciate it if you could provide them in English in the future.

It would also help if the function names were more readable for us:
C++
// void llenar_matriz_numero(int num, char c, vector<vector<char>> &matriz)
void fill_matrix_with_number(int num, char c, vector<vector<char>> &matrix)

// void imprimir_matriz(int num, char c, int FILAS, int COLUMNAS, vector<vector<char>> &matriz)
void print_matrix(int num, char c, int ROWS, int COLUMNS, vector<vector<char>> &matrix)

2. Please use appropriate code tags to make your source code easier to read. I have used C++ code tags here.

3. The code appears incomplete or disorganized. It’s possible that something got mixed up when copying.

The function fill_matrix_with_number() is expected to initialize matrices of arbitrary size, but due to the current switch-case implementation, it only initializes a limited number of elements. You should consider using for loops, similar to how they are used for output.

4. There might also be issues in the function print_matrix. The provided code snippet does not contain all the necessary information.

Note: The initialization of the matrix is not provided here. It would be important to know the actual size. If the matrix is supposed to have the dimensions ROWS * COLUMNS, this needs to be ensured before writing values into it. Accessing non-existent elements would cause errors.

If the matrix is expected to have the size ROWS * COLUMNS, the index boundaries in the loops must be respected. The existing code appears to exceed the valid range when accessing elements.

// edit: The parameters ROWS and COLUMNS are not needed in the print_matrix() function if you always intend to print the entire matrix. Vector objects already contain the size.

You can get the size of the matrix like this:
C++
size_t rows = matrix.size();           
size_t columns = matrix[0].size();


This way, you don't need to pass ROWS and COLUMNS as parameters, and the function can dynamically adapt to the matrix size.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900