CS 100: Section Assignment S8
Solutions
1.
public class ArrayOps
{
// Yields a reference to an array whose values are obtained by
// performing a perfect shuffle of a. Assumes that a has even length.
public static int[] Shuffle(int[] a)
{
int n = a.length;
int m = n/2;
int[] b = new int[n];
for(int i=0;i<m;i++)
{
b[2*i] = a[i];
b[2*i+1] = a[i+m];
}
return b;
}
// Perfect shuffles the entries in a. Assumes that a has even length.
public static void ShuffleAlt(int[] a)
{
int n = a.length;
int m = n/2;
int[] b = new int[n];
for(int i=0;i<m;i++)
{
b[2*i] = a[i];
b[2*i+1] = a[i+m];
}
for(int i=0;i<n;i++)
a[i] = b[i];
}
// Prints on a single line the values in the (presumably short) array a.
public static void println(int[] a)
{
int n= a.length;
for(int k=0;k<n;k++)
Format.print(System.out," %2d",a[k]);
System.out.println(" ");
}
// Yields a reference to an array whose values are obtained by
// performing a perfect 3-way shuffle of a. Assumes that a has multiple of 3 length.
public static int[] shuffle3(int[] a)
{
int n = a.length;
int m = n/3;
int[] b = new int[n];
for(int i=0;i<m;i++)
{
b[3*i] = a[i];
b[3*i+1] = a[i+m];
b[3*i+2] = a[i+2*m];
}
return b;
}
// Yields the number of array entries that are strictly bigger than the average.
public static int nBiggerThanAve(double[] a)
{
double sum = 0;
for(int k=0;k<a.length;k++)
sum = sum + a[k];
double ave = sum/a.length;
int count = 0;
for(int k=0;k<a.length;k++)
{
if (a[k]>ave)
count++;
}
return count;
}
// Yields a reference to an array having the same length as a and whose
// ith entry is a[(i+1)%a.length].
public static int[] leftShift(int[] a)
{
int n = a.length;
int[] b = new int[n];
for(int i=0;i<n;i++)
b[i] = a[(i+1)%n];
return b;
}
// Yields a reference to an array having the same length as a and whose
// ith entry is a[(i+k)%a.length]. Assume k is nonnegative.
public static int[] leftShift(int[] a,int k)
{
int n = a.length;
int[] b;
b = leftShift(a);
for (int j=1;j<k;j++)
{
b = leftShift(b);
}
return b;
}
}
// S8A
import java.io.*;
public class S8A
{
public static void main(String args[])
{
TokenReader in = new TokenReader(System.in);
int kMax = 10000;
int D1, D2,score;
int[] n = {0,0,0,0,0,0,0,0,0,0,0,0,0};
for (int k=1;k<=kMax;k++)
{
D1 = (int)(1 + Math.floor(6*Math.random()));
D2 = (int)(1 + Math.floor(6*Math.random()));
n[D1+D2]++;
}
for (int i=2;i<=12;i++)
System.out.println("n[" + i + "] = " + n[i]);
in.waitUntilEnter();
}
}
/* Output sample:
n[2] = 273
n[3] = 547
n[4] = 837
n[5] = 1103
n[6] = 1417
n[7] = 1674
n[8] = 1358
n[9] = 1143
n[10] = 821
n[11] = 548
n[12] = 279
*/
2.
import java.io.*;
public class S8B
{
public static void main(String args[])
{
TokenReader in = new TokenReader(System.in);
int[] x={10,20,30,40,50,60,70,80,90,100,110,120};
int[] y = ArrayOps.shuffle3(x);
System.out.println("The 3-way shuffled array:");
for(int i=0;i<12;i++)
System.out.println("y[" + i + "] = " + y[i]);
in.waitUntilEnter();
}
}
/* Output
The 3-way shuffled array:
y[0] = 10
y[1] = 50
y[2] = 90
y[3] = 20
y[4] = 60
y[5] = 100
y[6] = 30
y[7] = 70
y[8] = 110
y[9] = 40
y[10] = 80
y[11] = 120
*/
3.
import java.io.*;
public class S8C
{
public static void main(String args[])
{
TokenReader in = new TokenReader(System.in);
double[] x = {3.1,-3.1,4.1,-4.1,5.2,-5.2,6.0,0.0};
int n = ArrayOps.nBiggerThanAve(x);
System.out.println("n = " + n);
in.waitUntilEnter();
}
}
/* Output:
n = 4
*/
4.
import java.io.*;
public class S8D
{
public static void main(String args[])
{
TokenReader in = new TokenReader(System.in);
int[] x = {0,1,2,3,4,5,6};
int[] y;
y = x;
System.out.println(y[0] +" "+ y[1] +" "+ y[2]+" "+ y[3] +" "+ y[4] + " " + y[5] +" "+ y[6]);
y = ArrayOps.leftShift(x);
System.out.println(y[0] +" "+ y[1] +" "+ y[2]+" "+ y[3] +" "+ y[4] + " " + y[5] +" "+ y[6]);
y = ArrayOps.leftShift(x,3);
System.out.println(y[0] +" "+ y[1] +" "+ y[2]+" "+ y[3] +" "+ y[4] + " " + y[5] +" "+ y[6]);
in.waitUntilEnter();
}
}
/* Output:
0 1 2 3 4 5 6
1 2 3 4 5 6 0
3 4 5 6 0 1 2
*/