What do the following program segments print? Find the answers by tracing the code, not by using the computer.
- a. int n = 1;
for (int i = 2; i < 5; i++) { n = n + i; }
System.out.print(n);
- b. int i;
double n = 1 / 2;
for (i = 2; i <= 5; i++) { n = n + 1.0 / i; }
System.out.print(i);
- c. double x = 1;
double y = 1;
int i = 0;
do
{
y = y /2;
x = x + y;
i++;
}
while (x < 1.8);
System.out.print(i);
- d. double x = 1;
double y = 1;
int i = 0;
while (y >= 1.5)
{
x = x / 2;
y = x + y;
i++;
}
System.out.print(i);
Want to see the full answer?
Check out a sample textbook solutionChapter 4 Solutions
Big Java Late Objects
Additional Engineering Textbook Solutions
C How to Program (8th Edition)
Starting Out with Java: From Control Structures through Objects (6th Edition)
Starting Out With Visual Basic (8th Edition)
Artificial Intelligence: A Modern Approach
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Starting Out With Visual Basic (7th Edition)
- For each of the following program fragments: Give an analysis of the running time (Big-Oh). Justify your answer? A. public class GFG { // Linearly search x in att[]. if x is present then //return the index, otherwise return -1 static int search (int arr[], int n, intx) { int i; for (i = 0: iarrow_forwardint toplam = 0;for (int i = 0; i < 10; i++){if (i % 2 == 1) continue;for (int j = 1; j <= 5; j++){if (i * j > 30) continue;toplam += j;}} What will be the total number of iterations when the code runs?arrow_forwardQuestion #4: Re-write the program of Question #3 using data files. The input data numbers.txt contains (positive) integer numbers. Each time the program reads a number, check whether it is a prime and stores the result in the output file Results.txt. 373 373 is Prime!! 11 11 is Prime!! 2552 is NOT prime!! 5 is Prime!! 3561 is NOT prime!! 2552 3561 numbers.txt Results.txtarrow_forwardDescription A mathmatician Goldbach's conjecture: any even number(larger than 2) can divide into two prime number’s sum.But some even numbers can divide into many pairs of two prime numbers’ sum. Example:10 =3+7, 10=5+5, 10 can divide into two pairs of two prime number. Input Input consist a positive even number n(4<=n<=32766). Output Print the value of how many pairs are this even number can be divided into. Sample Input 1 1234 Sample Output 1 25arrow_forwardSystem.out.println (x); for (int i - 0; i < x.length System.out.println (x); for (int i = 1; i < x.lengh; i++) System.out.println (x[i]); for (int i -0; i < x.length; i++) System.out.println (x[i]); None of the above 3. int [] x new int[6]; To print all array elements we use: a. b. 1; i++) C. d. e.arrow_forwardint funcB(int); int funcA(int n) { if (n 4) { return n* funcA(n - 5); } else { return n- funcB(n - 1); int main() { cout << funcA(13); return 0; What is the output of this program? Please show our work.arrow_forward#include int k=0; int f(int t) { k += t; heturn k; } void main() { int i, result=1; for(i = 1;i <= 3;i++) result = f(i); printf("%d\n", result); } A 3 B C) 1 D 123arrow_forward#include double f(double x) { return 4 * exp(-x); } double trapezoidalRule(double a, double b, int N) { double h = (b - a) / N; double sum = 0.5 * (f(a) + f(b)); for (int i = 1; i < N; i++) { sum += f(a + i * h); } return h * sum; } int main() { double a = 0.0; // lower limit double b = 1.0; // upper limit int N = 5; // number of trapezoids double integral = trapezoidalRule(a, b, N); std::cout << "The estimate of the integral of f(x) = 4e^-x between " << a << " and " << b << " using " << N << " trapezoids is: " << integral << std::endl; return 0; } Please write down the explanation, step by step for this trapezoidal rule program, Emphasize on this part:for (int i = 1; i < N; i++) { sum += f(a + i * h); }arrow_forward%3B for ( int i = 0; i < n; ++i ) { for ( int j= 0; j< n; ++j) { sum += f( j, 10 ); } } iii. int sum = 0; %3D Assume f(n, m) runs in O( n + m) time.arrow_forwardFind an error in the following code and fix it. int x = 23;int* p = x; Group of answer choices int* x = 23;int* p = x; int x = 23;int& p = *x; int x = 23;int* p = *x; int x = 23;int* p = &x;arrow_forward1. a. Are Count and sum the same? yes or no, why? { int sum = 0; for ( int count = 0; count < 10; count++ ) { System.out.print( count + " " ); sum = sum+count; } } b. { int sum = 0; for ( int count = 0; count < 10; count++ ) { System.out.print( count + " " ); sum = sum+count; } System.out.println("\nAfter the loop count is: " + count ); } c. What is printed, why? class FindIt { private int sum; public FindIt( int sum ) { this.sum = sum; } public void increment( int inc ) { sum = sum + inc; System.out.println("FindIt sum: " + sum ); } } public class MainClass { public static void main ( String[] args) { int sum = 99; FindIt findObj = new FindIt( 34 ); findObj.increment( 6 ); System.out.println("sum: " + sum ); } }arrow_forwardint f(int &k){k++;return k * 2;}int main(){int i = 1, j = -1;int a, b, c;a = f(i) + i/2;b = j + f(j) + f(j);c = 2 * f(j);return 0;} What are the values of a, b and c id the operands in the expressions are evaluated from left to right and then what are the values when its evaluated right to left?arrow_forwardarrow_back_iosSEE MORE QUESTIONSarrow_forward_ios
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr