C-ohjelmointi : Lecture Schedule, Exercises and Programming Projects

 Weekly Schedule and Exercises

You can get help for the exercises in programminig workshops (paja) in rooms  B221, B121, CK110  on Tuesdays 12-15, Wednesdays 14-19, and Thurdays 12-18.

Introduction to NetBeans programming environment.  It is also assumed that everybody has some kind of a complete C book available.  The lecture notes do not cover all the aspect of the language.

Week 1: Comparison of C and Java. First example of a C program. Variables and data.  Control structures. Basic input and output.

Exercise 1.

Solutions to the tasks in Exercise 1 must be shown in Paja not later than Thursday, September 8, 6 pm.

Week 2Using memory in C, undefined behaviour in C. Lecture by Jylhä-Ollila. Supplementary reading about pointers: MIT5. Also links to Stanford lecture1 and lecture2.

Exercise 2. Obs! You can still show your solutions on Tuesday.  (Remember that you can find information about a standard C library
function by writing man 3 function_name in the terminal, or by going to the address: http://linux.die.net/man/3/function_name.) Solutions.
 

Week 3: Input and output with files, see  Basic input and output of the first week. Review and User defined datatypes: MIT6.     Some security issues (in Finnish).                                                                                   Example-1.     Example-2.   Example-3.  Example-4.  Example-5.  Example-6.

Exercise 3. (Some hints added September 17, 7:30 a.m.  Correction to the task 14, September 18, 23:27 p.m.)  Deadline for the solutions is Tuesday, September 27.

Week 4Bitwise operations.

Exercise 4. (Deadline Tuesday, October 4.)  Attachment-1. (Correction to Attachment-1: There is one extra transition in the adjacency list of state 4. Take away the transition with 2.) Solutions.

Week 5:   More about pointers. Macros

Exercise 5. (Deadline Tuesday, October 11.) Notice the new times for C instructors in paja: Mon 17-19, Tue 12-18, Wed 16-19, Thu 12-15.

Week 6:. About numerical calculations, C operators and C libraries.

 Exercise 6.(Minor corrections to exercises 6, 8, 9, October 8.)  Deadline is Friday, October 14.  C instructors are in paja October 10-14: Mon 17-19, Tue 12-18, Wed 16-19, Thu 12-15, Fri 10-16. 

 Programming Projects

 

The programming project can be done in groups of 2-3 students. Individual work is also possible. Choose one topic from the list below not later than Friday,

September 30. Send email of your choice to Timo Karvi. In your mail, say the topic of the project and the names participating in the project.

Instructions for the project can be found on the additional course page Programming Project.

 OBS: New suggestions for data structures in the task 3!

Topics:

1) Ckone: TTK-91 simulaattori

Ckone simuloi ttk-91 arkkitehtuurin suoritusta ja sillä saadaan samat lopputulokset kuin esim. Titokoneella. Ckoneessa ei ole Titokoneen debugger ympäristöä, assembleria, animaattoria ta graafista käyttöliittymää. Ckoneessa on lataaja.

Titokone tuottaa yksinkertaistettua "binääriä" (tiedostotyyppi .b91), jossa kukin konekäsky on esitetty kokonaislukuna. Tämä on syötetiedosto Ckoneelle. Katso esimerkkiä alla. Omista ttk-91 ohjelmista saa Ckoneen syötetiedostoja kääntämällä ne Titokoneella.

Simuloinnin lopuksi vedostetaan muisti ja rekisterit, josta näkee osaltaan ohjelman toimivuuden.

Lopputulosta voi myös vertailla Titokoneella saatuun tulokseen.

-- lähdekoodi ---
; sum - laske annettuja lukuja yhteen kunnes nolla annettu
Luku DC 0 ; nykyinen luku
Summa DC 0 ; nykyinen summa
Sum IN R1, =KBD ; ohjelma alkaa käskystä 0
STORE R1, Luku
JZER R1, Done ; luvut loppu?
LOAD R1, Summa ; Summa <- Summa+Luku
ADD R1, Luku
STORE R1, Summa ; summa muuttujassa, ei rekisterissa?
JUMP Sum
Done LOAD R1, Summa ; tulosta summa ja lopeta
OUT R1, =CRT
SVC SP, =HALT

-- titokoneen tuottama "binääri"
___b91___
___code___
0 9
52428801
18874378
572522503
36175883
287834122
18874379
536870912
36175883
69206016
1891631115
___data___
10 11
0
0
___symboltable___
sum 0
summa 11
kbd 1
done 7
halt 11
crt 0
luku 10
___end___

2) Sana- ja virkelaskuri

Tee ohjelma, joka lukee yhden tekstitiedoston. Käyttäjän pitää voida valita tiedoston syöttötapa. Hän voi joko antaa tiedoston nimen komentoriviparametrina tai ohjelma voi olettaa tiedon tulevan stdin-tiedostosta.

Ohjelman pitää tulostaa tiedostosta löytyneet sanat (vähintään kaksi peräkkäistä kirjainmerkkiä) ja niiden esiintymisten lukumäärät. Kukin sana ja sen esiintymisten lukumäärä tulostetaan omalle rivilleen. Lisäksi tulostetaan kuinka monta virkettä esiintyi tekstissä ja jakauma siitä miten monta sanaa virkkeisiin kuului. Tulostiedoston nimi käsitellään samoin kuin syöttötiedoston.

Käytä ratkaisussasi tietorakenteena trie:tä, hajautustaulua tai tasapainotettua binääripuuta.

 

In English:

Write a program that reads one text file. A user can choose how he or she gives the name of the text file. He can give it either as a command line parameter or the program can assume that the name comes from the stdin file.

The program must output the words (at least two sequental letters) and the number of every word. Every line contains just one word and its number.  In addition,  the program prints the number of sentences and the distribution how many words the sentences contain. The name of the output file is given either as a command line parameter or it is read from stdin. 

Use tries, hashtables or balanced binary trees as your data structure.

3)  Sukupuu

Tee ohjelma, jonka avulla käyttäjä voi tallentaa sukupuun. Sukupuuhun voi tallettaa tietoja henkilöistä (nimi, syntymäaika, kuolinaika), näiden välisistä suhteista (sekä viralliset avioliitot että epäviralliset suhteet - jälkimmäiset halutaan tallettaa esim. sen vuoksi, että voidaan helposti pitää kirjaa lasten vanhemmista) ja suhteista syntyneistä lapsista. Yksinkertaisuuden vuoksi voit tehdä järjestelmän sellaiseksi, että jokaisella henkilöllä on eri nimi.

Ohjelmasi pitää tarjota ainakin seuraavat toiminnot:

  • henkilön lisäys (henkilö voidaan lisätä joko "itsenäisenä" tai joidenkin lapsena)
  • henkilön tietojen muutos
  • suhteen lisäys
  • annetun henkilön kaikkien jälkeläisten tulostus
  • annetun henkilön kaikkien esivanhempien tulostus
  • annetun henkilön kaikkien rekisteröityjen suhteiden tulostus.

Ohjelmasi voi luottaa syötettyjen tietojen järkevyyteen. Sen ei tarvitse esim. tarkistaa, että joku ei ole naimisissa kahden henkilön kanssa yhtä aikaa tai että jonkun lapsi ei ole vanhempi kuin henkilö itse.

Ohjelmasi täytyy käyttää dynaamista muistinvarausta - vain taulukoihin perustuvia ratkaisuja ei hyväksytä.

 

In English:

Write a program that saves a family tree. A family tree contains a name,  year of birth, year of death, and official relations or unofficial relations between the people in the family tree (unofficial relations are neede because we want to know the parents of every person).  You can assume that every person has a different name.

Your program should offer at least the following services:

  • adding a person,
  • changing the data of a person,
  • adding a relation,
  • printing all the descendats of a person,
  • printing all the ancestors of a person,
  • printing all the registered relations of a person.

The program trusts on the validity of the data. For example, it is not necessary to check that a person has only one spouse or that child is younger than the parents.

Your program should use dynamical memory reservation. Your program is not accepted, if it uses only arrays.

 

4)  Lämmön johtuminen

Stationaarisen tilan lämmönjohtumisyhtälö kahdessa ulottuvuudessa on

 

d2T(x,y) /dx2 + d2T(x,y)/dy2 = 0.

Sen ratkaisu antaa lämpötilan tasapainojakauman mielivaltaisessa kaksiulotteisessa kappaleessa, kun reuna-ehdot on tunnettu. Yhtälön voi ratkaista numeerisesti hyvin yksinkertaisesti seuraavalla tavalla. Jaa haluttu tila neliölliseen hilaan. Tee mikä tahansa arvaus ratkaisulle T0(x,y), missä x ja y ovat nyt diskreettejä pisteitä neliöllisessä hilassa. Sen jälkeen iteroi ratkaisua siten, että jokaisessa pisteessä uuden iteraatio-askeleen i+1 uusi arvo Ti+1 lasketaan kaavalla

 

Ti+1(x,y) = 1/4 (Ti(x+1,y) + Ti(x-1,y) + Ti(x,y+1) + Ti(x,y-1)

eli yksinkertaisesti keskiarvona neljän lähimmän naapuripisteen lämpötilasta edellisellä iteraatio-askeleella. Kirjoita ohjelma, joka ratkaisee lämmönjohtumisyhtälön kahdessa ulottuvuudessa suorakulmaisessa kappaleessa, jonka korkeus on 2 m ja leveys 1 m. Suorakulmion pohjalla ja sivuilla lämpötila on 1000 K, mutta sen yläreunalla 0 K. Mikä on keskilämpötila kappaleessa, kun tasapaino on saavutettu?