Question

Given a matrix, clockwise-rotate elements in it. Please add code to problem3.cpp and the makefile. I...

Given a matrix, clockwise-rotate elements in it. Please add code to problem3.cpp and the makefile. I have also given the main_problem3.cpp and problem3.h to test.

problem3.cpp:::

#include "problem3.h"

// A function to rotate a matrix mat[][MAX]
void rotatematrix(int m, int n, int mat[][MAX])
{
// your code here   
}

Makefile:::

all : problem3.o
   g++ -o mainp3 # your code here

problem3.o : problem3.h problem3.cpp
   # your code here

clean:
   rm -f *.o mainp3

main_problem3.cpp:::

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

int main(){
int m=4,n=8;
int a[][100] = { {1, 2, 3, 4, 5, 6,7,8},
{5, 6, 7, 8, 9, 10, 11,12},
{9, 10, 11, 12, 13, 14,15,16},
{13, 14, 15, 16, 17, 18, 19, 20} };
  
for (int i=0; i<m; i++){
for (int j=0; j<n; j++)
cout << a[i][j] << " ";
cout << endl;
}
cout<<endl;
  
rotatematrix(m,n,a);
  
for (int i=0; i<m; i++){
for (int j=0; j<n; j++)
cout << a[i][j] << " ";
cout << endl;
}
}

problem3.h:::

#ifndef LAB4_PROBLEM3
#define LAB4_PROBLEM3
#define MAX 100
void rotatematrix(int m, int n, int mat[][MAX]);
#endif

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

Hello Learner,

Thanks for the question.

As per the question I need to write the code for rotatematrix that will rotate a given matrix in clockwise direction. As you haven’t specified the angle to which it needs to be rotated I have assumed it to the shift one element in clockwise direction.

Code:

void rotatematrix(int m,int n,int mat[][100]){

{

int row = 0, col = 0;

int prev, cur;

while (row < m && col < n)

{

if (row + 1 == m || col + 1 == n)

break;

prev = mat[row + 1][col];

for (int i = col; i < n; i++)

{

cur = mat[row][i];

mat[row][i] = prev;

prev = cur;

}

row++;

for (int i = row; i < m; i++)

{

cur = mat[i][n-1];

mat[i][n-1] = prev;

prev = cur;

}

n--;

if (row < m){

for (int i = n-1; i >= col; i--)

{

cur = mat[m-1][i];

mat[m-1][i] = prev;

prev = cur;

}

}

m--;

if (col < n)

{

for (int i = m-1; i >= row; i--)

{

cur = mat[i][col];

mat[i][col] = prev;

prev = cur;

}

}

col++;

}

}

}

Please let me know if you have any further questions.

If you find the answer useful, please upvote.

Add a comment
Know the answer?
Add Answer to:
Given a matrix, clockwise-rotate elements in it. Please add code to problem3.cpp and the makefile. I...
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
  • This needs to be in C++ Problem3.cpp Problem3.h: Given a matrix, clockwise-rotate elements in it. Please...

    This needs to be in C++ Problem3.cpp Problem3.h: Given a matrix, clockwise-rotate elements in it. Please add the code to problem3.h and problem3.cpp, provided. Input 4 Output: 1 8 For 4*4 matrix Input: 4 7 9 10 11 12 13 14 15 16 Output: 9 10 6 4 13 11 7 8 14 15 16 12 #include "problem3.h" 2 3 void rotatematrix(int m, int n, int mat[][MAX]) 4 //write your code here 5 1 #ifndet LAB3 PROBLEM3 2 #define LAB3PROBLEM3...

  • Need C++ coding for this lab exercise given below :) 4. Lab Exercise — Templates Exercise...

    Need C++ coding for this lab exercise given below :) 4. Lab Exercise — Templates Exercise 1 - Function Templating For this part of the lab make a template out of the myMax function and test it on different data types. Copy maxTemplate.cpp to your Hercules account space. Do that by: Entering the command:cp /net/data/ftp/pub/class/115/ftp/cpp/Templates/maxTemplate.cpp maxTemplate.cpp Compile and run the program to see how it works. Make a template out of myMax. Don't forget the return type. Modify the prototype appropriately. Test your myMax template on int, double,...

  • How do I compile c++ code in terminal. Below i have some code to apply Gaussian...

    How do I compile c++ code in terminal. Below i have some code to apply Gaussian Filter on an image using OpenCV. I have tried compling using the code g++ Project2.cpp -o Project2 `pkg-config --cflags --libs opencv` && ./Project2 and it gives me a whole list of errors saying directory not found. --------------------------------------------------------------------------------------------------------- #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <opencv2/core/core.hpp> #include <iostream> using namespace cv; using namespace std; int main() { Mat image = imread("//u//oowens//First.jpg"); int rows=image.rows; int cols=image.cols; if (image.empty())...

  • CONVERT CODE TO JAVA // A C++ program to Print all elements in sorted order from...

    CONVERT CODE TO JAVA // A C++ program to Print all elements in sorted order from row and // column wise sorted matrix #include<iostream> #include<climits> using namespace std;    #define INF INT_MAX #define N 4    // A utility function to youngify a Young Tableau. This is different // from standard youngify. It assumes that the value at mat[0][0] is // infinite. void youngify(int mat[][N], int i, int j) {     // Find the values at down and right sides of...

  • C++ Please complete the implementation of the following source code (Question3.cpp). You need to add your...

    C++ Please complete the implementation of the following source code (Question3.cpp). You need to add your code in the source code where the comment “// your code” locates. After you finish the implementation, please also provide the output of your program. #include <iostream> using namespace std; class Shape { protected: // your code public: void setWidth (int w) { // your code } void setHeight (int h) { // your code } }; class Rectangle: public Shape { public: int...

  • I need help fixing my code: In C++ *************** 1) I want to sum the digits...

    I need help fixing my code: In C++ *************** 1) I want to sum the digits of an n*n matrix 2) find the average I have completed the rest ****Do not use C++ standard library. You must use pointers and pointer arithmetic to represent the matrix and to navigate through it. MY CODE: (I have indicated at which point I need help) #include <iostream> using namespace std; void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp;...

  • C++: Need help debugging my code I am writing this and using some other examples as reference code while rewriting it with my own understanding of the material but am having trouble making it finally...

    C++: Need help debugging my code I am writing this and using some other examples as reference code while rewriting it with my own understanding of the material but am having trouble making it finally compile. Below is a picture of the error messages. //main.cpp //Semester Project //Created by J---on 5/6/2019 #include <iostream> #include <fstream> #include <string> #include <sstream> #include <bits/stdc++.h> using namespace std; void instructions(); //displays program details and instructions void openFile(); void takeInput(int*); void switchBoard(int*); struct price {...

  • The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried t...

    The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...

  • I'm having trouble sorting this square matrix (a 2d array that has number of rows and...

    I'm having trouble sorting this square matrix (a 2d array that has number of rows and same number of column n x n) using row-wise approach in C programming. Please help me program this in C to sort it using row-wise approach, here is the code: #include <stdio.h> #define MAX 100 int main() { int mat[MAX][MAX]; int i, j, m, n; int rowsum, columnsum, diagonalsum; int k; int magic = 0; int transpose[MAX][MAX]; printf("Enter the # of rows and columns...

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