You are given the following two pseudo-codes and asked to write C codes to compare their speeds and memory requirements for reasonably large arrays. Any meaningful performance differences between these two loops? Explain your answer.
LOOP A:
For I = 1, 1000
For J = 1, 1000
c(J) = c(J) + a(I, J)*b(J)
LOOP B:
For J = 1, 1000
For I = 1, 1000
c(J) = c(J) + a(I, J)*b(J)
observation:
Loop A traverses row wise
Loop B traverses column wise
since i and j both are size of 1000, we can assume this to be 1000X1000 matrix.
Traversing symmetric matrix row wise is far better than traversing it column wise.
reason:
2D matrix is represented as 1D array, row by row.
row-column uses better cpu-caches than column wise.
LOOP A is better than Loop B
You are given the following two pseudo-codes and asked to write C codes to compare their...
Write the output of the two codes below, unless there is a compiler error or a runtime error. In those cases, write CE or RE, respectively, and explain why those errors occur. If the code produces an infinite loop, write “LE: infinite loop”. public class ForLoopsDemo{ public static void main(String[] args){ int x = 4; fint x = 4; for( int i = 0; i < x; i++ ){ } } for( int j = 0; j < 2*i+1; j++)}...
Step 2. Write a C function to passed two numbers to the function and calculate the sum of all the numbers between them (both inclusive). [50 marks] a. Make a version with for loop b. Make a version with while loop c. Make a version with do.. while loop d. Make a version with goto loop Tip : Try to use the same number of variables and almost the same logic in all the four loops To do: Using the...
When asked to describe an algorithm you are expected to give a
clear pseudo-code description of the algorithm
1. (10 pts) Here is a new sorting algorithm NewSort Suppose the original call made is NewSort(A,0,n-1) where A is an array integers. == void NewSort(int A[], int i, int j){ \\ sorts the subarray Aſi..j] if (j i+1) \\when there are only 2 elements if (A[i] > A[j]) swap(A,i,j) \\swaps A[i] and A[j] else { int k = (j-i+1)/3; NewSort(A,i,j-k); \\...
Step 1. Write a program in assembly language (using macros) to print out the following messages on the screen [20 marks]: Hello, programmers! Welcome to the world of, Linux assembly programming! Step 2. Write a C function to passed two numbers to the function and calculate the sum of all the numbers between them (both inclusive). [50 marks] a. Make a version with for loop b. Make a version with while loop c. Make a version with do.. while loop...
Try to write two pseudo-codes for the tree-structural global sum, one for a shared-memory setting and the other for a distributed-memory setting. First consider how this might be done in a shared-memory setting. Then consider how this might be done in a distributed-memory setting. In the shared-memory setting, which variables are shared and which are private? and Suppose we have a program that generates large quantities of random floating point data that it stores in an array. In order to...
1 . Generate Assembly Codes for the two C programs
below
a. Compare the assembly codes generated
b. For code B , analyze and explain how the function
call “print_hello()” was materialized inside the computer
(compiler, OS and the hardware) for the code B. Use figures and
text descriptions if necessary
A)
B)
#include <stdio.h void print hello0; int main(int argc, char *argv[]) print hello0; return 0; void print hello(){ printf("Hello, worldln');
2. Determine for the following code how many pages are transferred between disk and main memory, assuming each page has 2000 words, the active memory set size is 1000 (i. e., at any time no more than 1000 pages may be in main memory), and the replacement strategy is LRU (the Least Recently Used page is always replaced); also assume that all two- dimensional arrays are of size (1:2000, 1:2000), with each array element occupying one word, N=2000 for I...
Consider the following C codes to compute the gcd of two integers. /// code 1 #include <stdio.h> int gcd(int a, int b) { while (a != b) { if (a > b) a = a - b; else b = b - a; } return a; } /// code 2 #include <stdio.h> int getint() { int i; scanf("%d", &i); return i; } void putint(int i) { printf("%d\n", i); } int main()...
Write a two player tic-tac-toe program in C# that has the following requirements: loops, classes, arrays, switch statements, methods and functions. The program must include at least 70 lines of code and does NOT require a GUI. The current program requirements are represented above but I’m having difficulty starting the program. Any help and example would be greatly appreciated.
Can someone help me with these C program problems? Thanks 1. Write a C program that creates two arrays of integers, one on the stack and one on the heap. You will loop through these arrays and populate them with values (any value is fine). You will then loop through these arrays again and print each value. Your output should look something like “Value in index 1 is 100 from stack array and 100 from heap array.” Do not forget...