0

I have to do this excercise:

A common punishment for school children is to write out the same sentence multiple times. Write a C++ stand-alone program that will write out the following sentence one hundred times: “I will always use objectoriented design.” Your program should number each of the sentences and it should “accidentally” make eight different random-looking typos at various points in the listing, so that it looks like a human typed it all by hand.

My knowledge is limited with C random numbers. I have tried this with no success. I can't get 8 errors. As we can see, I get Random errors with the "typo".

Here is my buggy code:

#include <iostream>
#include <ctime>
using namespace std;

int main()
{
    string strPunish = "I will always use objectoriented design.";
    int randFrom = 1;
    int randTo = 100;
    int typoCounter = 0;
    srand(time(NULL));
    int randNumber = randFrom + ( std::rand() % ( randTo - randFrom + 1 ) );
    for (int i = 1; i <= 100; i++)
    {
        if ((i == randNumber) && (typoCounter != 8))
        {
            randFrom = i;
            randNumber = randFrom + ( std::rand() % ( randTo - randFrom + 1 ) );
            string strTypo = strPunish;
            int randTypo = 0 + ( std::rand() % ( strTypo.length() - 0 + 1 ) );
            strTypo.insert(randTypo, "TYPO");
            cout << i << ": " << strTypo << endl;
            typoCounter++;
        }   
        else
            cout << i << ": " << strPunish << endl;
    }
    return EXIT_SUCCESS;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Lucas
  • 329
  • 1
  • 8
  • 5
    Suggestion: Randomly predetermine which lines will be corrupted with a typo by generating 8 random numbers bounded by the maximum number of lines. If you randomly select the lines as you go, you're likely to bias the errors toward the beginning of the list or run out of lines before corrupting all 8 lines. – user4581301 May 11 '20 at 18:19
  • `using namespace std;` - check this: https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – Michael Dorgan May 11 '20 at 18:22
  • 1
    Not using "using namespace" anymore. – Lucas May 11 '20 at 18:32
  • Im wondering. if we take 100/4 = 25 lines per show. And we random those 25 lines, then the next 25, and so on... is it well? – Lucas May 11 '20 at 18:47
  • What you want to do is randomly select 8 lines before doing anything. Your range is 0-99 and from this range you select 8 random numbers. Then you do your printing and upon encountering the predetermined lines you generate a typo. – Koenigsberg May 11 '20 at 18:49

2 Answers2

2

As said above, since it is your homework, you should solve it on your own. But I would give you some leads on possible improvements to your code:

  1. First and foremost, you should divide your code into small functions - it will make it more readable, and easier to follow!
  2. It is easier to create an array with all the numbers in range [0, 99], shuffle it, and just pick the first X lines to mangle (where X can be anything!). Look at std::array and std::shuffle for more information.
  3. Don't use hard-coded numbers in your code! You should instead define constexpr variables - it will make your code more readable (remember (1) ?)
  4. Extra: It is no longer common to use rand and srand, instead, it is better to use std::mt19937 from <random>. Also, it is more common to see usage of <chrono> instead of <ctime>.

Remark: Notice that you need to be sure that the line numbers you pick to mangle are unique; The fastest way to ensure it (which may not be stuck forever) is to do what I have suggested in (2).

Kerek
  • 1,106
  • 1
  • 8
  • 18
1

I've made the entire program for you:

#include <iostream>
#include <ctime>

void selectionSort(int[], int);

int main(void)
{
    std::string punishStr = "I will always use objectoriented design."; // declaration
    std::string tempStr = punishStr;
    std::string typoStr = "TYPO";
    int length = punishStr.length();
    int location = 0;
    short int count = 0;
    int randoms[8] = {0};

    srand(time(0)); // generates random different times

    for (int i = 0; i < 8; i++)
    {
        randoms[i] = (rand() % 100) + 1; // creates 8 random numbers
    }

    selectionSort(randoms, 8); // sorts random numbers, for sake of being unbiased

    for (int i = 0; i < 100; i++)
    {
        if (randoms[count] == i && count <= 8) // random number equals i
        {
            punishStr = tempStr;
            location = (rand() % length) + 1;
            std::cout << (i + 1) << ": " << punishStr.insert(location, typoStr) << std::endl;
            count++;
        }
        else
            std::cout << (i + 1) << ": " << tempStr << std::endl;
    }

    return 0;
}

void selectionSort(int a[], int n) // sorting algorithm
{
    int i, j, min, temp;

    for (i = 0; i < n - 1; i++)
    {
        min = i;
        for (j = i + 1; j < n; j++)
            if (a[j] < a[min])
                min = j;

        temp = a[i];
        a[i] = a[min];
        a[min] = temp;
    }
}

The program firstly declares some necessary variables including the required eight random numbers with zero. Then, by using the For loop, it redefines all the 8 array elements ranging random numbers between 1-100.

After that, simply it sorts the arrays (otherwise it'll get biased e.g. 54, 32...), to prevent it, we simply sorted it like 32, 54 ... . Thereafter, it checks whether the counter variable is <= 8, if it is, then increment and show the typo sentence. Otherwise, show correct sentence.

It'll repeat over and over again until i is lesser than 100.

Hope it works for you.

Note: Output's so big, better you check that by your own. :-)

Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
  • Please refrain from doing this. The embedded explanation keeps this from being a dreaded code-only answer, but homework needs to be more hands-off. If you solve the whole problem, the questioner doesn't learn as much as they should. – user4581301 May 11 '20 at 20:16
  • Side notes: There's no need for the roll-your-own sort. Unless writing a sort is the assignment, lead questioners to `std::sort`. Also try to nudge them in the direction of the `` library. [I find this link to be a helpful prod.](https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful) – user4581301 May 11 '20 at 20:21
  • @user4581301 I was getting confused in his program. That's why I recoded it rather than taking a long time. And I've commented wherever necessary. – Rohan Bari May 11 '20 at 20:27
  • @user4581301 I've added explanation for my program. Hope it's no more dreaded code-only answer. – Rohan Bari May 11 '20 at 20:31
  • It wasn't code-only in the first place. You'd side stepped that problem. My complaint was you did too much of the work, robbing the questioner of learning opportunities. – user4581301 May 11 '20 at 20:36
  • Also, notice that when running `randoms[i] = (rand() % 100) + 1; // creates 8 random numbers`, you don't necessarily get 8 ***unique*** indexes! – Kerek May 11 '20 at 20:42
  • @Kerek Obviously, it's not my fault. Randomness is randomness, no guarantee for unique index 8 times, but most chances. – Rohan Bari May 11 '20 at 20:58