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];
}
}
Related
I'm trying to find the smallest of the biggest sum of each column of every possible permutations of a given 2D array NxN, where the values in each row can shift towards the left. For example, the array
4 6
3 7
would have 4 possibles permutations:
4 6 6 4 4 6 6 4
3 7 3 7 7 3 7 3
The biggest sum of each permutation is respectively, 13, 11, 11, 13. Thus the smallest of the biggest sums is 11. I have written a recursive function that should work, but for some reason, it only works for arrays that are smaller than 6x6... I'm new at programming, and just recently learned about recursion, any help or counsel on how to think recursively and to debug code would be greatly appreciated...
For the array 4x4
7410 1371 2665 3195
4775 4130 6499 3414
300 2092 4009 7638
5351 210 7225 7207
The answer is 18349, and my code gives me the correct answer.
However, for the array 6x6
5219 842 7793 2098 5109 2621
1372 3253 3804 5652 810 1620
4894 6792 1784 4335 4772 6656
3203 1070 4716 5335 1157 6855
5529 2767 2205 408 7516 7454
375 7036 2597 5288 937 2893
The answer should be 23733, but I've got 24176. How is this possible?
Here's my code:
#include <iostream>
using namespace std;
#define MAX_N 1000
int n, matrix[MAX_N][MAX_N], shift[MAX_N] = {0}, minSum = 100000000;
void possibTree(int position){
//Base case
if(position == n){
for (int i = 0; i < n; i++) {
// Temporary array to store the values in the row that just shifted towards the left
int temp[MAX_N] = {0};
for (int j = 0; j < n; j++) {
if(j - shift[i] < 0)
temp[n+(j-shift[i])] = matrix[i][j];
else
temp[j-shift[i]] = matrix[i][j];
}
for (int k = 0; k < n; k++)
matrix[i][k] = temp[k];
}
int max = 0;
for (int i = 0; i < n; i++) {
int temp = 0;
for (int j = 0; j < n; j++) {
temp += matrix[j][i];
}
if(temp > max)
max = temp;
}
if(minSum > max)
minSum = max;
return;
}
for (int i = 0; i < n; i++) {
shift[position] = i;
possibTree(position+1);
}
return;
}
int main() {
while(cin >> n){
memset(matrix, 0, sizeof(matrix));
memset(shift, 0, sizeof(shift));
if(n == -1) // The user enters "-1" to end the loop and terminate the program.
return 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> matrix[i][j];
}
}
possibTree(0);
cout << minSum << endl;
minSum = 100000000;
}
return 0;
}
Ok I believe I understand my mistake, I have to reset the matrix to its original state at the end of each base case, when the matrices are small, the code is still capable of finding all the possible biggest sums, but when the matrices got bigger, some of the possibilities weren't generated. Here's my code:
#include <iostream>
using namespace std;
#define MAX_N 1000
int n, matrix[MAX_N][MAX_N], OrigMatrix[MAX_N][MAX_N], shift[MAX_N] = {0}, minSum = 100000000;
void possibTree(int position){
//Base case
if(position == n){
for (int i = 0; i < n; i++) {
// Temporary array to store the values in the row that just shifted towards the left
int temp[MAX_N] = {0};
for (int j = 0; j < n; j++) {
if(j - shift[i] < 0)
temp[n+(j-shift[i])] = matrix[i][j];
else
temp[j-shift[i]] = matrix[i][j];
}
for (int k = 0; k < n; k++)
matrix[i][k] = temp[k];
}
int max = 0;
for (int i = 0; i < n; i++) {
int temp = 0;
for (int j = 0; j < n; j++) {
temp += matrix[j][i];
}
if(temp > max)
max = temp;
}
if(minSum > max)
minSum = max;
//EDITS
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = OrigMatrix[i][j];
}
}
return;
}
for (int i = 0; i < n; i++) {
shift[position] = i;
possibTree(position+1);
}
return;
}
int main() {
while(cin >> n){
memset(matrix, 0, sizeof(matrix));
memset(shift, 0, sizeof(shift));
if(n == -1) // The user enters "-1" to end the loop and terminate the program.
return 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> matrix[i][j];
}
}
//EDITS
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
OrigMatrix[i][j] = matrix[i][j];
}
}
possibTree(0);
cout << minSum << endl;
minSum = 100000000;
}
return 0;
}
Need to create a spiral matrix m * n, the elements of which are arranged counterclockwise m * n. The output should be something like this enter image description here.
My code works only for arrays of the form n * n
#include <iostream>
#include <iomanip>
using namespace std;
const int n = 4;
static int arr[n][n];
int main(int argc, const char * argv[]) {
int numb = 0;
for (int counter = 0; counter < n; ++counter) {
for (int i = counter; i < n-counter; ++i) arr[i][counter] = ++numb;
for (int j = counter+1; j < n-counter; ++j) arr[n-counter-1][j] = ++numb;
for (int i = n-counter-2; i >= counter; --i) arr[i][n-counter-1] = ++numb;
for (int j = n-counter-2; j > counter; --j) arr[counter][j] = ++numb;
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) cout << setw(3) << arr[i][j];
cout << endl;
}
return 0;
}
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;
}
This program is meant to take in a square matrix of integers and outputs the largest sub-square-matrix sum.
The first line of input is an integer which indicates the dimension of the square matrix, followed by the actual matrix row-by-row.
My program works almost perfectly except it does not work when using small matrices with negative values. Can anyone help me optimise the code, I cant see where its is going wrong
Example Input1:
3
1 2 3
4 5 6
7 8 9
Output: 45
Example Input2:
3
1 2 3
4 5 6
-7 -8 -9
Output: 16
NB: Since the largest square matrix is [2 3; 5 6] which sums to 16
My code:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int Numberofelements,n,counter = 0,sum=0,result = 0,Maximumvalue = -1, *pointervalue = NULL;
int count = 0;
cin>>n;
int mat[n][n];
int TempMatrix[n][n];
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
cin>>mat[i][j];
if(mat[i][j]<0){
count++;
}
}
}
for (int i = 0; i < n; ++i)
{
for (int j = 0; j< n; ++j)
{
sum = sum + mat[i][j];
}
if (sum > 0 )
{
counter++;
}
sum = 0;
}
Numberofelements = counter;
for (int j = 0; j < n; j++)
{
sum = 0;
for (int i = 0; i < Numberofelements; i++)
{
sum = sum + mat[i][j];
}
TempMatrix[0][j] = sum;
for (int i=1; i<n-Numberofelements+1; i++)
{
sum = sum+(mat[i+Numberofelements-1][j] - mat[i-1][j]);
TempMatrix[i][j] = sum;
}
}
for (int i=0; i<n-Numberofelements+1; i++)
{
sum = 0;
for (int j = 0; j < Numberofelements; j++)
{
sum = sum + TempMatrix[i][j];
}
if (sum > Maximumvalue)
{
Maximumvalue = sum;
pointervalue = &(mat[i][0]);
}
for (int j = 1; j < n-Numberofelements+1; j++)
{
sum = sum + (TempMatrix[i][j+Numberofelements-1] - TempMatrix[i][j-1]);
if (sum > Maximumvalue)
{
Maximumvalue = sum;
pointervalue = &(mat[i][j]);
}
}
}
for (int i = 0; i < Numberofelements; i++)
{
for (int j = 0; j < Numberofelements; j++)
{
result+=*(pointervalue + i*n + j);
}
}
cout << result;
return 0;
}
I want to count all the numbers in a 2d array and store the count into another array so can I can use the values in a histogram equalization.I am counting values that range from 0 to 255, so everytime a number for example 18 comes up in the 2d array I want to count how many 18's there are in the 2d array and then store the count into num[17]. Problem is I don't get the right amount. I know is due to the temp not being in the right place but I cannot figure out where to put it. Any help would be appreciated.
#include <iostream>
void histeq(int **pix, int height, int width) {
int num[255];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int temp = 0;
for (int k = 1; k <= 255; k++)
{
if (pix[i][j] == k)
{
temp = temp + 1;
}
num[k - 1] = temp;
cout << num[k - 1] << endl;
}
}
}
}
It's not very clear form your question, but my best guess is that you need this:
void histeq(int **pix, int height, int width) {
int num[256] = {0};
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
num[pix[i][j]] += 1;
}
}
for (int i = 0; i < 256; ++i)
{
cout << num[i] << endl;
}
}
#include <map>
void histeq(int **pix, int height, int width) {
std::map <int, int> num;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
num[pix[i][j]]++;
}
}
for (int i = 0; i < num.size(); i++)
cout << num[i] <<endl;
}
I got your expectation. You mean you want to count how many time number of arange[0:255] in 2d Array:
#include <iostream>
void histeq(int **pix, int height, int width) {
int num[255];
for (int k =1; k <= 255; k++)
{
int temp = 0;
for (int i = 0; i < height; i++){
for (int j = 0; j < width; j++){
if (pix[i][j] == k)
{
temp +=1;
}
}
}
num[k - 1] = temp;
cout << num[k - 1] << endl;
}
}