Embed presentation
Download to read offline
![Vidhi Patel(265382110) Ques-8
For the given two pseudo codes, I have written C codes which are as follows:
LOOP A:
For I = 1, 1000
For J = 1, 1000
c(J) = c(J) + a(I, J)*b(J)
CODE A:
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[1000][1000], b[1000], c[1000];
for(int i=0;i<1000;i++){
for(int j=0; j<1000; j++){
a[i][j] = (rand() % 1000);
}
b[i] = (rand() % 1000);
}
for(int i=0; i<1000; i++){
for(int j=0; j<1000; j++){
c[j] = c[j] + a[i][j] * b[j];
}
}
return 0;
}
LOOP B:
For J = 1, 1000
For I = 1, 1000
c(J) = c(J) + a(I, J)*b(J)](https://image.slidesharecdn.com/ques-8-210417024253/75/Ques-8-1-2048.jpg)
![Vidhi Patel(265382110) Ques-8
CODE B:
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[1000][1000], b[1000], c[1000];
for(int i=0;i<1000;i++){
for(int j=0; j<1000; j++){
a[i][j] = (rand() % 1000);
}
b[i] = (rand() % 1000);
}
for(int j=0; j<1000; j++){
for(int i=0; i<1000; i++){
c[j] = c[j] + a[i][j] * b[j];
}
}
return 0;
}
For the analysis of these codes, I have do_Memory for the memory analysis:
import os
import time
start_time = time.time()
def do_Memory():
os.system("gcc type1.c")
os.system("./a.out")
print("---%s seconds ---" %(time.time() - start_time))
os.system("gcc type2.c")
os.system("./a.out")](https://image.slidesharecdn.com/ques-8-210417024253/75/Ques-8-2-2048.jpg)

The document describes two C code programs that implement nested loops to perform matrix multiplication. The first program (Code A) uses a nested loop with the outer loop iterating over i and the inner loop iterating over j. The second program (Code B) switches the order of the nested loops, with the outer loop iterating over j and the inner loop iterating over i. The document also includes a Python program that compiles and runs both C programs and times their execution to analyze their memory performance.
![Vidhi Patel(265382110) Ques-8
For the given two pseudo codes, I have written C codes which are as follows:
LOOP A:
For I = 1, 1000
For J = 1, 1000
c(J) = c(J) + a(I, J)*b(J)
CODE A:
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[1000][1000], b[1000], c[1000];
for(int i=0;i<1000;i++){
for(int j=0; j<1000; j++){
a[i][j] = (rand() % 1000);
}
b[i] = (rand() % 1000);
}
for(int i=0; i<1000; i++){
for(int j=0; j<1000; j++){
c[j] = c[j] + a[i][j] * b[j];
}
}
return 0;
}
LOOP B:
For J = 1, 1000
For I = 1, 1000
c(J) = c(J) + a(I, J)*b(J)](https://image.slidesharecdn.com/ques-8-210417024253/75/Ques-8-1-2048.jpg)
![Vidhi Patel(265382110) Ques-8
CODE B:
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[1000][1000], b[1000], c[1000];
for(int i=0;i<1000;i++){
for(int j=0; j<1000; j++){
a[i][j] = (rand() % 1000);
}
b[i] = (rand() % 1000);
}
for(int j=0; j<1000; j++){
for(int i=0; i<1000; i++){
c[j] = c[j] + a[i][j] * b[j];
}
}
return 0;
}
For the analysis of these codes, I have do_Memory for the memory analysis:
import os
import time
start_time = time.time()
def do_Memory():
os.system("gcc type1.c")
os.system("./a.out")
print("---%s seconds ---" %(time.time() - start_time))
os.system("gcc type2.c")
os.system("./a.out")](https://image.slidesharecdn.com/ques-8-210417024253/75/Ques-8-2-2048.jpg)
