2) Coding exercises. Write pseudocode segments in the style of Cheney and Kincai
ID: 3744689 • Letter: 2
Question
2) Coding exercises. Write pseudocode segments in the style of Cheney and Kincaid to solve the following problems: a) Assuming an integer variable n has already been initialized, output the integer prime numbers between 2 and n, inclusive. Y ou may assume that the value initialized in n is greater than 2. Make use of a special arithmetic operator, the modulo operator %, in your pseudocode. The modulo operator results in the remainder of an integer division operation. For example, 7 % 3-1, 10 % 4-2, and 12 % 6-0.Explanation / Answer
(a)
for i ranges from 2 to n //we will check each i from 2 to n if it is a prime no or not
isPrime = True //initialising isPrime to true
for j ranges from 2 to i-1 //we will check from 2 to i-1 whether i has a factor
if (i%j=0) then //if i%j=0, it means j is a factor of i
isPrime=false //we assign false to isPrime
exit inner loop //and exit the inner loop
endif
endfor
if(isPrime = true) then //we will check if isPrime is true
print i is a prime number //if it is true, then i is a prime number
else
print i is not a prime number //otherwise i is not a prime number
endif
endfor
(b)
1. initialise rp to 1 and rn to 2 //this is to make sure the loop runs for atleast one time
2. while (mod(rn - rp) > 0.000001) do //condition to if check |rn - rp|> 0.000001
rp=rn //assigning new rn to rp
rn = ( rp + x/rp )/2 //assign the value to rn according to formula
endwhile
3. print rn //this rn is the approximate square root of x
(c)
1. Initialise mean and sigma to 0 //initialising mean and sigma to 0
2. for i in range 1 to 100 //iterating over all the array numbers
mean=mean + x[i] //adding all array numbers to mean
endfor
3. mean = mean/100 //dividing mean by 100 as mean = sum/n
4. for i in range 1 to 100 //iterating over all array numbers
sigma = sigma + (x[i]-mean) * (x[i]-mean) //adding (x[i]-mean)2 to sigma for each i
endfor
5. sigma = sigma / n // completing the formula for sigma
6. print sigma and mean //printing sigma and mean
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.