3

I want to ask how can I print the word Chessmaster in 2d array randomly each letter of the word? I tried with srand but I don't how to combine it with characters. Can someone help me with is code. Also, can I create random letters in a 2d array without rand ?

#include <iostream>
#include <stdlib.h>
#include <time.h>

const int MAX = 11;

using namespace std;

void printwo()
{
    char word[MAX] = {'c', 'h', 'e', 's', 's', 'm', 'a', 's', 't', 'e', 'r'};
    int c, i, n, letters;

    cout << "I will print this word " << word << " sepereate" << endl;
    srand(time(NULL));

    for (int i = 0; i < 7; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            cout << "Print a random letter["
                 << "][" << word[i] << "]"
                 << "["
                 << "]";
            cout << endl;
        }
    }
}

int main()
{
    int c;

    cout << "Hello user press  _1_ to continue" << endl;
    cin >> c;

    if (c == 1)
    {
        printwo();
    }
    else
    {
        cout << "Bye";
        exit(0);
    }
    return 0;
}
5
  • 1
    Can you give an example of a valid output? Commented Apr 5, 2020 at 21:27
  • 1
    So is the 2d array meant to be 7 by 2? Commented Apr 5, 2020 at 21:44
  • 1
    May letters be repeated? Should they be filled with (random-positioned) spaces? Commented Apr 5, 2020 at 21:51
  • I am not sure what you are asking for. Is onlinegdb.com/Syh5NgODU the sort of thing or are you looking for something else? Commented Apr 6, 2020 at 0:11
  • If you want really want to use rand you could onlinegdb.com/SkGt5xdP8 but rand() doesn't produce a uniform distribution (some numbers are more likely than others) Commented Apr 6, 2020 at 0:40

1 Answer 1

1
#include <iostream>
#include <stdlib.h>
#include <time.h>

const int MAX = 11;

using namespace std;

// Generate a random number between min and max (inclusive)
// Assumes std::srand() has already been called
// Assumes max - min <= RAND_MAX
int getRandomNumber(int min, int max)
{
    static constexpr double fraction { 1.0 / (RAND_MAX + 1.0) };  // static used for efficiency, so we only calculate this value once
    // evenly distribute the random number across our range
    return min + static_cast<int>((max - min + 1) * (std::rand() * fraction));
}

void printwo()
{
    char word[MAX+1] = {"chessmaster"}; //If you are using C-style strings then you should declare +1 space
    //int c, i, n, letters; not needed

    cout << "I will print this word " << word << " sepereate" << endl;

    //srand only gives a seed to yout tand function, you need to call std::rand() to get a random integer
    //use the function getRandomNumber above that is based on rand() and gives you the possibility to select range
    srand(time(NULL));

    for (int i = 0; i < 7; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            cout << "Print a random letter["
                 << "][" << word[getRandomNumber(0,10)] << "]" 
                 << "["
                 << "]";
            cout << endl;
        }
    }
}

int main()
{        
    int c;
    cout << "Hello user press  _1_ to continue" << endl;
    cin >> c;

    if (c == 1)
    {
        printwo();
    }
    else
    {
        cout << "Bye";
        //exit(0); exit here not needed
    }
    return 0;
}

I wrote some comments.. please read them and ask me if you don't get something. I would suggest reading about C-style strings and rand() here: Rand() - https://www.learncpp.com/cpp-tutorial/59-random-number-generation/ C-style strings(basically arrays of char) - https://www.learncpp.com/cpp-tutorial/66-c-style-strings/

Sign up to request clarification or add additional context in comments.

3 Comments

Thank for the help but I want to ask something the compiler shows me some errors but somehow the code runs my main question is can I print the word Chessmaster in different positions for example [ ] [s ] [ ] [ ] [ ] [h ] bun not in the left side only in the 2 others array thank you in advance
I really don't understand what you are trying to do. Please, be more specific about what you want to print!
Sorry I will try to explain better I want to print the word Chessmaster randomly in array at each line but the first array in each line I want to be always empty also I posted a picture down in the comments to help you thank you in advance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.