Tentamen i DD2387 Programsystemkonstruktion med C++ Resultat:
Datum: Torsdag 8 januari 2009, 14-18 id:
Hjälpmedel: En eller två valfria läroböcker om C++
Tid: 4 timmar
Skriv inte ditt namn på tentan. Under tentan kommer ett id-nummer att delas ut och skrivas på tentan. An- teckna ditt id-nummer på separat papper. När du lämnar in tentan ska du säga vad du heter till tentavakten så att denne kan göra en extrakontroll av idnummret.
Tentan rättas i en första omgång två och två av er själva tio minuter efter tentans slut. Du måste komma tillbaka för att rätta! Skriv svaren direkt på tentan. Om du inte avser få tentan rättad kan du lämna in blankt.
Grova fel ger underkänt (betyg F). Om inte alltför många grova fel görs ges möjlighet att komplettera till godkänt betyg(betygU* eller FX) mer information om kompletteringkommer att stå på kurshemsidan. Gör ett försök på alla uppgifter. Tentaresultat anslås inte längre på anslagstavla i enlighet med ny policy från KTH centralt. Lycka till!
1. Udda figurer
(a) Skriv en typparametriserad funktion udda som tar två iteratorer som parameter och returnerar alla udda tal i intervallet.
(b) Vad returnerar din funktion?
(c) En arbetsgivare tycker din funktion verkar jättebra för att hålla koll på alla udda figurer i företaget.
Han tänker anropa din funktion med v.begin() och v.end() där v är en vektor av arbetstagare
2. Virtuellt
(a) Vad skriver programmet nedan ut när man kör det?
(b) Det bildas totalt fyra objekt. Illustrera med en minnesbild (vid behov på separat papper) hur minnet ser ut när koden körts.
using namespace std;
struct A {
A() : a(this), nr(++count) {}
virtual void foo() { cout << "a" << nr << " "; } A * a;
int nr;
static int count;
};
struct B : public A { B() : A() {};
B(const B & b) : A() {a = b.a;} // copy-konstruktor virtual void foo() { cout << "b" << nr << " "; } };
struct C : public B {
virtual void foo() { cout << "c" << nr << " "; } };
struct D : public C {
virtual void foo() { cout << "d" << nr << " "; } };
struct E : public D {
virtual void foo() { cout << "e" << nr << " "; } };
int A::count = 0; // Nollställer count int main() {
D d;
E e;
B & b = e;
C c = d; // copy
B & b1 = c;
B b2 = b; // copy d.foo();
e.foo();
cout << endl;
b1.foo();
b2.foo();
cout << endl;
b1.a->foo();
b2.a->foo();
cout << endl;
}
3. const
(a) Vad används const till i C++?
(b) Slå upp mutable och berätta vad är för något.
(c) Begreppet const används i olika sammanhang. Kommentera i högerkolumnen nedan vad const innebär i de olika fallen. Första svaret är givet.
(d) Fyll i förslag på kod i vänsterkolumnen där const genererar varningar eller kompilatorfel. Första svaret är givet.
const int a = 3; variabeln a är konstant a = 5;
void bar(const int & b) {
};
int * const c = &a;
const int & d = a;
const int * e = &a;
const int & f() {
}
struct A {
void g() const; {
4. Binärt
Ett binärt sökträd är en dynamisk datastruktur som används i grundläggande algoritmkurser. Binärträdet består av noder som innehåller två nodpekare left och right samt sökbart data.
(a) Implementera en rekursiv void-funktion insert som tar två parametrar, dels data som ska sorteras in, och dels en referens till en nodpekare. Den rekursiva algoritmen är ungefär, om noden är tom skapa en ny nod med datat annars om datat är större än nodens data stoppa in i högerträdet annars stoppa in i vänsterträdet.
(b) Om man skickar nodpekaren som värdeparameter iställer för som referens så kommer koden inte att fungera. Förklara och illustrera med figur vad som händer i bägge fallen.
void insert(Nod<T> *& p, T & data) {
5. Den stejkande spelaren
Häromdagen hängde sig ett antal musikspelare (manicker man lyssnar på, inte människor). Man misstänker nedanstående kod som dels returnerar innevarande år och dels antal dagar in på innevarande år. Koden tar emot antal dagar sedan 1/1 1980 och subtraherar 365 (eller 366 dagar om det är skottår).
#define ORIGINYEAR 1980 /*...*/
///////////////////////////////////////////////////////////////////////////
// get_year subtracts 365 (or 366) days each year since 1980.
//
// IN : days since 1 jan 1980
// OUT: current year and current days
unsigned int get_year(unsigned int & days) { unsigned int year = ORIGINYEAR;
while (days > 365) {
if (IsLeapYear(year)) // Tenta:: IsLeapYear fungerar korrekt {
if (days > 366) {
days -= 366;
year += 1;
} } else {
days -= 365;
year += 1;
} }
return year;
}
(a) Koden har inte testats ordentligt. Vad brukar man i allmänhet testa när man testar funktioner.
(b) Tänk ut fyra bra testfall och fyll i första och andra kolumnen i nedanstående tabell.
In Expected result Observed result Comment
days = 17 days = 17, year = 1980 days = 17, year = 1980 Testar med ett slumpvist antal dagar.
BBC NEWS | Technology | Microsoft Zune affected by 'bug'
ONE-MINUTE WORLD NEWS
The problem appears restricted to first- generation 30GB Zune players
We expect the internal clock on the Zune 30GB devices will automatically reset tomorrow Microsoft
Faulty software has crippled 30GB first generation versions of the Microsoft Zune digital media player, affecting thousands of customers worldwide.
The company said it was aware of the problem, and offered advice to Zune users on how to solve it.
Customers have reported that their devices refuse to boot up,
and freeze when the start-screen appears.
The Zune was launched in 2006 as Microsoft's contender to Apple's highly successful iPod player.
Uncertainty
Discussing the cause on some user forums, some owners have said they believe the problems are due it being the first time the Zune will have updated its calendars following a Leap year.
On the Zune Insider website, Microsoft said it had been aware of the problem since early in the morning of December 31.
It said its technical team "jumped
on the problem immediately and isolated the issue" - a fault in the internal clock driver related to the way the device handles a leap year.
Said Microsoft: "The issue should be resolved over the next 24 hours as the time change moves to January 1, 2009.
"We expect the internal clock on the Zune 30GB devices will automatically reset tomorrow (noon, GMT).
"By tomorrow you should allow the battery to fully run out of power before the unit can restart successfully then simply ensure that your device is recharged, then turn it back on."
When Microsoft launched the Zune in 2006 the company hoped it could become a serious rival to the iPod player.
But sales of the Zune so far have been a fraction compared to Apple's best-seller.
The problem appears restricted to 30GB first generation Zune players. Later 80GB and 120GB models appear to be unaffected.
SHARED READ WATCHED/LISTENED
Iranian commanders assassinated US balloon boy case 'was a hoax' Thailand covers Hitler billboard US decision after Afghan result Street fighting in Taliban bases Airport stops scans on children Swedes divided over bunny biofuel 'Ethical' stem cell crop boosted Iceland reaches savers' agreement Australian teen makes sailing bid Most popular now, in detail
News Front Page
Africa Americas Asia-Pacific Europe Middle East South Asia UK Business Health
Science & Environment Technology
Entertainment Also in the news ---
Video and Audio ---
Programmes Have Your Say In Pictures Country Profiles Special Reports
Related BBC sites Sport
Weather On This Day Editors' Blog BBC World Service
Page last updated at 22:20 GMT, Wednesday, 31 December 2008
E-mail this to a friend Printable version
Microsoft Zune affected by 'bug'
SEE ALSO
The DRM maze for consumers 11 Dec 07 | Technology
New Zunes to take on Apple's iPod 03 Oct 07 | Technology
Microsoft predicts 1m Zune sales 07 Dec 06 | Technology
Zune goes head to head with iPod 14 Nov 06 | Technology
Zune problems for MSN customers 06 Nov 06 | Technology
Muted welcome for Microsoft Zune 15 Sep 06 | Technology
RELATED INTERNET LINKS Zune
Zune Insider
The BBC is not responsible for the content of external internet sites
TOP TECHNOLOGY STORIES Laptop for every pupil in Uruguay ISP in file-sharing wi-fi theft Confused message on UK broadband
| News feeds
MOST POPULAR STORIES NOW Explore the BBC
Bookmark with:
Delicious Digg reddit Facebook StumbleUpon What are these?
Low graphics Help Search
Orginalkoden fråga 5 (enligt diverse hackersajter)
#define ORIGINYEAR 1980
BOOL ConvertDays(UINT32 days, SYSTEMTIME* lpTime) {
int dayofweek, month, year;
UINT8 *month_tab;
//Calculate current day of the week dayofweek = GetDayOfWeek(days);
year = ORIGINYEAR;
while (days > 365) {
if (IsLeapYear(year)) {
if (days > 366) {
days -= 366;
year += 1;
} } else {
days -= 365;
year += 1;
} }
// Determine whether it is a leap year
month_tab = (UINT8 *)((IsLeapYear(year))? monthtable_leap : monthtable);
for (month=0; month<12; month++) {
if (days <= month_tab[month]) break;
days -= month_tab[month];
}
month += 1;
lpTime->wDay = days;
lpTime->wDayOfWeek = dayofweek;
lpTime->wMonth = month;
Fyll i felkoder enligt rättningsmallen på varje uppgift. Markera felet på tentan i marginalen. Underlåtelse att rätta allvarliga fel kan påverka resultatet på rättarens egen tenta. Felkoden är fyra siffror X-Y-ZZ där X är frågenummer, Y är allvarlighetsgrad och Z ett fel-id. Allvarlighetsgraden är 1 - litet fel, 9 - allvarligt, 5 - okänd allvarlighetsgrad för oförutsedda fel (dessa får fel-id 99). Om det saknas plats, använd kringliggande frågor som är utan anmärkningar, ersätt frågenummer med aktuell fråga.