Sample Finals Answer CC-BY-NC

Maintainer: admin

note: may contain errors

1Question 1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<!doctype html>
<html>
    <head>
        <title>Sammy's Readings</title>
    </head>
    <body>
        <div style="text-align: center">
            <h1>Sammy's readings</h1>
        </div>
        <p>
            Please select books from the table of currently available
        books.  Each book is displayed with a description and cost.  On the right side you can
        select the quantity of books you would like to purchase.  Leaving the number at zero to
        indicate that you do not want that book.  Enjoy!
        </p>
        <form action="bill.py" method="POST">
            <table>
                <thead>
                    <tr style="background-color: #CCCCCC">
                        <th>Title</th>
                        <th>Description</th>
                        <th>Price</th>
                        <th>Qty</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>foo</td>
                        <td>bar</td>
                        <td>10.00</td>
                        <td><input type="text" name="foo" value="0" /></td>
                    </tr>
                    <tr>
                        <td>foo1</td>
                        <td>bar2</td>
                        <td>10.00</td>
                        <td><input type="text" name="foo1" value="0" /></td>
                    </tr>
                </tbody>
            </table>
            <div style="text-align: center">
                <input type="submit" value="Purchase!" name="submit" />
            </div>
        </form>
    </body>
</html>

1.1Discussion

Made the following changes:

  • XHTML for the input tags (<input /> instead of <input>)
  • Got rid of ugly ugly <center> tags (preferrably, you would center them in the CSS, but, baby steps)
  • Made use of <thead>, <tbody>, and <th>
  • Made use of <h1> for the header
  • Some indentation changes for clarity
  • Added a name attribute to the submit button
  • Enclosed the value of the "value" attribute in quotation marks

@dellsystem

  • Added doctype

@alexangelini

2Question 2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env python

import cgi

print "Content-type: text/html"
print ""
print """
<html>
<head><title>Sammy's Readings</title></head>
    <body>
    <center><span style="font-size:16px">Sammy's Readings</span></center>
"""
prices = {}
total = 0
f = open("products.csv")
for line in f:
    prices[f.split(",")[0]] = f.split(",")[1]
for item in cgi.FieldStorage.keys():
    quantity =  int(cgi.FieldStorage[item])
    if quantity == 0:
        continue # keeps looping; not the same as pass
    price = quantity * float(prices[item])
    total += quantity * price
    print "%s x %d at %s each: %f <br>" %(item, quantity, prices[item], price)
print total * 1.10
print """
</body>
</html>
"""

3Question 3

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>

int factorial(int n){
    int s = 1;
    for(;n>0;n--)
        s*=n;
    return s;
}
void main(char * args[]){
    int n = 0;
    int total = 0;
    for(;;){
        printf("Enter an integer greater or equal to zero: \n");
        scanf("%d",&n);
        if(n<0)
            break;
        total+=factorial(n);
    }
    printf("Sum =  %d", total);
}

4Question 4

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h>
#include <stdlib.h>

BOOLEAN insort(int x){
    //empty list
    if(head == NULL){
        head = (NODE*) malloc(sizeof(NODE));
        if(!head) return 1; //malloc fail
        head->value = x;
        return 0; //success
    }
    int curVal = head->value;
    NODE* curNode = head;
    if(head -> value > x){
        NODE newHead = (NODE*)malloc(sizeof(NODE));
            if(!newHead) return 1; //malloc fail
        newHead->value = x;
        newHead->next = head;
        head->next = NULL;
        head = newHead;
        return 0; //success
    }
    else{
        while(curNode < x && curNode->next != NULL && curNode->next->value < x){
            curNode = curNode -> next;
        }
        NODE newNode = (NODE*)malloc(sizeof(NODE));
        if(!newNode) return 1; //malloc fail
        newNode->value = x;
        newNode->next = curNode->next;
        curNode->next = newNode;
        return 0; //success
    }   
}

4.1Discussion

  • In the second if statement, why do you set head->next to NULL? Doesn't that disconnect the rest of the list from newHead and Head?

@s3admq

5Question 5

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
all:    myprogram

myprogram:  f1.o f2.o f3.o
    gcc f1.o f2.o f3.o -o myprogram

f1.o: f1.c
    gcc -c f1.c

f2.o: f1.c
    gcc -c f2.c

f3.o: f1.c
    gcc -c f3.c

clean:
    rm -rf *.o a.out

backup:
    mkdir backup && cp *.* ./backup

archive:
    tar -rvf myprogram.tar myprogram *.dat

6Question 6

a. The end result would be the sum of all 100000 numbers in the file
b. The advantage is that it benefits from parallelization which means it can take advantage of multiple processors
c. If fork does not succeed it means the system doesn't have enough resources to fork, thus we should try to compute it in a single process instead

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int main(void)
{
    FILE *in = fopen("data.txt","rt");
    int j, s=0;
    int id1, id2;
    if (in == NULL) exit(EXIT_FAILURE);
    for(j=0;j<100000;j++) fscanf(in,"%d",&array[j]);
    fclose(in);
    id1=fork();
    if(id1<0){//fork failed
        s1 = doit(0,50000);
        s2 = doit(50001,100000);
    }
    else if (id1 != 0){
        id2=fork();
        if(id2<0){//fork failed
            s2 = doit(50001,100000);
        }
        else if (id2 != 0) wait(0); // waits for all children to finish
        else s2 = doit(50001,100000);
    } else s1 = doit(0,50000);
    if (id1 != 0 && id2 != 0){
        s = s1 + s2;
        printf("s = %d\n", s);
        return EXIT_SUCCESS;
    }
    return EXIT_SUCCESS;
}