CS 100 & CS 102
CS 102 is a very different course from
CS 100: CS 102 is the entry course for computer science majors,
and it's a programming-intensive course. Perhaps think 15 weeks
of Python rather than the 5-6 weeks you see in 100. The language
used in CS 102 is C++, which is a variation of the C programming
language. We'll take a look at a CS 102 programming assignments
and how we might do these in Python. Unlike CS 100, most CS 102
assignments cannot be finished in a 2-hour lab. Students must
take considerable additional time to do these.
The following is one of the assignemnts in lab 2 for CS 102.
Fantasy sports leagues let participants draft players in football,
baseball, basketball, or whatever. A person gets points depending
on how well his/her players do in terms of statistics. So here, a
person is part of a fantasy football league, and has drafted players
from the NFL. Points are received for a player's weekly
performance:
....You get 1 point for every 10 yards a player rushes--e.g. 44 yards
nets you 4 pts. Anything less than 0 is counted as 0 yards.
....You get 1 point for every 5 receiving passes yards. No
negative points.
....You get 1 point for every 20 passing yards (i.e.
quarterback). No negative points.
....You get 6 points for every touchdown your player scores, rushing,
receiving, or passing.
....You lose 2 points for each turnover by your player.
WRITE A PROGRAM that reads in a player's name and stats, and prints out
how many points the player generates for you. For example, Snarf
had 12 yards rushing, no receiving yards, 160 passing yards, no
touchdowns (intercepted passes returned for touchdowns by the opponents
don't count), and 8 turnovers (in Snarf's case, 3 fumbles and 5
interceptions). Snarf's points: 1 pt for rushing, 0 for
receiving, 8 for passing, 0 for touchdowns, and -16 pts for
turnovers. Snarf has generated -7 points for your team. Not
so good!
#fantasy football points calculator
playername = raw_input("player name:")
rushyds = input("rushing yards:")
if rushyds < 0:
rushyds =
0 #remember--no negative points for
rushing.
recvyds = input("receiving yards:")
if recvyds < 0:
recvyds = 0
passyds = input("passing yards:")
if passyds < 0:
passyds = 0
touchdowns = input("touchdowns:")
turnovers = input("turnovers:")
points = rushyds/10 + recvyds/5 + passyds/20 + touchdowns
*6 -turnovers * 2
print "your player generated", points, "points"
Not so hard?
...........................................
The program written in the C programming language appears below.
Comments here are enclosed in /*...*/'s
#include <stdio.h> /* this is like the import in Python--it
brings in an input/output library */
main() { /*the start of the
program */
int rushyds, recvyds, passyds, touchdowns, turnovers,
points;
/* in C, most statements end with a ;
*/
/* in C, unlike Python, variables must
be DECLARED as to what type of value they hold--integer here */
char name[30]; /* a character array/string to
hold the player's name */
printf("player name:"); /* input and output are separate in
C */
scanf(("%s", name); /*%s for string--reads in name*/
printf("rushing yards:");
scanf("%d",&rushyds); /* %d for integer input.
reads in rushing yds */
if (rushyds < 0) rushyds = 0; /*C's version of the if */
printf("receiving yards:");
scanf("%d",&recvyds);
if (recvyds < 0) recvyds = 0;
printf("passing yards:");
scanf("%d",&passyds);
if (passyds < 0) passyds = 0;
printf("touchdowns:");
scanf("%d",&touchdowns);
printf("turnovers:");
scanf("%d",&turnovers);
points = rushyds/10 + recvyds/5 + passyds/20 + touchdowns *6 -turnovers
*2;
printf("your player generated %d points", points);
}
brackets {}'s enclose the C program. C isn't fussy about
indentation, but C is fussy about ;'s