C++ contiguous memory operation - c++

I have c++ program in which I am calculating determinant of a matrix using normal array which is as follows:
/* rand example: guess the number */
#include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;
int** generateStandardMatrix(int dimension);
void ijMinor(int *matrix[], int *minorMatrix[], int size, int row, int column);
int determinant(int *matrix[], int size);
void ijMinor(int *matrix[], int *minorMatrix[], int size, int row, int column) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (i < row) {
if (j < column)minorMatrix[i][j] = matrix[i][j];
else if (j == column)continue;
else minorMatrix[i][j - 1] = matrix[i][j];
}
else if (i == row)continue;
else {
if (j < column)minorMatrix[i - 1][j] = matrix[i][j];
else if (j == column)continue;
else minorMatrix[i - 1][j - 1] = matrix[i][j];
}
}
}
}
int determinant(int *matrix[], int size) {
if (size == 1)return matrix[0][0];
else {
int result = 0, sign = -1;
for (int j = 0; j < size; j++) {
int **minorMatrix;
minorMatrix = new int*[size - 1];
for (int k = 0 ; k < size - 1 ; k++)
minorMatrix[k] = new int[size - 1];
ijMinor(matrix, minorMatrix, size, 0, j);
sign *= -1;
result += sign * matrix[0][j] * determinant(minorMatrix, size - 1);
for (int i = 0; i < size - 1; i++) {
delete minorMatrix[i];
}
}
return result;
}
}
int main (int argc, char* argv[])
{
/* initialize random seed: */
srand (time(NULL));
// int iSecret, iGuess;
int dimension = atoi(argv[1]);
int rowCount = dimension , colCount = dimension;
//2d array storing the integer values
int** ary = new int*[dimension];
//vector of vector storing the indices across the array for the threads to pick up from
vector<vector<int> > vec;
ary = generateStandardMatrix(dimension);
printf("Array value : %d\n", ary[0][0]);
int detVal = determinant(ary, dimension);
printf("determinant value : %d\n", detVal);
return 0;
}
int** generateStandardMatrix(int dimension) {
int** ary = new int*[dimension];
int counter = 0;
for (int i = 0; i < dimension; ++i) {
ary[i] = new int[dimension];
counter = counter + 1;
for (int j = 0; j < dimension; ++j)
{
ary[i][j] = counter;
std::cout << ary[i][j] << "\t" << std::flush;
}
std::cout << std::endl;
}
return ary;
}
I want to replace it with code in which I allocate memory for the array before the start of the algorithm and then change the determinant and the ijMonor functions so that they don't create new array's but use the same array only.
The determinant will take parameter like: determinant(int *matrix, int *startOfMyWorkspace, int size) so that it knows where to start.
I am not good at c++ and so far I was not able to do it.
Can someone please provide some sample code.
I allocated some memory for array and created and array but was unable to change the ijMinor and determinant functions for that.
This is how I am allocating memory:
int main (int argc, char* argv[])
{
/* initialize random seed: */
srand (time(NULL));
// int iSecret, iGuess;
int dimension = atoi(argv[1]);
int *a;
size_t const N_BYTES = dimension * dimension * sizeof(int);
a = (int*)malloc(N_BYTES);
createData(dimension,a);
return 0;
}
void createData(int const dimension, int* const a)
{
int row, col;
srand((unsigned)time(NULL));
int counter;
for(int row = 0; row < dimension; row++) {
counter = counter + 1;
for(int col = 0; col < dimension; col++) {
int i = col + row * dimension;
a[i] = counter;
std::cout << a[i] << "\t" << std::flush;
}
std::cout << std::endl;
}
}

Try this.
Note if you use new to allocate an array, you need to use delete[] to free all of it. You'll get away with delete (i.e. it won't crash) but this will only free the first element. Your other functions are the same as you posted.
You're dynamically allocating space for minorMatrix in determinant function, but it's hard to see how that could be preallocated. I've modified determinant function to use allocate_arr and deallocate_arr.
int ** allocate_arr(int dimension)
{
int** a = new int*[dimension];
for (int i = 0; i < dimension; ++i)
a[i] = new int[dimension];
return a;
}
void deallocate_arr(int dimension, int **a)
{
for (int i = 0; i < dimension; ++i)
delete[] a[i];
delete[] a;
}
int determinant(int *matrix[], int size) {
if (size == 1)return matrix[0][0];
else {
int result = 0, sign = -1;
for (int j = 0; j < size; j++) {
int **minorMatrix = allocate_arr(size - 1);
ijMinor(matrix, minorMatrix, size, 0, j);
sign *= -1;
result += sign * matrix[0][j] * determinant(minorMatrix, size - 1);
deallocate_arr(size - 1, minorMatrix);
}
return result;
}
}
void generateStandardMatrix(int dimension, int**ary) {
int counter = 0;
for (int i = 0; i < dimension; ++i) {
counter = counter + 1;
for (int j = 0; j < dimension; ++j)
{
ary[i][j] = counter;
std::cout << ary[i][j] << "\t" << std::flush;
}
std::cout << std::endl;
}
}
int main(int argc, char* argv[])
{
srand(time(NULL));
int dimension = atoi(argv[1]);
int** a = allocate_arr(dimension);
generateStandardMatrix(dimension, a);
printf("Array value : %d\n", a[0][0]);
int detVal = determinant(a, dimension);
printf("determinant value : %d\n", detVal);
// ... do more computations here, reusing `a` ...
deallocate_arr(dimension, a);
return 0;
}

Related

stack around a variabe corrupted plus programs stops after inputting certain value c++

My program is meant to generate a dynamic 2d array then sort then transpose the array(swtitching columns and rows) My problem is when i input a certain value(7) for the rows and columns a stack around the variable indices becomes corrupted furthermore my code starts generating numbers that make no since i think its because it some is out of bounds please help me im relalitvely new to c++
//
// C++ program template
//
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
void init_array(int ** array_in, int rows, int cols, int list[]);
void print_array(int ** array_in, int rows, int cols);
void selection_sort(int array_in[], int elements);
int ** transpose(int ** array_in, int ** array_out, int rows, int cols);
const unsigned int SIZE = 4000;
int count1 = 0;
int main(void)
{
int rows = 0, cols = 0, j = 0, k = 0;
int**numbers = nullptr;
int**arraytranspose = nullptr;
cout << "Enter rows and columns" << endl;
cin >> rows >> cols;
int length = rows * cols;
int list[4000] = { 0 };
numbers = new int*[rows];
arraytranspose = new int*[rows];
for (k = 0; k < cols; k++)
{
numbers[k] = new int[cols];
arraytranspose[k] = new int[cols];
}
// initialize the array with unique values
init_array(numbers, rows, cols, list);
print_array(numbers, rows, cols);
selection_sort(list, count1);
int count3 = 0;
for (int count2 = 0; count2 < 3999; count2++) {
for (int i = 0; i < rows; i++)
{
for (int c = 0; c < cols; c++)
{
if (list[count2] != 0)
{
numbers[i][c] = list[count2];
}
count2++;
}
}
}
print_array(numbers, rows, cols);
cout << endl << endl;
print_array(transpose(numbers,arraytranspose,rows,cols), rows, cols);
system("pause");
return 0;
}
void selection_sort(int array_in[], int elements)
{
int index = 0, smallest = 0, hold = 0, count = 0, location = 0;
for (index = 0; index < SIZE - 1; index++) // Loop to control number of passes
{
smallest = index;
// Find the index of the smallest element
for (location = index + 1; location < SIZE; location++)
{
if (array_in[location] < array_in[smallest])
{
smallest = location;
} // End If
} // End Inner for loop
hold = array_in[smallest];
array_in[smallest] = array_in[index];
array_in[index] = hold;
count++; // Count number of swaps
}
cout << "There were " << count << " element exchanges during the sort. " << endl << endl;
return;
}
void init_array(int ** array_in, int rows, int cols, int list[])
{
int j = 0, k = 0, value = 0;
int indices[4000] = { 0 };
count1 = 0;
while (j < rows)
{
k = 0;
while (k < cols)
{
value = rand() & 4000;
if (indices[value] != 1)
{
array_in[j][k] = value;
indices[value] = 1;
list[count1] = array_in[j][k];
k++;
count1++;
}
}// end while
j++;
}
return;
}
void print_array(int ** array_in, int rows, int cols)
{
int j = 0, k = 0;
for (j = 0; j < rows; j++) {
for (k = 0; k < cols; k++) {
cout << setw(5) << array_in[j][k];
}
cout << endl;
}
return;
}
int** transpose(int ** array_in, int ** array_out,int rows, int cols)
{
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < cols; c++)
{
array_out[r][c] = array_in[c][r];
}
}
return array_out;
}
numbers = new int*[rows];
arraytranspose = new int*[rows];
This allocates memory for a pair of arrays, an array of rows values.
Immediately afterwards:
for (k = 0; k < cols; k++)
{
numbers[k] = new int[cols];
arraytranspose[k] = new int[cols];
}
And this set the first cols values in these arrays, but they are rows values in size. So, if rows is less than cols, this results in memory corruption and undefined behavior, as the shown code writes to values of the array that do not exist.
This is the first obvious flaw in the shown code that's obvious from a cursory inspection, but it's likely there are other similar flaws as well; they generally result from unsafe programming practices like the ones shown here, like manual memory allocation, and lack of bounds checking. Modern C++ code offers plenty of safe programming practices, like using std::vectors to manage dynamically-sized arrays, and iterators.
Simply fixing this specific bug will be merely a bandaid, even if it turns out to be the only bug fix. Your real, long-term fix is to rewrite this entire code, and start using modern C++ containers, containers, and algorithms, which, when used correctly, will eliminate most opportunities for this entire class of bugs.

A function which will display the contents of an array being sorted c++ using insertion sort

I have error, which is highlighted "cout << array[i] << endl;" in this section. The line is under array[i]. The error is "argument list for class template "std::array" is missing ". i need a function to display the contents of an array, using an insertion sort. If this code is incorrect, does anyone know the code to output the contents of the array, using linear search.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
int numbers[SIZE] = { 6,3,1,9,4,12,17,2 };
for (int i = 0; i < 8; i++)
{
cout << array[i] << endl;
}
system("pause");
}
const int SIZE = 8;
void insertionSort(int numbers[], int arraySize)
{
int i, j, insert;
for (i = 1; i < arraySize; i++)
{
insert = numbers[i];
j = i;
while ((j > 0) && (numbers[j - 1] > insert))
{
numbers[j] = numbers[j - 1];
j = j - 1;
}
numbers[j] = insert;
}
}
You didn't call your function insertionSort(int numbers[], int arraySize) in main(). Therefore nothing will happen to the original array.
Note that you need a return 0; statement inside int main(). And that you need to use numbers[i] instead of array[i]. And you need to set your insertionSort() to return "something" or to pass your numbers[] as a reference. Also not to forget about the function prototype before main().
This should work:
const int SIZE = 8;
void insertionSort(int [], int);
int main()
{
int numbers[SIZE] = { 6,3,1,9,4,12,17,2 };
insertionSort(numbers, SIZE);
for (int i = 0; i < 8; i++)
cout << numbers[i] << endl;
system("pause");
return 0;
}
void insertionSort(int MyArray[], int size)
{
int i, j, insert;
for (i = 1; i < size; i++){
insert = MyArray[i];
j = i;
while ((j > 0) && (MyArray[j - 1] > insert)){
MyArray[j] = MyArray[j - 1];
j = j - 1;}
MyArray[j] = insert;}
}

C++ replacing matrix values with sub-matrix values

I have a University assignment whereby I have a 1D array, containing 262144 values. I've created a matrix class which places these values into an object with the datasource being the double* list of 262144 values.
I need to be able to obtain a sub-matrix (which I'm able to do) from ANOTHER set of 262144 values (which I've also placed into a matrix object).
However, I'm having serious trouble and I've been trying so hard for the last 3 days to try and replace original matrix values from a sub-matrix. I've tried passing by reference, creating Matrix*'s. I've tried everything we've been taught and even researched a few more methods, all of which I haven't understood. I'll throw my code in here to see if anyone can explain a method to me which will be able to do this.
Matrix::Matrix()
{
"Matrix::Matrix() is invoked";
}
Matrix::Matrix(const Matrix& m)
{
"Matrix::Matrix(const Matrix&) is invoked";
_M = m._M;
_N = m._N;
_data = new double[_M*_N];
for (int i = 0; i < _M*_N; i++)
{
_data[i] = m._data[i];
}
}
Matrix::Matrix(int sizeR, int sizeC, double *input_data)
{
"Matrix::Matrix(int sizeR, int sizeC, double *input_data is invoked";
_M = sizeR;
_N = sizeC;
_data = new double[_M*_N];
for (int i = 0; i < _M*_N; i++)
{
_data[i] = input_data[i];
}
}
Matrix Matrix::get_Block(int start_row, int end_row, int start_coloumn, int end_coloumn)
{
int rows = (end_row - start_row);
int columns = (end_coloumn - start_coloumn);
int ctr = 0;
double *temp_Data = new double[rows*columns];
for (int x = start_row; x < (rows + start_row); x++)
{
for (int y = start_coloumn; y < (columns + start_coloumn); y++)
{
temp_Data[ctr] = get(x, y);
ctr++;
}
}
Matrix block(rows, columns, temp_Data);
delete[] temp_Data;
return block;
}
Matrix Matrix::operator+(const Matrix & other)
{
Matrix temp;
temp._M = other._M;
temp._N = other._N;
temp._data = new double[temp._M*temp._N];
for (int x = 0; x < (temp._M*temp._N); x++)
{
temp._data[x] = this->_data[x] + other._data[x];
}
return temp;
}
Matrix Matrix::operator*(const Matrix & other)
{
Matrix temp;
temp._M = other._M;
temp._N = other._N;
temp._data = new double[temp._M*temp._N];
for (int x = 0; x < (temp._M*temp._N); x++)
{
temp._data[x] = this->_data[x] * other._data[x];
}
return temp;
}
Matrix Matrix::operator-(const Matrix & other)
{
Matrix temp;
temp._M = other._M;
temp._N = other._N;
temp._data = new double[temp._M*temp._N];
for (int x = 0; x < (temp._M*temp._N); x++)
{
temp._data[x] = this->_data[x] - other._data[x];
}
return temp;
}
void Matrix::replace_Block(Matrix& noisy, Matrix& shuffled,int k, int j, int i)
{
int val_to_replace = 0;
for (int i = 0; i < 3 * 3; i++)
{
val_to_replace = shuffled.get(i, j);
noisy.set(i, j, val_to_replace);
}
}
void Matrix::set_Block(Matrix block, Matrix& Noisy, int start_row, int end_row)
{
int ctr = 0;
int ctr2 = 0;
int ctr3 = 0;
for (int i = 0; i < 3; i++)
{
Noisy._data[(start_row*_M)+i+4] = block.get(i, ctr);
ctr++;
}
for (int j = 0; j < 3; j++)
{
Noisy._data[((start_row + 1)*_M) + j + 3] = block.get(j, ctr2);
ctr2++;
}
for (int j = 0; j < 3; j++)
{
Noisy._data[((start_row + 1)*_M) + j + 2] = block.get(j, ctr3);
ctr3++;
}
}
double Matrix::get_Sum(Matrix m)
{
double total = 0;
short row = m.get_M();
short column = m.get_N();
for (int j = 0; j < row; j++)
{
for (int i = 0; i < column; i++)
{
total += m.get(j,i);
}
}
return total;
}
double Matrix::get_Sum(Matrix* m)
{
double total = 0;
short row = m->get_M();
short column = m->get_N();
for (int j = 0; j < row; j++)
{
for (int i = 0; i < column; i++)
{
total += m->get(i, j);
}
}
return total;
}
double Matrix::get(int i, int j)
{
return _data[(i * _M) + j];
}
void Matrix::write_Block(int i, int j)
{
for (int ctr = 0; ctr < i; ctr++)
{
for (int ctr2 = 0; ctr2 < j; ctr2++)
{
std::cout << " " << this->get(ctr,ctr2);
}
std::cout << std::endl;
}
}
void Matrix::set(int i, int j, double val)
{
this->_data[(i*_M) + j] = val;
}
void Matrix::set_N(int N)
{
_N = N;
}
void Matrix::set_M(int M)
{
_M = M;
}
int Matrix::get_N()
{
return _N;
}
int Matrix::get_M()
{
return _M;
}
Matrix::~Matrix()
{
"Matrix::~Matrix() is invoked";
delete[] _data;
}
If it would be helpful to see main() I can supply that too, however all it really contains is the creation of the matrix objects using overloaded constructors.
explanation
Answer is only 4 years late . . .
Anyway. Maybe it will help somebody else. The secret is to use a std::valarray. With that it is utmost simple to work on a matrix. And, many many functions are available.
All the functions that you want to implement are already available.
And you sub-matrix coy can be a one liner . . .
Please see example code:
#include <iostream>
#include <algorithm>
#include <numeric>
#include <valarray>
#include <iomanip>
constexpr size_t NRows = 6;
constexpr size_t NCols = 8;
constexpr size_t SubNRows = 2;
constexpr size_t SubNCols = 3;
void debugPrint(std::valarray<int> &v, size_t nrows = NRows, size_t ncols = NCols)
{
for (int r = 0; r < nrows; ++r) {
for (int c = 0; c < ncols; ++c)
std::cout << std::setw(3) << v[r*ncols+c] << ' ';
std::cout << '\n';
}
std::cout << '\n';
}
int main()
{
std::valarray<int> v1(NRows * NCols); // Define array with given size
std::iota(std::begin(v1),std::end(v1),0); // Fill the array with consecutive nunbers
debugPrint (v1); // Print the result
std::cout << "\nSum = " << v1.sum() << "\n\n"; // Print the sum of all values in matrix
std::valarray<int> v2(v1); // Create a 2nd matrix as a copy to the first
v2 += 100; // Add 100 to each value in the matrix
debugPrint(v2);
std::valarray<int> v3(NCols); // Get one column
v3 = v1[std::slice(2,NRows,NCols)];
debugPrint(v3,NRows,1);
std::valarray<int> subV2(SubNRows*SubNCols); // So, now the sub array
subV2 = v2[std::gslice(12,{SubNRows, SubNCols},{NCols,1})]; // Slice it out
debugPrint(subV2, SubNRows, SubNCols);
v1[std::gslice(25,{SubNRows, SubNCols},{NCols,1})] = subV2; // And copy to the first array
debugPrint (v1);
return 0;
}

Shouldn't this recursive method end as soon as you get to the return?

I would like to know why my output is:
a-b-b-b-b-0
When I think it should just be a-1.
Shouldn't a recursive method end as soon as you get to a return? and why doesn't it here?
I only put the letters with '-' to clarify that the returns are being met but not stopping there.
#include <stdio.h>
#include <iomanip>
#include <iostream>
using namespace std;
void printv(int mask[], int elements[], int n)
{
int i;
printf("{ ");
for (i = 0; i < n; i++)
if (mask[i])
printf("%d ", elements[i]);
printf("}");
}
int next(int mask[], int size)
{
int i;
for (i = 0; (i < size) && mask[i]; i++)
mask[i] = 0;
if (i < size) {
mask[i] = 1;
return 1;
}
return 0;
}
void nSubsets(int mask[], int elements[], int size, int n)
{
int sum = 0;
int temp[10], count = 0;
for (int i = 0; i < 10; i++) //this MUST be here
temp[i] = 0;
for (int i = 0; i < size; i++)
{
if (mask[i])
{
count++;
for (int k = 0; k < 44; k++)
if (temp[k] == 0)
{
temp[k] = elements[i];
sum += elements[i];
break;
}
}
}
if (sum == n)
{
cout << "{ ";
for (int l = 0; l < count; l++)
cout << temp[l] << " ";
cout << "}";
}
}
int isEmptySet(int mask[], int elements[], int size, int n, int sizeRecursion)
{
int sum = 0;
int temp[10], count = 0;
for (int i = 0; i < 10; i++) //this MUST be here
temp[i] = 0;
for (int i = 0; i < size; i++)
{
if (mask[i])
{
count++;
for (int k = 0; k < 44; k++)
if (temp[k] == 0)
{
temp[k] = elements[i];
sum += elements[i];
break;
}
}
}
if (sum == n)
{
cout << "a-";
return 1;
}
sizeRecursion--;
if (sizeRecursion > 0)
{
next(mask, size);
isEmptySet(mask, elements, size, n, sizeRecursion);
}
cout << "b-";
return 0;
}
int main()
{
int n, size = 10;
int elements[size];
size = 6; n = 5;
elements[0] = 5;
elements[1] = 2;
elements[2] = 3;
elements[3] = 2;
elements[4] = 1;
elements[5] = 1;
int mask[10];
int i;
for (i = 0; i < size; ++i)
mask[i] = 0;
cout << "Subsets of elements: ";
printv(mask, elements, size); //this prints first subset
while (next(mask, size))
printv(mask, elements, size);
n = 3;
cout << "\nSubsets equal to " << n << "\n";
while (next(mask, size))
nSubsets(mask, elements, size, n);
cout << "\n" << isEmptySet(mask, elements, size, n, size);
return 0;
}
Shouldn't a recursive method end as soon as you get to a return? and why doesn't it here?
No, the way it works in general is that when a function call returns, it only returns for that function call, and then the immediate caller may continue execution. It doesn't matter whether the function is recursive or not, each function call is separate and each call needs to hit a return statement at some point (unless the return type is void).
When you have this code
...
if(sizeRecursion > 0)
{
next(mask,size);
isEmptySet(mask, elements, size, n,sizeRecursion);
}
cout<<"b-";
return 0;
}
What's going to happen is, as soon as the recursive call to isEmptySet returns, its going to go right to the cout << "b-"; line and then to return 0;. If you don't want that then you should put those in an else block, and maybe also modify the line that calls isEmptySet so that it returns the value returned from that call.

MPI scatterv gatherv

In the (semplified) code below, I have a class whose first member is a pointer to pointers (a dynamic matrix), I want to know how to use scatterv and gatherv. My target is to split the matrix "horizontally" using the pointers to the rows.
class:
#include <iostream>
using namespace std;
class Mat{
private:
int **m;
unsigned cols;
unsigned rows;
void allocate_mem(int ***ptr, unsigned r, unsigned c){
*ptr = new int *[r];
(*ptr)[0] = new int[r*c];
for(unsigned i = 1; i < r; i++)
(*ptr)[i] = (*ptr)[0] + i*c;
}
public:
Mat(){
rows = 1;
cols = 1;
allocate_mem(&m, 1, 1);
m[0][0] = 0;
}
Mat(unsigned n_rows, unsigned n_cols){
rows = n_rows;
cols = n_cols;
allocate_mem(&m, rows, cols);
for(unsigned i = 0; i < rows; i++)
for(unsigned j = 0; j < cols; j++)
m[i][j] = i+j;
}
Mat &operator=(const Mat &mrx){
int **new_ptr = NULL;
allocate_mem(&new_ptr, mrx.rows, mrx.cols);
for(unsigned i = 0; i < mrx.rows; i++)
for(unsigned j = 0; j < mrx.cols; j++)
new_ptr[i][j] = mrx[i][j];
delete[] m[0];
delete[] m;
m = new_ptr;
rows = mrx.rows;
cols = mrx.cols;
return *this;
}
const int *operator[](unsigned n) const{
return m[n];
}
void create_output(){
for(unsigned i = 0; i < cols; i++){
for(unsigned j = 0; j < rows; j++){
cout << m[i][j] << "\t";
}
cout << "\n";
}
}
~Mat(){
delete[] m[0];
delete[] m;
}
};
main:
#include "Mat.cpp"
#include <mpi.h>
#include <iostream>
using namespace std;
int main(int argc, char **argv){
MPI_Init(&argc, &argv);
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
int p;
MPI_Comm_size(MPI_COMM_WORLD, &p);
Mat m;
if(rank==0){
m = Mat(10,10);
cout << "First:" << endl;
m.create_output();
}
MPI_Barrier(MPI_COMM_WORLD);
//MPI_Scatterv();
//MPI_Gatherv();
if(rank==0){
cout << "Second:" << endl;
m.create_output();
}
MPI_Finalize();
return 0;
}