How do I clear an array at the end of a do-while loop in C++?

210 views Asked by At

The purpose of my function is to generate 8 random letters n times (specified when it is compiled). I approached this by writing two functions called genNumbers - which generates 8 random numbers (between 0 and 25) and places them into an array called numbers, and convert - which converts each number in the array to the corresponding letter in the alphabet and places those letters into an array called letters. Those two functions work perfectly, I tested them out multiple times.

Now I am having trouble implementing them in main.

I want main to take in a parameter which specifies how many letters arrays are desired. So for example calling a.out 3 would output something like

SAFJPONG   1
VYREMNXZ   2
QWMKUZXC   3

This is what I have in my main:

int numbers[8];
string letters[8];

void genNumbers() {
int i;

srand (time(NULL));

for (i = 0; i < 8; i++) {
    numbers[i] = rand() % 25;
}
}

void convert() {
int i;

for (i = 0; i < 8; i++) {
    if (numbers[i] == 0) {
        letters[i] = "A";
    }
    else if (numbers[i] == 1) {
        letters[i] = "B";
    }
    else if (numbers[i] == 2) {
        letters[i] = "C";
    }
    else if (numbers[i] == 3) {
        letters[i] = "D";
    }
    else if (numbers[i] == 4) {
        letters[i] = "E";
    }
    else if (numbers[i] == 5) {
        letters[i] = "F";
    }
    else if (numbers[i] == 6) {
        letters[i] = "G";
    }
    else if (numbers[i] == 7) {
        letters[i] = "H";
    }
    else if (numbers[i] == 8) {
        letters[i] = "I";
    }
    else if (numbers[i] == 9) {
        letters[i] = "J";
    }
    else if (numbers[i] == 10) {
        letters[i] = "K";
    }
    else if (numbers[i] == 11) {
        letters[i] = "L";
    }
    else if (numbers[i] == 12) {
        letters[i] = "M";
    }
    else if (numbers[i] == 13) {
        letters[i] = "N";
    }
    else if (numbers[i] == 14) {
        letters[i] = "O";
    }
    else if (numbers[i] == 15) {
        letters[i] = "P";
    }
    else if (numbers[i] == 16) {
        letters[i] = "Q";
    }
    else if (numbers[i] == 17) {
        letters[i] = "R";
    }
    else if (numbers[i] == 18) {
        letters[i] = "S";
    }
    else if (numbers[i] == 19) {
        letters[i] = "T";
    }
    else if (numbers[i] == 20) {
        letters[i] = "U";
    }
    else if (numbers[i] == 21) {
        letters[i] = "V";
    }
    else if (numbers[i] == 22) {
        letters[i] = "W";
    }
    else if (numbers[i] == 23) {
        letters[i] = "X";
    }
    else if (numbers[i] == 24) {
        letters[i] = "Y";
    }
    else {
        letters[i] = "Z";
    }
}
}

int main(int argc, char * argv[]) {
 int i = 0;
 int g,n;

if (argc == 2 && argv[1][0] > 0) {
    n = atoi(argv[1]);

    do {
        genNumbers();
        convert();
        for (g = 0; g < 8; g++) {
            cout << letters[g];     // this is where I believe the problem is
        }
        cout << "   " << i+1 << endl;
        i++;
    }
    while (i < n);
}

return 0;
}

When I run a.out 3, my output looks like:

SAFJPONG   1
SAFJPONG   2
SAFJPONG   3

So evidently, my main is returning the same letters array every time, and not creating 3 different ones like I want it to.

I have commented where I believe my problem is arising. I feel like it has something to do with having to delete the array so that a new one is made every time. Can anyone spot where I am going wrong?

1

There are 1 answers

0
lakshayg On BEST ANSWER

Here's how I implemented the same program. I think there is something fishy with the second line in your code string letters[8];. Also you may want to check your genNumbers and convert functions.

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

int nums[8];
char letters[9];

void genNumbers() {
    for (int i=0; i<8; i++)
        nums[i] = rand()%26;
}

void convert() {
    for (int i=0; i<8; i++)
        letters[i] = 'A' + nums[i];
}

int main(int argc, char **argv) {
    letters[8] = '\0';
    srand(time(NULL));
    int n = 0;
    if (argc == 2) {
        n = atoi(argv[1]);
        for (int i=0; i<n; i++) {
            genNumbers();
            convert();
            printf("%s\n", letters);
        }
    }
    return 0;
}

EDIT:

You are going wrong in your call to srand(time(NULL)) in the genNumbers function. Calling it every time when you make a call to genNumbers causes you to reset the seed thus restarting the pseudo-random number sequence. It gives the same number even though you use time as a seed because the execution of this code is quick and the time difference is negligible. Your code would work if you had a call to something like usleep(1000000); in your code as it would cause significant time different between the function calls. For example: This code yields the desired result:

#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>

int nums[8];
char letters[9];

void genNumbers() {
    srand(time(NULL));
    for (int i=0; i<8; i++)
        nums[i] = rand()%26;
}

void convert() {
    for (int i=0; i<8; i++)
        letters[i] = 'A' + nums[i];
}

int main(int argc, char **argv) {
    letters[8] = '\0';
    int n = 0;
    if (argc == 2) {
        n = atoi(argv[1]);
        for (int i=0; i<n; i++) {
            genNumbers();
            convert();
            printf("%s\n", letters);
            usleep(1000000);
        }
    }
    return 0;
}

EDIT:

If you still want to avoid the call to srand() in main then here's something you can do in genNumbers itself:

void genNumbers() {
    srand(time(NULL)*rand()); // notice time(NULL)*rand()
    for (int i=0; i<8; i++)
        nums[i] = rand()%26;
}