C++ Parallel Arrays - c++

In this Parallel Array I can not see or figure out why I am printing the memory address instead of the values Entered. I ask the C++ gods to enlighten me and Point me to some good tutorials about this issue so that I can learn from. Code is below.
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <string>
#include <ios>
using namespace std;
// Assign a constant
const int rowSize = 6;
const int columnSize = 4;
void displayWelcome()
{
cout << "\tCorporation X\n";
cout << "Quartily Reports for the six Entities\n";
cout << "Showing you the Highest Average out of the six\n";
cout << endl;
}
// Assign Words in a row of 6
string* entitiesArray(string(entities)[rowSize])
{
string entitiesName[rowSize] = {"East ","West ","Central ","Midwest"
,"South ","South East "};
//string entitiesName;
for(int i=0; i < rowSize; i++)
{
cout << entitiesName[i] << endl;
}
return entitiesName;
}
// Assign numbers to a 6x4
double* entriesArray(double(corpArray)[rowSize][columnSize])
{
// Assign Random Numbers to the Array
srand(time(0));
double rowSum = 0.0, average = 0.0;
for (int r = 0; r < rowSize; ++r)
{
rowSum = 0;
for (int c = 0; c < columnSize; ++c)
{
corpArray[r][c] = rand() % 101;
cout << setw(5) << left << corpArray[r][c] << " ";
rowSum += corpArray[r][c];
average = rowSum / 4;
}
cout << average;
cout << endl;
}
return 0;
}
// Parallel Array This is where is prints the Memory Address
void pArray(string &theEntitiesArray,double &theEntriesArray)
{
string entity = &theEntitiesArray;
double entry = &theEntriesArray;
for(int row_index = 0; row_index < 6; row_index++)
{
cout << setw(10) << entity <<endl;
for (int col_index = 0; col_index < 4; col_index++)
{
cout << setw(10) << entry << endl;
}
}
}
int main(int argc, const char * argv[])
{
// Declare an Array & Variables
double corpArray[rowSize][columnSize];
string entities[rowSize];
double* theEntriesArray;
string* theEntitiesArray;
char parallelArray;
displayWelcome();
theEntitiesArray = entitiesArray(entities);
theEntriesArray = entriesArray(corpArray);
pArray(*theEntitiesArray, *theEntriesArray);
cout << endl;
return 0;
}

#JSF beat me to it in a comment, but in the line double entry = &theEntriesArray;, you're assigning the address of theEntriesArray to the variable entry, rather than the numerical value it refers to. Change it to:
double entry = theEntriesArray;

You have Some Problems in Functions which initzlize the array now its fine try to read about pointer
1- http://www.cplusplus.com/doc/tutorial/pointers/
2- http://www.tutorialspoint.com/cplusplus/cpp_pointer_to_pointer.htm
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <string>
#include <ios>
using namespace std;
// Assign a constant
const int rowSize = 6;
const int columnSize = 4;
void displayWelcome()
{
cout << "\tCorporation X\n";
cout << "Quartily Reports for the six Entities\n";
cout << "Showing you the Highest Average out of the six\n";
cout << endl;
}
// Assign Words in a row of 6
string* entitiesArray(string(entities)[rowSize])
{
string* entitiesName = new string[rowSize];
string temp[]= { "East ", "West ", "Central ", "Midwest"
, "South ", "South East " };
for (int i = 0; i < rowSize; i++)
{
entitiesName[i] = temp[i];
}
//string entitiesName;
for (int i = 0; i < rowSize; i++)
{
cout << entitiesName[i] << endl;
}
return entitiesName;
}
// Assign numbers to a 6x4
double** entriesArray(double(corpArray)[rowSize][columnSize])
{
double** tempcorpArray = new double*[rowSize];// [columnSize];
for (int i = 0; i < rowSize; ++i)
{
tempcorpArray[i] = new double[columnSize];
}
// Assign Random Numbers to the Array
srand(time(0));
double rowSum = 0.0, average = 0.0;
for (int r = 0; r < rowSize; ++r)
{
rowSum = 0;
for (int c = 0; c < columnSize; ++c)
{
tempcorpArray[r][c] = rand() % 101;
cout << setw(5) << left << tempcorpArray[r][c] << " ";
rowSum += tempcorpArray[r][c];
average = rowSum / 4;
}
cout << average;
cout << endl;
}
return tempcorpArray;
}
// Parallel Array This is where is prints the Memory Address
void pArray(string* theEntitiesArray, double** theEntriesArray )
{
string* entity = theEntitiesArray;
double** entry = theEntriesArray;
for (int row_index = 0; row_index < 6; row_index++)
{
cout << setw(10) << (entity[row_index]) << endl;
for (int col_index = 0; col_index < 4; col_index++)
{
cout << setw(10) << (entry[row_index][col_index]) << endl;
}
}
}
int main(int argc, const char * argv[])
{
// Declare an Array & Variables
double corpArray[rowSize][columnSize];
string entities[rowSize];
char parallelArray;
displayWelcome();
string* theEntitiesArray = entitiesArray(entities);
double** theEntriesArray = entriesArray(corpArray);
pArray(theEntitiesArray, theEntriesArray);
cout << endl;
return 0;
}
if there is any problem feel free to ask :D

Related

Swapping two initialized arrays in C++ using Void and Pointers

I need to write a C++ program where it swaps between two 1-dimensional
arrays using pointers and functions. Firstly, a void function named showValues to display both arrays before swapping takes and also a void function named swap to swap the elements between both arrays.
My question is: I'm supposed to swap the function but for some reason it wont run and I am not sure where is the error in my code
#include <iostream>
#include <iomanip>
using namespace std;
const int SIZE = 5;
void showValues(int[],int[]);
void swap(int[],int[]);
int main() {
int array1[SIZE] = {10,20,30,40,50};
int array2[SIZE] = {60,70,80,90,100};
showValues (array1, array2);
swap(array1, array2);
return 0;
}
void showValues(int array1[], int array2[]){
cout<<"The original arrays are as shown below: " << endl;
cout << " Array 1 is: ";
for (int i = 0; i < 5; ++i) {
cout << array1[i] << " ";
}
cout << "\n Array 2 is: ";
for (int i = 0; i < 5; ++i) {
cout << array2[i] << " ";
}
}
void swap(int array1[], int array2[])
{
int temp,i;
for(i=0; i<5; ++i)
{
temp = array1[SIZE];
array1[SIZE] = array2[SIZE];
array2[SIZE] = temp;
}
cout << "\nThe swapped arrays are as shown below: " << endl;
cout << " Array 1 is: ";
for (int i = 0; i < 5; ++i) {
cout << array1[i] << " ";
}
cout << "\n Array 2 is: ";
for (int i = 0; i < 5; ++i) {
cout << array2[i] << " ";
}
}
This part of your code doesn't make sense:
temp = array1[SIZE];
array1[SIZE] = array2[SIZE];
array2[SIZE] = temp;
SIZE is 5. So, you are accessing array1[5] and array2[5], i.e. the 6th element of the array. Yet, your arrays have only 5 elements to begin with (array1[0] to array1[4], same for array2), so you are accessing elements beyond the end of the array, which is undefined behavior that is probably just corrupting memory somewhere!
You probably meant to use i here, not SIZE, then the code makes sense. Instead, it would be useful to replace the "magic number" 5 with SIZE:
for(i = 0; i < SIZE; ++i)
{
temp = array1[i];
array1[i] = array2[i];
array2[i] = temp;
}
The void swap(int array1[], int array2[]) function is where you are having trouble. You actually don't even need to have another function for the swapping. You could just use std::swap() which is defined in the #include <utility> header. Since both arrays have the same size.
For example you could do something along these lines:
#include <iostream>
#include <iomanip>
#include <utility>
const int SIZE = 5;
void showValues(int[], int[]);
void swap(int[], int[]);
int main() {
int array1[SIZE] = { 10,20,30,40,50 };
int array2[SIZE] = { 60,70,80,90,100 };
int n = sizeof(array1) / sizeof(array2[0]);
showValues(array1, array2);
std::swap(array1, array2);
std::cout << "\n\nThe swapped arrays are as shown below:\n ";
std::cout << "\nArray 1 is: ";
for (int i = 0; i < n; i++)
std::cout << array1[i] << ", ";
std::cout << "\nArray 2 is: ";
for (int i = 0; i < n; i++)
std::cout << array2[i] << ", ";
return 0;
}
void showValues(int array1[], int array2[]) {
std::cout << "The original arrays are as shown below: " << std::endl;
std::cout << "\nArray 1 is: ";
for (int i = 0; i < 5; ++i) {
std::cout << array1[i] << " ";
}
std::cout << "\nArray 2 is: ";
for (int i = 0; i < 5; ++i) {
std::cout << array2[i] << " ";
}
}
Also consider not using using namespace std;.

Vector subscript out of range Visual Studio C++

I'm working with matrices in C++. The task is to find odd numbers in a matrix. I created a function, but when executed with Debug > Start Without Debugging... I get the error:
Debug assertion failed. Expression: vector subscript out of range.
This is my code:
File MatrixVec.h:
#include "MatrixVec.h"
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
MatrixVec::MatrixVec(int rows, int cols, int range)
{
row.assign(cols, 0);
mat.assign(rows, row);
for (int i = 0; i < mat.size(); i++) {
for (int j = 0; j < mat[i].size(); j++){
mat[i][j] = rand() % range;
}
}
}
void MatrixVec::process()
{
int *oddNum = new int(mat[0].size());
for (int i = 0; i < mat[0].size(); i++) {
oddNum[i] = 0;
}
for (int i = 0; i < mat.size(); i++) {
for (int j = 0; j < mat[0].size(); j++) {
oddNum[j] += mat[i][j] % 2;
}
}
for (int i = 0; i < mat[0].size(); i++) {
cout << i + 1 << ". kolona " << oddNum[i]<< " neparnih elemenata." << endl;
}
//Edit
delete[] oddNum; //forgot this line
}
File MatrixVec.h:
#include "Matrix.h"
#include <vector>
class MatrixVec : public Matrix {
public:
MatrixVec(int rows, int cols, int range);
void print();
void process();
private:
std::vector<int> row;
std::vector<std::vector<int> > mat;
};
File Matrix.h:
class Matrix {
public:
virtual void print() = 0;
virtual void process() = 0;
};
File main.cpp
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include "Matrix1D.h"
#include "Matrix2D.h"
#include "MatrixVec.h"
using namespace std;
#define NUMBER_RANGE 10
int main(int argc, char* argv[])
{
if (argc != 3)
{
cout << "Niste uneli potrebne argumente za pokretanje programa!" << endl;
cout << "Argumenti komandne linije treba da budu:" << endl;
cout << "1. N dimenzija matrice" << endl;
cout << "2. M dimenzija matrice" << endl;
exit(-1);
}
// inicijalizacija generatora nasumičnih brojeva
srand(unsigned int(time(NULL)));
int rowNum = atoi(argv[1]);
int colNum = atoi(argv[2]);
// a)
cout << endl << endl << "a) Matrix 1D representation" << endl;
Matrix1D mat1(rowNum, colNum, 10);
mat1.print();
mat1.process();
//b)
cout << endl << endl << "b) Matrix 2D representation" << endl;
Matrix2D mat2(rowNum, colNum, 10);
mat2.print();
mat2.process();
//c)
cout << endl << endl << "c) Matrix vector of vector representation" << endl;
MatrixVec mat3(rowNum, colNum, 10);
mat3.print();
mat3.process();
return 0;
}
This line int *oddNum = new int(mat[0].size()); actually creating one int with value of mat[0].size(). You want something like this to create an array of size mat[0].size().
int *oddNum = new int[mat[0].size()];
Note: This still not answer the exact error. Seems like your mat vector is empty.

C++ Integer Array Palindrome Checker

I used the following code for palindrome check of an integer array and used the value of variable 'declare' as check of palindrome. I used the technique that if declare is 1 at the end, array is palindrome, else not. But its not working. In the end of code, it always keeps the value of declare which was initialized, independent of rest of the code. Please Debug.
#include <iostream>
using namespace std;
void main()
{
int array1[3] = {0,0,1};
int j = 2;
cout << "Given Array is:\n";
for (int i = 0; i < 3; i++)
cout << array1[i];
cout << endl;
int determiner[3];
for (int i = 0; i <3; i++){
determiner[j] = array1[i];
j -= 1;
}
cout << "Reversed Array is:\n";
for (int i = 0; i < 3; i++)
cout << determiner[i];
cout << endl;
int declare;
for (int u = 0; u < 3; u++)
{
if (array1[u] = determiner[u])
{
declare = 1;
}
if (array1[u] != determiner[u])
{
declare = 0;
break;
}
}
cout << endl;
cout << declare<< endl;
if (declare==1)
cout << "Given Array is Palindrome. Cheers!!!\n";
if (declare==0)
cout << "Emhmm! This aint Palindrome.\n";
system("pause");
}
if (array1[u] = determiner[u])
should be
if (array1[u] == determiner[u])

Matrix Overload C++

In the header file I am suppose to add the declaration of functions which I need to overload operators. Then implement the functions I added into the header file.
Can any one help me with what I am suppose to do? Please explain so I can wrap my head around overloading if possible.
I have listed the code below. As for main I am not suppose to edit it.
Header File
Matrix.h
#ifndef MATRIX_H
#define MATRIX_H
class Matrix
{
public:
Matrix (int sizeX, int sizeY);
~Matrix();
int GetSizeX() const { return dx; }
int GetSizeY() const { return dy; }
long &Element(int x, int y); // return reference to an element
void Print () const;
private:
long **p; // pointer to a pointer to a long integer
int dx, dy;
};
#endif /* MATRIX_H */
Matrix.cpp File
#include <iostream>
#include <cassert>
#include "Matrix.h"
using namespace std;
Matrix::Matrix (int sizeX, int sizeY) : dx(sizeX), dy(sizeY)
{
assert(sizeX > 0 && sizeY > 0);
p = new long*[dx];
// create array of pointers to long integers
assert(p != 0);
for (int i = 0; i < dx; i++)
{ // for each pointer, create array of long integers
p[i] = new long[dy];
assert(p[i] != 0);
for (int j = 0; j < dy; j++)
p[i][j] = 0;
}
}
Matrix::~Matrix()
{
for (int i = 0; i < dx; i++)
delete [] p[i]; // delete arrays of long integers
delete [] p; // delete array of pointers to long
}
long &Matrix::Element(int x, int y)
{
assert(x >= 0 && x < dx && y >= 0 && y < dy);
return p[x][y];
}
void Matrix::Print () const
{
cout << endl;
for (int x = 0; x < dx; x++)
{
for (int y = 0; y < dy; y++)
cout << p[x][y] << "\t";
cout << endl;
}
}
main.cpp
#include <cstdlib>
#include <iostream>
#include <cassert>
#include <ctime>
#include "Matrix.h"
using namespace std;
int main(int argc, char** argv) {
int size1, size2, size3;
const int RANGE = 5; //using to generate random number in the range[1,6]
cout << "Please input three positive integers: (size)";
cin >> size1 >> size2 >> size3;
assert(size1 > 0 && size2 > 0 && size3 > 0);
Matrix myMatrix1(size1, size2), myMatrix2(size2,size3);
Matrix yourMatrix1(size1, size2), yourMatrix2(size2, size3);
Matrix theirMatrix1(size1, size2), theirMatrix2(size1, size3);
srand(time(0));
for (int i = 0; i < size1; i++)
for (int j = 0; j < size2; j++)
myMatrix1(i,j) = rand() % RANGE + 1;
yourMatrix1 = 2 * myMatrix1;
theirMatrix1 = myMatrix1 + yourMatrix1;
cout << "myMatrix1: " << endl;
cout << myMatrix1 << endl << endl;
cout << "yourMatrix1 = 2 * myMatrix " << endl;
cout << yourMatrix1 << endl << endl;
cout << "myMatrix1 + yourMatrix1: " << endl;
cout << theirMatrix1 << endl << endl;
for (int i = 0; i < size2; i++)
for (int j = 0; j < size3; j++)
myMatrix2(i,j) = rand() % RANGE + 1;
yourMatrix2 = myMatrix2 * 3;
theirMatrix2 = myMatrix1 * yourMatrix2;
cout << "myMatrix1: " << endl;
cout << myMatrix1 << endl << endl;
cout << "myMatrix2: " << endl;
cout << myMatrix2 << endl << endl;
cout << "yourMatrix2 = myMatrix2 * 3 " << endl;
cout << yourMatrix2 << endl << endl;
cout << "myMatrix1 * yourMatrix2: " << endl;
cout << theirMatrix2 << endl << endl;
return 0;
}
You need to look into the main and see what all operators are used with the Matrix objects. You have to overload those operators into the Matrix class given to you i.e. write member functions into this class.
Refer following link for details on operator overloading with examples:
http://en.wikibooks.org/wiki/C%2B%2B_Programming/Operators/Operator_Overloading

Merge Sort Iterative Issue

I have searched many examples and have yet to be able to find where exactly my problem lies. I am trying to implement the merge sort algorithm from the Cormen intro to algorithms book-- here is where I am at so far-- I have tried throwing in print statements to follow how the arrays are getting rebuilt but I am not seeing it... can anyone help?
Code:
#include <iostream>
#include <cmath>
#include <ctime>
#include <cstdlib>
using namespace std;
int p = 0;
int q = 0;
int r = 0;
int getRandom()
{
int randNum = 0;
randNum = 1 + rand() % 100;
return randNum;
}
void populateArray(int * array, int size)
{
for (int i = 0; i < size; i++) {
array[i]=getRandom();
}
}
void merge (int * array, int p, int q, int r) // p = start, q = mid, r = end
{
int i = 0; // left array iterator
int j = 0; // right array iterator
int n1 = q - p + 1;
int n2 = r - q;
// int arrayL[n1 + 1];
//int arrayR[n2 + 1];
int arrayL[n1];
int arrayR[n2];
for (i = 0; i < n1; i++) {
arrayL[i] = array[p + i];
}
cout << "TEST ARRAY MS A: ";
for (int count = 0; count < n1; count++) {
cout << arrayL[count] << " ";
}
cout << endl << endl;
for (j = 0; j < n2; j++) {
arrayR[j] = array[q + j + 1];
}
cout << "TEST ARRAY MS B: ";
for (int count = 0; count < n2; count++) {
cout << arrayR[count] << " ";
}
cout << endl << endl;
//arrayL[n1 + 1] = 1000;
//arrayR[n2 + 1] = 1000;
//i = 0;
//j = 0;
for (int k = p, i = j = 0; k <= r; k++) {
if (j >= n2 || (i <= n1 && arrayL[i] <= arrayR[j])) {
array[k] = arrayL[i];
i++;
cout << "TEST ARRAY in loop A: ";
for (int tempIt = 0; tempIt < r; tempIt++) {
cout << array[tempIt] << " ";
}
cout << endl << endl;
}
else {
array[k] = arrayR[j];
j++;
cout << "TEST ARRAY in loop B: ";
for (int tempIt = 0; tempIt < r; tempIt++) {
cout << array[tempIt] << " ";
}
cout << endl << endl;
}
cout << "TEST ARRAY in loop: ";
for (int tempIt = 0; tempIt < r; tempIt++) {
cout << array[tempIt] << " ";
}
cout << endl << endl;
}
}
void mergeSort (int * array, int p, int r)
{
if (p < r) {
q = floor((p + r) / 2);
mergeSort(array, p, q);
mergeSort(array, q + 1, r);
merge(array, p, q, r);
}
}
int main(int argc, const char * argv[])
{
unsigned seed = time(0);
srand(seed);
int testArray[5];
populateArray(testArray, 5);
cout << "TEST ARRAY: ";
for (int count = 0; count < 5; count++) {
cout << testArray[count] << " ";
}
cout << endl << endl;
mergeSort(testArray, 0, 4);
cout << "TEST ARRAY after mergeSort: ";
for (int count = 0; count < 5; count++) {
cout << testArray[count] << " ";
}
cout << endl << endl;
return 0;
}