I am relatively new to C++ and I have an assignment that is making perform mathematical operations with matrices. I was able to complete the operations but I can't seem to display the original matrices I inputted into the output with out doing a "cout" command for each number. I'm trying to display the original matrices before the answer to the 5 questions.
This is what I have done:
#include <stdio.h>
#include <iostream>
using namespace std;
void matrix(int A[3][3], int B[3][3], int result[3][3])
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
for (int k = 0; k < 3; k++)
result[i][j] += (A[i][k] * B[k][j]);
}
}
}
void scalar(int A[3][3], int num, int result[3][3])
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
result[i][j] = A[i][j] * num;
}
}
}
void display(int result[3][3])
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
cout << result[i][j] << ",";
cout << endl;
;
}
}
int main()
{
//inputting matrix values
int A[3][3]={{13,9,7},{8,7,4},{6,4,0}};
int B[3][3]={{1,3,5},{6,2,-3},{2,0,4}};
int C[3][3]={{1,0,0},{0,1,0},{0,0,1}};
int result[3][3]={{0,0,0},{0,0,0},{0,0,0}};
//question 1
matrix(A,B,result);
cout<<"A * B ="<<endl;
display(result);
int result0[3][3]={{0,0,0},{0,0,0},{0,0,0}};
//question 2
matrix(B,A,result0);
cout<<"B * A ="<<endl;
display(result0);
int result1[3][3]={{0,0,0},{0,0,0},{0,0,0}};
//question 3
matrix(C,B,result1);
cout<<"C * B ="<<endl;
display(result1);
int result2[3][3]={{0,0,0},{0,0,0},{0,0,0}};
//question 4
scalar(A,2,result2);
cout<<"2 * A ="<<endl;
display(result2);
int result3[3][3]={{0,0,0},{0,0,0},{0,0,0}};
//question 5
scalar(B,-4,result3);
cout<<"-4 * B ="<<endl;
display(result3);
return 0;
}
Related
The problem with my code is that it is not identifying my function, I am not sure if the function is incorrect or written with the wrong syntax. What I have tried is to create a new array for the location of the largest index but it doesn't seem to work.
#include <iostream>
#include <iomanip>
using namespace std;
void locateLargest(const double a[][4], int location[]);
const int ROW_SIZE = 3;
const int COLUMN_SIZE = 4;
int main(){
int location [ROW_SIZE][COLUMN_SIZE];
double matrix [ROW_SIZE][COLUMN_SIZE];
double input;
cout<<"Enter the array: "<< endl;
for (int i = 0; i < ROW_SIZE; i++){
for(int j = 0; j < COLUMN_SIZE; j++){
cin>>input;
matrix[i][j] = input;
}
}
for(int i = 0; i < ROW_SIZE; i++){
for(int j = 0; j < COLUMN_SIZE; j++){
cout<< setw(4)<<matrix[i][j]<< " ";
}
cout<< endl;
}
locateLargest(matrix, location)
}
You can keep track of the max value's indices while iterating through the matrix.
void max_idx(const double (&arr)[RS][CS]) {
double curr_max = arr[0][0];
size_t max_i = 0, max_j = 0;
for (size_t i = 0; i < RS; ++i) {
for (size_t j = 0; j < CS; ++j) {
if (curr_max < arr[i][j]) {
curr_max = arr[i][j];
max_i = i;
max_j = j;
}
}
}
cout << "Largest value is at (i=" << max_i << ", j=" << max_j << ")\n";
}
Demo
First of all, you have to make sure that your code is consistent : in the prototype of your locateLargest function, location is a one-dimensional array but in your main() function it is a two-dimensional one.
This is how I would write this :
#include <iostream>
#include <iomanip>
using namespace std;
void locateLargest(double** a, int* location);
const int ROW_SIZE = 3;
const int COLUMN_SIZE = 4;
int main()
{
int location [2];
double* matrix [ROW_SIZE];
for(int s= 0; s< ROW_SIZE; s++)
{
matrix[s]= new double[COLUMN_SIZE];
}
double input;
cout<<"Enter the array: "<< endl;
for (int i = 0; i < ROW_SIZE; i++)
{
for(int j = 0; j < COLUMN_SIZE; j++)
{
cin>>input;
matrix[i][j] = input;
}
}
for(int i = 0; i < ROW_SIZE; i++)
{
for(int j = 0; j < COLUMN_SIZE; j++)
{
cout<< setw(4)<<matrix[i][j]<< " ";
}
cout<< endl;
}
locateLargest(matrix, location);
}
void locateLargest(double** a, int* location)
{
int i, j;
double maxVal= a[0][0]; location[0]= location[1]= 0;
for(i = 0;i < ROW_SIZE; i++)
{
for(j = 0; j < COLUMN_SIZE; j++)
{
if(maxVal < a[i][j])
{
location[0] = i;
location[1]= j;
maxVal= a[i][j];
}
}
}
cout << "The location of the largest element is at ("<< location[0] << " , "<<
location[1] <<" ) . it is : "<< maxVal<<endl;
}
max represents the maximum value of your matrix's elements, you first set it to be equal to the first element and then compare it to each element of the matrix. Each time you find an element that is larger than max, you assign his value to max and his position to location and at the end of the iterations, you have the largest value and his location.
I am writing some code that writes a matrix with a 10x10 size and prints it using functions. What I need is a function that takes the sum of the entire matrix array. What I have done is that I tried to sum up each row and column separately and add the total together. The problem with mine is that I believe that it only prints out the sum of the row or the column.
This is the code I have:
#include <iostream>
#include <ctime>
#include <iomanip>
#include <cstdlib>
using namespace std;
const int ROW_SIZE = 10;
const int COLUMN_SIZE = 10;
void initialize(int [][10], int, int);
void display(int matrix[][10], int, int);
void sum(int matrix[][COLUMN_SIZE], int, int);
int main() {
int matrix [ROW_SIZE][COLUMN_SIZE];
initialize(matrix, ROW_SIZE, COLUMN_SIZE);
display(matrix, ROW_SIZE,COLUMN_SIZE);
sum(matrix, ROW_SIZE,COLUMN_SIZE);
return 0;
}
void initialize(int matrix[][COLUMN_SIZE], int ROW_SIZE, int COLUMN_SIZE){
for (int i = 0; i < ROW_SIZE; i++){
for(int j = 0; j < COLUMN_SIZE; j++){
matrix[i][j] = 1 + rand() % 99;
}
}
}
void display(int matrix[][COLUMN_SIZE], int ROW_SIZE, int COLUMN_SIZE){
for(int i = 0; i < ROW_SIZE; i++){
for(int j = 0; j < COLUMN_SIZE; j++){
cout<< setw(4)<<matrix[i][j]<< " ";
}
cout<< endl;
}
}
void sum(int matrix[][COLUMN_SIZE], int ROW_SIZE, int COLUMN_SIZE){
int sum_row;
int sum_col;
for(int i = 0; i < ROW_SIZE; i++){
int sum_row = 0;
for(int j = 0; j < COLUMN_SIZE; j++){
sum_row = sum_row + matrix[i][j];
}
}
for(int i = 0; i < ROW_SIZE; i++){
int sum_col = 0;
for(int j = 0; j < COLUMN_SIZE; j++){
sum_col = sum_col + matrix[i][j];
}
}
int sum = sum_row + sum_col;
cout<<"The sum of the matrix is "<<sum<< endl;
}
What you have done is sum of last row + last column. This should be enough:
void sum(int matrix[][COLUMN_SIZE], int ROW_SIZE, int COLUMN_SIZE){
int sum = 0;
for(int i = 0; i < ROW_SIZE; i++){
for(int j = 0; j < COLUMN_SIZE; j++){
sum += matrix[i][j];
}
}
cout<<"The sum of the matrix is "<<sum<< endl;
}
few early problems in your code
void sum(int matrix[][COLUMN_SIZE], int ROW_SIZE, int COLUMN_SIZE){
int sum_row; // uninitialized make it equal to 0
int sum_col; // uninitialized make it equal to 0
for(int i = 0; i < ROW_SIZE; i++){
int sum_row = 0; // This is local to block and masks the sum_row above
for(int j = 0; j < COLUMN_SIZE; j++){
sum_row = sum_row + matrix[i][j];
}
}
for(int i = 0; i < ROW_SIZE; i++){
int sum_col = 0;
for(int j = 0; j < COLUMN_SIZE; j++){
sum_col = sum_col + matrix[i][j]; //Iterating over rows again matrix[j][i] may be
}
}
int sum = sum_row + sum_col;
cout<<"The sum of the matrix is "<<sum<< endl;
}
void sum(int matrix[][COLUMN_SIZE], int ROW_SIZE, int COLUMN_SIZE){
int sum_row = 0;
int sum_col = 0;
for(int i = 0; i < ROW_SIZE; i++)
for(int j = 0; j < COLUMN_SIZE; j++)
sum_row += matrix[i][j];
for(int i = 0; i < ROW_SIZE; i++)
for(int j = 0; j < COLUMN_SIZE; j++)
sum_col += matrix[j][i];
int sum = sum_row + sum_col;
cout<<"The sum of the matrix is "<<sum<< endl;
}
Now of course the question and logic doesn't seem to co-exist together and if sum of entire matrix means sum of all elements which is what it sounds like it can be achieved with the one here
void sum(int matrix[][COLUMN_SIZE], int ROW_SIZE, int COLUMN_SIZE){
int sum = 0;
for(int i = 0; i < ROW_SIZE; i++){
for(int j = 0; j < COLUMN_SIZE; j++){
sum += matrix[i][j];
}
}
cout<<"The sum of the matrix is "<<sum<< endl;
}
You're doing something wrong in terms of naming. You have constants called ROW_SIZE and COLUMN_SIZE and then you have parameters with the same names to functions like sum(). This is confusing for something reading your code. Call your parameters something else, like rows and columns.
Here is an alternative to the proposed solutions using std::accumulate().
const int ROW_SIZE = 3;
const int COLUMN_SIZE = 3;
void sum(int matrix[][COLUMN_SIZE], int const rows, int const cols)
{
int sum = std::accumulate(&matrix[0][0], &matrix[rows-1][cols], 0);
std::cout<<"The sum of the matrix is "<<sum<< std::endl;
}
int main()
{
int m[ROW_SIZE][COLUMN_SIZE] {{1,2,3}, {4,5,6}, {7,8,9}};
sum(m, ROW_SIZE, COLUMN_SIZE);
}
std::accumulate() takes the iterators that delimit a range. Those are the iterators to the first element and the one past the last element of the range. In this case, these are &matrix[0][0] and &matrix[row-1][col] (since &matrix[row-1][col-1] is the last element of the matrix). There are other ways to put that, such as the following:
auto begin = std::begin(matrix[0]);
int sum = std::accumulate(begin, begin + rows * cols, 0);
when I tried to multiple two negative numbers the value it is zero in c++,
for example -5 * -3
the result is zero,
why?
this is my code
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
void Multiply(const int v_arr[], const int m_arr[][3], int signed
o_arr[], int size)
{
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
o_arr[i] = 0;
for (int k = 0; k < 3; k++)
o_arr[i] += v_arr[k] * m_arr[k][i];
}
}
//End your code here
}
int main()
{
int n;
cin >> n;
int v_array[n];
int m_array[n][3];
int signed o_array[3];
for (int i = 0; i < n; i++) {
cin >> v_array[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++) {
cin >> m_array[i][j];
}
}
//fuction
Multiply(v_array, m_array, o_array, n);
for (int j = 0; j < 3; j++) {
cout << o_array[j] << " ";
}
return 0;
}
how to fix it to get the correct result?
the input is
2
2 -3
2 -3
2 -4
Your issue is here:
for (int k = 0; k < 3; k++)
o_arr[i] += v_arr[k] * m_arr[k][i];
}
You access elements at indices 0, 1 and 2 in v_arr, but it only has 2 elements. That's Undefined Behaviour.
Assuming this is matrix*vector multiplication code, it should look like this (untested):
for (int k = 0; k < 3; k++)
o_arr[k] += v_arr[i] * m_arr[i][k];
}
Also, your loop based on j is useless. You can remove it:
void Multiply(const int v_arr[], const int m_arr[][3], int signed o_arr[], int size)
{
for(int k = 0; k < 3; k++) { //initialize output array
o_arr[k] = 0;
}
for (int i = 0; i < size; i++) {
for (int k = 0; k < 3; k++)
o_arr[k] += v_arr[i] * m_arr[i][k];
}
}
Hello guys I need to know what should I pass to qsort function to make this work?
Everything else must stay as it is except arguments of qsort function.
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
void printMatrix(int **matrix, int n){
for(int i = 0; i<n; i++){
for(int j =0 ; j < n; j++)
cout<<matrix[i][j]<<" ";
cout<<endl;
}
}
void initMatrix(int **matrix, int n){
for(int i = 0; i<n; i++){
for(int j =0 ; j < n; j++){
matrix[i][j] = rand()%10;
}
}
}
int compar(const void *a, const void *b){
int ia = *((int*)a);
int ib = *((int*)b);
return ia-ib;
}
void deleteMatrix(int **matrix){
delete [] matrix;
}
int main()
{
cout<<"How many rows and cols?"<<endl;
int n;
cin>>n;
int **matrix;
matrix = new int* [n];
for(int j = 0; j < n; j++)
matrix[j] = new int[n];
initMatrix(matrix,n);
printMatrix(matrix,n);
cout<<"Sorted matrix:"<<endl;
qsort(matrix,n*n,sizeof(int),compar); //<-----------
printMatrix(matrix,n);
deleteMatrix(matrix);
return 0;
}
P.S
There must be ** pointer on matrix.
Thanks in advance!
You would probably be better off flattening the matrix to 1 dimension and using a step variable to determine where each row would start. Then sorting is easy.
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
void printMatrix(int *matrix, int matrixsize, int rowsize) {
for (int i = 0; i < matrixsize; i+= rowsize)
{
for (int j = 0; j < rowsize; j++)
{
cout << matrix[i + j] << " ";
}
cout << endl;
}
}
void initMatrix(int *matrix, int n)
{
for (int i = 0; i < n; i++)
{
matrix[i] = rand() % 10;
}
}
void deleteMatrix(int *matrix) {
delete[] matrix;
}
int main()
{
cout << "How many rows and cols?" << endl;
int row;
cin >> row;
int matrixSize = row*row;
int *matrix = new int[matrixSize];
initMatrix(matrix, matrixSize);
printMatrix(matrix, matrixSize, row);
cout << "Sorted matrix:" << endl;
sort(matrix,matrix + matrixSize); //<-----------
printMatrix(matrix, matrixSize, row);
deleteMatrix(matrix);
return 0;
}
Alright, I'm implementing a dynamic 2-dimensional matrix class. For a basis, this is what I have so far:
template <typename Type>
class dyMatrix {
private:
Type *mat;
int lines, columns;
int length;
void assimilate (const dyMatrix<Type>& that) {
lines = that.lines;
columns = that.columns;
length = that.length;
}
public:
dyMatrix (int height, int width)
: lines(height), columns(width), mat(0)
{
length = lines * columns;
mat = new Type[length];
};
// ---
int getLines() {
return lines;
};
int getColumns() {
return columns;
};
int getLength() {
return length;
}
// ---
Type& operator() (int i, int j) {
return mat[j * columns + i];
};
Type& operator() (int i) {
return mat[i];
};
// ---
dyMatrix (const dyMatrix<Type>& that) {
this->assimilate(that);
// ---
mat = new Type[length];
for (int i = 0; i < length; i++) {
mat[i] = that.mat[i];
}
};
dyMatrix& operator= (const dyMatrix<Type>& that) {
Type *local_mat = new Type[that.length];
for (int i = 0; i < that.length; i++) {
local_mat[i] = that.mat[i];
}
// ---
this->assimilate(that);
delete[] mat;
mat = local_mat;
// ----
return *this;
};
~dyMatrix() {
delete mat;
};
};
My problem is that I've been having some specific problems when trying to read input into it. For example, I have the following main():
int main() {
int lanes, cols;
cin >> lanes >> cols;
dyMatrix<int> map(lanes, cols);
/* CONTINUE READING */
cout << endl;
for (int i = 0; i < lanes; i++) {
for (int j = 0; j < cols; j++) {
cout << map(i, j) << ' ';
}
cout << endl;
}
}
Where it is a commented section, if I put the following lines of code:
int aux;
for (int i = 0; i < lanes; i++) {
for (int j = 0; j < cols; j++) {
cin >> map(i, j);
}
}
or:
int aux;
for (int i = 0; i < lanes; i++) {
for (int j = 0; j < cols; j++) {
cin >> aux;
map(i, j) = aux;
}
}
or even:
int aux;
for (int i = 0; i < lanes; i++) {
for (int j = 0; j < cols; j++) {
cin >> aux;
map(i, j) = i + j; // !!!
}
}
...It won't work. For some reason, however, the following will:
int aux;
for (int i = 0; i < lanes; i++) {
for (int j = 0; j < cols; j++) {
// cin >> aux;
map(i, j) = i + j; // Still the same thing as before, logically
}
}
I do not understand. What is happening here?
edit: Sorry, forgot to include my debugging procedure.
Input:
3 5
1 2 3 4 5
1 2 3 4 5
The first line has the dimensions of the matrix. The second and third lines are values written into it; After pressing Enter at the end of the third line, however, the program crashes.
It doesn't even work with this input:
3 5
1
2
3
4
5
1
2
3
4
5
It crashes right after the second 5, again.
Your indices appear the wrong way around:
Type& operator() (int i, int j) {
return mat[j * columns + i];
};
Here, i is clearly the column and j is the row. However, in your loops the arguments appear in the opposite order:
for (int i = 0; i < lanes; i++) {
for (int j = 0; j < cols; j++) {
map(i, j) = ...; // row then column
}
}
Also, the destructor should be using delete[] and not delete.