Error on the result vector - c++

My problem is that when I make the multiplication it only multiplies the the first row of the matrix with the first element of the vector and the next elements makes them zero so the result vector gives a wrong result.
using namespace std;
#define N 100
#define F 3
#define X 7
__global__ void matvec(int *MAT, int *VEC, int *SOL) {
int bx = blockIdx.x;
int tx = threadIdx.x;
int i = 32*bx+tx;
for (int j = 0; j < X; j++) {
SOL[i] = ((MAT[i * X + j] * VEC[j]) +SOL[i]) % 2;
}
}
int main () {
int i, j;
int MAT[N][N], VEC[N], SOL[N];
int *MAT_dev, *VEC_dev, *SOL_dev;
size_t nBytes = X * X * sizeof(int);
cout << "\t- - - - - MATRIX - - - - -\n\n";
for (i = 0; i < X; i++) {
for (j = 0; j < X; j++) {
cout << "Element [" << i << "][" << j << "]: ";
cin >> MAT[i][j];
}
}
cout << endl << endl;
for (i = 0; i < X; i++) {
for (j = 0; j < X; j++) {
cout << MAT[i][j] << " ";
if (j == (X - 1))
cout << endl;
}
}
cout << endl << endl;
cout << "\t- - - - - VECTOR - - - - -\n\n";
for (i = 0; i < X; i++) {
cout << "Element [" << i << "]: ";
cin >> VEC[i];
}
cout << endl << endl;
for (i = 0; i < X; i++) {
cout << VEC[i] << " ";
}
cout << endl << endl;
cudaMalloc((void**)&MAT_dev, nBytes);
cudaMalloc((void**)&VEC_dev, nBytes);
cudaMalloc((void**)&SOL_dev, nBytes);
cudaMemcpy(MAT_dev, MAT, nBytes, cudaMemcpyHostToDevice);
cudaMemcpy(VEC_dev, VEC, nBytes, cudaMemcpyHostToDevice);
dim3 dimBlock(X,X);
dim3 dimGrid(1,1);
matvec<<<dimGrid,dimBlock>>>(MAT_dev, VEC_dev, SOL_dev);
cudaMemcpy(SOL, SOL_dev, nBytes, cudaMemcpyDeviceToHost);
cout << "\t- - - - - RESULT - - - - -\n\n";
for (i = 0; i < X; i++)
{
cout << SOL[i] << " ";
}
cout << endl << endl;
cudaFree(MAT_dev);
cudaFree(VEC_dev);
cudaFree(SOL_dev);
system("PAUSE");
return 0;
}
Thanks for the help

This is because the size of MAT is much larger than it should be. Basically you need N == X, which shouldn't be a problem because both are known at compile time. Memory for a 2D array is laid out in a single contiguous block, row major for C--so in your case the first row corresponds to the first 400 (sizeof(int)*N) bytes, the second row to the second 400, etc. The length of the row is called the 'stride' However, cudaMemcpy has no idea what the stride is or which elements of MAT have been filled in, it just copies the first nBytes bytes from MAT into MAT_DEV. Since nBytes is sizeof(int)*X*X and X == 7 << N the second and subsequent rows of your matrix never get copied. Only the first 196 bytes from the MAT get copied, explaining why your second row is all zeros.

Related

Print 2D array in reverse (C++)

Write a program that reads 12 integers into a 2D integer array with 4 rows and 3 columns. The program then outputs the 2D array in reverse order according to both rows and columns.
Ex: If the input is:
5 7 3
6 4 3
5 6 9
5 2 8
then the output is:
8 2 5
9 6 5
3 4 6
3 7 5
For coding simplicity, output a space after every integer, including the last one on each row.
#include <iostream>
using namespace std;
int main() {
const int ROWS = 4;
const int COLS = 3;
int arr[ROWS][COLS];
int i, j;
for(i = 0; i < ROWS; i++){
for(j = 0; j < COLS; j++){
cin>>arr[i][j];
}
}
cout << arr[3][2] << " " << arr[3][1] << " " << arr[3][0] << " " << endl;
cout << arr[2][2] << " " << arr[2][1] << " " << arr[2][0] << " "<< endl;
cout << arr[1][2] << " " << arr[1][1] << " " << arr[1][0] << " "<< endl;
cout << arr[0][2] << " " << arr[0][1] << " " << arr[0][0] << " "<< endl;
return 0;
}
I ended up having to hardcode this question because I couldnt find a way to reverse the 2D array with a loop and get it to be outputted in the form of a graph. Is there a way i could reverse the 2D array using for loops and would it be possible to be able to change the amount of rows and columns and still output the corresponding graph of values?
try this:
#include <iostream>
using namespace std;
int main() {
const int ROWS = 4;
const int COLS = 3;
int arr[ROWS][COLS];
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
cin >> arr[i][j];
}
}
// output the reversed array
for (int i = ROWS - 1; i >= 0; i--) {
for (int j = COLS - 1; j >= 0; j--) {
cout << arr[i][j] << " ";
}
cout << endl;
}
return 0;
}
You can reverse a 2D array using nested for loops, try
#include <iostream>
using namespace std;
int main() {
const int ROWS = 4;
const int COLS = 3;
int arr[ROWS][COLS];
int i, j;
// Input the values into the 2D array
for(i = 0; i < ROWS; i++) {
for(j = 0; j < COLS; j++) {
cin >> arr[i][j];
}
}
// Reverse the rows and columns of the 2D array
for(i = ROWS - 1; i >= 0; i--) {
for(j = COLS - 1; j >= 0; j--) {
cout << arr[i][j] << " ";
}
cout << endl;
}
return 0;
}
As mentioned in comments below if you don't know ROWS and COLS size at compile time dynamically allocate the memory for 2D array(arr) in C++ using new operator.
There is very little point reading the data into a 2D array for this program. A std::vector would do the trick, sized with ROWS * COLS values. You then have the benefit of being able to read those dimensions from the user, which addresses the second part of your question.
size_t size = ROWS * COLS;
// Read data
std::vector<int> data;
data.reserve(size);
for (int value; std::cin >> value; )
{
data.push_back(value);
}
// Validate data
if (data.size() != size)
{
std::cerr << "Unexpected end of input!\n";
return EXIT_FAILURE;
}
When outputting, you can use a reverse iterator through the vector, and simply write a newline every COLS values.
// Output in reverse
int col = 0;
for (auto it = data.rbegin(); it != data.rend(); it++)
{
std::cout << *it << " ";
if (++col == COLS)
{
std::cout << "\n";
col = 0;
}
}
You can even easily fix the "space at the end of the line" problem by adjusting your output loop as follows:
// Output in reverse
int col = 0;
for (auto it = data.rbegin(); it != data.rend(); it++)
{
std::cout << *it;
if (++col == COLS)
{
std::cout << "\n";
col = 0;
}
else
{
std::cout << " ";
}
}

How can I merge three functions into one? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have written next code but 3 functions must be replaced by 1 and I don't know how to.
The program creates 3 arrays but only 1 function must calculate negative numbers of each column and find the max element in each column. Here's the code:
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
int n = 0;
const int m = 3, k = 3, b = 4, u = 5;
int i, j;
void calc(float** array, int i, int j );
void calc1(float** array, int i, int j);
void calc2(float** array, int i, int j);
int main()
{
float** array = new float* [m];
for (int l = 0; l < m; l++) {
array[l] = new float[k];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < m; i++) {
for (int j = 0; j < k; j++) {
array[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < k; j++) {
cout << setprecision(2) << setw(4) << array[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc(array, i, j); // FUNCTION !!!
float** arr = new float* [b];
for (int l = 0; l < b; l++) {
arr[l] = new float[b];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < b; i++) {
for (int j = 0; j < b; j++) {
arr[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < b; i++)
{
for (int j = 0; j < b; j++) {
cout << setprecision(2) << setw(4) << arr[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc(arr, i, j); // FUNCTION !!!
float** ar = new float* [u];
for (int l = 0; l < u; l++) {
ar[l] = new float[u];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < u; i++) {
for (int j = 0; j < u; j++) {
ar[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < u; i++)
{
for (int j = 0; j < u; j++) {
cout << setprecision(2) << setw(4) << ar[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc2(ar, i, j); // FUNCTION !!!
}
void calc(float** array, int i, int j) {
int max = array[0][0];
for (int j = 0; j < k; j++)
{
max = array[0][0];
for (int i = 0; i < k; i++) {
if (array[i][j] > max)
max = array[i][j];
if (array[i][j] < 0) {
n += 1;
}
}
cout << endl << "IN the [" << j + 1 << "] column is " << n << " negative elements" << endl << endl; n = 0;
cout << "IN the [" << j + 1 << "] column is " << max << " maximal element" << endl;
}
}
void calc1(float** arr, int i, int j) {
int max = arr[0][0];
for (int j = 0; j < b; j++)
{
max = arr[0][0];
for (int i = 0; i < b; i++) {
if (arr[i][j] > max)
max = arr[i][j];
if (arr[i][j] < 0) {
n += 1;
}
}
cout << endl << "IN the [" << j + 1 << "] column is " << n << " negative elements" << endl << endl; n = 0;
cout << "IN the [" << j + 1 << "] column is " << max << " maximal element" << endl;
}
}
void calc2(float** ar, int i, int j) {
int max = ar[0][0];
for (int j = 0; j < u; j++)
{
max = ar[0][0];
for (int i = 0; i < u; i++) {
if (ar[i][j] > max)
max = ar[i][j];
if (ar[i][j] < 0) {
n += 1;
}
}
cout << endl << "IN the [" << j + 1 << "] column is " << n << " negative elements" << endl << endl; n = 0;
cout << "IN the [" << j + 1 << "] column is " << max << " maximal element" << endl;
}
}
The parameters to calc() should be the number of rows and columns in the array. Then it should use these as the limits in the for loops.
Also, since you're calculating total negative and maximum for each column, you must reset these variables each time through the column loop.
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
const int m = 3, k = 3, b = 4, u = 5;
void calc(float** array, int rows, int cols);
int main()
{
float** array = new float* [m];
for (int l = 0; l < m; l++) {
array[l] = new float[k];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < m; i++) {
for (int j = 0; j < k; j++) {
array[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < k; j++) {
cout << setprecision(2) << setw(4) << array[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc(array, m, k); // FUNCTION !!!
float *arr = new float* [b];
for (int l = 0; l < b; l++) {
arr[l] = new float[b];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < b; i++) {
for (int j = 0; j < b; j++) {
arr[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < b; i++)
{
for (int j = 0; j < b; j++) {
cout << setprecision(2) << setw(4) << arr[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc(arr, b, b); // FUNCTION !!!
float** ar = new float* [u];
for (int l = 0; l < u; l++) {
ar[l] = new float[u];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < u; i++) {
for (int j = 0; j < u; j++) {
ar[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < u; i++)
{
for (int j = 0; j < u; j++) {
cout << setprecision(2) << setw(4) << ar[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc(ar, u, u); // FUNCTION !!!
}
void calc(float** array, int rows, int cols) {
for (int j = 0; j < cols; j++)
{
int n = 0;
int max = array[0][j];
for (int i = 1; i < rows; i++) {
if (array[i][j] > max)
max = array[i][j];
if (array[i][j] < 0) {
n += 1;
}
}
cout << endl << "IN the [" << j + 1 << "] column is " << n << " negative elements" << endl << endl; n = 0;
cout << "IN the [" << j + 1 << "] column is " << max << " maximal element" << endl;
}
}

I need some assistance with creating a function

I am new, not that good with functions, and I am trying to solve this question:
Suppose A, B, C are arrays of integers of size [M], [N], and [M][N], respectively. The user will enter the values for the array A and B. Write a user defined function in C++ to calculate the third array C by adding the elements of A and B. If the elements have the same index number, they will be multiplied. C is calculated as the following: -
Use A, B and C as arguments in the function.
Below is my attempt at the problem.
#include<iostream>
using namespace std;
void Mix(int(&A)[], int(&B)[], int(&C)[][100], int N, int M);
//dont understand why you used Q
int main()
{
//variable declaration
int A[100], B[100], C[100][100], n, m, l = 0;
//input of size of elements for first ararys
cout << "Enter number of elements you want to insert in first array: ";
cin >> n;
cout << "-----------------" << endl;
cout << "-----------------" << endl;
cout << "Enter your elements in ascending order" << endl;
//input the elements of the array
for (int i = 0; i < n; i++)
{
cout << "Enter element " << i + 1 << ":";
cin >> A[i];
}
cout << endl << endl;
//input of size of elements for first ararys
cout << "Enter number of elements you want to insert in second array: ";
cin >> m;
cout << "-----------------" << endl;
cout << "-----------------" << endl;
cout << "Enter your elements in descending order" << endl;
//input the elements of the array
for (int i = 0; i < m; i++)
{
cout << "Enter element " << i + 1 << ":";
cin >> B[i];
}
Mix(A, B, C, n, m);
cout << "\nThe Merged Array in Ascending Order" << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
{
cout << C[i][j] << " ";
}
cout << "\n"; //endline never use endl its 10 times slower
}
system("pause");
return 0;
}
void Mix(int(&A)[], int(&B)[], int(&C)[][100], int N, int M)
{
// rows is the index for the B array, cols is index for A array
int rows = 0;
int cols = 0;
while (rows < M) {
while (cols < N) {
if (rows == cols) { // remember ==
C[rows][cols] = B[rows] * A[cols];
}
else {
C[rows][cols] = B[rows] + A[cols];
}
cols++; // increment here
}
rows++; // increment here
}
return;
}
Here is an example of the output:
enter image description here
In order to make the C array two-dimensional, it needs to be expressed as C[100][100], instead of C[200]. That is the first step. Next, in your Mix() function, you need to cycle through each element of both A and B (ex. two for loops). Your rows change as B changes, and your columns change as A changes. Include a check for identical indices that will determine whether to add or multiply the two values together.
void Mix(int A[], int B[], int C[][], int N, int M) {
// rows is the index for the B array, cols is index for A array
for (int rows = 0; rows < M; rows++) {
for (int cols = 0; cols < N; cols++) {
if (rows == cols) { // remember ==
C[rows][cols] = B[rows] * A[cols];
} else {
C[rows][cols] = B[rows] + A[cols];
}
}
}
}
Make sure your arrays are properly defined and print out the C array by row and column to match the specifications.
UPDATE: If you want to use while loops, I would default to deconstructing the for loops and apply the same logic:
void Mix(int A[], int B[], int C[][], int N, int M) {
// rows is the index for the B array, cols is index for A array
int rows = 0;
int cols = 0;
while (rows < M) {
while (cols < N) {
if (rows == cols) { // remember ==
C[rows][cols] = B[rows] * A[cols];
} else {
C[rows][cols] = B[rows] + A[cols];
}
cols++; // increment here
}
rows++; // increment here
}
}
I would definitely recommend the for loop approach, as it is more compact, yet does the exact same operations.
There are a lot of things wrong with your code. First off an 2D array must be declared with 2 squared brackets so C[200][200]. In the Mix function the logical operator is == not = in if (A[I] = B[J])
Anyway here's the function that you need:
#include<iostream>
using namespace std;
void Mix(int A[], int B[], int C[], int N, int M) {
//dont understand why you used Q
int i, j;
for(i=0; i<N; i++) {
for(j=0; j<M; j++) {
if(i==j){
C[i][j] = A[i] * B[j];
}
else {
C[i][j] = A[i] + B[j];
}
}
}
return C[i][j];
}
int main()
{
//variable declaration
int A[100], B[100], C[200], j, i, n, m, l = 0;
string Comma;
//input of size of elements for first ararys
cout << "Enter number of elements you want to insert in first array: ";
cin >> n;
cout << "-----------------" << endl;
cout << "-----------------" << endl;
cout << "Enter your elements in ascending order" << endl;
//input the elements of the array
for (i = 0; i < n; i++)
{
cout << "Enter element " << i + 1 << ":";
cin >> A[i];
}
cout << endl << endl;
//input of size of elements for first ararys
cout << "Enter number of elements you want to insert in second array: ";
cin >> m;
cout << "-----------------" << endl;
cout << "-----------------" << endl;
cout << "Enter your elements in descending order" << endl;
//input the elements of the array
for (j = 0; j < m; j++)
{
cout << "Enter element " << j + 1 << ":";
cin >> B[j];
}
C = Mix(A, B, C, n, m);
cout << "\nThe Merged Array in Ascending Order" << endl;
for(i=0; i<n; i++) {
for(j=0; j<m; j++) {
cout<<C[i][j]<<" ";
}
cout<<"\n" //endline never use endl its 10 times slower
}
system("pause");
return 0;
}
Because M and N are defined at run time, you'll really want to use vectors to represent them. Additionally consider returning a 2D container so as to leverage return value optimization.
I'm going to write an example using a vector of vectors for simplicity (see What are the Issues with a vector-of-vectors? for more on why that's really just good for a toy example):
vector<vector<int>> Mix(const vector<int>& A, const vector<int>& B) {
vector<vector<int>> result(size(B), vector<int>(size(A)));
for(size_t i = 0U; i < size(B); ++i) {
for(size_t j = 0U; j < size(A); ++j) {
result[i][j] = A[j] * B[i];
}
}
return result;
}
Live Example
EDIT:
If you must use arrays you'll miss out on return value optimization. I'd only choose this as a good option in the situations:
That you weren't returning anything, in which case your function would probably look something like:
void Mix(const int* A, const int* B, const size_t size_A, const size_t size_B)
{
for(size_t i = 0U; i < size_B; ++i) {
for(size_t j = 0U; j < size_A; ++j) {
cout << '[' << i << "][" << j << "]: " << A[j] * B[i] << '\t';
}
cout << endl;
}
}
That you weren't calling a function and you'd already been given int A[M] and int B[N] as inputs and int C[N][M] as an output, in which case the code you'd inline would probably look something like this:
for(size_t i = 0U; i < size(B); ++i) {
for(size_t j = 0U; j < size(A); ++j) {
C[i][j] = A[j] * B[i];
}
}

global variable and class versus local

I am writing a program, it is an optimization problem. As part of the problem I have defined a class in which I save the optimization parameters. It is composed of some vectors that I resize them and give initial values to them in a function. The class is as follows:
typedef class Chrom
{
public:
vector<vector <short int>> bit;
vector<vector <float>> WaitingTime;
vector <short int> WaitingJob;
vector<vector <float>> StartTime;
vector<vector <float>> finish;
//short int FinishTime;
float fit;
void variablesresize(){
WaitingTime.resize(Jobs);
WaitingJob.resize(Jobs);
StartTime.resize(Jobs);
finish.resize(Jobs);
bit.resize(Jobs);
//cout << "machine size is:" << Machines;
for (int i = 0; i < Machines - 1; ++i)
{
WaitingTime[i].resize(Jobs);
}
for (int i = 0; i < Machines; ++i)
{
StartTime[i].resize(Jobs);
finish[i].resize(Jobs);
bit[i].resize(Jobs);
}
}
} chrom;
Then I define two classes of this type (popcurrent and popnext) and also functions y and x before main function:
chrom popcurrent[populationsize];
chrom popnext[populationsize];
void *initialize(chrom popcurrent[populationsize]); //defining the
functions that we will use
chrom x(chrom popcurrent,int pindex);
float y(chrom chromp);
void *selection(chrom popcurrent[populationsize]);
void *crossover(chrom popnext[populationsize]);
void *mutation(chrom popnext[populationsize]);
Problem ProblemConstraint;
void SetProblemSize(short int &Machines, short int &Jobs);
void vectordimension(chrom ch[populationsize]);
void ytest(chrom popcurrent);
void main()
{
My Issue is that whenever I call x and y within other functions and I assign some values in other functions, it gives back only zero for the members of those classes. For example below is a part of assigning value in function x:
chrom x(chrom popnext,int pindex)
{
int z = 0, i, j, k, tempindex, previousjobindex, l;
float temp;
//sorting chromosoms based on the population
//if
cout << '\n';
cout << "popcurrent.bit[0][0] =" << popnext.bit[0][0] << '\n';
cout << "popcurrent.bit[0][1] =" << popnext.bit[0][1] << '\n';
//for the first machine
//for (int i = 0; i < populationsize;i++)
for (i = 0; i < Machines; i++)//**just for test
for (j = 0; j < Jobs; j++)//**just for test
{
cout << "popcurrent.bit[" << i << "][" << j << "] =" << popnext.bit[i]
[j] << '\n';
}
for (int j = 0; j < Machines; j++)//after first machine so j=1 and not 0
{
for (int k = 0; k < Jobs; k++)
{
for (l = 0; l < Jobs; l++)
if (popnext.bit[j][l] == 1)
{
cout << "j= " << j << "k = " << k;
popnext.WaitingTime[j][k] = ProblemConstraint.t1[j][k];
popnext.StartTime[j][k] = popnext.finish[j - 1][k] +
popnext.WaitingTime[j - 1][k];
popnext.finish[j][k] = popnext.WaitingTime[j][k] + ProblemConstraint.Processing[j][k] + popnext.StartTime[j][k];
cout << "
cout << "popcurrent.StartTime[" << j << "][" << k << "]= " << popnext.StartTime[j][k] << " popcurrent.finish[j - 1][k]" << popnext.finish[j - 1][k] << '\n';
cout << " popcurrent.WaitingTime[" << j - 1 << "][" << k << "]= " << popnext.WaitingTime[j - 1][k] << '\n';//start time of machine j is equal to finish time of machine j-1
}
}
for (k = 1; k < Jobs; k++)
{
if (popnext.bit[j][k] == k + 1)
{
for (int l = 0; l < Jobs; l++)
if (popnext.bit[j][l] == 1 + k)
{
temp = popnext.finish[j][l];//finish time of previous job
tempindex = l;
}
popnext.StartTime[j][k] = temp;
if (popnext.StartTime[j][k] < popnext.finish[j - 1][k] + popnext.WaitingTime[j - 1][k]);
popnext.StartTime[j][k] = popnext.finish[j - 1][k] + popnext.WaitingTime[j - 1][k];//we need to update the start time and also we need to update w as well.
else if (popnext.StartTime[j][k] > popnext.finish[j - 1][k] + popnext.WaitingTime[j - 1][k])
popnext.WaitingTime[j - 1][k] = popnext.StartTime[j][k] - popnext.finish[j - 1][k];//we need to update the waiting time which is greater than t1 here.
popnext.finish[j][k] = popnext.StartTime[j][k] + ProblemConstraint.Processing[j][k] + popnext.StartTime[j][k];
popnext.WaitingTime[j][k] = ProblemConstraint.t1[j][k];
cout << "popcurrent.finish[" << j << "][" << k << "]= " << popnext.finish[j][k] << '\n';//debugging code
}//start of '{' is: else if (popcurrent.bit[j][k] >0)
}
}
cout << "In x functionn, popcurrent.finish[Machines - 1][k]= " <<
popnext.finish[1][2] << '\n';//debugging cout
return(popnext);
}
The function returns popnext but I call it with x(popcurrent,pindex) and x(popnext,pindex). The problem is that after calling I expect that values of members such as finish change but they do not change and therefore when I have
x(popcurrent) ;
Because of last command in x function it prints out popcurrent.finish[Machines - 1][k]=32 but when I have a cout in another function such as main it gives me popcurrent.finish[Machines - 1][k]=0;
It only gives me zero for all values (I assigned zero to vector values in another function while defining them which is initial values of the vector). I wonder why this happens while I have defined popcurrent and popnext global classes?

Why do I get a program crash for large values but not small values for my program?

Why do I get a program crash for large values but not small values for my program? If I input 1-3 the program does what it is supposed to but when I enter a number greater than that the program crashes and/or does not complete? Is it something to do with a pointer error or the way I've referenced something? I'm unsure so any help is appreciated. Thanks!
Code:
#include <iostream>
using namespace std;
void getData (int size, int *Arr){
cout << "\n\nEnter integer data one line at a time\n" << endl ;
for (int i=0; i < size; i++){
cin >> Arr[i];
}
}
void findMinAndMax(int array[], int size, int *min, int *max) {
int smallest = array[0];
int largest = array[0];
*min = smallest;
*max = largest;
for (int i = 1; i < size; i++)
{
if (array[i] > *max){
*max = array[i];
cout << "Max Value (loop): " << *max << endl;
}
if (array[i] < *min){
*min = array[i];
cout << "Min Value (loop): " << *max << endl;
}
}
// testing code
cout << "Min Value: " << *min << endl;
cout << "Max Value: " << *max << endl;
}
int *makeFrequency (int data[], int dSize, int *minDataValue, int *maxDataValue) {
cout << "Min Value Pre: " << *minDataValue << endl;// testing code
cout << "Max Value Pre: " << *maxDataValue << endl;// testing code
findMinAndMax(data, dSize, minDataValue, maxDataValue);
cout << "Min Value Post: " << *minDataValue << endl; // testing code
cout << "Max Value Post: " << *maxDataValue << endl;// testing code
int fSize = *minDataValue + *maxDataValue;
cout << "fSize: " << fSize << endl; // testing code
int *frequency;
frequency = new int [fSize];
// if frequency is 0, end
if (frequency == 0)
{
return 0;
}
// set all elements to 0 in array frequency
for (int i = 0; i <= fSize; i++) {
frequency[i] = 0;
}
for (int i = 0; i <= dSize; i++) {
int j = data[i] - (*minDataValue) + 1;
frequency[j] = frequency[j] + 1;
}
return frequency;
}
void makeHistogram (int *freq, int min, int max ){
cout << "Frequency Value HISTOGRAM: " << *freq << endl;
cout << "\n\n\n ----------- Histogram ----------------\n" << endl;
int size = min + max;
cout << "Size Value HISTOGRAM: " << size << endl;
for (int i = 0; i < size; i++){
if (freq[i] > 0) {
cout << "\n" << min + i - 1 << ": ";
for (int j = 0; j < freq[i]; j++) {
cout << '*';
}
}
}
cout << endl << endl;
}
int main() {
int dSize;
int *ArrayOfInts;
cout << "How many data values? ";
cin >> dSize;
ArrayOfInts = new int [dSize];
getData(dSize, ArrayOfInts);
int *frequency, min, max;
frequency = makeFrequency(ArrayOfInts, dSize, &min, &max);
if (frequency == 0) return -1;
cout << "Min Value MAIN: " << min << endl; // testing code
cout << "Max Value MAIN: " << max << endl; // testing code
cout << "Frequency Value MAIN: " << *frequency << endl;
makeHistogram(frequency, min, max);
delete [] frequency;
return 0;
}
One place where you have undefined behaviour which can cause crashes:
here you allocate fSize elements:
frequency = new int [fSize];
later you iterate it until fSize:
for (int i = 0; i <= fSize; i++) {
you should change to i < fSize, because there is no fSize element in your array. And the same problem with i <= dSize later on. Should be i < dSize.
btw. I dont see why only large values should cause crashes in your code, maybe this is just UB.
You're setting fSize incorrectly. It should be the difference between the maximum and minimum values, not the sum of them. Otherwise, if you have negative numbers in your list, the frequency array will be too small. And if absolute value of any of the negative numbers is larger than the highest number, fSize will be negative, which is not valid for the size of an array.
Then you need to add 1 to include both endpoints. So it should be:
int fSize = *maxDataValue - *minDataValue + 1;
Then, as the other answer pointed out, you need to fix your for loops. When the size of an array is N, the array indexes from from 0 to N-1. So it should be:
for (int i = 0; i < fSize; i++) {
using < as the loop test, not <=. If you try to write outside an array, you invoke undefined behavior, so anything can happen -- if you're lucky you get a crash, but that's not guaranteed.
You have a similar problem when you assign to frequency:
for (int i = 0; i <= dSize; i++) {
int j = data[i] - (*minDataValue) + 1;
frequency[j] = frequency[j] + 1;
}
There's no need to add 1 when subtracting *minDataValue, and doing so will cause you to go outside the array when data[i] is the maximum.