I've got a bigint class that uses an array called SafeArray that I created in a different class. I couldn't use vectors. The set and get function calls are from the SafeArray class. Get takes an int parameter for array position and set takes 2 int parameters (one for position and one for value). All methods in this bigint class work fine (we don't have to account for negative integers) except my compare method needs work. I want it to be able to compare two bigints and if the (const bigint and &A) number is larger than the other (cout 1) if it is smaller (cout 2) if they are the same (cout 0). Any help with this method would be greatly appreciated. Thanks
int size = 35; //will get bigger, small now just for testing
class bigint
{
SafeArray<int> *arr;
public:
bigint() //initializes to zero
{
arr = new SafeArray<int>;
for(int i =0;i < size; i++)
arr->set(i,0);
}
void print() //prints numbers without zeroes in front
{
bool start_num=false;
for(int i = 0;i <arr->get_size() ;i++)
{
if(arr->get(i)!=0 && start_num==false )
{start_num=true;
cout << arr->get(i);}
else if(start_num==true)
cout<<arr->get(i);
}
cout<<endl;
}
void assign(const bigint &A) //
{
for(int i=0;i<arr->get_size();i++)
{ //Ways to initialize stuff
arr->set(i,A.arr->get(i));
}
}
void assign(int num) //
{
for(int i = arr->get_size()- 1; i >= 0; i--)
{
arr->set(i,num%10);
num /=10;
}
}
void assign(string num) //
{
long len = num.length();
int j=arr->get_size()-1;
for(long i=len-1;i>=0;i--)
{
arr->set(j,num[i]-48);
j--;
}
}
void add(const bigint &A) //add big ints
{
int carry=0;
for(int i=size-1;i>=0;i--)
{
int result = arr->get(i)+A.arr->get(i)+carry;
arr->set(i,result%10);
carry=result/10;
}
}
void subtract(const bigint &A) //subtract big ints
{
int borrow = 0;
for(int i=size-1; i >= 0; --i)
{
int result=((arr->get(i) - A.arr->get(i) - borrow));
if(result < 0)
{
arr->set(i, result + 10);
borrow = 1;
}
else
{
arr->set(i, result);
borrow = 0;
}
}
}
//int compare(const bigint &A) //compare big ints
// {
//
// for(int i<size;i>0;i--)
// {
// if(A.arr->get(i) > arr->get(i))
// {
// return 1;
// }
// else if(A.arr->get(i) < arr->get(i))
// {
// return -1;
// }
// else
// {
// return 0;
// }
// }
//
// }
};
int main()
{
bigint a, b, c;
a.assign("12345678"); //for testing
b.assign("12345678");
//a.compare(b);
a.print();
c.assign(24691357); // 696969 is small enough to be an int.
a.add(b); // a += b;
a.subtract(c); // a -= b;
a.print();
return 0;
}
The logical problem with comparison is that you're returning two numbers as equal at the first digit that is equal.
You need instead to keep comparing next digit in that case and return 0 only if all of them are equal.
Related
I am trying to see if an array contains multiple values at any given index in the array, and if it does I want it to return true and if not return false. I want it to return true when it finds the numbers 1-9 and false if it does not.
bool isSolved(int a[], int size) {
int count = 0;
for (int i = 1; i < 10; i++) {
if (hasNum(a,size,i)) {
count++;
}
}
if (count == 9) {
return true;
}
else {
return false;
}
}
bool hasNum(int a[], int size, int num) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (a[j] == num) {
return true;
}
else {
return false;
}
}
}
}
This is what I have so far and it just gets stuck and never ends.
Man, that's C++. So use a standard vector and the count_if function from the standard library:
#include <algorithm>
#include <vector>
std::vector<int> a { /* fill vector */ };
std::count_if(std::begin(a), std::end(a), [](auto const& x){ return x == 1;});
Return the number of elements with value 1.
Also good, to question whether there's any value of 1:
std::any_of(std::begin(a), std::end(a), [](auto const& x){ return x == 1;});
I know this is strictly not an answer ...
There's no need to have 2 for loops in your hasNum function. Also, you are returning false if any of the values in the array is not equal to the number passed. You need to return false after the for loop ends.
Rewrite your hasNum function as shown below:
bool hasNum(int a[], int size, int num) {
for (int i = 0; i < size; i++) {
if (a[i] == num) {
return true;
}
}
return false;
}
I am trying to see if an array contains multiple values at any given index in the array
An array always contains exactly one value in every index.
Basically, your code is far from any C++ guidelines.
First, you don't pass array in C++ as int a[]. Use std::vector<int> instead.
Second your algorithm is badly inefficient. Consider using histogram approach.
bool isSolved(const std::vector<int>& a)
{
std::array<bool,10> hist;
for(int i=0; i<10; i++)
{
hist[i]=false;
}
for(auto x : a)
{
if(x>=0 && x<10)
{
hist[x] = true;
}
}
for(int i=0; i<10; i++)
{
if(!hist[i]) return false;
}
return true;
}
Here is a hint to the solution to your problem:
class BinaryTreeSet : public BinarySearchTree {
...
operator==
...
};
You are best off using std algorithms like find_if:
#include <algorithm>
int a[42]; // or vector<int> a(42) or any other container of any size
std::find_if(std::begin(a),std::end(a),[](auto const& v){return (v>=1)&&(v<=9);});
Here I've got a bigint calculator that uses a safearray class(not shown) to store large numbers and do arithmetic operations on them. I've got add and subtract working but when I try multiplication it compiles and runs but nothing happens. Could someone help me fix this? Thanks
int size = 100;
class bigint
{
SafeArray<int> *arr;
public:
char sign;
bigint() //initializes to zero
{
arr = new SafeArray<int>;
for(int i =0;i < size; i++)
arr->set(i,0);
}
void print() //prints numbers without zeroes in front
{
bool start_num=false;
for(int i = 0;i <arr->get_size() ;i++)
{
if(arr->get(i)!=0 && start_num==false )
{start_num=true;
cout << arr->get(i);}
else if(start_num==true)
cout<<arr->get(i);
}
cout<<endl;
}
void assign(const bigint &A) //
{
for(int i=0;i<arr->get_size();i++)
{ //Ways to initialize stuff
arr->set(i,A.arr->get(i));
}
}
void assign(int num) //
{
for(int i = arr->get_size()- 1; i >= 0; i--)
{
arr->set(i,num%10);
num /=10;
}
}
void assign(string num) //
{
long len = num.length();
int j=arr->get_size()-1;
for(long i=len-1;i>=0;i--)
{
arr->set(j,num[i]-48);
j--;
}
}
void add_pos(const bigint &A) //add big ints
{
int carry=0;
for(int i=size-1;i>=0;i--)
{
int result = arr->get(i)+A.arr->get(i)+carry;
arr->set(i,result%10);
carry=result/10;
}
}
void multiply(bigint &A)
{
bigint temp;
for(int i=0;i<size;i +=1)
{
temp.arr->set(i,arr->get(i));
arr->set(i,0);
}
int i1, i2;
for(i2=0; i2<size; i2++)
{
int borrow =0;
for(i1=0;i1+i2<size;i1++)
{
int total=temp.arr->get(i1)*A.arr->get(i2);
int totalsum=total+arr->get(i1+i2)+borrow;
arr->set(i1+i2,totalsum%10);
borrow = totalsum/10;
}
}
};
int main()
{
bigint a, b, c;
a.assign("2543281");
b.assign("3434");
a.mulitply(b);
a.print();
return 0;
}
The result of multiplication of two bigint numbers of size = 100 certainly needs more than 100 digits, perhaps 200?
For minimal modification, you may pass size as constructor argument to bigint. Ideally, bigint may use a vector so that the digit array can grow dynamically in a convenient and flexible way.
class bigint {
std::vector<int8_t> digits_; // each digit must be in 0-9 range
...
These were lil complicated so I came up with this:
InfInt InfInt::operator*(const InfInt& a) const{
InfInt final = 0;
std::string result;
InfInt* temp;
int carry;
int current;
//fast mult algorithm. the same we were taught in elementary.
for(long i=length() - 1;i >= 0; i--){
carry = 0;
result = "";
for (long j=a.length() - 1; j >= 0; j--){
current = (value[i] - '0') * (a.value[j] - '0') + carry;
result = (char)(current % 10 + '0') + result;
carry = current / 10;
}
if (carry > 0)
result = (char)(carry + '0') + result;
temp = new InfInt(result);
final += *new InfInt(temp->alignLeft(length() - i - 1));
}
final.setSign(sign ^ a.sign);
return final;
}
Hope it helps
Here I have a bigint class that uses an array called SafeArray that I created in a different class(we couldnt use vectors) the set and get function calls are from the SafeArray class, get takes an int parameter for array position and set takes 2 int parameters(one for position and one for value) all methods in this class work fine except for my subtract and compare methods. What I really need to work is my subtraction method. Any help? Thanks
int size = 20; //just for testing, will increase later
class bigint
{
SafeArray<int> *arr;
public:
bigint()
{
arr = new SafeArray<int>;
for(int i =0;i < size; i++)
arr->set(i,0);
}
void print()
{
for(int i = 0;i <arr->get_size() ;i++)
{
cout << arr->get(i);
}
cout<<endl;
}
void assign(const bigint &A)
{
for(int i=0;i<arr->get_size();i++)
{
arr->set(i,A.arr->get(i));
}
}
void assign(int num)
{
for(int i = arr->get_size()- 1; i >= 0; i--)
{
arr->set(i,num%10);
num /=10;
}
}
void assign(string num)
{
long len = num.length();
int j=arr->get_size()-1;
for(long i=len-1;i>=0;i--)
{
arr->set(j,num[i]-48);
j--;
}
}
void add(const bigint &A)
{
int carry=0;
for(int i=size-1;i>=0;i--)
{
int result = arr->get(i)+A.arr->get(i)+carry;
arr->set(i,result%10);
carry=result/10;}
}
void subtract(const bigint &A) {
for(int i=0, borrow=0; i<size; ++i)
{ int result=((arr->get(i) - A.arr->get(i) + borrow));
if(borrow == result < 0) {
A.arr->set(i,result+=10);
} } }
void compare(const bigint & A)
{
//int comp;
//for(int i =0;i<size;i++)
if(arr->get(size-1)>A.arr->get(size-1))
cout<<0;
else
cout<<1;
}
};
int main()
{
bigint A,B,C,D;
A.assign(12345);
A.print();
cout<<endl;
C.assign("123456789000");
C.print();
cout<<endl;
B.add(C);
B.print();
//B.compare(A);
//B.subtract(A);
//B.print();
return 0;
}
I think it should look like this
void subtract(const bigint &A) {
int borrow = 0;
for(int i=size-1; i >= 0; --i)
{
int result=((arr->get(i) - A.arr->get(i) - borrow));
if(result < 0) {
arr->set(i, result + 10);
borrow = 1;
} else {
arr->set(i, result);
borrow = 0;
}
}
}
// equals A
bool equals(const bigint &A) {
for(int i=0; i < size; ++i) {
if(A.arr->get(i) != arr->get(i)) {
return false;
}
}
return true;
}
// less than A
bool lt(const bigint &A) {
for(int i=0; i < size; ++i) {
if(arr->get(i) != A.arr->get(i)) {
return arr->get(i) < A.arr->get(i);
}
}
}
#include<iostream>
using namespace std;
class darray
{
private:
int n; // size of the array
int *a; // pointer to the 1st element
public:
darray(int size)
{
n = size;
a = new int[n];
}
~darray(){ delete[] a; }
void get_input();
int get_element(int index);
void set_element(int index, int value);
int count(){ return n; }
void print();
};
void darray::get_input()
{
for (int i = 0; i < n; i++)
{
cin >> *(a + i);
}
}
int darray::get_element(int index)
{
if (index == -1)
index = n - 1;
return a[index];
}
void darray::set_element(int index,int value)
{
a[index] = value;
}
void darray::print()
{
for (int i = 0; i < n; i++)
{
cout << a[i];
if (i < (n - 1))
cout << " ";
}
cout << endl;
}
// perform insertion sort on the array a
void insertion_sort(darray d)
{
int v = d.get_element(-1); // v is the right-most element
int e = d.count() - 1; // pos of the empty cell
// shift values greater than v to the empty cell
for (int i = (d.count() - 2); i >= 0; i--)
{
if (d.get_element(i) > v)
{
d.set_element(e,d.get_element(i));
d.print();
e = i;
}
else
{
d.set_element(e, v);
d.print();
break;
}
}
}
int main()
{
int s;
cin >> s;
darray d(s);
d.get_input();
insertion_sort(d);
system("pause");
return 0;
}
I use the darray class to make a array of size n at runtime. This class gives basic functions to handle this array.
This programs says debugging assertion failed at the end.
It gives this error after ruining the program.Other than that the program works fine. What is the reason for this error ?
You need to declare and define a copy constructor:
darray::darray(const darray& src)
{
n = src.n;
a = new int[n];
for (int i = 0; i < n; i++)
{
*(a + i) = *(src.a + i);
}
}
I am trying to make a random number generator for a uni project.
I am trying to implement a function to get the period of this list of numbers generated (how long before the numbers start to repeat).
When I compile this and run it it returns 507 (which is the correct period).
Yet about 50% of the time it returns a Segmentation Fault.
Any idea what is happening:
#include<iostream>
#include<fstream>
#include<vector>
#include<math.h>
#include<ctime>
using namespace std;
class Random
{
public:
// Constructor
Random(int a, int b, int m)
{
this->setMagicNumbers(a,b,m);
}
// Function to set the magic numbers of the random number generator
void setMagicNumbers(int a, int b, int m)
{
this->A = a;
this->B = b;
this->M = m;
}
// Function that returns a list of uniformly distributed random numbers
vector<double> GetUniform(int N, double initialValue = time(0))
{
double currentNumber = initialValue;
vector<double> RandomNumbers;
for(int i = 0; i <= N; i++)
{
currentNumber = fmod((this->A*currentNumber) + this->B, this->M);
RandomNumbers.push_back(currentNumber / this->M); // The number is normalised here
}
return RandomNumbers;
}
private:
int A, B, M;
};
class NumberList
{
public:
// Function to add an element to the list
void push(double x)
{
this->v.push_back(x);
}
// Function to pop an element off the list
double pop()
{
if(v.size() > 0)
{
int popped = v.back();
v.pop_back();
return popped;
}
else
{
cout << "Cannot pop element off empty list." << endl;
return 0;
}
return 0;
}
// Function to get the value at a given location on the list
double getAt(int i)
{
return this->v[i];
}
// Function to set the value at a given location on the list
void setAt(int i, double value)
{
this->v[i] = value;
}
// Function to find the size of the list
unsigned int size()
{
return this->v.size();
}
// Function to get the vector itself
vector<double> getvector()
{
return this->v;
}
// Function to set the value of the vector itself
void setVector(vector<double> vec)
{
this->v = vec;
}
// Function to print the list of numbers as coordinates to a data file
void printCoordinates()
{
ofstream data("coordinates.dat");
for(unsigned int i = 0; i <= this->v.size(); i++)
{
data << this->v[i] << " " << this->v[i + 1] << "\n";
}
data.close();
}
// Function to print the list of numbers to the terminal
void print()
{
for(unsigned int i = 0; i <= this->v.size(); i++)
{
cout << this->v[i] << endl;
}
}
// Function to get the period of the list of numbers
int getPeriod()
{
int i = 2;
while(true)
{
if(isPeriod(i) == true)
{
return i;
}
else
{
i = i + 1;
}
}
}
private:
// Vector to hold the values for the list
vector<double> v;
// Function to test if 'i' is the period of the list
bool isPeriod(int i)
{
for(int j = 0; j != (i-1); j++)
{
if(this->getAt(j) != this->getAt(i + j))
{
return false;
}
}
return true;
}
};
int main()
{
Random RandNumGenerator(100,104001,714025); // Create a new random number generator with given magic numbers
NumberList numbers; // Create a blank list of numbers
numbers.setVector(RandNumGenerator.GetUniform(10000)); // Add 10000 random numbers to the list
numbers.printCoordinates(); // Print out the random numbers as coordinates to a data file
cout << numbers.getPeriod() << endl; // Print out the period of the random number list
return 0;
}
Thanks in advance.
This line could cause a problem
for(unsigned int i = 0; i <= this->v.size(); i++)
With it you are using size as the last index, which is overflow, try changing it for this:
EDIT: change for this actually
for(unsigned int i = 0; i < this->v.size()-1; i++)
On the loop you are accessing the i+1 th element of the vector.
EDIT:
This probably wouldn't cause a crash but instead of creating N elements you are creating N+1
for(int i = 0; i <= N; i++)
Change if for this:
for(int i = 0; i < N; i++)
EDIT:
With fixes
#include<iostream>
#include<fstream>
#include<vector>
#include<math.h>
#include<ctime>
using namespace std;
class Random
{
public:
// Constructor
Random(long a, long b, long m)
{
this->setMagicNumbers(a,b,m);
}
// Function to set the magic numbers of the random number generator
void setMagicNumbers(long a, long b, long m)
{
this->A = a;
this->B = b;
this->M = m;
}
// Function that returns a list of uniformly distributed random numbers
vector<double> GetUniform(int N, double initialValue = time(0))
{
double currentNumber = initialValue;
vector<double> RandomNumbers;
for(int i = 0; i < N; i++)
{
currentNumber = fmod((this->A*currentNumber) + this->B, this->M);
RandomNumbers.push_back(currentNumber / this->M); // The number is normalised here
}
return RandomNumbers;
}
private:
long A, B, M;
};
class NumberList
{
public:
// Function to add an element to the list
void push(double x)
{
this->v.push_back(x);
}
// Function to pop an element off the list
double pop()
{
if(v.size() > 0)
{
int popped = v.back();
v.pop_back();
return popped;
}
else
{
cout << "Cannot pop element off empty list." << endl;
return 0;
}
return 0;
}
// Function to get the value at a given location on the list
double getAt(int i)
{
return this->v[i];
}
// Function to set the value at a given location on the list
void setAt(int i, double value)
{
this->v[i] = value;
}
// Function to find the size of the list
unsigned int size()
{
return this->v.size();
}
// Function to get the vector itself
vector<double> getvector()
{
return this->v;
}
// Function to set the value of the vector itself
void setVector(vector<double> vec)
{
this->v = vec;
}
// Function to print the list of numbers as coordinates to a data file
void printCoordinates()
{
ofstream data("coordinates.dat");
for(unsigned int i = 0; i < this->v.size()-1; i++)
{
data << this->v[i] << " " << this->v[i + 1] << "\n";
}
data.close();
}
// Function to print the list of numbers to the terminal
void print()
{
for(unsigned int i = 0; i < this->v.size(); i++)
{
cout << this->v[i] << endl;
}
}
// Function to get the period of the list of numbers
int getPeriod()
{
int i = 2;
while(true)
{
if(isPeriod(i) == true)
{
return i;
}
else
{
i = i + 1;
}
}
}
private:
// Vector to hold the values for the list
vector<double> v;
// Function to test if 'i' is the period of the list
bool isPeriod(int i)
{
std::cout << "trying period " << i << endl;
if (i >= v.size()) return true;
for(int j = 0; j < v.size()-1; j++)
{
if(this->getAt(j) == this->getAt(i + j))
{
std::cout << "value at j " << this->getAt(j) << "value at i+j " << this->getAt(i+j) <<endl;
return true;
}
}
return false;
}
};
int main()
{
Random RandNumGenerator(100,104001,714025); // Create a new random number generator with given magic numbers
NumberList numbers; // Create a blank list of numbers
numbers.setVector(RandNumGenerator.GetUniform(10000)); // Add 10000 random numbers to the list
numbers.printCoordinates(); // Print out the random numbers as coordinates to a data file
cout << numbers.getPeriod() << endl; // Print out the period of the random number list
return 0;
}
Played around with this a bit and I initially thought that your vector of doubles was somehow getting corrupted. I found that if you size it to the exact value that you want - which is a known value - the seg faults stop:
vector<double> RandomNumbers(N); // In GetUniform()
Running it through gdb was inconclusive as it looked as if the stack was getting corrupted. I also found that when I changed the push_back() just to take 0:
RandomNumbers.push_back(0.0);
instead of the normalised number, even without the initial size for the vector (above) it worked fine, suggesting an issue with one of the variables being used.
I also found that if I set currentNumber to 1, say, instead of calling fmod() - even with everything as it originally was, I got no seg faults:
currentNumber = 1;
This kind of suggests that it might be something to do with the use of fmod()
Interestingly, I then took out the call to push_back completely and found that it still had segfaults. Sorry I can't be much more help than that, but it certainly looks as if some kind of corruption is happening somewhere in this area.
In C++ index of array and vector will be from 0..size - 1, and you misused it all over your code(inside GetUniform, inside printCoordinates, ...) and beside that in printCoordinates you written this->v[i + 1] at last it will be converted to this->v[this->v.size() + 1] that is 2 index after last valid index.
But I think source of your error is in getPeriod and isPeriod, look at it you start from 0 to i - 1 and check if that index match item at index i + j, so if i become size() - 1 and size be 500, then in worse case you are accessing v[499 + 498] that is really large than last valid index and you certainly get a segmentation fault. Now to solve it check your index and never go beyond end of the vector size, that mean you should never use index > v.size() - 1