[Note: this webpage last modified Monday, 29-Apr-2013 23:23:22 EDT]
... char s[100] = "hi"; int i; for(i=0;i<3;i++) printf("%d\n", s[i]);
... int nums[5] = {5, 4, 3, 2, 1}; int i; for(i=4; i>=0; i--) { printf("%d\n", nums[i]); i = i / 2; }
... int x, y, *z; x = y = 1; z = &x; *z += 1; y = *z * x; printf("%d, %d, %d\n", x, y, *z);
... char s[100]="hi", *ch; ch = s; printf("%c, %c\n", ch[0], ch[1]); printf("%d\n", ch == s);
... char s[100] = "this is the song that ends."; int i; for(i=0;i<strlen(s);i++) { s[i] = s[i] - 1; if (s[i] == 'a'-1) s[i] = 'a'; toupper(s[i]); //s[i] = toupper(s[i]); if (s[i] == ' ') i += 1; } printf("%s\n", s);
... int *fun(int *x, int *y) { if (x == y) return y; else if (*x < *y) return x; return y; } int main(int argc, char *argv[]) { int i=2, j=3, x; x = *fun(&j,&i); printf("x = %d\n", x); return 0; }
answer: well, you can run it to see what happens. fun takes two pointers as parameters and returns one of them. If the numbers they point to have the first one smaller, then the first pointer is returned. When this is called in main, the values are 3 and 2. 3 is not smaller than 2, so the second pointer is returned, which is &i. x is set to * of that address, meaning the value of i, which is 2. This means the print statement prints 2.
... FILE *myFile = fopen("blah.txt", "w"); char s[100]=""; int i; for(i=0;i<3;i++) { s[strlen(s)+1] = '\0'; s[strlen(s)] = 'a'; fprintf(myFile, "%s\n", s); } fclose(myFile); myFile = fopen("blah.txt", "r"); char ch = fgetc(myFile); int count=0; while (ch != EOF) { if (ch == '\n') count += 1; ch = fgetc(myFile); } fclose(myFile); fprintf("count = %d\n", count);
answer: The first for loop prints the following into blah.txt
a aa aaaThe while loop then reads every character from blah.txt and counts the number of new line characters. There was one for each iteration of the for loop, so 3. That means count will be 3.
... int f(int x, int y, int n) { int temp; for( ; n > 0; n--) { temp = y; y = x + y; x = temp; } return y; } int main(int argc, char *argv[]) { int i = f(0,1,3); i = f(i,i,3); printf("%d\n", i); return 0; }
Rules.
Can work with a partner or alone. Only groups of 2 are allowed, no groups of 3. Groups will be required to turn in something saying how much work each person did, and that will influence each person's grade.
Check in with me each week about what you need to do on your project that week. These will count for HW grades.
First checkin is this week - come talk to me about what you plan to do for your project. We'll come up with intermediate goals to work towards.
Final version of your code is due by May 4 (end of exam week).
You will give a ~ 5 minute presentation of your project during the last week of classes or exam week.
Final version of your code must use: multiple functions, multiple loops, user input/output, arrays, use of files.
You can pick from the ideas listed below. If you want more description about an idea, then ask. If you have some other idea you want to do, you have to get it approved by me first.
Project choices.
Encryption/decryption. Possibilities: Caesar/keyword/permutation cipher, Vigenere, scytale, playfair, AES, RSA.
Cryptanalysis. Breaking any of the preceding.
Ncurses game. Possibilities: snake/worm/centipede, asteroids/astrosmash, maze/adventure.
Equation grapher. Graph equations involving polynomials, sin, cos, tan, ...
Line drawings. Save a bunch of lines as a bitmap image.
Equation solver. Using Newton's method.
Eliza/doctor/chat-bot.
Web search/crawl.
Ncurses text editor.
Shell. Make your own shell, implement your own commands, login stuff, ...
Text search. Search a text file for (blank), count number of times (blank), ...
Ncurses planetary simulator.
Scratch game/project. Possibilities: ISU adventure, ISU tour/map, typing/spelling/math tutor/game, .
Computing digits of pi, e, or other numbers.
Prime numbers sieve.
Card games: poker, go fish, pokemon, ... .
Board games: monopoly, scrabble, chess, ... .
Factoring algorithms: trial division, ...
Typing tutor/game.
Spelling tutor/game.
Math/arthmetic tutor/game.
Topics. The exam covers everything we have done so far in terms of C programming and algorithms.
Topics that were on the first exam that are NOT on this exam include: assembly language programming, converting between binary/decimal/hexadecimal. Shell commands (cp, mv, etc.) will not be tested per se (there won't be questions where you get points just for knowing how to use the shell/terminal), but you'll need to be able to do those things to do the programming parts on the computer.
Note that the schedule has on it a few things we have not done yet (file input/output, malloc/free, time/clock, struct, enum, typedef, union). Those will not be on this test.
Okay then. The areas you should focus on for the test are...
if (grade >= 80 && grade < 90) { ...}
Exam format. Same as the last time - half on paper, half on the computer.
Practice questions.
// #includes ... void okay(int num, int *x, int z[]) { int i, total=1; for(i=0; i<num; i++) { z[i] = total * (i+1); total = total * (i+1); } *x = z[num-1]; } int main(int argc, char *argv[]) { int numbers[4] = {0, 0, 0, 0}; int howMany = 4, result = 0; okay(howMany, &result, numbers); printf("%d, %d\n", result, numbers[0]); }
// #includes ... int smallestBiggerThan(int numbers[], int howMany, int what) { int smallSoFar = numbers[0]; int i; for(i=0;i<howMany; i++) { if (numbers[i] < smallSoFar && numbers[i] > what) smallSoFar = numbers[i]; } return smallSoFar; } int main(int argc, char *argv[]) { int nums[4] = {5, 3, 7, 2}; int smallest = smallestBiggerThan(nums, 4, 0); int second = smallestBiggerThan(nums, 4, smallest); printf("%d and %d\n", smallest, second); }
* ** *** **** *** ** *
Topics. The exam covers everything we have done so far. In particular, focus on the following as you study.
Exam format. The exam will be in two parts - one on paper and one on the computer. Each of the practice questions is labeled as either on paper or on computer. For the on computer problems, you will have a terminal/putty, but there will be no internet access.
Practice questions.
On paper.
Convert between decimal, binary, and hexadecimal. Use a calculator
that has these choices or Wolfram alpha to test yourself. Examples...
Convert to binary: 99 decimal, FA hex.
Convert to decimal: 101011 binary, 8E7 hex.
Convert to binary: 156 decimal, 60B hex.
On paper. What is the largest number that can be represented with: 2 bytes, 2 hex digits, 4 decimal digits, 7 bits?
On paper.
Given assembly code what does it do? Examples...
setmem 98 5 load 3 98 add 3 3 mult 3 3 cpreg 1 3 store 1 90 print 90 halt
setmem 98 4 load 1 98 setmem 97 1 load 2 97 cpreg 3 1 cpreg 4 1 sub 4 2 sub 4 3 store 4 97 print 97 halt
// complete so the code adds up 11+13+17+19 and prints the result. store 99 11 load 1 99 store 99 13 load 2 99 store 99 17 load 3 99 store 99 19 load 4 99 add 1 2 add 1 3 // insert instruction to finish adding values... // insert two instructions to store and print the final answer.
On paper. Given some C code, what is the output and what happens to the variables. You would list the different values the variables had as the code ran, and would say what gets printed to the screen. Examples...
// usual #include and int main stuff... inside of main is... int x = 3; int y = x * 5; int z; z = y + y; y = y - y; x = x / 10; printf("%d, %d, %d\n", z, y, x);
// usual #include and int main stuff... inside of main is... float x, y, z; x = 3 / 6; y = x * x; z = x - y; if (z < y) printf("%d\n", z); else printf("%d\n", y);
On computer. Given some C code that has a variety of syntax errors, you should be able to fix them so it works correctly. Examples...
#include<stdio.h> #include<stdlib.h> int main(int argc, char *argv[]) { char choice printf("Even or odd? Type E or O. "); scanf(%c, &choice); if (choice = 'E') printf("2 is even.\n"); else printf('1 is odd.\n'); return 0; }
#include<stdio.h> #include<stdlib.h> float feetToMeters(float num) float x; x = num*.914 \ 3.0; return x; int main(int argc, char *argv[]) { printf("10 feet is %d meters.\n", feetToMeters 10); return 0; }
On computer. Be able to use the shell/putty commands to do things.
Examples...
Create a directory called examPractice in your home directory, and
copy your hw5 solution there.
Go to the directory /net/web/dept. How many .html files are in that
directory?
Copy the degrees.c file from the in class code. Put the copy in
your examPractice directory, and make it named degreeCopy.c.
Go to the / directory. What directories do you see?
On paper. Questions showing basic understanding of how the computer
works, what assembly language is, how programming works, etc.
Examples...
List strengths and weaknesses of hard drives versus RAM memory.
What are the main steps in creating a program that one of your friends
would be able to use?
On computer. Be able to take one of our examples from class and
make some basic changes to it. Examples...
Take the degrees.c
program, and make it so it prints the message "That's really cold"
if the user enters a temperature less than 0 Celsius or 32
Fahrenheit.
Take the adding.151asm program and have it add up
the numbers from -100 to 200.
Take the add.c program and make it
test 30 through 39 for whether they are prime or not.
Installing Scratch. We will have one or two assignments using a simple programming system called Scratch. Install it by going to Scratch Download.
Installing C. Either just use gcc by logging into the CS server using putty and your cs151xx account, or install a C compiler on your computer. If you use Linux or Mac, it is likely installed already. If you use Windows, you can install Cygwin.
It is absolutely required that you have access to a working computer with C installed (or a computer with Putty installed and an internet connection) that you have access to outside of class time. The biggest factor in your success in the course is how much time you spend programming outside of class.
Installing Putty and FileZilla. To install the FileZilla ftp client, go to FileZilla. To work on your assignments on the CS server, you must install Putty if you are using Windows. If you are using Linux or Mac, you can just use ssh from the terminal/shell.
cs151xx login to CS server. You will turn your assignments in by uploading your finished assignment to the Computer Science server. I have administrator access to this server and can easily grade your homeworks once they are on the server. You will receive an email giving you a username and login to the CS server. The username will be cs151xx, where xx is just a number - so the "first" student in the class will get cs15100 as their login, etc. You use either FileZilla or WinSCP with your CS login/password to upload your homework files to the CS server. This is demonstrated in the first lecture. That lecture has been recorded and is available to view in blackboard - go to blackboard for this course, and click "Tegrity Classes".
Emacs If you are programming by logging into the CS server or using Cygwin to edit your files, you have some choice about what text editor to use. Those installed on the CS server include: vi, pico, nano, emacs. If you want to use a strictly Windows text editor, you might try Notepad++. I use emacs because it does auto-indenting and other things. If you decide to use emacs, you'll need to learn the shortcuts for things. The ones I use most are the following (search online for others).
Basic idea: create a "significant" program that uses most of the key programming features we have learned in class. You can choose what the program does, but your choice must be approved by Jeff.
Rules:
May work alone or with one partner. If working together, you will include at each stage a description of what was done by each person.
Final code must use: well-chosen functions, loops, structures, user input and output, file input and output.
The homework assignments are posted in the in class code. hw1.c contains the first homework assignment, hw2.c is the second homework assignment, etc. I will announce in class and/or by email when a new assignment has been posted. Each homework assignment will list the date the assignment is due. Late assignments will not receive any credit; I will grade them just so you know how you did.
To complete a homework assignment, (1) download the assignment from the in class code, (2) make changes to the file provided to complete the assignement, (3) upload your completed assignment to the CS server before the due date. To download the file, you should browse to the in class code on the course website, right-click, and choose "Save as" or "Save target as" (depending on your browser) to save the file to your computer. For each assignment, you must download the template file to use as a starting point, make changes to this file, and then upload this file. If you do not turn in your homework in this way, I will take off points! DO NOT copy/paste into a word document, or send an email with your assignment. DO NOT change the name of the homework file - keep the name exactly the same as on the class website.
For each hw assignment, I will post a Tegrity video where I describe the hw assignment and show how my correct solution works. You will need to watch this video on your own time. The reason I do this is to FORCE you to spend some time outside of class looking at the homeworks.