Assignment4

advertisement
Introduction to programming Concepts and tools E2005
Students:
Henrik Loop (loop)
Flemming Thor Hansen (flemmingth)
Assignment no. 4
Link to code:
http://www.itu.dk/people/flemmingth/twoDimToOneDimArray.java
http://www.itu.dk/people/loop/birthdayTest.java
http://www.itu.dk/people/flemmingth/birthdayTestOptimal.java
Question 1:
(a) Returns 3 due to the assignment i line 5, whichs copyes the refernce point from a1 to a2,
there by the a1 and a2 is pointing to the same array. Changing an index will there for show
for both.
(b) Returns 10 because a1 now has the reference of a2.
(c) Returns ”goo = 4” and 1=4, 2=4.
Question 2:
public class twoDimToOneDimArray {
public static void main (String[] args){
// sample input from question
int [][] array = { {0, 1, 2}, {10, 11, 12}, {21, 22, 23} };
// An array to hold the resulting one dimensional array
int [] result;
// A simple call of the mthod to convert the array
result = convert(array);
// The resulting array is printed line by line
for (int i = 0; i < result.length; i++) {
System.out.println("result[" + i + "] = " + result[i]);
}
}
// This method first count the number of integers in the
// input two dimensional array. This is done using two
// loops one for each dimension. This method works even
// for irregular shaped two dimensional arrays
// input must be a two dimensional array
// output is an one dimensional array with all the numbers
// from the input array
static int[] convert(int[][] a) {
int count = 0; // variable to count number of cells in input array
int index = 0; // variable to keep track of the assignments of
// the output
// First count the number of elements in the input array
for (int i = 0; i < a.length; i++){
for (int j = 0; j < a[i].length; j++){
count++;
}
}
// For each element assign the value to the onedimensional
// resulting array
int[] converted = new int[count];
for (int i = 0; i < a.length ; i++){
for (int j = 0; j < a[i].length ; j++){
converted[index] = a[i][j];
index++;
}
}
return converted;
}
}
Sample output:
result[0] = 0
result[1] = 1
result[2] = 2
result[3] = 10
result[4] = 11
result[5] = 12
result[6] = 21
result[7] = 22
result[8] = 23
Question 3:
public class birthdayTest {
public static void main (String[] args){
//int[] people = new int[100]; // array to hold the birthdays
int testTrue; // counter to hold testresults
// Outer loop that iterates throug the different groupsizes
for (int groupSize = 2; groupSize <= 100; groupSize++){
int[] people = new int[groupSize];
// Before each loop the counteres are reset
testTrue = 0;
// For each group of people 10000 tests are to be caried out
for (int testNumber = 1; testNumber <= 10000; testNumber++){
// the array are filled out until the current group size
fillArray(people);
if (isTrueTrial(people))
testTrue++;
}
if (groupSize < 10)
System.out.print("Groupsize " + groupSize + " true " );
else
if (groupSize < 100)
System.out.print("Groupsize " + groupSize + " true ");
else
System.out.print("Groupsize " + groupSize + " true ");
if (testTrue/100 < 10)
System.out.print(" " + (testTrue/100));
else
if (testTrue/100 < 100)
System.out.print(" " + (testTrue/100));
else
System.out.print(testTrue/100);
System.out.println("%
false " + (100-testTrue/100) + "%");
}
}
static void fillArray (int[] a ) {
// This method assigns each element in the array a a random integer
// between in the range from 0 to 364
for (int i = 0; i < a.length; i++){
a[i] =(int)(Math.random()*365);
};
}
static boolean isTrueTrial (int[] a){
// This method returns true if the array a contains at least two identical
// values, otherwise it returns false.
if (a.length == 1) return true;
else {
boolean foundEqual = false;
for (int i = 0; i < a.length - 1; i++){
for (int j = i +1; j < a.length; j++){
if (a[i] == a[j]){
foundEqual = true;
break;
};
};
if (foundEqual == true) break;
}
return foundEqual;
}
}
}
please find below a diffrent method wich is a bit faster
public class birthdayTestOptimal {
public static void main (String[] args){
/* This algorithm uses an array of 365 integers corresponding to the 365
possible birthdays a person may have. Each cell corresponds to one birthday
Before each of the 10000 tests the array is filled with zeros
For each person in the group a random birthday (number between 1 and 365) is
calculated
To see if this birthday has occured before in the group it is checked
if the array cell corresponding to the calculated birthday has the value 1.
If the cell has a value of 1, the birthday has occured before in that
group of people, and there is no need to calculate more birthdays and
no more checks are necessary.
That means that in a group of say 100 people, if the 20th random birthday
has occured before, its not necessary to continue with the 80 remaining
people in the group
If the bithday has not occured, the value of the cell corresponding to the
birthday is changed to one to mark the occurence of the birthday
10000 tests are performed for each group size from 2 to 100 people
*/
int testTrue;
int i;
int randomDay;
int numberOfTests = 10000;
for (int groupSize = 2; groupSize <= 100; groupSize++){
testTrue = 0;
for (int testNumber = 1; testNumber <= numberOfTests; testNumber++){
int [] birthday = new int[365];
i = 1; // Counter for each person in the group
// Calculate a random birthday for next person in the test
randomDay = (int)(Math.random()*365);
// Test to see if the birthday has already occured during this test
while ((birthday[randomDay] == 0) && (i <= groupSize)){
// if not mark this birthday as used
birthday[randomDay] = 1;
// increment counter to keep track of number of people
i++;
//Calculate next birthday to be tested
randomDay = (int)(Math.random()*365);
};
if (birthday[randomDay] != 0){
// A duplicate birthday has been found
testTrue++;
}
}
// Print the results for the tested group size
// Format by inserting spaces to keep nice columns
if (groupSize < 10)
System.out.print("Groupsize " + groupSize + " true: ");
else
if (groupSize <100)
System.out.print("Groupsize " + groupSize + " true: ");
else
System.out.print("Groupsize " + groupSize + " true: ");
if ((100*testTrue/numberOfTests) < 10)
System.out.print(" " + (100*testTrue/numberOfTests));
else
if ((100*testTrue/numberOfTests) < 100)
System.out.print(" " + (100*testTrue/numberOfTests));
else
System.out.print((100*testTrue/numberOfTests));
System.out.println("%
false: " + (100-(100*testTrue/numberOfTests)) + "%");
}
}
}
Question 4:
(a)
Before loop l=0 c=0 r=18
At line pp l=0 c=9 r=8
At line pp l=0 c=4 r=3
At line pp l=2 c=1 r=3
Value 28 was found
l=2 c=2 r=3
(b)
Yes, the method return false.
The variable r is assigned the value -1 before the while loop.
The condition in the while loop is false and will therefore not be executed.
The method returns false in case the while loop is not executed.
Download