Troubles with extracting data

So I have a program that has to calculate the coefficients of this formula (ax+by)^n using Pascal's triangle, but I have some troubles with getting data from file and converting it into int.

About file pascal.txt it is the text file containing the Pascal's triangle/pyramid like below:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
...

In line 23 I open that file(pascal.txt) which is in the same foler with the program.

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
 #include <iostream>
#include <cmath>
#include <fstream>
#include <sstream>
//fstream to read Pascal's pyramid from .txt file


using namespace std;

int main () {
	int a, b, n;
        cout << "Enter the coefficients of the formula(Ax+By)^N, A, B, N:";
	cin >> a >> b >> n;
	
	long int ar[26]={1}, br[26]={1};
	unsigned long long int cr[26]={1};
	//getting powers
	for (int i=0; i<=n; i++) {
		ar[i] = pow(a, i);
		br[26-i] = pow(b, i);
	}
	
	//getting input from .txt file (not working?)
	ifstream pascal ("pascal.txt");
	string sline;
	int line_no = 0;
	while (line_no != n) {
		getline(pascal, sline);
		++line_no;
	}

	//reading coefficients from table / pyramid
	int l = sline.length();
	unsigned long long int dr[26]={1};
	int count=0;
	fstream strtoi ("temporary_file.txt");
	strtoi << "";
	
	for (int i=0; i<l; i++) {
		char ch = sline[i];
		int c = (int)ch;
		if (c>=48 && c<=57) { //checkin' number's ASCII, if it's between the values
			strtoi << ch;     //it will be stored into file
		}
		else if (c == 32) {   //getting the number from file...
			string temp = "";
			getline(strtoi, temp);
			int k;
			istringstream (temp) >> k;
			
			dr[count] = k;
			count++;
		}
	}
	
	//finally multiplying it all;
	for (int i=0; i<=n; i++) {
		cr[i]=ar[i]*br[26-i]*dr[i];
		cout << cr[i] <<" "<< ar[i] <<" "<< br[26-i] <<" "<< dr[i] << endl;
	}
	
	return 0;
}
Last edited on
Hello electro amperkin,

I seem to be having a hard time extracting data from the "pascal.txt" too. Maybe that is because you did not post that file and I have no idea what is in it.

Provide the input file, or a fair sample if large, so everyone can use the same information when working with your program.

Line 23 opens a file for input, but how do you know it is open? You could be trying to process a file that can not be read.

Line 12 gives me a blank screen with a flashing cursor. What do I do at that point? Line 12 should have a prompt before it to say what needs to be done. Not every user will have the code to refer to when it comes to entering the numbers.

Here is a suggestion:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
	constexpr size_t MAX_ARRAY_SIZE{ 26 };

	int a, b, n;
	 // <--- Needs a prompt.
	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[26 - i] = pow(b, i);
	}

Using the uniform initializer, the {}s, the "=" are not needed, but OK if you want to use them.

Notice that a couple of blank lines make it easier to read the code. The spacing in the for loop was inserted by my IDE, but it does help.

Andy
Handy Andy, so the pascal.txt file is the file containing pascals triangle in the next form:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
...

Then sorry about line 12, there you just have to enter the coefficients of the formula (ax+by)^n, maybe you are familiar with this formula. I will add the instructions later, sorry for that.

About line 23, so in a same folder i have a ready text file(pascal.txt) containing the Pascal's triangle like above.

Thank you for your suggestions, I will try to not repeat my mistakes when asking a question
Hello electro amperkin,

Most times it is not a good idea to change your Original Post. In this case adding the input file is OK, but changing the code might allow people who read it think that there is nothing wrong.


maybe you are familiar with this formula


Sorry no. My geometry is limited to one class in high school. But I will look it up and see what I can learn.

Now that I have the input file I will give the program a run and see what happens.

Andy
Hello Handy Andy,
I am just new at the forums so please, pardon me.

About formula, it is the formula from algebra named abbreviated multiplication formula or special algebra (/polynomial) expansions. I do not know the exact names because I am translating it from my native language.

Really hope for your help.

electro
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
I am getting a bettering understanding of Pascal's triangle, but it is not a quick learn.
-------------------
Andy, its just the binomial coefficents. The first few (4 or 5?) terms are 11 to the nth power (and for most practical equation usage, it works, but for larger equations, it gets wonky due to inner terms being multi digit).

that is, for this:
(a+b) to the nth power.
if N == 3, you use 11 to the third: 1 3 3 1 are your coefficients on the terms.
meaning it explodes into (1)a^3 + (3)(absomthing) + (3)(absomethingelse) + (1)b^3
and so on.

I forget if there are any other big uses of the triangle.
its computed by summing the inner terms and keeping the leading and trailing 1s, for lack of a better way to describe it.

The OP already has his coefficents from the file, so I dunno what is going on after that bit.
Last edited on
Hello Handy Andy,
I read your reply and realized some big mistakes, especially line 17, that you mentioned.

I will edit my code in the editor and maybe open a new question on the forum.

Anyway, I will use better names for my variables.

Little bit about fstream on line 36, so I need this file to both write and read so I just left it blank.

And the reason for the triangle to be so big because I had some math tasks and I am little bit lazy to count everything so i just did till' the biggest power and tried to automate finding coefficients.

electro
Topic archived. No new replies allowed.