Why is this printing memory addresses instead of values - c++

Here is my driver
int square[2][2] = {
{1,2},
{3,4}
};
matrixMulti(square, 2);
Here is my function
void matrixMulti(const int a[][2], const int rows) {
int b[2][2];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
b[i][j] += a[i][k] * a[k][j];
}
}
}
cout << "new matrix " << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
cout << b[i][j] << " ";
}
cout << endl;
}
}
The output is:
new matrix
-858993453 -858993450
-858993445 -858993438
I am confused as to why it is printing the memory addresses rather then the values stored in them and what can be done to get it to print the values rather than the memory address.

You wrote:
int b[2][2];
and then
b[i][j] += ...
Where did you initialize b ?
int b[2][2] = {};
or
int b[2][2] = {0,0};
Should do the job.

Garbage values are being printed, not the address of the array.
Here is why.
In this snippet of code
void matrixMulti(const int a[][2], const int rows) {
int b[2][2];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
b[i][j] += a[i][k] * a[k][j];
}
}
}
You allocated space for int b[2][2], but you do not initialize it to a value. This would be fine except in the for-loop body.
b[i][j] += a[i][k] * a[k][j]; <=> b[i][j] = b[i][j] + a[i][k] * a[k][j];
you are referencing the value of b[i][j] before it is initialized. In other words you say b[i][j] is equal to b[i][j] + a[i][k] * a[k][j], but you can't do this as, the value of b[i][j] is unknown.
The solution to this is to initialize int b[2][2] = {{0,0},{0,0}};

Related

Find the column with the maximum negative matrix element and the column with the minimum element

I want to know the column with the maximum negative matrix element and the column with the minimum element so that I can rearrange them. More specifically, I'm interested in return values of The column with the maximum negative element: and The column with the minimum element: But about half the time, the results about which columns they are return completely wrong. The interesting thing is that the results don't always come back wrong. What am I doing wrong?
#include <iostream>
#include <time.h>
#include <cmath>
using namespace std;
int main(void) {
// random number for rand()
srand(time(0));
int m = 5; // row count
int n = 5; // column count
// declaration of a dynamic array of pointers
double **arr = (double**) malloc(n * sizeof(double));
// filling the array with pointers
for (int i = 0; i < n; i++)
arr[i] = (double*) malloc(m * sizeof(double));
// array initialization with random numbers
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
arr[i][j] = (double) (rand() % 400 - 199) / 2.0; // (-100.0; 100.0)
// matrix output
cout << "\n\033[92mOriginal array:\033[94m" << endl;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
printf("%5.1f ", arr[i][j]);
cout << endl;
}
// array for the sums of modules of the row elements
float *sumOfAbsolutes = (float*) malloc(m * sizeof(float));
// Initializing the array with zeros
for (int i = 0; i < m; i++) sumOfAbsolutes[i] = 0;
// filling the array with the sums of element modules
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
sumOfAbsolutes[i] += abs(arr[i][j]);
// output
cout << "\n\033[92mSums of modules of array row elements:" << endl;
for (int i = 0; i < m; i++)
cout << "\033[92m" << i << ": \033[94m"<< sumOfAbsolutes[i] << " ";
cout << "\n\n";
// sorting
for (int i = 0; i < (m - 1); i++)
for (int j = i; j < m; j++)
if (sumOfAbsolutes[i] > sumOfAbsolutes[j]) {
double tmp = sumOfAbsolutes[i];
sumOfAbsolutes[i] = sumOfAbsolutes[j];
sumOfAbsolutes[j] = tmp;
double *tmp2 = arr[i];
arr[i] = arr[j];
arr[j] = tmp2;
}
// matrix output
cout << "\033[92mSorted array:\033[94m" << endl;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
printf("%5.1f ", arr[i][j]);
cout << endl;
}
int columnWithMaxNegNum = 0; // the column with the maximal negative element
int minNumber = 0; // the column with the minimum element
// search for the maximal negative element
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (arr[i][j] < 0 && arr[i][j] > arr[i][columnWithMaxNegNum])
columnWithMaxNegNum = j;
// minimum element search
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (arr[i][j] < arr[i][minNumber]) minNumber = j;
cout << "\n\033[92mThe column with the maximum negative element: \033[94m" << columnWithMaxNegNum << endl;
cout << "\033[92mThe column with the minimum element: \033[94m" << minNumber << endl;
// rearrangement of columns
for (int i = 0; i < m; i++) {
double temp = arr[i][columnWithMaxNegNum];
arr[i][columnWithMaxNegNum] = arr[i][minNumber];
arr[i][minNumber] = temp;
}
cout << "\n\033[92mRearrangement of columns:" << endl;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
printf("\033[94m%5.1f ", arr[i][j]);
cout << "\n\033[0m";
}
// memory cleanup
free(sumOfAbsolutes);
for (int i = 0; i < n; i++)
free(arr[i]);
free(arr);
}
I assume that this is some kind of homework, since normally, we'd use vectors and algorithms to write much shorter code.
The following loop doesn't find the maximal negative:
// search for the maximal negative element
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (arr[i][j] < 0 && arr[i][j] > arr[i][columnWithMaxNegNum])
columnWithMaxNegNum = j;
Because the the search is dependent of the line. You'd need to go for:
double maxNegNum = -100.0; // tbd
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (arr[i][j] < 0 && arr[i][j] > maxNegNum) {
columnWithMaxNegNum = j;
maxNegNum = arr[i][j];
}
The same applies for the minimum :
double minN=100.0;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (arr[i][j] < minN) {
minNumber = j;
minN = arr[i][j];
}
Not related: if you go for C++ forget malloc and free, and use new/delete and new[]/delete[] instead. Here a first attempt: Online demo
Caution/Limitations:
If by no negative number are found, the maximum negative value would be inaccurate.
If several equal maximum negative values and minimum values are found, only the first one( from left to right and to bottom) will be considered.

C++ multiplying matrices using dynamically allocated memory

I am trying to write a function in C++ that multiplies two matrices A, B which have been dynamically allocated. I am currently trying to get the multiplication code working, then I will attempt to turn it into a function. Right now I am getting an error of sorts; "segmentation fault (core dumped)". I have narrowed it down to the multiplication part of my code but I have no idea what is wrong with it. Can someone help me please? My code is shown below.
#include <iostream>
#include <cassert>
int main()
{
int rowsA = 5; // number of rows
int colsA= 3; // number of coloumns
// dynamically allocating A
double** A;
A = new double* [rowsA];
A[0] = new double [rowsA*colsA];
for (int i = 1; i < rowsA; i++)
{
A[i] = A[i-1] + colsA;
}
// Storing elements of matrix A
for(int i = 0; i < rowsA; ++i)
{
for(int j = 0; j < colsA; ++j)
{
std::cout << "Enter element A" << i + 1 << j + 1 << " : ";
std::cin >> A[i][j];
}
}
int rowsB = 3; // number of rows
int colsB = 5; // number of coloumns
// dynamically allocating B
double** B;
B = new double* [rowsB];
B[0] = new double [rowsB*colsB];
for (int i = 1; i < rowsB; i++)
{
B[i] = B[i-1] + colsB;
}
// Storing elements of matrix B
for(int i = 0; i < rowsB; ++i)
{
for(int j = 0; j < colsB; ++j)
{
std::cout << "Enter element B" << i + 1 << j + 1 << " : ";
std::cin >> B[i][j];
}
}
// checking matrix multiplication qualification
assert(colsA == rowsB);
// dynamically allocating C
double** C;
C = new double* [rowsA];
C[0] = new double [rowsA*colsB];
for (int i = 1; i < rowsA; i++)
{
C[i] = C[i-1] + colsB;
}
// Initializing elements of matrix C to 0
for(int i = 0; i < rowsA; ++i)
{
for(int j = 0; j < colsB; ++j)
{
C[i][j]=0;
}
}
// multiplication
for(int i = 0; i < rowsA; ++i)
{
for(int j = 0; j < colsB; ++j)
{
for(int k = 0; k < colsB; ++k)
{
C[i][j] += A[i][k] * B[k][j];
}
}
}
// Displaying the multiplication of matrices A, B
std::cout<< "Matrix C: " << std::endl;
for(int i = 0; i < rowsA; ++i)
{
for(int j = 0; j < colsB; ++j)
{
std::cout << " " << C[i][j];
if(j == colsB-1)
{
std::cout << std::endl;
}
}
}
// deallocation
delete[] C[0];
delete[] C;
delete[] B[0];
delete[] B;
delete[] A[0];
delete[] A;
}
First, you say you are allocating the matrix properly but I'm not seeing any proof of that. You are allocating an array of pointers and only initializing the first index.. A[0] for example. This is incorrect. You need to allocate EACH ROW.
You have:
double** A;
A = new double* [rowsA];
A[0] = new double [rowsA*colsA]; //Incorrect. You only allocated A[0].
You need to allocate A[0] through A[rowsA - 1]..
Next, your multiplication loop (the inner most loop) is incorrect. It should be:
Iterate ColsA for that inner loop.
You have:
for(int i = 0; i < rowsA; ++i)
{
for(int j = 0; j < colsB; ++j)
{
for(int k = 0; k < colsB; ++k) //ColsB is incorrect. Should be colsA..
{
C[i][j] += A[i][k] * B[k][j]; //Segfaults here due to bad iteration..
}
}
}
The following would be correct:
#include <iostream>
#include <cassert>
double** create_matrix(int rows, int cols)
{
double** mat = new double* [rows]; //Allocate rows.
for (int i = 0; i < rows; ++i)
{
mat[i] = new double[cols](); //Allocate each row and zero initialize..
}
return mat;
}
void destroy_matrix(double** &mat, int rows)
{
if (mat)
{
for (int i = 0; i < rows; ++i)
{
delete[] mat[i]; //delete each row..
}
delete[] mat; //delete the rows..
mat = nullptr;
}
}
int main()
{
int rowsA = 5; // number of rows
int colsA= 3; // number of coloumns
double** matA = create_matrix(rowsA, colsA);
int rowsB = 3; // number of rows
int colsB = 5; // number of coloumns
double** matB = create_matrix(rowsB, colsB);
//Checking matrix multiplication qualification
assert(colsA == rowsB);
double** matC = create_matrix(rowsA, colsB);
//Multiplication
for(int i = 0; i < rowsA; ++i)
{
for(int j = 0; j < colsB; ++j)
{
for(int k = 0; k < colsA; ++k) //ColsA..
{
matC[i][j] += matA[i][k] * matB[k][j];
}
}
}
//Clean up..
destroy_matrix(matA, rowsA);
destroy_matrix(matB, rowsB);
destroy_matrix(matC, rowsA);
}

Adding two 2D arrays together in C++ - Why do this program crash?

Hey I am beginner at C++ programming. I have made a program that is meant to add two 2D arrays together. However, The program outputs the values until the program crashes. Can someone help me to identify the problem?
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
int a[10][10], c[10][10], i, j;
for (i = 1; i <= 10; ++i)
{
for(j=0; j < 10; ++j)
{
a[i][j] = i * j;
}
}
// We are able to treat the individual columns as arrays
for (int i = 0; i < 10; ++i)
{
int *b = a[i];
for (int j = 0; j < 10; ++j)
{
cout << b[j] << " ";
}
cout << endl;
}
cout << "****" << endl;
// Declare a multidimensional array on the heap
int **b = new int*[10];
// need to allocate all members individually
for (int i = 0; i < 10; ++i)
{
b[i] = new int[10];
}
// Set the values of b
for (int i = 0; i < 10; ++i)
{
for (j = 0; j < 10; ++j)
{
b[i][j] = (i * 10) + j;
}
}
for (i = 0; i < 10; ++i)
{
for (j = 1; j <= 10; ++j)
{
c[i][j] = a[i][j] + b[i][j];
}
}
for (i = 0; i < 10; ++i)
{
for (j = 1; j <= 10; ++j)
{
cout << c[i][j] << endl;
}
}
// Delete the multidimensional array - have to delete each part
for (int i = 0; i < 10; ++i)
{
delete[] b[i];
}
delete[] b;
return 0;
}
I corrected your code.Now, It's working and program didn't crash. You can try it out.
#include<conio.h>
#include<iostream.h>
int main(int argc, char** argv)
{
int a[10][10], c[10][10], i, j;
for (i = 0; i <10; ++i)
{
for(j=0; j < 10; ++j)
{
a[i][j] = i * j;
}
}
//We are able to treat the individual columns as arrays;
for (i = 0; i < 10; ++i)
{
int *b = a[i];
for (int j = 0; j < 10; ++j)
{
cout << b[j] << " ";
}
cout << endl;
}
cout << "****" << endl;
//Declare a multidimensional array on the heap;
int **b = new int*[10];
//need to allocate all members individually
for (i = 0; i < 10; ++i)
{
b[i] = new int[10];
}
//Set the values of b
for ( i = 0; i < 10; ++i)
{
for (j = 0; j < 10; ++j)
{
b[i][j] = (i * 10) + j;
}
}
for (i = 0; i < 10; ++i)
{
for (j = 0; j <10; ++j)
{
c[i][j] = a[i][j] + b[i][j];
}
}
for (i = 0; i < 10; ++i)
{
for (j = 0;j < 10; ++j)
{
cout << c[i][j] << " ";
}
cout<<endl;
}
// Delete the multidimensional array - have to delete each part
for (i = 0; i < 10; ++i)
{
delete[] b[i];
}
delete[] b;
return 0;
}

Solving an equation in an array with loop

An array of 'N' integers, it is necessary to calculate the next equation:
у = х1 * (х1 + х2) * (х1 + х2 + х3) * ... * (x1 + ... + xN)
I have two questions:
Is there a better way to find the solution y?
How to generate random number except of 0?
Code:
srand(time(NULL));
const int size = 10;
int arr[size];
int pro=1;
for (int i = 0; i < size; i++)
{
arr[i] = rand() % 10;
}
for (int i = 0; i < size; i++)
{
cout << arr[i] << ' ';
}
cout << endl;
for (int i = 0; i < size; i++)
{
int sum = 0;
for (int j = 0; j <= i; j++)
{
sum = arr[j];
}
pro *= sum;
}
cout << pro << endl;
How to generate random number except 0?
arr[i] = rand() % 9 + 1; // rando number in range 1 .. 9
Is there a better way to find the solution y?
long pro = 1;
long sum = 0;
for (int i = 0; i < size; i++)
{
sum += arr[i]; // i==0: x1, i==1: x1+x2, i==3: x1+x2+x3, ....
pro *= sum;
}
This should be your code:
srand(time(NULL));
const int size = 10;
int arr[size];
for (int i = 0; i < size; i++)
arr[i] = rand() % 9 + 1;
for (int i = 0; i < size; i++)
cout << arr[i] << ' ';
cout << endl;
long pro = 1;
long sum = 0;
for (int i = 0; i < size; i++)
{
sum += arr[i];
pro *= sum;
}
cout << pro << endl;

Position 2D array bug as parameter causes memory dumps

This is my program in C++, which accepts an 2D array a[m][n]. If an element a[i][j] is zero, then set all the ith row and jth column elements to zero.
This is code sample:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class SetZero{
public:
static void setZero(int **, int , int);
};
void SetZero::setZero(int ** a, int m, int n){
int i, j, k;
int ** b = new int *[m]; //flags to identify whether set to zero or not.
for(i = 0; i < m; i++){
b[i] = new int[n];
for(j = 0; j < n; j++)
b[i][j] = 1;
}
for(i = 0; i < m; i++)
for(j = 0; j < n; j++)
if(a[i][j] == 0 && b[i][j]){//DUMP here. If I change it to (a+i)[j], then works.
for (k = 0; k < n; k++){
a[i][k] = 0;//but there is NO dump here. Weird!
b[i][k] = 0;
}
for(k = 0; k < m; k++){
a[k][j] = 0;
b[k][j] = 0;
}
j = n;//break. next row loop.
}
for(int i = 0; i < m; i++)
delete[] b[i];
delete[] b;
}
int main(){
int a[4][5];
srand(time(NULL));
for(int i = 0; i < 4; i++){//create an 2D array
for(int j = 0; j < 5; j++){
a[i][j] = rand() % 100;
cout << a[i][j] << " ";
}
cout << endl;
}
SetZero::setZero((int **)a, 4, 5);//type cast.
cout << endl;
for(int i = 0; i < 4; i++){//print result
for(int j = 0; j < 5; j++)
cout << a[i][j] << " ";
cout << endl;
}
return 0;
}
Environment: WIN8 Visual Studio 2012.
Edit:
The program can compile but cannot execute normally. It will stop when it reaches if(a[i][j] == 0 && b[i][j]){
The error message is:
Unhandled exception at 0x012875DD in CCLC.exe: 0xC0000005: Access
violation reading location 0x0000004B.
SetZero::setZero((int **)a, 4, 5)
a is not an array of pointers, it is simply a 2 dimensional array.
notice how the access violation is reading address 0x0000004B? that's 75, a number between 0 and 99 :) because you are treating a 2 dimensional array (which is just a one dimensional array with a neat way of accessing it) as an array of arrays, it is taking one of the values in your array (75) to be the address of a sub array, then trying to read the non existent array at address 75 (or 0x0000004B)
I suggest that you 'flatten' your arrays and work with them as one dimensional arrays, which I find simpler:
void SetZero::setZero(int * a, int m, int n){
int i, j, k;
int * b = new int [m*n]; //flags to identify whether set to zero or not.
for(i = 0; i < m; i++){
b[i] = new int[n];
for(j = 0; j < n; j++)
b[i*n+j] = 1;
}
for(i = 0; i < m; i++)
for(j = 0; j < n; j++)
if(a[i*n+j] == 0 && b[i*n+j]){//DUMP here. If I change it to (a+i)[j], then works.
for (k = 0; k < n; k++){
a[i*n+k] = 0;//but there is NO dump here. Weird!
b[i*n+k] = 0;
}
for(k = 0; k < m; k++){
a[k*n+j] = 0;
b[k*n+j] = 0;
}
j = n;//break. next row loop.
}
delete[] b;
}
int main(){
int a[4*5];
srand(time(NULL));
for(int i = 0; i < 4; i++){//create an 2D array
for(int j = 0; j < 5; j++){
a[i*5+j] = rand() % 100;
cout << a[i*5+j] << " ";
}
cout << endl;
}
SetZero::setZero(a, 4, 5);//type cast.
cout << endl;
for(int i = 0; i < 4; i++){//print result
for(int j = 0; j < 5; j++)
cout << a[i*5+j] << " ";
cout << endl;
}
return 0;
}
One suggestion about the SetZero(). There is a function called memset() which allows you to set all bytes to a specific value given a starting pointer and the range. This function could make your SetZero() function more cleaner:
void * memset ( void * ptr, int value, size_t num );
Fill block of memory. Sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char).
Parameters
ptr: Pointer to the block of memory to fill.
value: Value to be set. The value is passed as an int, but the function fills the block of memory using the unsigned char conversion of this value.
num: Number of bytes to be set to the value, size_t is an unsigned integral type.
For example, the following code block from your program:
for (k = 0; k < n; k++){
a[i][k] = 0;//but there is NO dump here. Weird!
b[i][k] = 0;
}
can be achieved by memset in a cleaner way:
memset(a[i], 0, n * sizeof(int));
memset(b[i], 0, n * sizeof(int));