Question

In: Computer Science

declare a struct named matrix and typedef the struct to type name Matrix. A mathematical matrix...

  1. declare a struct named matrix and typedef the struct to type name Matrix. A mathematical matrix is a two-dimensional, rectangular data structure. The matrix struct should have three fields (in this order): an unsigned variable named "rows", an unsigned variable named "columns", and a pointer to a pointer to double (i.e. double**) named "data". (Code #including matrix.h should be able to declare Matrix variables.)
  2. In matrix.c, implement the create_matrix function. The Matrix should be filled with 0.0 values. data[i] should be a pointer to the ith row. data[i][j] should be the value in the ith row and jth column.
  3. In matrix.c, implement the print_matrix function. The function should print the matrix's data, one row per line with a single space between elements and a newline at the end of each row. Each data value should be printed with 6 digits to the right of the decimal point.
  4. In matrix.c, implement the free_matrix function. The function should free all the matrix's data that was dynamically allocated in the create_matrix function.

Solutions

Expert Solution

Solution

/*** matrix.h ***/

#ifndef MATRIX_H
#define MATRIX_H

//Matrix structure definition
typedef struct
{
unsigned rows;
unsigned columns;
double **data;
}Matrix;

//function prototypes
Matrix create_matrix(unsigned r, unsigned c);
void print_matrix(Matrix mat);
void free_matrix(Matrix mat);

#endif // MATRIX_H

/*** matrix.c ***/

#include <stdio.h>
#include <stdlib.h>
#include"matrix.h"

//function to create Matrix
Matrix create_matrix(unsigned rows, unsigned columns)
{
Matrix mat;
mat.columns = columns;
mat.rows = rows;

mat.data =(double**)malloc(rows * sizeof(double*));
double *q =(double*)malloc(rows * columns * sizeof(double));

for(unsigned i=0; i<rows; i++)
{
mat.data[i] = q;
q += columns;
}

for(unsigned i=0; i<rows; i++)
for(unsigned j=0; j<columns; j++)
mat.data[i][j] = 0.0;

return mat;
}

//function to print the Matrix
void print_matrix(Matrix mat)
{
for(unsigned i=0; i<mat.rows; i++){
for(unsigned j=0; j<mat.columns; j++)
printf("%0.6f ", mat.data[i][j]);
printf("\n");
}
}

//function to free the Matrix
void free_matrix(Matrix mat)
{
for(unsigned i=0; i<mat.rows; i++)
{
free(mat.data[i]);
}
free(mat.data);
}

/*** main.cpp */

#include <stdio.h>
#include <stdlib.h>
#include"matrix.h"

//main function
int main()
{
int rows = 3;
int columns = 3;

Matrix mat = create_matrix(rows, columns);
print_matrix(mat);
free_matrix(mat);

return 0;
}

Output:

Solving your question and helping you to well understand it is my focus. So if you face any difficulties regarding this please let me know through the comments. I will try my best to assist you. However if you are satisfied with the answer please don't forget to give your feedback. Your feedback is very precious to us, so don't give negative feedback without showing proper reason.
Always avoid copying from existing answers to avoid plagiarism.
Thank you.


Related Solutions

Consider the following declaration: typedef struct{                                  &nbsp
Consider the following declaration: typedef struct{                                          int A;                                          char B[10];                                          float C;                                          char D;                                  }rectype;                                   typedef rectype matrix[121][4][5];                                   matrix A1; Compute the address of element A1[120][3][3] given the base address at 2000.
Create a program that uses typedef to declare a new type vec2df which is a fixed...
Create a program that uses typedef to declare a new type vec2df which is a fixed double array of size 2. Next, create another type using typedef to declare a struct which has a fixed double array of size 2. Name this type vec2d. In main, declare two variables which use the types created above. Using stdlib.h, print the size of each variable on a new line and observe the similarity or difference. Finally, create two functions which take as...
#include<stdlib.h> #include<stdio.h> typedef struct node {    void* dataPtr;    struct node* next; } QUEUE_NODE; typedef...
#include<stdlib.h> #include<stdio.h> typedef struct node {    void* dataPtr;    struct node* next; } QUEUE_NODE; typedef struct {    QUEUE_NODE* front;    QUEUE_NODE* rear;    int count; } QUEUE; //Prototype Declarations QUEUE* createQueue(void); QUEUE* destroyQueue(QUEUE* queue); bool dequeue(QUEUE* queue, void** itemPtr); bool enqueue(QUEUE* queue, void* itemPtr); bool queueFront(QUEUE* queue, void** itemPtr); bool queueRear(QUEUE* queue, void** itemPtr); int queueCount(QUEUE* queue); bool emptyQueue(QUEUE* queue); bool fullQueue(QUEUE* queue); /*================= createQueue ================ Allocates memory for a queue head node from dynamic memory and returns...
Using the following definitions for a binary tree, typedef struct bintreenode {     int data;     struct bintreenode*...
Using the following definitions for a binary tree, typedef struct bintreenode {     int data;     struct bintreenode* left;     struct bintreenode* right; } btreenode; // Used for a node in the queue. typedef struct node {     btreenode* nodePtr;     struct node* next; } node; // Used to represent the queue efficiently. typedef struct queue {     node* front;     node* back; } queue; Implement the following functions: void bfs(btreenode* root) // Prints a breadth first search traversal of the binary search tree rooted at root....
#include <stdio.h> typedef struct Coordinates { int x; int y; } Coordinate; typedef union uCoordinates {...
#include <stdio.h> typedef struct Coordinates { int x; int y; } Coordinate; typedef union uCoordinates { int x; int y; } uCoordinate; // TODO - Populate 4 different Coordinates with any numbers between 1 and 99 for x & y values // using coordinate1, coordinate2, coordinate3, & coordinate4 as Coordinate names // TODO - Print to screen the x & y values of each coordinate // TODO - Replace the following with your name: Ethin Svoboda int main() { //...
#include #include #include int reverse(int); // Stack ADT Type Defintions typedef struct node { void* dataPtr;...
#include #include #include int reverse(int); // Stack ADT Type Defintions typedef struct node { void* dataPtr; struct node* link; } STACK_NODE; typedef struct { int count; STACK_NODE* top; } STACK; /* =============== createStack ============== This algorithm creates an empty stack. Pre Nothing Post Returns pointer to a null stack -or- NULL if overflow */ STACK* createStack(void) { // Local Definitions STACK* stack; // Statements stack = (STACK*)malloc(sizeof(STACK)); if (stack) { stack->count = 0; stack->top = NULL; } // if return...
#include<stdio.h> #include<stdlib.h> struct listNode{ int data; struct listNode *nextptr; }; typedef struct listNode node; void insert(node*);...
#include<stdio.h> #include<stdlib.h> struct listNode{ int data; struct listNode *nextptr; }; typedef struct listNode node; void insert(node*); void showList(node*); void printListBackwards(node *); int main(void) { node *list1; printf("\n Create a sorted list.."); printf("\n Enter values for the first list (-999 to end):"); list1=(node*)malloc(sizeof(node*)); //Allocate memory for the list node insert(list1); //insert values by calling the function insert showList(list1); //display values entered by user printf("\n After recursively reversing the list is :\n"); printListBackwards(list1); //print the values in reverse order using the function...
Create a typedef fruitType using the struct fruitType_struct to store the following data about a fruit:...
Create a typedef fruitType using the struct fruitType_struct to store the following data about a fruit: name (string, up to 50 characters long) color (string, up to 10 characters long) fat (integer) sugar (integer) carbohydrate (integer) Write a void function called printFruit that takes a fruitType parameter and prints the data (as shown in the example below). Declare a variable of type fruitType to store the following data: name: banana color: yellow fat: 1 sugar: 15 carbohydrate: 22 then use...
Please debug the code and answer the questions: #include <stdio.h> typedef struct node { int value;...
Please debug the code and answer the questions: #include <stdio.h> typedef struct node { int value; struct node *next; } node; int ll_has_cycle(node *first) { node * head = first; while (head->next) { head = head->next; if (head == first) return 1; } return 0; } void test_ll_has_cycle(void) { int i,j; node nodes[5]; for(i=0; i < sizeof(nodes)/sizeof(node); i++) { nodes[i].next = NULL; nodes[i].value = i; } nodes[0].next = &nodes[1]; nodes[1].next = &nodes[2]; nodes[2].next = &nodes[1]; printf("Checking first list for cycles....
Below is for C language typedef struct _node { Node *next; char *str; } Node; You...
Below is for C language typedef struct _node { Node *next; char *str; } Node; You are given a function that takes in two Node* pointers to two different linked lists. Each linked list node has a string represented by a character array that is properly null terminated. You're function is supposed to return true if the concatenation of strings in a linked list match each other, else return false. EXAMPLES List 1: "h" -> "el" -> "l" -> "o"...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT