//prog01.cpp
/*
Randomly assign chapters to the students
An array names is initialized with names of the students of size n
Algorithm1:
make another array chapters[n]
initialize chapters[n] with {0, 1, 2,...}
shuffle chapters[]
display i+1, names[chapters[i]] for i=0 to n-1 */
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
void shuffleDisplay1(void);
void shuffleDisplay2(void);
void shuffleDisplay3(void);
char *names[]={
"BELLES MARK R ",
"BELTRAN Daniel ",
"BHANDARKAR SUMEER ",
"BILES STEPHEN J ",
"BOGGESS MICHAEL W ",
"BOHANNON JAIMEE L ",
"CHANG CHIA-CHEN (Jennifer) "};
/*
"COFFEY RICHARD A ",
"CRAIG JAMES L ",
"ERISMAN GARY L ",
"FALKNER CHRIS ",
"FLEURY Just CHRIS ",
"HAHN CALEB M ",
"HALL TONY A ",
"HARRIS JOHN R ",
"IBBETSON JON ",
"JANSSEN JOHN P ",
"KHATTAR SIDDHARTH ",
"KHOSLA VINEET ",
"LONG ARON A ",
"MCFARLAND JAMES B ",
"MCFEETERS LORI A ",
"OLINGER MICHAEL Z ",
"PHIPPS SCOTT A ",
"PIERCE TIM ",
"PORTER ANDREW P ",
"SERVAES JAIME S ",
"SHARMA RAJIT ",
"SHARMA SHOBHIT ",
"WILLHITE RYAN M ",
"YATES CHRIS M "
}; */ const int N = sizeof(names)/sizeof(names[0]);
void main(void)
{
srand(time(NULL)); for (int i=0; i<N; i++)
cout << names[i] << endl;
cout << endl; shuffleDisplay3(); } void shuffleDisplay1(void)
/*
Algorithm1:
make another array chapters[n]
initialize chapters[] with {0, 1, 2,...}
shuffle chapters[]
display i+1, names[chapters[i]] for i=0 to n-1
*/
{
int chapters[N];
int i, k, temp; for (i=0; i<N; i++)
chapters[i] = i; for (i=0; i<N; i++)
{
k = rand()%N;
temp = chapters[i];
chapters[i] = chapters[k];
chapters[k] = temp;
} for (i=0; i<N; i++)
cout << i+1 << ' ' << names[chapters[i]] << endl;
}
void shuffleDisplay2(void)
/*
Algorithm1:
shuffle names[] (names[] is an array of pointers to the strings)
display i+1, names[i] for i=0 to n-1
*/
{
int i, k;
char *temp; for (i=0; i<N; i++)
{
k = rand()%N;
temp = names[i];
names[i] = names[k];
names[k] = temp;
} for (i=0; i<N; i++)
cout << i+1 << ' ' << names[i] << endl; }
void shuffleDisplay3(void)
/*
Algorithm3:
do the following N times
select two names randomly
swap these two names */
{
int i, pick1, pick2;
char *temp; for (i=1; i<=N; i++)
{
pick1 = rand()%N;
pick2 = rand()%N; temp = names[pick1];
names[pick1] = names[pick2];
names[pick2] = temp;
} for (i=0; i<N; i++)
cout << i+1 << ' ' << names[i] << endl; } |