You are on page 1of 6

1.

(10 pts) List the following function in increasing order:

Answer:

Log n => 𝑛log n => n => 2^n => N^2 => N^3 => n!

2. (20 pts) What is the big-O of the following functions:

Answer:

(i) is O(n^5)
(ii) is O(nlog(n^8))

Programming

3. (10 pts) Run/test program example 1 (slide 9). Understand loop structure and know how to
write and call a method (only screenshot of the output required)
4. (20 points) Write a main method to calculate the lowest grade among 100 random grades
(ranging 0-100) by modifying and calling the method in example 2 (slide 10) (submit code and
screenshot).

package example2;

import java.util.Random;

public class Example2


{

public static int lowest(int [] arr, int n)


{
int lowest;
lowest = arr[0];

for (int i =0; i <n; i++)


if (arr[i] <= lowest )
lowest = arr[i];

return lowest;
};

public static void main(String[] args)


{
/* Write a main method to calculate the lowest grade among 100 random grades
(ranging 0-100) by modifying and calling the method in example 2 (slide 10) (submit
code
and screenshot)*/

int[] a = new int[100];

Random rand = new Random();

for(int i=0; i<100;i++)


a[i] = rand.nextInt(101);

int Min=lowest(a,100);
System.out.println(Min);
}

}
5. (15 pts) Complete Search method in example 3 (slide 10) by adding the method header. Then
write a main method to test it (submit code and screenshot).

package example3;

import java.util.Random;

public class Example3


{

public static int Search(int [] arr, int n,int givenInt)


{
int i = 0;
while((arr[i]!=givenInt) && (i <= n-1))
i = i + 1;

if (i < n)
return i;
else return -1;
};

public static void main(String[] args)


{
/* Complete Search method in example 3 (slide 10) by adding the method header.
Then write a main method to test it (submit code and screenshot). */

int[] a = new int[100];

Random rand = new Random();


for(int i=0; i<100;i++)
a[i] = rand.nextInt(101);

//finding value 99
int findPos=Search(a,100,99);
System.out.println(findPos);
}

6. (25 points) Add a method into the class studentStatistics (slide 12) that finds the student who
has the highest score in an array of students. Print that student (name, ID, score). Test it in the
main method (submit code and screenshot page).

package studentstatistics;

public class StudentStatistics


{

public static float AverageScore(student[] arr, int n)


{
float sum;
sum = 0;

for (int i = 0; i <= n - 1; i++)


sum = sum + arr[i].score;

return sum/n;
}

public static void Display(student[] arr, int n)


{
for (int i = 0; i < n; i++)
System.out.printf("%1$s %2$s %3$s \n", arr[i].id,arr[i].name, arr[i].score);
}

/*Add a method into the class studentStatistics (slide 12) that finds the student
who has the highest score in an array of students, Print that student (name, ID, score).*/

public static void highest(student [] arr, int n)


{
float highestS;
int indexMax=0;
highestS = arr[0].score;

for (int i =0; i <n; i++)


if (arr[i].score >= highestS )
{
highestS = arr[i].score;
indexMax=i;
}

System.out.printf("The highest score is: %1$s %2$s %3$s \n",


arr[indexMax].id,arr[indexMax].name, arr[indexMax].score);

};

public static void main(String[] args)


{

//Test it in the main method


student[] arr=new student[3];

for(int i=0;i<3;i++)
arr[i]=new student();

Display(arr,3);

highest(arr,3);
}

You might also like