Docsity

Solved questions for Assignment 2 - Statistical Computing | STAT 534, Assignments of Statistics

Toggle sidebar
bg1
Hil Lyons
STAT 534
HW 3
1) Table of Results
Below is the text output from the test case for HW 3. To save space, the output has been
formatted into two columns.
Initial set
Set: 3493240
Elements:
0
1
2
3
4
5
6
7
8
9
Found 0: 1
Found 15: 0
After trying to add 0
Found = 1
Set: 3493240
Elements:
0
1
2
3
4
5
6
7
8
9
After trying to remove 0
Found = 1
Set: 3493240
Elements:
1
2
3
4
5
6
7
8
9
Using iterator to loop over set
1
2
3
4
5
6
7
8
9
Set 2
Set: 3493384
Elements:
4
5
6
7
8
9
10
11
12
13
Union
Set: 3486408
Elements:
1
2
3
4
5
6
7
8
9
10
11
12
13
Intersection
Set: 3499056
Elements:
4
5
6
7
8
9
2) Code (please see reverse)
Hil Lyons Page 1 4/24/2006
pf3

Partial preview of the text

Download Solved questions for Assignment 2 - Statistical Computing | STAT 534 and more Assignments Statistics in PDF only on Docsity!

Hil Lyons

STAT 534

HW 3

1) Table of Results

Below is the text output from the test case for HW 3. To save space, the output has been

formatted into two columns.

Initial set Set: 3493240 Elements: 0 1 2 3 4 5 6 7 8 9 Found 0: 1 Found 15: 0 After trying to add 0 Found = 1 Set: 3493240 Elements: 0 1 2 3 4 5 6 7 8 9 After trying to remove 0 Found = 1 Set: 3493240 Elements: 1 2 3 4 5 6 7 8 9 Using iterator to loop over set 1 2 3

Set 2 Set: 3493384 Elements: 4 5 6 7 8 9 10 11 12 13 Union Set: 3486408 Elements: 1 2 3 4 5 6 7 8 9 10 11 12 13 Intersection Set: 3499056 Elements: 4 5 6 7 8 9

2) Code (please see reverse)

Hil Lyons Page 1 4/24/

/* STAT 534 HW3 functions -- Hil Lyons */

#include<stdlib.h>

#include<stdio.h>

#include "global-definitions.h"

#include "set.h"

#include "home3.h"

/* set_add_element: adds a specified element to a set.

This is accomplished by iterating through the list

to check whether the element is already present. This

check is done via a user specified equivalence test.

If the element is not present, the element is added to

the list.

Boolean set_add_element (Element_type element, Set *set){

Link previous = set->head;

Link current;

while (previous->next != NULL){

current = previous->next;

if (((Boolean (*) (Element_type, Element_type)) set->eq_test)

(current->item, element))

return(TRUE);

previous = previous->next;

previous->next = malloc(sizeof(struct List_node));

previous->next->item = element;

previous->next->next = NULL;

return(FALSE);

/* set_union: creates a pointer to the union set of

two input sets.

This is done by first copying one of the lists and

second running set_add_element on the second set to

add non-intersecting elements to the union list.

A Set_iterator object could be used to iterate

through the sets, but there appear to be no

real code savings. */

Set *set_union (Set *set1, Set *set2){

Set *result = set_make(set1->eq_test);

Link cur_result, cur_set1, cur_set2;

cur_result = result->head;

cur_set1 = set1->head->next;

cur_set2 = set2->head->next;

/* Copy first set to the union set */

while (cur_set1 != NULL){

cur_result->next = malloc(sizeof(struct List_node));

cur_result->next->item = cur_set1->item;

cur_result = cur_result->next;

cur_set1 = cur_set1->next;