Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Exercises for Day 3
Write C++ programs for each of the exercises below. You should use only features
in the language which have been discussed in the notes through this day.
Shorter Exercises
1. Average with Functions (*)
Write a function that takes three floats and returns their average. Test your function
using a main function.
2. Arithmetic with Functions
Write a function that takes three floats, adds 10% to each one, and returns the average of
the three new values. The three values should be changed after the function call. Test
your function by reading in three values in main, calling your function and printing the
three old values, three new values, the average of the original three values and the
average of the three new values..
3. Functions with Array Parameters (*)
Write a function that takes an array of positive integers and the size of the array, and
returns the average (type float) of the values. Test your function with a main function,
that reads in an array of positive integers (it stops reading when it sees a negative
number), passes the array and its size to your function, and prints the resulting average.
4. Functions with String Parameters (*)
Write a function that takes three strings as input and returns a string equal to the
concatenation of the three strings in alphabetical order. Test your function with a main
function.
5. A Mathematical Function – The Least Common Multiple
Write a function that takes three integers and returns the smallest integer that is a multiple
of all three integers. Test your function using a main function.
6. Golf in the Arrays
Write a function that takes two arrays of integers, each of size 18. The first array
represents par score on each of 18 holes in a golf course. The second array represents the
actual scores of a player’s round. Your function should return an integer that represents
how many strokes the player’s round was above or below par. (Your function should
return 0 if the player shot even par). Test your function with a main function.
Longer Exercises
1. MAXSORT Using Functions (*)
Let’s rewrite MAXSORT using functions.
a. Write a function called MAX which takes an array A of size strings and returns the
index of the largest string in the first size strings. MAX should use the strcmp function.
b. Write a function called MAXSORT which takes an array A of size strings and sorts it
by swapping the largest string in the first size strings in A, with the current last location.
The current last location starts at size - 1 and size is decremented at each iteration until it
equals 1. MAXSORT should use MAX and a SWAP function. The SWAP function
must use strcpy and hence cannot use the function template in this chapter.
c. Write a function that prints out an array A of size strings.
d. Write a function that reads in an array A of strings, and returns the number of strings.
e. Write a main function that defines a dynamic array of dynamic strings. The user
should be asked for the maximum number of strings and for the maximum length of each
string. The array should be read in a string at a time until eof, then sorted and printed.
The details of working with a dynamic array of dynamic arrays, may cause the beginner a
little trouble. To help with a model, we include the program below:
#include <iostream.h>
//
//
//
//
A program to dynamically allocate an array of strings, whose sizes
themselves can be dynamically allocated
Note -- The program does not do anything with these strings!!
The example below is for instructional purposes only.
main()
{
int numrows,numcols;
cout << "how many rows?";
cin >> numrows;
char* *arr = new char * [numrows];
//
//
//
//
//
This is the key line
It sets up "arr" to be a pointer to
"numrows" rows of pointers to char (i.e. strings)
The Length of each of these strings is as yet undetermined.
So now arr is an array of pointers dynamically sized to numrows.
//
Now we are ready to read in the strings
cout << "what's the maxsize of each string";
cin >> numcols;
for (int i=0; i<=numrows-1;i++)
{
cout << "enter string";
arr[i] = new char [numcols];
//
//
//
//
//
The second key line.
Notice the alternate syntax here:
we do not write: char arr[i] = new char [numcols]
because arr[i] has already been defined.
We now simply allocate space for it.
//
//
//
Note that we could have (by putting the numcols request inside the
loop) made each string a different dynamic length
which is sometimes known as a "ragged array".
//
//
//
//
//
Also note, that there is NO way to dynamically allocate the
2d-array "all at once".
C++ has no feature to define a type dynamically.
We cannot first define an arbitrary length array of char as a TYPE
and then use that new TYPE to dynamically declare a 2-d array.
cin >> arr[i];
// Of course you can use getline, if needed.
}
}
2. Games of Chance and Simulations (*)
Sometimes the easiest way for a casino to analyze whether or not a particular gambling
game is in its favor or not, is to simulate the game by computer and analyze the results.
The analytical alternative of rigorous mathematical analysis is not always easy or
practical.
The game of CRAPS is played by a player spinning two dice repeatedly until the player
wins or loses. If the player spins a 7 or 11, she wins immediately and the game is over.
If she spins a 2, 3 or 12, she loses immediately and the game is over. Otherwise, her spin
is remembered and is called her point. This point stays fixed for the rest of the game. If
she subsequently spins her point again before she spins a 7, then she wins. If she spins a
7 before she spins her point again, she loses. The number 7 is called the “point breaker”.
Once a point is made, the numbers 2, 3 ,11 and 12 are irrelevant. Only the point and the
point breaker are considered.
a. Write a function that plays an arbitrary variation of CRAPS, and returns 1 if the player
wins or 0 if the player loses. By a variation, we mean that the numbers for winning and
losing can be changed, but the rest of the game is the same. The point breaker should be
the smallest of the winning spins (e.g. 7 in the normal game). The input is in the form of
an array A of char of size 11. Each character can be W for win, L for lose, or N for
neither. A[i] (i ranges from 0 to 10) indicates whether the value i + 2 is an immediate
win or an immediate loss or neither.
b. Write a main function that reads in variations for CRAPS and simulates 100 games of
the variation and reports the results. Try your function on the standard variation and on
the variation where 3 is no longer an immediate loss.
3. Functions with Pointer Parameters
a. In a file called Linked.h, write declarations for the following functions and data
structures. In a file called Linked.cpp, write the definitions for these functions and data
structures.
i. A linked list structure of student. The data structure student should have 4
fields: a string of max length 25 for the name, an integer id, a float gpa, and a character
(m/f) for gender.
ii. A function to read in a list.
iii. A function to print out a list.
iv. A function that takes a list and returns the number of nodes in it.
v. A function that calculates the average gpa of all the persons in a list.
vi. A function that takes two lists and returns TRUE when they are equal and
FALSE otherwise. For our purposes, we consider two lists to be “equal” when they
contain the same names in the same order. (What are other options?)
b. Write a main function that includes Linked.h, which reads in a list of students, prints
out the number of students, prints the average gpa of the students, and prints the names of
the students in reverse order from that in which they were input.
c. Compile Linked.cpp,
d. Compile the main function and link and run it with the compiled Linked.cpp.
Note on Interface Versus Implementation:
If you have trouble figuring out how to separately compile and link C++ files, then this
problem can also be done by including both Linked.h and Linked.cpp in main, and
compiling and running main all at once.
This problem emphasizes the distinction of the declaration of functions and data
structures from the definition of functions and data structures. The idea is to separate the
interface from the implementation. The problem foreshadows the notion of classes in the
next chapter where the interface and implementation of an object are clearly
distinguished.