Creating Vectors: (a) By using the linspace function, create a row vector c whos
ID: 3884751 • Letter: C
Question
Creating Vectors: (a) By using the linspace function, create a row vector c whose first element is 3, whose last element is 17 and which has 27 elements (columns). (b) By using the linspace function, create a column vector x whose first element is 30, whose last element is -10 and which has 13 elements (rows). Creating Vectors: (a) By using the linspace function, create a row vector d whose first element is 3, whose last element is -17 and which has 7 elements (columns). (b) By any method other than directly entering the values, create a column vector v whose first element is 30, whose last element is -10 and which has 41 elements (rows). Creating 2D arrays: (a) Create a square matrix A with 16 elements, each column of which is populated with its column number. Do not enter the elements of A directly: use another method. (b) Create a 5 times 3 matrix B which is populated by random integers between 0 and 5. Do not enter the elements of B directly: use another method. (c) Create a 3 times 7 matrix C whose first row is 7 equally spaced numbers beginning with -2 and ending with 14 and whose second and third rows are random integers between 0 and 3. Do not enter the elements of C directly: use another method. (d) Create a 4 times 3 array D, whose first 3 rows are those of the 3 times 3 identity matrix and whose last row is a row of 4's. (e) Create a 4 times 4 array E, each row of which is populated with its row number. (f) Create a 4 times 6 array F, populated with random integers between -3 and 3.Explanation / Answer
5. Code:
/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void initialize_matrix_A(int ROW,int COLUMN,int matrix[][COLUMN])
{
int i=0,j=0;
for(i=0;i<ROW;i++)
{
for(j=0;j<COLUMN;j++)
{
matrix[i][j]=j+1;
}
}
}
void initialize_matrix_B(int ROW,int COLUMN,int matrix[][COLUMN])
{
int i=0,j=0;
for(i=0;i<ROW;i++)
{
for(j=0;j<COLUMN;j++)
{
matrix[i][j]=rand()%5;
}
}
}
void initialize_matrix_C(int ROW,int COLUMN,int matrix[][COLUMN])
{
int i=0,j=0;
for(i=0;i<ROW;i++)
{
for(j=0;j<COLUMN;j++)
{
if(i>0)
{
matrix[i][j]=rand()%3;
}
else
{
matrix[i][j]=-2+j*2.75;
}
}
}
}
void initialize_matrix_D(int ROW,int COLUMN,int matrix[][COLUMN])
{
int i=0,j=0;
for(i=0;i<ROW;i++)
{
for(j=0;j<COLUMN;j++)
{
if(i==j && i<ROW-1)
{
matrix[i][j]=1;
}
if(i==ROW-1)
{
matrix[i][j]=4;
}
}
}
}
void initialize_matrix_E(int ROW,int COLUMN,int matrix[][COLUMN])
{
int i=0,j=0;
for(i=0;i<ROW;i++)
{
for(j=0;j<COLUMN;j++)
{
matrix[i][j]=i+1;
}
}
}
void initialize_matrix_F(int ROW,int COLUMN,int matrix[][COLUMN])
{
int i=0,j=0;
for(i=0;i<ROW;i++)
{
for(j=0;j<COLUMN;j++)
{
matrix[i][j]=rand()%7+(-3);
}
}
}
void display(int ROW,int COLUMN,int matrix[][COLUMN])
{
int i=0,j=0;
printf("Matrix Contents ");
for(i=0;i<ROW;i++)
{
for(j=0;j<COLUMN;j++)
{
printf("%d ",matrix[i][j]);
}
printf(" ");
}
}
int main()
{
int matrix_A[4][4]={0};
int matrix_B[5][3]={0};
int matrix_C[3][7]={0};
int matrix_D[4][3]={0};
int matrix_E[4][4]={0};
int matrix_F[4][6]={0};
initialize_matrix_A(4,4,matrix_A);
display(4,4,matrix_A);
initialize_matrix_B(5,3,matrix_B);
display(5,3,matrix_B);
initialize_matrix_C(3,7,matrix_C);
display(3,7,matrix_C);
initialize_matrix_D(4,3,matrix_D);
display(4,3,matrix_D);
initialize_matrix_E(4,4,matrix_E);
display(4,4,matrix_E);
initialize_matrix_F(4,6,matrix_F);
display(4,6,matrix_F);
return 0;
}
Output:
Matrix Contents
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
Matrix Contents
0 1 0
0 3 0
4 0 3
2 4 4
4 1 1
Matrix Contents
-2 0 3 6 9 11 14
1 0 0 0 2 1 0
0 1 2 0 0 1 0
Matrix Contents
1 0 0
0 1 0
0 0 1
4 4 4
Matrix Contents
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
Matrix Contents
-3 0 1 -2 -3 3
2 -1 0 3 0 -3
-3 1 2 -1 -3 0
-3 -1 -3 0 -2 2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.