Determine the number of statement executions (precise big-Oh) for each of the fo
ID: 667769 • Letter: D
Question
Determine the number of statement executions (precise big-Oh) for each of the following sample code, as described in the lecture for the MCS algorithm.
Sample #1:
for (int i = 0; i < n; i++){
sum += i;
}
int j = 0;
while (j < n)
{
sum--;
j++;
}
Sample #2:
int sum=0;
for (int i=0; i<n; i++)
if (i % 2 =0)
sum++;
Sample #4:
for (int i = 0; i < n; i++)
{
sum += i;
}
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
{
sum--;
}
}
Sample #5:
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = j; k < n; k++)
{
sum += j;
}
}
}
Explanation / Answer
sample 1
// class
public class Print
{
//int n, sum;
public static void main(String[] args)
{
int n = 5, sum = 0;
for (int i = 0; i < n; i++)
{
sum += i;
}
System.out.println(sum);
System.out.println(n);
int j = 0;
while (j < n)
{
sum--;
j++;
}
System.out.println("second");
System.out.println(sum);
System.out.println(n);
System.out.println(j);
}
}
output
10
5
second
5
5
5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.