Search code examples
c++ntl

Resize of NTL vector


i would like to resize my ZZ vector during running program. Is there any way, how to make it? I found methods .setLenght() alternatively .DosetLenght(), but it seems like only initialization step, due to my pro/gram refuses change the vector with these methods..

Many thanks.

 Vec<ZZ> v1,v2;
 v1.SetLength(8);
 v2.SetLength(8);
 ZZ velkeCislo,odmocnina,factor,test;
 long i = 0;
 cin >> velkeCislo;
 odmocnina = SqrRoot(velkeCislo);
 cout << "new v1 dlzka " << v1.length() << endl;
 for(i=0;i<v1.length();i++) {
 v1(i) = odmocnina;
 odmocnina++;
 cout << "Number1 " << v1(i) << endl;
 }
 for(i=0;i<v1.length();i++){
   v2(i)=(v1(i)*v1(i))-velkeCislo;
  cout << "Number2 " << v2(i) << endl;
 }
 bool found=false;
 int tp = v1.length();
 cout << "old v1 " << v1.length() << endl;
 v1.SetLength(tp+1); //causes error
  cout << "new v1 " << v1.length() << endl;

Solution

  • The problem with your code is also explained here. You are using the method v1(i) to access the array, but this is a 1-based indexing system so you have out of bounds accesses in your program. Replace v1(i) with v1[i] (which is zero-based) and your program should work.