Question

Wondering if I could get some guidance on setting up newMatrix(). Language is C Matrix ADT Specifications In addition to the main program Sparse.c and the altered List.c from pal, you will implement a Matr

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include "Matrix.h"
using namespace std;

int main()
{
   Matrix *M = nullptr;
   M = M->newMatrix(5);

   Matrix *N = nullptr;
   N = N->newMatrix(10);

   cout << M->equals(*M, *N);

   M->printMatrix();
   N->printMatrix();
  
   cout << "Zeros: " << N->NNZ(*N) << endl;
   cout << "Size: " << N->size(*N) << endl;
  
   /*N->freeMatrix(N);
   N->printMatrix();*/

   return 0;
}

#pragma once
#include <iostream>
using namespace std;
struct Entry
{
   int size;
   double **Arr2;
};

class Matrix
{
private:
   Entry *EntryObj;
public:
   Matrix()
   {
       EntryObj = new Entry();
   }
   Matrix *newMatrix(int n)
   {
       Matrix *ret = new Matrix();
       ret->EntryObj->size = n;
       ret->EntryObj->Arr2 = new double*[n];
       for (int i = 0; i < n; i++)
       {
           ret->EntryObj->Arr2[i] = new double[n];
       }
       for (int i = 0; i < n; i++)
       {
           for (int j = 0; j < n; j++)
           {
               ret->EntryObj->Arr2[i][j] = 0;
           }
       }
       return ret;
   }
   void freeMatrix(Matrix *M)
   {
       M->~Matrix();
   }
   int size(Matrix M)
   {
       return EntryObj->size;
   }
   int NNZ(Matrix M)
   {
       int count = 0;
       for (int i = 0; i < M.EntryObj->size; i++)
       {
           for (int j = 0; j < M.EntryObj->size; j++)
           {
               if (M.EntryObj->Arr2[i][j] == 0)
                   count++;
           }
       }
       return count;
   }
   int equals(Matrix A, Matrix B)
   {
       if (A.EntryObj->size == B.EntryObj->size)
       {
           for (int i = 0; i < A.EntryObj->size; i++)
           {
               for (int j = 0; j < A.EntryObj->size; j++)
               {
                   if (A.EntryObj->Arr2[i][j] != B.EntryObj->Arr2[i][j])
                       return 0;
               }
           }
       }
       else return 0;
       return 1;
   }
   void printMatrix()
   {
       cout << endl;
       for (int i = 0; i < EntryObj->size; i++)
       {
           for (int j = 0; j < EntryObj->size; j++)
           {
               cout << EntryObj->Arr2[i][j] << " ";
           }
           cout << endl;
       }
       cout << endl;
   }
   ~Matrix()
   {
       EntryObj = nullptr;
   }
};

8 8 8 8 9 8 8 8 8 9 ග ග ග ග ග ග ග ග ග 9 9 9 0 8 9 Zeros: 100 Size: 10 Press any key to continue ...

IF THERE IS ANY PROBLEM OR YOU HAVE ANY QUERY REGARDING THE SOLUTION KINDLY FEEL FREE TO ASK, AND LEAVE A THUMBS UP WHEN YOU ARE SATISFIED. THANKS!.

Add a comment
Know the answer?
Add Answer to:
Wondering if I could get some guidance on setting up newMatrix(). Language is C Matrix ADT...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Language: C++ Complete this function 1.Object &raw_front() { // Return the element at the front of...

    Language: C++ Complete this function 1.Object &raw_front() { // Return the element at the front of the list *without* using the iterator classes // (You may assume the list is not empty) // Place your code here. Code: #ifndef LIST_H #define LIST_H #include using namespace std; template class List { private: // The basic doubly linked list node. // Nested inside of List, can be public // because the Node is itself private struct Node { Object data; Node *prev;...

  • Hello, I have some errors in my C++ code when I try to debug it. I...

    Hello, I have some errors in my C++ code when I try to debug it. I tried to follow the requirements stated below: Code: // Linked.h #ifndef INTLINKEDQUEUE #define INTLINKEDQUEUE #include <iostream> usingnamespace std; class IntLinkedQueue { private: struct Node { int data; Node *next; }; Node *front; // -> first item Node *rear; // -> last item Node *p; // traversal position Node *pp ; // previous position int size; // number of elements in the queue public: IntLinkedQueue();...

  • Need C programming help. I've started to work on the program, however I struggle when using...

    Need C programming help. I've started to work on the program, however I struggle when using files and pointers. Any help is appreciated as I am having a hard time comleting this code. #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LINE 100 #define MAX_NAME 30 int countLinesInFile(FILE* fPtr); int findPlayerByName(char** names, char* target, int size); int findMVP(int* goals, int* assists, int size); void printPlayers(int* goals, int* assists, char** names, int size); void allocateMemory(int** goals, int** assists, char*** names, int size);...

  • I've posted 3 classes after the instruction that were given at start You will implement and...

    I've posted 3 classes after the instruction that were given at start You will implement and test a PriorityQueue class, where the items of the priority queue are stored on a linked list. The material from Ch1 ~ 8 of the textbook can help you tremendously. You can get a lot of good information about implementing this assignment from chapter 8. There are couple notes about this assignment. 1. Using structure Node with a pointer point to Node structure to...

  • This is my code in C++, my code is working pretty good but there are some...

    This is my code in C++, my code is working pretty good but there are some stuff needs ti be changed I included theb below the my code: include<iostream> #include<string> #include<vector> #include<sstream> #include<algorithm> using namespace std; //Structure struct TokenFreq { //Structure variables string token; int freq=1; }; //Method matrixInit() void matrixInit(vector<vector<int> > &matrix, int numRows, int numCols) { //Declare the needed variables int index1, index2;    //Resize matrix.resize(numRows, vector<int>(numCols));    //Loop for(index1 = 0; index1 < numRows; index1++) { //Loop...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT