Above program denotes how many matrix elements need to be crossed to get our required adress.
For example to get a[1][1] we need to get 4 matrix elements like a[0][0], a[0][1], a[0][2], a[1][0]
Program in execution:
K=0;
a[0][0]=k++=0 We need to cross 0 matrix elements.
a[0][1]=k++=1 We need to cross 1 matrix elements.
a[0][2]=k++=2 We need to cross 2 matrix elements.
a[1][0]=k++=3 We need to cross 3 matrix elements.
a[1][1]=k++=4 We need to cross 4 matrix elements.
a[1][2]=k++=5 We need to cross 5 matrix elements.
After Execution:
a=[0,1,2
3,4,5]
********************************************Other way******************************************
It is also used to find address of our element as our array stored in row major orde.
Row major order:
assume matrix a of order m*n where m is number of rows and n is nummber of columns.
If starting indices of a is 0,0: (Assuming that each integer takes one memory location)
address of a[i][j] = i*n + j
here a is of order 2*3 i.e. 2 rows and 3 columns and starting with indices 0,0.
address of a[1][1] = 1*3 + 1 = 4
This array would be stored as follows.
Address |
Value |
0 |
a[0][0] |
1 |
a[0][1] |
2 |
a[0][2] |
3 |
a[1][0] |
4 |
a[1][1] |
5 |
a[1][2] |
Final answer is a[1][1]=4
I don’t understand can you help 5. Given the definition and code fragment: (2 points int...
What is the output of the following code fragment? int i = 1; while( i <= 5 ) if(i == 2 || i == 4) System.out.println(i + ":" + " is an even index) System.out.println("i: " + i); i++;
1(5 pts): For each code fragment below, give the complexity of the algorithm (O or Θ). Give the tightest possible upper bound as the input size variable increases. The input size variable in these questions is exclusively n. Complexity Code public static int recursiveFunction (int n)f f( n <= 0 ) return 0; return recursiveFunction (n - 1) 1; for(int i 0i <n; i+) j=0; for ( int j k=0; i; k < < j++) for (int j; m <...
What is printed when the following code is executed? #include <stdio.h> int main() { for (int i=3, j=5; i>=0; i-=2, j+=j*i) { printf("%d", j); منم }
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
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
or (int i - 3; i<ll; i-i+2) Solution: cout< iend; i-i2 B) What will the following program fragment output? (2 points) int mainc int j 7, d 2; double sum 0; 5 Solution: sumd; --ji 9 10 cout<esum; 12 13
(c) int sum(int n) un { int sum=0; for (int i=0; i<n; i++) for(int j=0; j<i/2; j++) for(int k=0; k<min(j,5); k++) { sum=sum+1; } return sum; }
Using C++ please explain What is the Big-O time complexity of the following code: for (int i=0; i<N; i+=2) { ... constant time operations... Select one: o a. O(n^2) O b. O(log n) c. O(n) O d. 0(1) What is the Big-O time complexity of the following code: for(int i=1; i<N; i*=2) { ... constant time operations... Select one: O O a. O(n^2) b. 0(1) c. O(n) d. O(log n) O What is the Big-O time complexity of the following...
Java Be able to write or understand simple recursive code, including code that uses divide and conquer static int calculate(int v)l System.out.println("calculate "+v); if( v<-1) return 2; return 7*calculate(v/2); What will be the output of the statement calculate(6)?
What is the output?