mardi 29 mars 2016

Displaying 2 iterated arrays in one for loop

I am working on creating a simulation of a test that will 1. randomize multiple choice answers 2. display the choices from a) b) c) d)

I have both codes done separately however can I use on for-loop to go about displaying this? Is this the best way to do this? All help is appreciated thank you!

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

int main (){
const int TEST_SIZE = 13;
srand(time(0));
string animals[TEST_SIZE] = {"dog","cat","fish","elephant","rhinoceros","cheetah","tiger","lion","zebra","giraffes","alligators","sloths","kangaroos" };

for (int i = 0; i < TEST_SIZE; i++){
    //generate random index number (0,1,2,3,4,5...)
    int index = rand() % FACE_SIZE;

    //swap animals[i] with animals[index]
    string temp = animals[i];
    animals[i] = animals[index];
    animals[index] = temp;

}
//loop through array and print values
for (int i = 0; i < 7; i++){
    cout << animals[i] << "  ";
}

}

//separate code for part 2: choices from a-g

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

int main()
{
const int CHOICE_SIZE = 7;
string choices[] = { "a)", "b)","c)","d)","e)","f)","g)" };
for (int i = 0; i < CHOICE_SIZE; i++) {
cout << choices[i] << "  ";
}
}

Aucun commentaire:

Enregistrer un commentaire