Write a C program, using arrays, for the circuit, as shown below, that will prin
ID: 3578971 • Letter: W
Question
Write a C program, using arrays, for the circuit, as shown below, that will print a table of the voltage across R3 and the power in R3 for E from 5 to 50 V in steps of 5V. Draw the circuit using printf statements before solving the problem. Write a C program to read two matrices A (MxN) and B (MxN) and perform addition OR subtraction OR multiplication of A and B. Display the given matrices, their sum or differences or multiplication result. Ask the user for their preferences (Addition or Subtraction or Multiplication)Explanation / Answer
2)
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
void display_mat(int **mat, int M, int N)
{
int i,j;
for(i = 0; i<M; i++)
{
for(j = 0; j<N; j++)
{
printf("%d ", mat[i][j]);
}
printf(" ");
}
}
int main()
{
int i,M,N,choice,j,sum=0,k;
srand(time(NULL));
printf("Enter number of rows M : ");
scanf("%d", &M);
printf("Enter Number of columns N : ");
scanf("%d", &N);
int **mat1 = (int **)malloc(M * sizeof(int *));
int **mat2 = (int **)malloc(M * sizeof(int *));
int **result = (int **)malloc(M * sizeof(int *));
for(i = 0; i < M; i++)
{
mat1[i] = (int *)malloc(N * sizeof(int));
mat2[i] = (int *)malloc(N * sizeof(int));
result[i] = (int *)malloc(N * sizeof(int));
}
//generating matrix randomly
for(i = 0; i<M; i++)
{
for(j = 0; j<N; j++)
{
mat1[i][j] = rand();
mat2[i][j] = rand();
}
}
printf("1. Addition 2. subtraction 3. Multiplication ");
printf("Enter choice : ");
scanf("%d", &choice);
if(choice == 1)
{
for(i = 0; i<M; i++)
{
for(j = 0; j<N; j++)
{
result[i][j] = mat1[i][j] + mat2[i][j];
}
}
}
else if(choice == 2)
{
for(i = 0; i<M; i++)
{
for(j = 0; j<N; j++)
{
result[i][j] = mat1[i][j] - mat2[i][j];
}
}
}
else
{
for (i = 0; i < M; i++)
{
for (j = 0; j < N; j++)
{
for (k = 0; k < M; k++)
{
sum = sum + mat1[i][k]*mat2[k][j];
}
result[i][j] = sum;
sum = 0;
}
}
}
printf("Matrix A ");
display_mat(mat1,M,N);
printf(" Matrix B ");
display_mat(mat2,M,N);
printf(" Resultant Matrix ");
display_mat(result,M,N);
return 0;
}
please refer below output
Enter number of rows M : 3
Enter Number of columns N : 3
1. Addition 2. subtraction 3. Multiplication
Enter choice : 1
Matrix A
255 4000 23293
26353 19028 1887
11382 10250 20648
Matrix B
28539 15572 25382
31276 19270 7351
18390 31204 15450
Resultant Matrix
28794 19572 48675
57629 38298 9238
29772 41454 36098
Process returned 0 (0x0) execution time : 6.365 s
Press any key to continue.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.