# Answer to Question #58562 in C for OZ

Question #58562

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.

Expert's answer

#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");

}

#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 orderand get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

## Comments

## Leave a comment