Thread: RND exercise

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    16

    RND exercise

    Hello ,

    I would like share this beginner code in order to get any advises.
    ( if you have time , thanks)

    Description:
    Choose a level. Level number determines the maximum numbers of words to turn into an hidden word
    Every hidden word is overwrote by "_" char and identified by a progressive number
    You have to choice a number and guess what words is back before to press enter

    Example:
    original text : Students compile a collection of their texts in a variety of genres

    <<Difficulty level 1-10 ( 1 = harder): ? 5>>

    Students 1______ a collection of 2____ texts 3__ a variety 4__ genres

    <<What number you choose ? 2>>
    speak loudly what word you have guessed..

    Here is : their

    <<What number you choose ? >>
    and so on...

    Code:
    #include "pch.h"
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <vector>
    #include <stdio.h>
    #include <set>
    #include <ctime> 
    #include <random>
    
    
    using namespace std;
    
    
    vector <wstring> countWords(wstring text);
    vector<int> mersenne(int seed, int start, int end, size_t howManyNumb);
    vector<wstring> quizIt(vector <wstring> words, vector <int> rnd);
    
    const unsigned int seed = time(0);
    
    int main()
    {
        wstring text( // Odissey
            L"Narrami, o musa , l'uomo dall'agile mente che a lungo andó vagando, "
            "poi che cadde troia ,la forte città e di molte genti vide le terre "
            "e così conobbe la natura dell'anima, molti dolori patì nel suo cuore "
            " lungo le vie del mare lottando per tornare in patria coi compagni. "
            " Ma per loro follià (come simili a fanciulli !) non li potè sottrarre "
            " alla morte. Poi che mangiarono i buoi del sole , figlio del cielo , "
            " tolse loro il tempo di ritorno. Questo narrami,o dea , figlia di Zeus "
            " e comincia di dove tu vuoi ");
        
        vector <wstring> quiz;
        vector <wstring> Words;
        vector <int> rnd;
    
    
        size_t level = 1;
        int difficulty = 1;
        
        cout << "Difficulty level 1-10 ( 1 = harder):> ";
        cin >> difficulty;
    
        Words = countWords(text);
        level = (Words.size() / difficulty)-1;
        cout << level << " " << Words.size();
    
    
        rnd = mersenne(seed, 1, Words.size() - 1, level);
        quiz = quizIt(Words,rnd);
    
        int wordNumber = 0;
        while (1) {
            cout << "Completa numero:> "  ;
            cin >> wordNumber;
            wcout <<  quiz.at(wordNumber) << endl;
        }// ctrl-c to exit
    
    
        return 0;
    }
    vector <wstring> countWords(wstring text) {
    
    
        wstring wordExtracted;
        vector <wstring> words;
    
        wistringstream iss(text);
        while (iss >> wordExtracted) { words.push_back(wordExtracted); }
    
    
        return words;
    }
    vector<wstring> quizIt(vector <wstring> words, vector <int> rnd) {
        
        vector <wstring> hiddenWords;
        vector <wstring>::iterator itP;
        vector <int>::iterator it;
        int newLine = 0;
        int contator=0;
        
        std::cout << "Hello World!\n";
    
    
        for (it = rnd.begin(); it < rnd.end(); it++) {
    
    
            hiddenWords.push_back(words[*it]);    // saves the hidden words choices by random numb generator 
            // overwrites the hiddenwords by '_' char and add an id number
            words.at(*it) = to_wstring(contator++) + wstring(words[*it].size(), '_');
        }
        
        setlocale(LC_ALL, ".437");
        for (itP = words.begin(); itP < words.end(); itP++) {
    
            if (!(newLine = (newLine + 1) % 12)) cout << endl;
            wcout << *itP << ' ';
        }
        cout << endl;
        return hiddenWords;
    }
    vector<int> mersenne(int seed, int start, int end, size_t howManyNumb) {
    
    
        set<int> numb;
        vector<int> result;
        int rnd = 0;
    
        mt19937_64 rndGen(seed);
        uniform_int_distribution<int> rndDist(1, end);
        
        while (numb.size() < howManyNumb) { 
            
            rnd = rndDist(rndGen);
    
            if (numb.insert(rnd).second) { // If doesn't fail ,saves rnd to result
                result.push_back(rnd);
            }
        }
        return result;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,938
    My advice would be:
    • Use nullptr, not 0, when you need a null pointer constant.
    • You shouldn't default to passing containers like vector (or wstring, which technically isn't a container but has similiar expensive copying semantics) by value. The use of move semantics aside, if you don't need a copy of the container and won't modify the container, then pass by const reference, otherwise pass by non-const reference. For a more comprehensive and advanced overview: Parameter passing: Prefer simple and conventional ways of passing information
    • Declare variables near first use. That is, if you can only provide a sensible initial value to a variable somewhere in the middle of the function, declare the variable there, not at top of the function. If you can avoid it, don't default initialise -- or provide some useless default initial value -- early on, only to always overwrite it later.
    • Make use of range-based for loops instead of explicit iterator-based for loops. If you do need explicit iterator-based for loops, make use of the auto keyword instead of explicitly specifying the iterator type when declaring it.
    • I might have the random number generator as a local variable in main then pass it around to those functions that need a random number generator. If you prefer to have a global random state, then the random number generator rather than the seed should be global, otherwise repeated calls to mersenne will result in the same sequence.

    Oh, and do you really need to #include <stdio.h>? It seems to me that you need to #include <locale.h>, or <clocale> for setlocale.

    As a matter of style, I would avoid having consecutive blank lines in function definitions (i.e., one blank line would suffice to demarcate logical sections within a function), and I would definitely separate my function definitions by at least one blank line, maybe two (up to you, but be consistent).
    Last edited by laserlight; 04-10-2019 at 08:07 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2019
    Posts
    16
    Quote Originally Posted by laserlight View Post
    My advice would be:
    • Use nullptr, not 0, when you need a null pointer constant.
    Done
    Quote Originally Posted by laserlight View Post
    You shouldn't default to passing containers like vector (or wstring, which technically isn't a container but has similiar expensive copying semantics) by value. The use of move semantics aside, if you don't need a copy of the container and won't modify the container, then pass by const reference, otherwise pass by non-const reference. For a more comprehensive and advanced overview: Parameter passing: Prefer simple and conventional ways of passing information
    I've got to study it , but I tried to do something
    You're right , passing by value it doesn't make sense

    Code:
    vector <wstring> countWords(const wstring &text) {
        
        wstring wordExtracted;
        vector <wstring> words;
        wistringstream iss(text);
        while (iss >> wordExtracted) { words.push_back(wordExtracted); }
        return words;
    }
    vector<wstring> quizIt(vector <wstring> &words, vector <int> &rnd) {
        
        vector <wstring> hiddenWords;
        std::cout << "Hello World!\n";
        int contator = 0;
        for (auto it = rnd.begin(); it < rnd.end(); it++) {
            hiddenWords.push_back(words[*it]);    // saves the hidden words choices by random numb generator 
            // overwrites the hiddenwords by '_' char and add an id number
            words.at(*it) = to_wstring(contator++) + wstring(words[*it].size(), '_');
        }
        setlocale(LC_ALL, ".437");
        int newLine = 0;
        for (auto itP = words.begin(); itP < words.end(); itP++) {
            // Prints the quiz
            if (!(newLine = (newLine + 1) % 12)) cout << endl;
            wcout << *itP << ' ';
        }
        cout << endl;
        return hiddenWords;
    }
    Thanks Light

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. exercise in c
    By hugoguan in forum C Programming
    Replies: 1
    Last Post: 11-30-2010, 04:32 AM
  2. K&R Exercise 1-9
    By Anarchy in forum C Programming
    Replies: 6
    Last Post: 01-10-2010, 12:42 PM
  3. gdb exercise!
    By MK27 in forum C Programming
    Replies: 9
    Last Post: 11-24-2008, 10:17 AM
  4. Exercise
    By bumfluff in forum C++ Programming
    Replies: 15
    Last Post: 04-21-2006, 12:18 PM
  5. K&R Exercise 1-14
    By Lee134 in forum C Programming
    Replies: 3
    Last Post: 02-16-2006, 11:20 AM

Tags for this Thread