I know that it is usually not the best idea, but i decided to give it a try. I'm trying to overload + operator to make element wise addition of two vectors. For some reason it correctly returns only the first value in the vector.
I cant figure out what is going wrong ? It also wont work if i'll use push_back instead resizing when calculating elements of sum array. Here is the code :
#include "stdafx.h"
#include <iostream>
#include "conio.h"
#include <vector>
using namespace std;
class dvector{
public:
vector<double> q;
dvector(vector<double>);
dvector();
};
dvector::dvector(vector<double> a) {
q=a;
};
dvector::dvector() {
vector<double> q;
};
dvector operator+(dvector c1, dvector c2)
{
int sz=c1.q.size();
dvector sum;
sum.q.resize(sz);
double temp;
for (int i=0; i<sz; i++){
sum.q[i]=c1.q[i]+c2.q[i];
return sum;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
vector<double> test1;
test1.resize(10);
for (int i=0; i<10;i++){
test1[i]=i;
}
vector<double> test2;
test2.resize(10);
for (int i=0; i<10;i++){
test2[i]=2;
}
dvector dtest1(test1);
dvector dtest2(test2);
dvector sum=dtest1+dtest2;
for (int i =0;i<10;i++)
{
cout<<sum.q[i]<<' ';
}
getch();
return 0;
}
You return in your for-loop right after the first iteration. You should move return sum; to the last line of operator+.
Because you return out too quickly. return statement will not only break out of for loop but also return out of function.
dvector operator+(dvector c1, dvector c2)
{
int sz=c1.q.size();
dvector sum;
sum.q.resize(sz);
double temp;
for (int i=0; i<sz; i++){
sum.q[i]=c1.q[i]+c2.q[i];
//return sum; // don't return until you finish calculating
}
return sum; // move to here
}
Related
I have written this merge sort program in c++ but I am getting "Segmentation fault (core dumped)" error after running the code. Even though there is no compilation error. Can you please tell me what's the mistake I am doing? While taking input in the array it is showing that error. If I change it to push_back, the input is fine but later in merge function, it is showing the same error.
//merging 2 sorted subarrays.
#include <iostream>
#include <vector>
using namespace std;
void merge(vector <int> &a,vector <int> &b,vector <int> &c)
{
int i=0,j=0,k=0,bL=b.size(),cL=c.size();
while(i<bL && j<cL)
{
if(b[i]<c[j])
{
a[k]=b[i];
i++;k++;
}
else
{
a[k]=c[j];
j++;k++;
}
}
while(i<bL)
{
a[k]=b[i];
i++;k++;
}
while(j<cL)
{
a[k]=c[j];
j++;k++;
}
cout<<"array a inside merge is: "<<endl;
for(int p=0;p<a.size();p++)
{
cout<<a[p]<<endl;
}
}
void mergeSort(vector <int> &a)
{
vector <int> l, r;
int mid;
if(a.size()<2) return;
mid = a.size()/2;
for(int i=0;i<mid;i++)
{
l[i]=a[i];
}
for(int i=mid;i<a.size();i++)
{
r[i-mid]=a[i];
}
mergeSort(l);
mergeSort(r);
merge(a, l, r);
}
int main()
{
int n;
vector <int> a;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
mergeSort(a);
for(int p=0;p<n;p++)
{
cout<<a[p]<<endl;
}
return 0;
}
Whever you access an element in a vector using [] you might get a seg fault.
This code,
vector <int> a;
gives you an empty vector.
Asking for a[0] won't work if there's nothing in a yet.
Trying to set a[0] to a value won't work either.
It doesn't exist.
Yet.
You have a similar problem in mergeSort when you use
vector <int> l, r;
These are also empty vectors.
You can use push_back (or even emplace_back) to add new elements.
Or use a constructor overload to state how many elements you want.
For example,
vector <int> a(10);
gives you a vector of ten ints, so a[0] is fine to read from or write to.
a[11] isn't.
Practise using a vector first then try your merge sort.
The reason for getting a segmentation fault is you access the location of the memory which does not exist (more accurate to say as not allocated). Say you have a vector of length 3 and you try to access 4th position, you get a segmentation fault.
As opposed to #doctorlove's answer, I would say it is possible to use []. However, you need the following implementation (only shown for main(), please implement with the same logic in the other functions). See the documentation of std::vector for more info.
int main()
{
size_t n;
std::cin >> n;
std::vector <int> a(n);
for(int i=0;i<n;++i)
{
std::cin >> a[i];
}
// mergeSort(a);
for(int i=0;i<n;++i)
{
std::cout << a[i] << "\n";
}
return 0;
}
Hope this helps. Cheers.
here is the final code after changes:
//merging 2 sorted subarrays.
#include <iostream>
#include <vector>
using namespace std;
void merge(vector <int> &a,vector <int> &b,vector <int> &c)
{
int i=0,j=0,k=0,bL=b.size(),cL=c.size();
while(i<bL && j<cL)
{
if(b[i]<c[j])
{
a[k]=b[i];
i++;k++;
}
else
{
a[k]=c[j];
j++;k++;
}
}
while(i<bL)
{
a[k]=b[i];
i++;k++;
}
while(j<cL)
{
a[k]=c[j];
j++;k++;
}
cout<<"array a inside merge is: "<<endl;
for(int p=0;p<a.size();p++)
{
cout<<a[p]<<endl;
}
}
void mergeSort(vector <int> &a)
{
vector <int> l, r;
int mid;
if(a.size()<2) return;
mid = a.size()/2;
for(int i=0;i<mid;i++)
{
l.push_back(a[i]);
}
//change2
for(int i=0;i<a.size()-mid;i++)
{
r.push_back(a[mid+i]);
}
mergeSort(l);
mergeSort(r);
merge(a, l, r);
}
int main()
{
int n;
cin>>n;
//change1
vector <int> a(n);
for(int i=0;i<n;i++)
{
cin>>a[i];
}
mergeSort(a);
cout<<"Final array is:"<<endl;
for(int p=0;p<n;p++)
{
cout<<a[p]<<endl;
}
return 0;
}
I have a C++ program, in which I have to create a recursive function that writes all the digits of a given positive integer in an array - in this case a vector.
However, when I compile the program and enter a number, it stops working. I want to ask why this happens?
#include <iostream>
#include <vector>
using namespace std;
vector <int> arr;
int temp;
int fill_Array(int num)
{
if(num>=1 && num<=9)
{
arr.push_back(num);
}
temp = fill_Array(num)%10;
arr.push_back(temp);
num/=10;
}
int main()
{
int n;
cin>>n;
fill_Array(n);
for(int i=0; i<arr.size(); i++)
{
cout<<arr[i]<<endl;
}
return 0;
}
In the given code, recursion function does not returning any value, so return type for that function have no use.
calling the function for recursion is in the wrong place. Correct code given below:
#include <iostream>
#include <vector>
using namespace std;
vector <int> arr;
int temp;
void fill_Array(int num)
{
if(num>=1 && num<=9)
{
arr.push_back(num);
}
else{
temp = num%10;
arr.push_back(temp);
fill_Array(num/=10);
}
}
int main()
{
int n;
cin>>n;
fill_Array(n);
for(int i=0; i<arr.size(); i++)
{
cout<<arr[i]<<endl;
}
return 0;
}
A couple of reasons I can see:
There is no conditional to stop the recusion so it will keep going until it runs out of stack or memory. I presume you want to stop when num is zero
fill_Array has no return value so will assign temp with some random value which will be pushed into the array
Also why use recursion for this when iterative would be easier and more obvious what it is doing
I am trying to implement a code from a standard quotient and remainder algorithm namely:
function divide(x,y)
if x=0: return (q,r)=(0,0)
(q,r)=divide(floor(x/2),y)
q=2*q, r=2*r
if x is odd: r=r+1
if r>=y: r=r-y, q=q+1
return (q,r)
with a c++ code here is the relavent part of my function
bool compareVector(const vector<int> &A, const vector<int> &B){
if(A.size()<B.size())
return(1==0);
if(A.size()>B.size())
return(1==1);
for(int i=A.size()-1;i>=0;i--){
if (A[i]>B[i])
return(1==1);
if(A[i]<B[i])
return(1==0);
}
return(1==1);
}
struct QandR{
vector<int> r;
vector<int> q;
};
QandR Division(vector<int> BinaryA, const vector<int> & BinaryB, QandR &x){
vector<int> one, zero;
one.clear();
one.push_back(1);
zero.clear();
zero.push_back(0);
if(BinaryA==zero){
return x;
}
else if(BinaryA==one){
BinaryA[0]=0;
}
else if(BinaryA.size()>1)
pop_front(BinaryA);
x=Division(BinaryA,BinaryB,x);
x.q=addBinary(x.q,x.q);
x.r=addBinary(x.r,x.r);
if(BinaryA[0]==1)
x.r=addBinary(x.r,one);
if(compareVector(x.r,BinaryB))
{
x.r=Subtract(x.r,BinaryB);
x.q=addBinary(x.q,one);
}
return x;
}
However this simply does not work for example with BinaryA={1,0,1,1} and BinaryB={0,1}. This is 13/2 so q should be {0,1,1}, and r should be {1}. But my code out puts r={1} and q={0,1}. I don't understand what is going on. All functions that are used above that are not defined work. Also, is there a easier way to return two values in c++ if there is I would be grateful to know. Thank you.
Add is the whole code
#include <iostream>
#include <string> //this is only used for the user to insert a number
#include <stdlib.h> //this is used to convert the user iputed string to a vector
#include <vector>
#include <algorithm>
using namespace std;
void CleanArray(vector<int> & Array){ //CleanArray() is mainly for the Multiply function where we need to keep removing the enteries
for(int i=0;i<Array.size();i++){
Array[i]=0; //of a vector<int> Array. And then I clean some of the other struct vector<int>'s for saft
}
}
vector<int> addBinary(vector<int> A,vector<int> B){ //addBinary() adds two struct vector<int>'s and returns a new struct vector<int>
vector<int> C; // C is our carry array but we take advantage of the fact that after we carry to a new column we nolonger need the old one so we
// can also use C to store our answere.
C.assign(A.size()+1,0);
CleanArray(C);
for(int i=0; i<B.size();i++){ //Case 1 we are adding the first part where we are adding columns and we still have vector<int> A and vector<int> B
if(C[i]+B[i]+A[i]==3){
C[i]=1;
C[i+1]=1;
}
else if(B[i]+A[i]+C[i]==2){
C[i]=0;
C[i+1]=1;
}
else if(B[i]+ A[i]+C[i] ==1){
C[i]=1;
}
}
for(int i=B.size();i<A.size();i++){ //Case 2 we adding where vector<int> B has been exasted and we only have vector<int> A and vector<int> C.
if(C[i]+A[i]==2){
C[i]=0;
C[i+1]=1;
}
else if(A[i]==1){
C[i]=1;
}
} // this is fine but not necessary.
if(C[C.size()-1]==0) // We want to change C's member length_a if the aswere is one bigger then the size of A.size().
C.pop_back();
return C;
}
vector<int> Subtract(vector<int> A, vector<int> B){ // this function is almost exactly the same as Multiply() using a vector<int> C to hold the value of A-B
vector<int> C;
C.assign(A.size(),0);
CleanArray(C);
// reverse(B.begin(), B.end());
for(int i=A.size()-B.size();i>0;i--)
B.push_back(0);
// reverse(B.begin(), B.end());
for(int i=A.size()-1;i>=0;i--){
if((A[i]+B[i])==2)
C[i]=0;
else if(A[i]==1 && B[i]==0)
C[i]=1;
else if(B[i]==1 && A[i]==0){
C[i]=1;
int j=0;
int k=i+1;
while(j==0){ //we need this while loop bc when we have 0-1 this changes all values of
if(C[k]==1){ //C[i+1] to the next C[]==1, changing all of those to 1 so ex 1000-1=0111
C[k]=0;
j++;
}
else if(C[k]==0){
C[k]=1;
k++;
}
// if(i==C.size()-1 && C.size()>1) // this removes the zero's in front of the numberso the answer is not like 001 but 1.
// C.pop_back();
}
}
// else this was the problem with subtraciton
// C[i]=0; "" " " " " "
}
int i=C.size()-1;
while(C[i]==0 && i!=0){
C.pop_back();
i--;
}
return C;
}
vector<int> Multiply(const vector<int> & A, const vector<int> &B){ // This also uses the concept of having a vector<int> C to store the values of the succesive additions of the rows
vector<int> C;
C.assign(A.size(),0);
for(int j=0;j<B.size();j++){
vector<int> D;
D.assign(A.size(),0);
for(int i=0;i<A.size();i++){
if(B[j]==1)
D[i]=A[i];
// this makes a new row if B[j]==1 so if 1110101*1=1110101(0...0) there are j zero's in
}
D.insert(D.begin(),j,0);
C=addBinary(D,C); //this adds the pervious value of C with the next row.
}
return C;
}
void pop_front(vector<int> & A){
reverse(A.begin(),A.end());
A.pop_back();
reverse(A.begin(),A.end());
}
bool compareVector(const vector<int> &A, const vector<int> &B){
if(A.size()<B.size())
return(1==0);
if(A.size()>B.size())
return(1==1);
for(int i=A.size()-1;i>=0;i--){
if (A[i]>B[i])
return(1==1);
if(A[i]<B[i])
return(1==0);
}
return(1==1);
}
struct QandR{
vector<int> r;
vector<int> q;
};
QandR Division(vector<int> BinaryA, const vector<int> & BinaryB, QandR &x){
vector<int> one, zero;
one.clear();
one.push_back(1);
zero.clear();
zero.push_back(0);
if(BinaryA==zero){
return x;
}
else if(BinaryA==one){
BinaryA[0]=0;
}
else if(BinaryA.size()>1)
pop_front(BinaryA);
x=Division(BinaryA,BinaryB,x);
x.q=addBinary(x.q,x.q);
x.r=addBinary(x.r,x.r);
if(BinaryA[0]==1)
x.r=addBinary(x.r,one);
if(compareVector(x.r,BinaryB))
{
x.r=Subtract(x.r,BinaryB);
x.q=addBinary(x.q,one);
}
return x;
}
/*
vector<int> modexp(vector<int> x,vector<int> y, vector<int> N){
vector<int> one;
vector<int> r;
vector<int> q;
one.push_back(0);
if(y.size()==1 && y[1]==0)
return one;
y.pop_back();
vector<int> z=modexp(x,y,N);
if(y[0]==1){
vector<int> D=Multiply(z,z);
Division(D,N,q,r);
z=q;
return z;
}
else{
vector<int> C =Multiply(Multiply(z,z),x);
Division(C,N,q,r);
z=q;
return z;
}
}
*/
int main(){
int arraya[4]={1,1,0,1};
int arrayb[2]={1,1};
vector<int> a(arraya,arraya+4);
vector<int> b(arrayb,arrayb+2);
for(int i=0;i<a.size();i++){
cout<<a[i]<<" ";
}
cout<<endl;
pop_front(a);
for(int i=0;i<a.size();i++)
cout<<a[i]<<" ";
cout<<endl;
QandR x;
x.r.clear();
x.q.clear();
x.r.push_back(0);
x.q.push_back(0);
x=Division(a,b,x);
for(int i=0;i<x.r.size();i++){
cout<<x.r[i]<<" ";
}
cout<<endl;
for(int i=0;i<x.q.size();i++)
cout<<x.q[i]<<" ";
cout<<endl;
return 0;
}
When you divide BinaryA by 2, and try to call Division again, you have to return BinaryA to its original state so that you can verify if it was odd or not.
So, in these two cases:
else if(BinaryA==one){
BinaryA[0]=0;
}
else if(BinaryA.size()>1)
pop_front(BinaryA);'
you have to keep the bit that is lost and restore it after the recursive Division has returned.
I'm trying to use clear functions to do a matrix multiplication with random generated values. Therefore I'm hoping to use a function(mat_def) to generate the matrices and another function(mat_mul) to multiply them when the matrices are sent as parameters.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
double mat_def(int n) //how to return the matrix
{
double a[n][n];
double f;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
f= rand();
cout<<f ;
a[i][j]=f;
}
}
return 0;
}
double mat_mul( int n, double a[n][n], double b[n][n]) //how to send matrix as parameter
{
return 0;
}
int main()
{
/* initialize random seed: */
srand (time(NULL));
mat_def(10);
}
Here's a nice, standard C++ Matrix template for you.
Matrix.h
#include <vector>
class Matrix
{
class InnerM
{
private:
int ydim;
double* values;
public:
InnerM(int y) : ydim(y)
{
values = new double[y];
}
double& operator[](int y)
{
return values[y];
}
};
private:
int xdim;
int ydim;
std::vector<InnerM> inner;
public:
Matrix(int x, int y) : xdim(x), ydim(y), inner(xdim, InnerM(ydim))
{
}
InnerM& operator[](int x)
{
return inner[x];
}
};
All the memory leaks are there for you but you get the idea. From here you can handle the multiplication by overiding ::operator*() in the Matrix class.
I assume your problem is to define 2-D array and then pass it to mat_mul function to multiply the matrices. And the rest will be quite simple.
Defining the 2-D array(considering memory needs are known at run time):
int rows,cols;
cin >> rows;
cin >> cols;
int **arr = new int*[rows]; // rows X cols 2D-array
for(int i = 0; i < rows; ++i) {
arr[i] = new int[cols];
}
You can define another 2-D array exactly the same way with required rows and column.
now, Passing the 2-D array to function:
void mat_mul(int **arr1, int **arr2, int m, int n, int p, int q){
//define a 2-D array to store the result
//do the multiplication operation
//you could store the result in one of the two arrays
//so that you don't have to return it
//or else the return type should be modified to return the 2-D array
}
example:
void display(int **arr, int row, int col){
for (int i=0; i<row; i++){
for(int j=0;j<col; j++){
cout << arr[i][j] << '\t';
}
cout << endl;
}
}
Delete the memory if not required anymore with the following syntax:
for(int i=0; i<rows; i++){
delete[] array[i];
}
delete[] array;
hope this will be sufficient to get your work done!
there is already an answer on how to return a 2-D array on SO. Check the link below.
https://stackoverflow.com/a/8618617/8038009
Returning the raw allocation is a sucker bet. You need to manage all of the memory allocated yourself and pass it around with the matrix size parameters.
Why suffer? Use a matrix class
template<class Type>
class Matrix{
int rows;
int cols;
std::vector<type> data;
public:
Matrix(int row, int col):rows(row), cols(col), data(rows*cols)
{
// does nothing. All of the heavy lifting was in the initializer
}
// std::vector eliminates the need for destructor, assignment operators, and copy
//and move constructors.
//add a convenience method for easy access to the vector
type & operator()(size_t row, size_t col)
{
return data[row*cols+col];
}
type operator()(size_t row, size_t col) const
{
return data[row*cols+col];
}
};
Usage would be
Matrix<double> mat_mul(const Matrix<double> &a, const Matrix<double> &b)
{
Matrix<double> result;
// do multiplication
return result;
}
int main()
{
/* initialize random seed: */
srand (time(NULL));
Matrix<double> matA(10, 10);
matA(0,0) = 3.14; // sample assignment
matA(9,9) = 2.78;
double x = matA(0,0) * matA(9,9)
Matrix<double> matB(10, 10);
Matrix<double> matC = mat_mul(matA, matB) ;
}
More functionality, such as construction from an initializer list, can be added to the class to make your life easier. You can also specify an operator * overload for Matrix and use that in place of mat_mul if you chose. Read Operator overloading for more on that option.
#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
double fractional_knapsack(vector<int>& val,vector<int>& wt,int weight)//vectors of values and their respective weights and max weight are passed as parameter
{
int sz=val.size();
vector<double> ratio(sz); //vector to store ratio of values and weights
for(int i=0;i<sz;i++){
ratio[i]=double(val[i]/wt[i]);
}
sort(ratio.begin(),ratio.end(),greater());
//reverse(ratio.begin(),ratio.end());
double max=0.0;
int j=0;
while(max<=weight&&j<sz){
double(weight[j]);
max=max+(ratio[j]*weight[j]);
}
return max;
}
int main()
{ int max_weight,n;
cin>>n>>max_weight;
vector<int>values;
vector<int>weights;
for(int i=0;i<n;i++){
cin>>values[i];
}
for(int i=0;i<n;i++){
cin>>weights[i];
}
double result=fractional_knapsack(values,weights,max_weight);
cout<<"done/n";
cout<<result;
return 0;
}
D:\COdeBlock Projects\Fractional Knapsack\main.cpp|12|error: missing template arguments before '(' token|
it is compiling in devcpp but program_name.exe is crashing
in the method fractional_knapsack(vector<int>& val,vector<int>& wt,int weight) why we pass vector as refrence.
A quick look at the error message reveal the problem lies with the line
sort(ratio.begin(),ratio.end(),greater());
My guess is that you want
sort(ratio.begin(),ratio.end(),greater<double>());
The sort method expects a comparator. If you look at the doc for greater, there's an example on how to use it.
I got the right code.
#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
double fractional_knapsack(vector<int>& val,vector<int>& wt,int weight)//vectors of values and their respective weights and max weight are passed as parameter
{
int sz=val.size();
vector<double> ratio(sz); //vector to store ratio of values and weights
for(int i=0;i<sz;i++){
ratio[i]=(val[i]/wt[i]);
}
for(int i=0;i<sz;i++){
for(int j=i+1;j<sz;j++){
if(ratio[i]<ratio[j]){
int temp;
temp=ratio[i];
ratio[i]=ratio[j];
ratio[j]=temp;
temp=val[i];
val[i]=val[j];
val[j]=temp;
temp=wt[i];
wt[i]=wt[j];
wt[j]=temp;
}
}
}
//sort(ratio.begin(),ratio.end(),greater<double>());
// sort(val.begin(),val.end(),greater<int>());
//sort(wt.begin(),wt.end(),greater<int>());
//reverse(ratio.begin(),ratio.end());
double max=0.0;
int j=0;
int quantity_left =weight;
while(wt[j]<=quantity_left&&j<sz){
//double(wt[j]);
max=max+(ratio[j]*wt[j]);
cout<<max<<" ";
quantity_left=quantity_left-wt[j];
j++;
}
if(wt[j]>quantity_left&&j<sz){
max=max+(ratio[j]*quantity_left);
// cout<<max<<" ";
}
return max;
}
int main()
{ int max_weight,n;
cin>>n>>max_weight;
vector<int>values(n);
vector<int>weights(n);
for(int i=0;i<n;i++){
cin>>values[i];
}
for(int i=0;i<n;i++){
cin>>weights[i];
}
double result=fractional_knapsack(values,weights,max_weight);
cout<<result;
return 0;
}