something doesn’t make sense with the for loop

so this program should be getting values of x coeff for any polynomial of degree n then gives the value of it when substituting x with (0 , 1 , 2 )
I think that I got the math right. and it gives me back the results I want
here’s the problem
the first loop should be iterated t times where t is the test cases needed to be solved
for (int i=0;i<t;i++)
why is this just not working right ?
I tried some other ways
for (int i=1;i<(t+1);i++)
and surprisingly it worked with t=2 only !! nothing is making sense here :)

so, how’s that ? :/
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

#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

int main() {
	
	//opening the input and output file
	ifstream infile;
	ofstream outfile;
	infile.open("input.txt");
	outfile.open("output.txt");
	
	int t,n;
	infile >> t;
	if ( t>=1 && t<=1000){
		// here’s the problem 
		for (int i=0;i<t;i++){ //here’s where the problem is !!!!!!
			infile >> n;
			if (n>=1 && n<=30){
				long long int poly [n+1];
				//getting the values of the x coeff.
				for (int i=0;i<(n+1);i++){
					infile >> poly [i];
				}
				
				//f(0)
				long long int TotalZero=0;
				TotalZero += poly [n];
				outfile << TotalZero << "  ";
			
				//f(1)
				long long int TotalOne=0;
				for (i=0;i<n;i++){
					TotalOne+= poly[i]*pow(1, n-i);
				}
				outfile << (TotalOne + poly[n]) << "  ";
				
				//f(2)
				long long int TotalTwo=0;
				for (i=0;i<n;i++){
					TotalTwo+= poly[i]*pow(2, n-i);
				}
				outfile << TotalTwo+poly[n] << "  ";
				outfile << endl;
			} else {
				outfile << "n value must be 1 <= n >= 30";
			}
		}
	} else {
		outfile << "test cases value must be 1 <= t >= 1000";
	}
	
		//closing the input and output file
	infile.close();
	outfile.close();
	return 0;
}

Last edited on
Show an example of input file.


Note: This requires support for VLA:
1
2
infile >> n;
long long int poly [n+1];


How about:
1
2
infile >> n;
std::vector<long long int> poly( n+1 );

what does this actually do ? I don’t get it ?
..
that’s not where the problem is though :/
I got it :)
I used the variable i twice that’s why :D
Topic archived. No new replies allowed.