Using the Scientific Method, determine if this program can generate all permutat
ID: 3694268 • Letter: U
Question
Using the Scientific Method, determine if this program can generate all permutations (shuffles) of a deck of 4 cards numbered 1, 2, 3, 4 in array cards.
#include <cstdlib>
#include <iostream>
#include <time.h>
using namespace std;
int main( )
{
int cards[] = { 1, 2, 3, 4 } ;
int n = 4 , temp , i , rando ;
srand(time(NULL)); // seed rand()
for (i=0; i < n; ++i)
{
rando = rand() % n ; // pick a random spot
temp = cards[i] ; // do a swap
cards[i] = cards[ rando ];
cards[ rando ] = temp ;
}
for (int j = 0; j < n; ++j ) cout << cards[j] << ". "; cout <<" ";
system("PAUSE");
return EXIT_SUCCESS;
}
Explicitly write all hypotheses as you formulate them,
write all experiments done (pasting on some results, say of runs),
determine if something is wrong, and
if so, diagnose it precisely.
Explanation / Answer
#include #include // for srand and rand #include // for time using namespace std; int main() { int card[52]; // array of cards; int n; // number of cards to deal srand(time(0)); // initialize seed "randomly" for (int i=0; i> n) { //--- Shuffle elements by randomly exchanging each with one other. for (int i=0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.