Hello electro amperkin,
I am getting a bettering understanding of Pascal's triangle, but it is not a quick learn.
After working with the program this is some of what I have found.
When it comes to variable names try to avoid using a single letter. It is better to use a name that means something. As I have read this is usually a noun and sometimes a verb that describes what the variable is or is used for. For example "n" could be called "powerOf" or something like that.
It would help if "main" looked something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
int main()
{
constexpr size_t MAX_ARRAY_SIZE{ 26 };
int a, b, n;
cout << "Enter the coefficients of the formula(Ax+By)^N, A, B, N:";
cin >> a >> b >> n;
long int ar[MAX_ARRAY_SIZE]{ 1 }, br[MAX_ARRAY_SIZE]{ 1 };
unsigned long long int cr[MAX_ARRAY_SIZE]{ 1 };
//getting powers
for (int i = 0; i <= n; i++)
{
ar[i] = pow(a, i);
br[MAX_ARRAY_SIZE - i] = pow(b, i);
}
//getting input from .txt file (not working?)
ifstream inFile ("pascal.txt");
if (!inFile) // <--- Added.
{
std::cout << "\n File \"" << "pascal.txt" << "\" did not open" << std::endl;
//std::this_thread::sleep_for(std::chrono::seconds(3)); // <--- Needs header files chrono" and "thread". Optional.
return 1;
}
| |
Line 17 is a problem. A small thing try to avoid using a magic number here. Having to go through a large program and change numbers like (26) is not easy. You could miss one or two then the program stops working the way it did.
Next is that (26 - 0) is (26). the array had (26) elements to it numbered 0 - 25, so 26 is past the end of the array and you do not know what you are storing you number in. I think what would work better here is
br[(MAX_ARRAY_SIZE - 1) - i] = pow(b, i);
. That would start at element 25 and go down. The next thing to be careful of is not going less than zero.
In the for loop be careful of using "<=". Most times this will cause the for loop to loop one extra time which could be a problem when accessing an array. When starting at zero "<" is most often used.
Bear with me. When it comes to file streams I like to use the names "inFile" and "outFile". The real point here is that when opening a file for input it is a
MUST that you check that the stream is open and usable before you continue.
For an optput stream this is not always necessary because if the file does not exist it will be created before writing to it. If the file name has a path to it then you would need to check that it is open because the path could be wrong.
When you get to the line
fstream strtoi("temporary_file.txt");
. Come up with a better name. Although "strtoi" is not part of "cstdlib" it could be. There is "strtol" to convert a C string to "long". Close enough to be a problem is you mistype the name. Also the name does not quite go with what it is doing.
The next problem is the it is defined as a "fstream". This is fine, but you need to tell it if it is for input or output or both, i.e.,
fstream outFile("temporary_file.txt", std::ios::in | std::ios::out);
. This would be for input and output otherwise you would use the second part for output.
string temp = "";
If you mean to insert a space then say so. Empty "" do not make a space, but " " does. If you mean to initialize the string to empty that is not necessary because the string is empty when defined and has no length or value.
That is what I see for now.
When it comes to entering values for "a", "b" and "n" it would help if could share test values and an idea of the expected output.
Andy