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)
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")
Vidhi Patel(265382110) Ques-8
print("---%s seconds ---" %(time.time() - start_time))
do_Memory()
And the outputs are:

Ques 8

  • 1.
    Vidhi Patel(265382110) Ques-8 Forthe 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)
  • 2.
    Vidhi Patel(265382110) Ques-8 CODEB: #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")
  • 3.
    Vidhi Patel(265382110) Ques-8 print("---%sseconds ---" %(time.time() - start_time)) do_Memory() And the outputs are: