Consider the code below
int s =1;
for (int i=1; i<N; i++){
//Assertion: s =
s = s+(2*i+1);}
printf("%d", s);
a) What is a reasonable assertion for the main loop?
b) What does the code accomplish?
c) Using the method of initalization-maintenance-termination, prove that the invariant holds, and that the final output is correct.
a)
What is a reasonable assertion for the main loop?
In main loop we use assertion as s==i*i (reason in part b)
int s =1;
for (int i=1; i<N; i++){
//Assertion: s =
assert(s == i*i);
s = s+(2*i+1);
}
b) What does the code accomplish?
This code is finding the sum of first N odd numbers. We are starting with s=1 and then we are adding below :
When i=1; we are adding 2*i+i =3
i=2; we are adding 2*i+1 =5
i=3; we are adding 2*i+1 =7
i=4; we are adding 2*i+1 =9
i=5; we are adding 2*i+1 =11
and so on…
Now sum of first N odd numbers is N*N so that’s why our assertion is i*i for sum of first i terms.
Checked in c program :
c) Using the method of initalization-maintenance-termination, prove that the invariant holds, and that the final output is correct.
We must show:
Suppose N = 10
Initialization:
When loop starts s=1 which is sum of first odd number and it is correct.
Maintenance:
Now when it enters the loop it updates the value of s as shown below:
Start Value |
Value of i |
Updated Value |
Expected Value |
Correctness |
S=1 |
1 |
S=1+(2*1+1) = 4 |
2*2=4 |
True |
S=4 |
2 |
S=4 +(2*2+1) = 9 |
3*3=9 |
True |
S=9 |
3 |
S=9+(2*3+1) = 16 |
4*4=16 |
True |
S=16 |
4 |
S=16+(2*4+1) = 25 |
5*5=25 |
True |
S=25 |
5 |
S=25+(2*5+1) = 36 |
6*6=64 |
True |
S=36 |
6 |
S=36+(2*6+1) = 49 |
7*7=49 |
True |
S=49 |
7 |
S=49+(2*7+1) = 64 |
8*8=64 |
True |
S=64 |
8 |
S=64+(2*8+1) = 81 |
9*9=81 |
True |
S=81 |
9 |
S=81+(2*9+1) = 100 |
10*10=100 |
True |
Termination:
Sum of first N(i.e 10) odd numbers should be 100 which is 10*10 and we are getting the same results.
Hence final output is correct
Consider the code below int s =1; for (int i=1; i<N; i++){ //Assertion: s = s...
Q4) what is the output after the code below is executed? #include <stdio.h> #1 nclude<string.h> int main) char abc[100]-"Good Luck in Your Programming Final" int i, ; while(abc[i]!= '\0'){ if(abc[i]' if(abc[i+1]-'&&abci+1] -'0' else printf"c", abcli-1]) printf("\n w = %d return 0; \n", w); Place you answer here 4 l Page
Please use induction Consider the following code fragment: int i -1; int s=1; while (i <- n) Show that at the start of every iteration holds using induction
20) What is the output of the following segment of C code: int avg(int n, int* a); int main () { int array[4]={1,0,6,9}; printf("%d", avg(4, array)+ 1); system("pause"); return 0; } int avg(int n, int* a) { int i, sum=0; for (i=0;i<n;i++) { sum+=a[i]; } return sum/n; } a) 16 b) 5 c) 4 d) 8 21) What is the output of the following segment of C code: int x = 2; int y = 3;...
Int partition(int arr[], int first, int last) {int mid = first; int pivot = arr[first]; for (int sweep = first+1; sweep <= last; sweep++) {//Assertion:. .. if (arr[sweep] < pivot) {int tmp = arr[sweep]; arr[sweep] = arr[mid+l]; arr[mid+l] = tmp; mid++;}}//Post:. .. arr[first] = arr[mid]; arr[mid] = pivot; return mid;} The focus for this problem will be the main loop of partition. You will make arguments about what it attempts to accomplish. a) The diagram depicts what we hope is...
Consider the following code: Void F1 (int n) { int a; for(int i = 0; i < n; i += 2) a = i; } Which of the following characterization, in terms of n, of the running time of the above code (F1) is correct? Θ(n3/2) · O(1/n) · O(n) · Ω(n2) Consider the following code: Void F1 (int n) { int a; for(int i = 0; i < n; i += 2) a = i; }...
Consider the following C++ code segment: for (int i = 0; i <n; ++i) { for (int j = 0; j <m; ++j) if (i != j) cout << "0"; else cout << "1"; } } Which of the options below gives the correct output if the value of nis 2and the value of mis 3? 1. 100010 2. 011101 3. 100100 4. 010001
i need flowchart for the following c code #include <stdio.h> int main() { int n, i, sum = 0; printf("Enter an integer: "); scanf("%d",&n); i = 1; while ( i <=n ) { sum += i; ++i; } printf("Sum = %d",sum); return 0; }
(15 points) Consider the algorithm for insertion sort shown below. The input to this algorithm is an earray A. You must assume that indexing begins at 1. 1: for j = 2: A.length do key = A i=j-1 while i > 0 and A[i] > key do Ali + 1] = Ai i=i-1 7: A[i+1] = key (a) Follow this algorithm for A[1..4) =< 7,9,6,8 >. Specifically, please indicate the contents of the array after each iteration of the outer...
#include <stdio.h> int main(int argc, char *argv[]) { int i; for (i = argc - 1; i > 0; i--) printf("%s ", argv[i]); printf("\n"); return 0; } can you explain this code in c and why use this function
#include <stdio.h> int josephus(int n, int k) { if (n == 1) return 1; else /* The position returned by josephus(n - 1, k) is adjusted because the recursive call josephus(n - 1, k) considers the original position k%n + 1 as position 1 */ return (josephus(n - 1, k) + k-1) % n + 1; } // Driver Program to test above function int main() { int n = 14; int k = 2; printf("The chosen place...