CS449, Introduction to Systems Software, Spring 2007

TA: Ricardo Villamarín-Salomón

Lab #2

Exercise 1: Floating point types and typedef

1.  Compile and run the program shown in Listing 1 (below). What happens with the approximation of ex as x grows large?

2.  Change the program and increase SERIES_LENGTH to 50, compile and run it and see what happens now.

3.  Change the typedef to typedef double floating; compile and run and see what happens now.

4.  Increase SERIES_LENGTH to 300, compile, run and see what happens.

5.  Change the typedef to typedef long double floating; compile and run and see what happens now (don’t forget to change the printf modifier to use %Lf not %f to print expo);

Listing 1

/* compile with -lm */
/* i.e., gcc -lm -o <outfile> <srcfile>.c*/
#include <stdio.h>
#include <math.h>
#define SERIES_LENGTH 30
#define ITERATIONS 17
#define EULER 2.71828182845904523536
typedef float floating;
floating iter_expo(floating n, int expn)
{
int k;
floating result = 1.0;
for (k=1; k<=expn; k++)
result *= n;
return result;
}
floating factorial(int n)
{
floating fact = 1;
if (n==0 || n==1)
return fact;
while (n>1)
{
fact = fact*n;
n = n - 1;
}
return fact;
}
floating eexp(int x)
{
int j = 0;
floating eexp_res = 0;
//x^0/0! + x^1/1! + x^2/2! + x^3/3!
for (j=0; j<=SERIES_LENGTH; j++)
{
eexp_res += iter_expo(x, j) / factorial(j);
}
return eexp_res;
}
int main()
{
int x;
floating expo = 0;
for (x=0; x<ITERATIONS; x++)
{
expo = eexp(x);
printf("%d: %f, %f\n", x, expo, pow(EULER, x) );
}
return 0;
}

Exercise 2: Using enum and typedef

The program in Listing 2 (below) simulates a game for two players (the computer plays on behalf of both). It uses #define for ROCK, SCISSORS and PAPER for the moves, and the literals 0, 1 and 2 to select a winner.

1.  Modify the program, so that it uses the following enums instead of the constants/literals mentioned above:
enum MOVE {ROCK, SCISSORS, PAPER};
enum WINNER {NONE, PLAYER1, PLAYER2};
Note that you also have to use the enum you defined above for the functions (parameters, return type) and variables (e.g., choice1, choice2 or victor) that used the constants/literals.

2.  Modify your program of the previous point so that it uses these typedef instead:
typedef enum {NONE, PLAYER1, PLAYER2} WINNER;
typedef enum {ROCK, SCISSORS, PAPER} MOVE;

3.  Modify the program (any of the versions) and include a function that asks the user for her/his move (in uppercase: ROCK, SCISSORS or PAPER). After the computer (player 1) makes his move, ask the user for his choice (until he enters a valid choice) and then determine the winner (with getWinner). You need to know the following:

·  A simple way (but not very robust) to enter a string is:
char usrInput[20]; // you can increase the limit
scanf("%s", usrInput); // enter without spaces

·  To compare two strings, use the function strcmp (you have to include string.h to make it work) that returns 0 if two strings are the same (distinguishes lower and uppercase, so CS449 is different from cs449). For example:
if (strcmp(usrInput, "ILOVEC")==0) printf(“You love C!”);

Listing 2

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROCK 0
#define SCISSORS 1
#define PAPER 2
char *moveNames[] = {"Rock", "Scissors", "Paper"};
int getWinner(int choice1, int choice2);
int getComputerMove( void );
void printChoice( int turn, int choice );
int main()
{
int choice1, choice2;
int victor;
srand( time( NULL ) );
choice1 = getComputerMove();
printChoice(1, choice1);
choice2 = getComputerMove();
printChoice(2, choice2);
victor = getWinner(choice1, choice2);
if (victor == 0)
printf("This game was a tie!\n");
else
printf("Winner is player: %d\n", victor);
return 0;
}
int getWinner(int choice1, int choice2)
{
if (choice1 == choice2)
return 0;
switch(choice1)
{
case ROCK:
return (choice2 == SCISSORS ? 1 : 2);
break;
case SCISSORS:
return (choice2 == PAPER ? 1 : 2);
break;
case PAPER:
return (choice2 == ROCK ? 1 : 2);
break;
}
}
int getComputerMove( void )
{
int result = ( rand() % 3 );
return result;
}
void printChoice( int turn, int choice )
{
printf("Choice of player #%d is: %s\n", turn, moveNames[choice]);
}

Exercise 3: Arrays and Functions

You have been given the results of the games (regular season only) of four teams that are candidates for the Super Bowl XLI. Table 1 shows the results where:

·  P+: the points scored by the team in that game

·  P-: the points scored by its rival in that game (name not shown)

·  A row with a -1 indicates a week when the team did not play.

Table 1

Week / New Orleans Saints / Chicago Bears / Indianapolis Colts / New England Patriots
P+ / P- / P+ / P- / P+ / P- / P+ / P-
1 / 19 / 14 / 26 / 0 / 26 / 21 / 19 / 17
2 / 34 / 27 / 34 / 7 / 43 / 24 / 24 / 17
3 / 23 / 3 / 19 / 16 / 21 / 14 / 7 / 17
4 / 18 / 21 / 37 / 6 / 31 / 28 / 38 / 13
5 / 24 / 21 / 40 / 7 / 14 / 13 / 20 / 10
6 / 27 / 24 / 24 / 23 / -1 / -1 / -1 / -1
7 / -1 / -1 / -1 / -1 / 36 / 22 / 28 / 6
8 / 22 / 35 / 41 / 10 / 34 / 31 / 31 / 7
9 / 31 / 14 / 13 / 31 / 27 / 20 / 20 / 27
10 / 31 / 38 / 38 / 20 / 17 / 16 / 14 / 17
11 / 16 / 31 / 10 / 0 / 14 / 21 / 35 / 0
12 / 31 / 13 / 13 / 17 / 45 / 21 / 17 / 13
13 / 34 / 10 / 23 / 13 / 17 / 20 / 28 / 21
14 / 42 / 17 / 42 / 27 / 17 / 44 / 0 / 21
15 / 10 / 17 / 34 / 31 / 34 / 16 / 40 / 7
16 / 30 / 7 / 26 / 21 / 24 / 27 / 24 / 21
17 / 21 / 31 / 7 / 26 / 27 / 22 / 40 / 23

Create a program that allows you to estimate the chances of every team to win the Superbowl:

1.  Calculate a ranking for every team using the following formula:

Where

·  games_won: the number of games that the specific team won in the season (i.e., the games where P+P-). For example, the Chicago Bears won the game in week #1, but lost in week #17.

·  sum_effective_points: the sum of the differences between P+ and P- for all the games played by the specific team during the season. For example, sum_effective_points for the New England Patriots during the first two weeks only is: (19-27) + (24-17) = 9.

2.  After computing TeamRank for every team, print the names of the two teams with the highest TeamRank. The team with the highest TeamRank is the most likely winner of the Super Bowl XLI (do you believe it? J)

You can start with the code in Listing 3. To calculate TeamRank, use a function that accepts two arrays of a team and returns the respective rank.

Listing 3

#include <stdio.h>
#define NUM_WEEKS 17
#define NUM_TEAMS 4
int main ( )
{
// NOS = New Orleans Saints
// CB = Chicago Bears
// etc.
// PP = P+, PN = P-
int NOS_PP = {19,34,23,18,24,27,-1,22,31,31,16,31,34,42,10,30,21};
int NOS_PN = {14,27,3,21,21,24,-1,35,14,38,31,13,10,17,17,7,31};
int CB_PP = {26,34,19,37,40,24,-1,41,13,38,10,13,23,42,34,26,7};
int CB_PN = {0,7,16,6,7,23,-1,10,31,20,0,17,13,27,31,21,26};
int IC_PP = {26,43,21,31,14,-1,36,34,27,17,14,45,17,17,34,24,27};
int IC_PN = {21,24,14,28,13,-1,22,31,20,16,21,21,20,44,16,27,22};
int NEP_PP = {19,24,7,38,20,-1,28,31,20,14,35,17,28,0,40,24,40};
int NEP_PN = {17,17,17,13,10,-1,6,7,27,17,0,13,21,21,7,21,23};
return 0;
}

You need to know this regarding arrays:

Syntax for declaring an array:
TYPE ARRLENGTH[ARRAYLENGTH] ;
For example, the following code declares an array of 5 integers
int arrIntegers[5];
Declaring an initialized array of chars:
char charArr[3] = {‘a’, ‘b’, ‘c’};
Accessing elements of an array:
char First = charArr[0]; // first element
Modifying elements of an array:
charArr[2] = ‘d’; // replaces ‘c’ in the example above
Iterating over the elements of an array:
int i;
for (i=0; i<3; i++)
printf(“Element %d is %c”, i, charArr[i]);
Summing elements of an array
// Initialize the elements of arrIntegers before using this code
int i, sum = 0;
for (i=0; i<5; i++)
sum = sum + arrIntegers[i]
printf(“Sum is: %d”, sum);
Prototype of a function that receives an array of chars and returns an integer
int funcarr(char arr[]);

-3/7-