Answer to Question #58562 in C for OZ
2016-03-18T10:19:41-04:00
write a C program that will ask the user to input an integer “n” at the console. Your program will generate two n x n matrices of random ints between 0 and 1023 (inclusive), then multiply, add, and subtract them together. The matrices are to be represented as 2-dimensional arrays. Your program should display the result of each matrix operation (multiplication, subtraction and addition). Write a report explaining for which value of n your program performance slows down.
1
2016-03-19T05:49:04-0400
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <malloc.h> #include <windows.h> void printmatrix(int**, int); int main(){ srand((unsigned)time(NULL)); int n,i,j; printf("Input n: "); scanf("%d",&n); int** first = (int**)malloc(sizeof(int*)*n); int** second = (int**)malloc(sizeof(int*)*n); int** result = (int**)malloc(sizeof(int*)*n); for(i = 0 ; i < n; i++){ first[i] = (int*)malloc(sizeof(int)*n); second[i] = (int*)malloc(sizeof(int)*n); result[i] = (int*)malloc(sizeof(int)*n); } for(i = 0 ; i < n ; i++) for(j = 0 ; j < n ; j++){ first[i][j] = rand()%1024; second[i][j] = rand()%1024; } printf("First massive\n"); printmatrix(first,n); printf("Second massive\n"); printmatrix(second,n); //+ for(i = 0; i < n; i++) for(j = 0; j < n; j++) result[i][j] = first[i][j] + second[i][j]; printf("Sum of first and second\n"); printmatrix(result,n); //- for(i = 0; i < n; i++) for(j = 0; j < n; j++) result[i][j] = first[i][j] - second[i][j]; printf("Subtract of first and second\n"); printmatrix(result,n); //* for(i = 0; i < n; i++) for(j = 0; j < n; j++) result[i][j] = first[i][j] * second[i][j]; printf("Multiply of first and second\n"); printmatrix(result,n); for(i = 0 ; i < n; i++){ free(first[i]); free(second[i]); free(result[i]); } free (first); free (second); free (result); return 0; } void printmatrix(int** mass, int size){ int i, j; for(i = 0 ; i < size; i++){ for(j = 0 ; j < size; j++) printf("%8d",mass[i][j]); printf("\n"); } printf("\n\n"); }
Need a fast expert's response?
Submit order
and get a quick answer at the best price
for any assignment or question with DETAILED EXPLANATIONS !
Learn more about our help with Assignments:
C
Comments
Leave a comment