Program
39
C++
K.
Dzwonkiewicz
This program will require the use of an array of structures and pointers to structures.
Enter 10 names of NOBEL PRIZE winners for years 1979-1998. For various fields.
Check a new almanac of the WEB.
(This is an award given on December 10 for the world figure who has
done the most for the abolition of armies and the promotion of peace. More recently, it has also been awarded for
Physics, Medicine, Literature, Chemistry, and Economics. The money was first donated by Alfred Nobel,
who invented dynamite, and the Bank of Sweden.)
Also, enter the year it was awarded, country of residence and the field it was awarded in. (You probably would be better off to store the data to a data file then read it into the variables as you run the program.) Use array of structs.
Have your program print…
n Original list
n Alpha list
n Chronological list
n Original list again
No menu is needed but if you’d prefer, use one.
The program MUST use the sorting algorithm by POINTERS (see your ditto). By doing this you leave the original list unchanged so you can print it out at the end.
When you print the original list use point(-> arrow) access operator. Don’t mix up pointers with access by pointers!
OUTPUT
Original List
Name Year Country Field
Aung San Suu Kyi 1991 Myanmar Peace
Dali Lama 1989 Tibet (exile) Peace
Erwin Neher 1991 Germany Medicine
Tele-Tuby 1999 United Kingdom Peace
Kenichi Fukui 1981 Japan Chemistry
Wole Soyinka 1986 Nigeria Literature
//fchoice=atoi(fchar);
//fchoice=getche(
);
cin>>fchoice;
print(nobel, fchoice);
}while(fchoice<4);
}
void print ( prize nobel
[ ], int choice)
{
prize *hold, *ptrs[10], *ptr;
short x, y;
switch ( choice )
{
case 1: clrscr ( );
ptr = nobel;
cout <<
“\t\tOriginal LIST”;
cout << “\nNAME YEAR COUNTRY”
<< “ FIELD”;
getch ( );
break;
case 2: clrscr ( );
for ( x = 0;
x<10; ++x )
ptrs[x] =
&nobel[x];
for ( x = 0;
x<9; ++x )
for
(y = x+1; y<10; ++y )
if
( strcmp (ptrs[x] -> name, ptrs[y] -> name) > 0)
{
hold = ptrs[x];
ptrs[x] = ptrs[y];
ptrs[y] = hold;
}
cout << “\t\tAlpha
LIST”;
cout << “\nNAME YEAR COUNTRY”
<< “ FIELD”;
for ( x = 0;
x<10; ++x )
cout
<< ‘\n’ << ptrs[x] -> name
<< setw(24 – strlen(ptrs[x] ->
name))
<< ptrs[x] -> year << “ ”
<< ptrs[x] -> country <<
setw(17 – strlen(ptrs[x] -> country))
<< ‘ ’ << ptrs[x] -> field;
getch ( );
break;
case 3: clrscr
( );
for (x = 0;
x<10; ++x )
ptrs[x] =
&nobel[x];
for (x = 0;
x<9; ++x)
for
(y = x+1; y<10; ++y)
if
((ptrs[x] -> year) < (ptrs[y] -> year))
{
hold = ptrs[x];
ptrs[x] = ptrs[y];
ptrs[y] = hold;
}
cout <<
“\t\tChronological LIST”;
cout << “\nNAME YEAR COUNTRY”
<< “ FIELD”;
for (x = 0; x<10; ++x) cout << ‘\n’ << ptrs[x] -> name