So I've been working on problem 15 from the Project Euler's website , and my solution was working great up until I decided to remove the cout statements I was using for debugging while writing the code. My solution works by generating Pascal's Triangle in a 1D array and finding the element that corresponds to the number of paths in the NxN lattice specified by the user. Here is my program:
#include <iostream>
using namespace std;
//Returns sum of first n natural numbers
int sumOfNaturals(const int n)
{
int sum = 0;
for (int i = 0; i <= n; i++)
{
sum += i;
}
return sum;
}
void latticePascal(const int x, const int y, int &size)
{
int numRows = 0;
int sum = sumOfNaturals(x + y + 1);
numRows = x + y + 1;
//Create array of size (sum of first x + y + 1 natural numbers) to hold all elements in P's T
unsigned long long *pascalsTriangle = new unsigned long long[sum];
size = sum;
//Initialize all elements to 0
for (int i = 0; i < sum; i++)
{
pascalsTriangle[i] = 0;
}
//Initialize top of P's T to 1
pascalsTriangle[0] = 1;
cout << "row 1:\n" << "pascalsTriangle[0] = " << 1 << "\n\n"; // <--------------------------------------------------------------------------------
//Iterate once for each row of P's T that is going to be generated
for (int i = 1; i <= numRows; i++)
{
int counter = 0;
//Initialize end of current row of P's T to 1
pascalsTriangle[sumOfNaturals(i + 1) - 1] = 1;
cout << "row " << i + 1 << endl; // <--------------------------------------------------------------------------------------------------------
//Iterate once for each element of current row of P's T
for (int j = sumOfNaturals(i); j < sumOfNaturals(i + 1); j++)
{
//Current element of P's T is not one of the row's ending 1s
if (j != sumOfNaturals(i) && j != (sumOfNaturals(i + 1)) - 1)
{
pascalsTriangle[j] = pascalsTriangle[sumOfNaturals(i - 1) + counter] + pascalsTriangle[sumOfNaturals(i - 1) + counter + 1];
cout << "pascalsTriangle[" << j << "] = " << pascalsTriangle[j] << '\n'; // <--------------------------------------------------------
counter++;
}
//Current element of P's T is one of the row's ending 1s
else
{
pascalsTriangle[j] = 1;
cout << "pascalsTriangle[" << j << "] = " << pascalsTriangle[j] << '\n'; // <---------------------------------------------------------
}
}
cout << endl;
}
cout << "Number of SE paths in a " << x << "x" << y << " lattice: " << pascalsTriangle[sumOfNaturals(x + y) + (((sumOfNaturals(x + y + 1) - 1) - sumOfNaturals(x + y)) / 2)] << endl;
delete[] pascalsTriangle;
return;
}
int main()
{
int size = 0, dim1 = 0, dim2 = 0;
cout << "Enter dimension 1 for lattice grid: ";
cin >> dim1;
cout << "Enter dimension 2 for lattice grid: ";
cin >> dim2;
latticePascal(dim1, dim2, size);
return 0;
}
The cout statements that seem to be saving my program are marked with commented arrows. It seems to work as long as any of these lines are included. If all of these statements are removed, then the program will print: "Number of SE paths in a " and then hang for a couple of seconds before terminating without printing the answer. I want this program to be as clean as possible and to simply output the answer without having to print the entire contents of the triangle, so it is not working as intended in its current state.
There's a good chance that either the expression to calculate the array index or the one to calculate the array size for allocation causes undefined behaviour, for example, a stack overflow.
Because the visibility of this undefined behaviour to you is not defined the program can work as you intended or it can do something else - which could explain why it works with one compiler but not another.
You could use a vector with vector::resize() and vector::at() instead of an array with new and [] to get some improved information in the case that the program aborts before writing or flushing all of its output due to an invalid memory access.
If the problem is due to an invalid index being used then vector::at() will raise an exception which you won't catch and many debuggers will stop when they find this pair of factors together and they'll help you to inspect the point in the program where the problem occurred and key facts like which index you were trying to access and the contents of the variables.
They'll typically show you more "stack frames" than you expect but some are internal details of how the system manages uncaught exceptions and you should expect that the debugger helps you to find the stack frame relevant to your problem evolving so you can inspect the context of that one.
Your program works well with g++ on Linux:
$ g++ -o main pascal.cpp
$ ./main
Enter dimension 1 for lattice grid: 3
Enter dimension 2 for lattice grid: 4
Number of SE paths in a 3x4 lattice: 35
There's got to be something else since your cout statements have no side effects.
Here's an idea on how to debug this: open 2 visual studio instances, one will have the version without the cout statements, and the other one will have the version with them. Simply do a step by step debug to find the first difference between them. My guess is that you will realize that the cout statements have nothing to do with the error.
Related
code can be run/compiled here https://onlinegdb.com/9yhzLeVu3
this code will only print value 4 for all the values if tested on a 2x2 matrix
[1][2]
[3][4]
I believe its with the cout statement, I am fairly convinced it is saving the values to the matrix but, am not seeing it print correctly.
If could please tell me what I am not seeing? this syntax is very new to me, this program functions as functional programming but, is difficult once I start converting it to methods/functions.
#include <iostream>
using namespace std;
// Create a matrix based graph representation.
// It will need to support the following operations.
// Ask the user how many points there are. DONE
// Ask the user to label those points, ie "ABC", "XYZ", "C12"... DONE
// Define the matrix as a square matrix (2 dimensional array) based on the number of points, also keep an array of the labels. DONE
// Repeatedly ask the user to define edges between two points. Add these edges to the matrix. DONE
// Have a list method that will list out all of the edges in the graph.
// REFERENCE
// https://www.geeksforgeeks.org/how-to-access-elements-of-a-square-matrix/
// https://www.geeksforgeeks.org/comparison-between-adjacency-list-and-adjacency-matrix-representation-of-graph/
// https://www.cplusplus.com/reference/utility/make_pair/
// https://www.tutorialspoint.com/cplusplus-program-to-implement-adjacency-matrix
// https://stackoverflow.com/questions/2828648/how-to-pass-a-multidimensional-array-to-a-function-in-c-and-c
// https://www.techiedelight.com/pass-2d-array-function-parameter-cpp/
// https://www.tutorialspoint.com/Passing-two-dimensional-array-to-a-Cplusplus-function
// https://stackoverflow.com/questions/71907069/my-print-function-does-not-print-correctly-when-passing-a-2d-array-please-tell/71907317#71907317
// one possible implementation would be make pair to track the label for edge cout statement for user input
// Code is formatted to be best read with labels the size of 3 this is hard coded per implementation requirements.
int main()
{
// PROTOTYPE
void printMatrix(string *labelArray, int *matrix, int rowIndex, int columnIndex, int size);
int size ;
cout << "How many points would you like this to be. Points meaning -size- of matrix: ";
cin >> size;
// example 2x2 matrix
// A B
// A [][]
// B [][]
// determine size, and modulo to create matrix form
string label; // labeling convention for determining assignments
string labelArray[size]; // label containing array to track for printing and labeling purposes
int edgeYesOrNo;
int counter = 0;
cout << "Will now ask to label the points of graph matrix.\n";
int *matrix = new int[size * size]; // this is how to define a 2d array
int rowIndex; // these are to access individual elements
int columnIndex; // ^^
for(int i=0; i<size; i++)
{
cout << "Enter label: "; // enter the label here to insure that there is no redundancy
cin >> label;
labelArray[i] = label;
}
// Get the square matrix
cout << "Enter 1 for edge 0 for no edge" << endl;
for (rowIndex = 0; rowIndex < size; rowIndex++)
{
for (columnIndex = 0; columnIndex < size; columnIndex++)
{
cout << "Is there an edge to: " << labelArray[counter] << " and " << labelArray[columnIndex] << ": ";
cin >> edgeYesOrNo;
matrix[rowIndex * size + columnIndex] = edgeYesOrNo;
}
counter++;
}
printMatrix(labelArray, matrix, rowIndex, columnIndex, size);
delete[] matrix;
return 0;
}
// Display the matrix
void printMatrix(string *labelArray, int *matrix, int rowIndex, int columnIndex, int size)
{
cout << "The matrix is\n" << endl;
cout << " ";
for(int i=0; i<size; i++)
{
cout << labelArray[i] << " "; // To print the labels so its understandable
}
cout << endl;
for (rowIndex = 0; rowIndex < size; rowIndex++)
{
cout << labelArray[rowIndex] << " ";
for (columnIndex = 0; columnIndex < size; columnIndex++)
{
cout << matrix[rowIndex * size + columnIndex] << " ";
}
cout << endl;
}
return;
}
What do you think these lines are going to do:
matrix[size * size] = edgeYesOrNo;
cout << matrix[size * size] << " ";
I find it far more likely you need rowIndex * size + colIndex in both places.
I'll explain the difference. You've already allocated your array when you did this:
int *matrix = new int[size * size];
This is not a 2D array. It's a single dimension array. But that's okay. If size is four, size * size is 16, so you allocated space for 16 integers.
matrix[rowIndex * size + colIndex]
This is how I use it in both my places. What this means is you'll store them kind of like this:
[ 0][ 1][ 2][ 3]
[ 4][ 5][ 6][ 7]
[ 8][ 9][10][11]
[12][13][14][15]
That is, row 0, col 0 is thus in the 0'th index. row 1, col 1, is in the row * size + col location, or 5.
Does that make sense now?
C++ doesn't actually have 2D arrays. You can simulate it. This is one way to do so.
What you were doing was using (for size == 4) the 16th spot each time, which is actually one slot past the end. You were stepping on who knows what.
I'm a C++ beginner. I just made a coin change calculator and now I want the code to be shorter (with a loop or something). How can I do that?
#include <iostream>
int main() {
int amount = 0;
int result[4] = { 0, 0, 0, 0 };
int values[4] = { 25, 10, 5, 1 };
int x = 0;
std::cout << "Welcome to this super advanced (not) coin change calculator! \n";
std::cout << "Please enter the amount in cents: ";
std::cin >> amount;
x = amount;
result[0] = x / values[0];
x = x % values[0];
result[1] = x / values[1];
x = x % values[1];
result[2] = x / values[2];
x = x % values[2];
result[3] = x / values[3];
x = x % values[3];
std::cout << "Optimal change: " << result[0] << " quarter(s), " << result[1] << " dime(s), " << result[2] << " nickel(s), and " << result[3] << " pennie(s)!";
return 0;
}
A few techniques apply
1) Rather than outputting multiple string literals to std::cout, combine all the literals into one, and output that. Bear in mind that a pair of string literals following each other are combined. For example, the construct "ab" "cd" becomes "abcd"
2) Eliminate any variables that are not needed. In your case, your code reads to amount, does an assignment x = amount, and then never uses amount again. This means there is an opportunity to eliminate either amount (read directly to x and proceed from there) or x (don't assign x = amount, and then do all operations on amount).
3) If you are reusing logic that only differs by an index such as result[0] = x/values[0] and later result[1] = x/values[1] then consider a loop.
4) If you have multiple strings to be output (you do!) consider placing them in an array too - then access elements of that array if the loops too.
5) Don't be afraid to break statements into pieces and re-order operations if it allows you to rationalise.
6) If we are doing x = x op y change that to x op = y. For example, change amount = amount % values[i] to amount %= values[i].
Putting all that together, you get.
#include <iostream>
int main()
{
int amount = 0;
int result[4] = { 0, 0, 0, 0 };
int values[4] = { 25, 10, 5, 1 };
const char *denom[4] = {"quarter(s),",
"dime(s),",
"nickel(s), and",
"pennie(s)!"
};
std::cout << "Welcome to this super advanced (not) coin change calculator!\n"
"Please enter the amount in cents: ";
std::cin >> amount;
for (i = 0; i < 4; ++i)
{
result[i] = amount / values[i];
amount %= values[i];
}
std::cout << "Optimal change: ";
for (i = 0; i < 4; ++i)
{
std::cout << result[i] << denom[i] << " ";
}
return 0;
}
But we can go further. In the above, I've broken the initialisation of denom into multiple lines for clarity, but the initialisation of the array can be combined into a single line (at some cost of readability)
const char *denom[4] = {"quarter(s),", "dime(s),", "nickel(s), and", "pennie(s)!"};
Having done this, we see that can combine the two loops into one if we move the output statement that is between them, and then not actually need the array result (elements are only calculated in the loop, and then output). So we can eliminate that array, and make it a single variable - local to the loop.
std::cout << "Optimal change: ";
for (int i = 0; i < 4; ++i)
{
int result = amount / values[i];
amount %= values[i];
std::cout << result << denom[i] << " ";
}
return 0;
}
Having done that, look inside the loop, and note that result is only calculated so we can output it. So eliminate it by changing the loop to
for (int i = 0; i < 4; ++i)
{
std::cout << amount/values[i] << denom[i] << " ";
amount %= values[i];
}
Having done all that, we get
#include <iostream>
int main()
{
int amount = 0;
int values[4] = { 25, 10, 5, 1 };
const char *denom[4] = {"quarter(s),", "dime(s),", "nickel(s), and", "pennie(s)!"};
std::cout << "Welcome to this super advanced (not) coin change calculator!\n"
"Please enter the amount in cents: ";
std::cin >> amount;
std::cout << "Optimal change: ";
for (int i = 0; i < 4; ++i)
{
std::cout << amount/values[i] << denom[i] << " ";
amount %= values[i];
}
return 0;
}
In doing the above, I've focused on making your code shorter. I have taken liberties with changing the order in which operations are done, but the output produced will be the same.
There is more that can be done too. Your code is fairly straight forward, but the C++ standard library includes containers (to manage collections of values) that represent vectors, lists, strings, etc. These allow you to eliminate raw pointers or raw arrays entirely, and operations (like resizing, inserting elements, removing elements, etc) are handled more cleanly than doing them by hand. There is also a set of algorithms (in standard header <algorithm>) that can operate on every element of containers - if you are writing loops that run over every element of a container (or even a raw array), then there is often (not always) an algorithm that can be used to do the same thing - with more concise code that is easier to read, therefore easier to get right.
I am trying to create a program to print first 200 elements following a specific numerical series condition which is
1-1-3-6-8-8-10-20
But instead of showing, just 200 elements is showing 802. I assume is because of the code inside the for loop. I have hours thinking on how to reduce that code to the job and I cannot think anything else. I am getting frustrated and need your help.
The exercise is on the code comments
//Print the following numerical series 1-1-3-6-8-8-10-20 until 200
#include <stdafx.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int Num1=200, z = 0, x = 1, y = 1;
cout << "\n\n1,";
cout << " 1,";
for (int i = 1; i <= Num1; i++)
{
z = y + 2;
cout << " " << z << ","; //It will print 3
z = z * 2;
cout << " " << z << ",";//It will print 6
z = z + 2;
cout << " " << z << ",";//It will print 8
z = z;
cout << " " << z << ",";//It will print 8
y = z;
}
cout << "\n\n";
system("pause");
return 0;
}
You're looping 200 times, and each time you loop, you're printing out 4 different numbers. You're also printing twice at the start so thats 2 + 4 * 200 = 802, which is where your 802 number output is coming from.
I assume is because of the code inside the "for" loop but I've hours
thinking on how to reduce that code to the job and I cannot think
anything else. I'm getting frustrated and need your help.
So you basically wanna simplify your code. Which can be done by noticing the repetitions.
There you can find only two types of change in the series; either a +2 or x2 with the previous element.
In each iteration this can be achieved by:
If reminder i%4 == 1 or i%4 == 3, need an increment of 2 (assuming 1 <= i <= MAX)
If reminder i%4 == 0, nothing but a multiplication of 2.
When you do like so, you can simply neglect, printing of first two ones and other complications in the total numbers in the series.
Also not that, you are trying to get 200 terms of this series, which increases in each step very fast and exceed the maximum limit of int. Therefore, long long is needed to be used instead.
The updated code will look like this:
#include <iostream>
typedef long long int int64;
int main()
{
int size = 200;
int64 z = -1;
for (int i = 1; i <= size; i++)
{
if ((i % 4 == 1) || (i % 4 == 3)) z += 2;
else if (i % 4 == 0) z *= 2;
std::cout << z << "\n";
}
return 0;
}
See the Output here: https://www.ideone.com/JiWB8W
To practice using pointers and arrays i'm trying to do a simple program capable of converting a binary input to denary.. i think i have a good idea for the logic but i haven't even got round to trying to implement it because im struggling to get my for loop running!
It seems silly but i know the code inside the for loop works fine outside of it, so it must be something wrong with the condition..? im trying to start at the back of the char array (navigating using pointers) and output each char(as an int) up to the first element.
So the desired output is "0 - 1 - 0 - 1 -"
#include <iostream>
using std::cout;
using std::endl;
//prototypes
void binaryToDenary(const char* input, int& inputLength);
int main(){
const char binaryInput[] = {1,0,1,0};
int inputLength = sizeof(binaryInput)/sizeof(binaryInput[0]);
binaryToDenary(binaryInput, inputLength);
return 0;
}
void binaryToDenary(const char* input, int& inputLength){
//testing some stuff--- this all works as expected
//cout << input[2] << " " << (int)*(input+2) << " " << inputLength <<endl;
int i;
for(i = inputLength; i < 0; i--){
cout << (int)*(input+i) << " - ";
}
}
Your for loop should be this:
for(i = inputLength -1 ; i >= 0; i--)
{
cout << (int)*(input+i) << " - ";
}
There are two problems in your code:
i = inputLength which should be i = inputLength -1
i < 0 which should be i >= 0
Also, change the second parameter type from int & to int:
void binaryToDenary(const char* input, int inputLength) //now its better!
The type int& reduces the use cases, and benefits almost nothing. If you use int &, then all of these would give compilation error:
const int inputLength = sizeof(binaryInput)/sizeof(binaryInput[0]);
^^^^ note this
binaryToDenary(binaryInput, inputLength); //compilation error
binaryToDenary(binaryInput, sizeof(binaryInput)/sizeof(binaryInput[0])); //error
binaryToDenary(binaryInput, 4); ////compilation error
So use int, and all of the above would compile fine!
Array indexes start from zero, so the last element is at inputLength - 1. With i < 0 you exit from the loop immediately as that never be true...
for(i = inputLength - 1; i >= 0; i--){
cout << (int)*(input+i) << " - ";
}
for(i = inputLength; i < 0; i--)
will run only if inputLength is less than 0, which is not possible?
You need:
for(i = (inputLength-1); i >= 0; i--)
^^^^^^^^^^^^^^ ^^
C arrays are 0 based so a valid index is given by
(0 <= i) && (i < array_length)
In your program, this means that the position of the last digit in your initialization should be inputLength - 1 and the loop condition should be i >= 0.
(As for why you loop is not running, at the start you have i == inputLength, so i is positive, failing the i < 0 condition immediately).
You want to run as long as i is bigger (or maybe equal) to zero. You were trying to run the loop as long as i was less than zero, and beginning with a value greater than zero results to that you never enter the loop.
for(i = inputLength; i > 0; i--){
cout << (int)*(input+i) << " - ";
}
You must check for iteration loop variable i to be positive..
However you should use a STL iterator on the binary input vector and not loop on it's content in a c fashion, if you want to practice C++, possible solution could be:
vector<char> binaryInput;
binaryInput.push_back(1);
binaryInput.push_back(0);
binaryInput.push_back(1);
binaryInput.push_back(0);
vector<char>::iterator it;
for ( it=myvector.begin() ; it < myvector.end(); it++ ){
cout << " " << *it << endl; //or whatever you need to do with vector content
}
So I create and initialize a vector (of size nmask+3) to 0, and I assign an initial value to one of the elements. I then make a for loop that goes through the first nmask elements of the vector and assigns to each element an average of 26 other elements in the vector (defined by the 4D int array voxt, which contains vector addresses).
My problem is that when I check the values of nonzero elements in my vector (phi) within the nested loop (the first cout), the values are fine and what I expect. However, when the loop finishes going through all nmask elements (for (int i= 0; i<nmask; i++) exits), I check the nonzero elements of phi again, and they are all lost (reset to 0) except for the last non-zero element (and element tvox which is manually set to 1).
I feel that since phi is initialized outside of all the loops, there should be no resetting of values going on, and that any updated elements within the nested loop should remain updated upon exit of the loop. Any ideas as to what is going on / how to fix this? Code is below; I tried to comment in a sense of the outputs I'm getting. Thanks in advance.
vector<double> phi(nmask+3, 0); //vector with nmask+3 elements all set to 0 (nmask = 13622)
phi[tvox]= 1; //tvox is predefined address (7666)
for (int n= 0; n<1; n++)
{
vector<double> tempPhi(phi); //copy phi to tempPhi
for (int i= 0; i<nmask; i++)
{
for (int a= -1; a<=1; a++)
{
for (int b= -1; b<=1; b++)
{
for (int c= -1; c<=1; c++)
{
if (!(a==0 && b==0 && c==0))
{
//oneD26 is just (double) 1/26
phi[i]= tempPhi[i]+oneD26*tempPhi[voxt[i][1+a][1+b][1+c]];
if (phi[i]!=0)
{
//this gives expected results: 27 nonzero elements (including tvox)
cout << n << " " << i << " " << a << b << c << " " << phi[i] << endl;
}
}
}
}
}
}
phi[svox]= 0; //svox = 7681
phi[tvox]= 1;
for (int q= 0; q<nmask; q++)
{
//this gives only 2 nonzero values: phi[tvox] and phi[9642], which was the last nonzero value from 1st cout
if (phi[q]!=0)
cout << q << " " << phi[q] << endl;
}
}
Difficult to tell just what is going on, but the easiest explanation is that after phi[i] gets set to non-zero and displayed to cout, it gets set to zero again in one of the later iterations through the inner loops.
If you do some tracing and check phi[i] just before updating you'll see that you often overwrite a non-zero element with zero.
Note: I have no idea what your code does, this is pure Sherlock Holmes reasoning.. if after the loops you find only 2 non-zero elements then the only logical consequence is that after updating something to non-zero later in the loop you update it to zero.
phi[i]= tempPhi[i]+oneD26*tempPhi[voxt[i][1+a][1+b][1+c]];
The nested for-loops using a, b, and c run for a combined 9 iterations with the same value of i. Since you overwrite phi[i] to a new value every time, you only retain the value from the last iteration where a, and c are all 1. If that last iteration happens to produce zero values, then phi[i] will have lots of zeroes. Perhaps you meant to do something like phi[i] += ... instead of phi[i] = ...?
I do suggest to replace the meat of the loop with something like
const boost::irange domain(-1,2);
for (int i: boost::irange(0, nmask)) for (int a: domain) for (int b: domain) for (int c: domain)
{
if (a==0 && b==0 && c==0)
continue;
//oneD26 is just (double) 1/26
phi[i]= tempPhi[i]+oneD26*tempPhi[voxt[i][1+a][1+b][1+c]];
if (phi[i]!=0)
{
//this gives expected results: 27 nonzero elements (including tvox)
cout << n << " " << i << " " << a << b << c << " " << phi[i] << endl;
}
}
Of course, for brevity I assume both boost/range.hpp and c++0x compiler. However, with trivial macro's you can achieve the same. That is without writing/using a proper combinations algorithm (why is that not in the standard, anyway).