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

51,933 answers

573 users

How to create an m x n grid of random characters in C++

2 Answers

0 votes
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>

// Function to generate a random uppercase letter
char randomLetter() {
    return 'A' + std::rand() % 26;
}

// Function to create an m x n grid filled with random letters
std::vector<std::vector<char>> createGrid(int m, int n) {
    std::vector<std::vector<char>> grid(m, std::vector<char>(n));
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            grid[i][j] = randomLetter();
        }
    }
    return grid;
}

// Function to print the grid
void printGrid(const std::vector<std::vector<char>>& grid) {
    for (const auto& row : grid) {
        for (char c : row) {
            std::cout << c << " ";
        }
        std::cout << "\n";
    }
}

int main() {
    int m = 4, n = 5;

    // Seed random number generator
    std::srand(std::time(0));

    // Create and print grid
    auto grid = createGrid(m, n);
    printGrid(grid);
}


/*
run:
 
N V D G L 
L N P M S 
R K E D Q 
X K O E D 
 
*/

 



answered Nov 22, 2025 by avibootz
0 votes
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>

// Function to generate a random character
char randomCharacters() {
    const std::string characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
    return characters[std::rand() % characters.length()];
}

// Function to create an m x n grid filled with random letters
std::vector<std::vector<char>> createGrid(int m, int n) {
    std::vector<std::vector<char>> grid(m, std::vector<char>(n));
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            grid[i][j] = randomCharacters();
        }
    }
    return grid;
}

// Function to print the grid
void printGrid(const std::vector<std::vector<char>>& grid) {
    for (const auto& row : grid) {
        for (char c : row) {
            std::cout << c << " ";
        }
        std::cout << "\n";
    }
}

int main() {
    int m = 4, n = 5;

    // Seed random number generator
    std::srand(std::time(0));

    // Create and print grid
    auto grid = createGrid(m, n);
    printGrid(grid);
}


/*
run:
 
q w c s C 
4 J f b C 
R 1 8 k 7 
T h 6 O B 
 
*/

 



answered Nov 22, 2025 by avibootz
edited Nov 22, 2025 by avibootz
...