I am writing a program to find if a given sum is possible from a subset of an array. (Subset sum problem). Although many implementations are available on the internet, I have chosen to code it myself. please suggest appropriate corrections. I am doing this using dynamic programming.
#include<iostream>
using namespace std;
bool subsetsum(int a[],int n,int s){
bool T[n][s];
for(int j=0;j<n;j++){
T[j][0]=true;
}
for(int k=1;k<n;k++){
T[0][k]=false;
for(int l=1;l<n;l++){
if((a[k]<s) && ((subsetsum(a,n-1,s-a[k])) || (subsetsum(a,n-1,s)))){
T[k][l]=true;
}
else{
T[k][l]=false;
}
}
}
return T[n][s];
}
int main(){
int n,s;
int a[n];
cout<<"Enter number of elements:\n";
cin>>n;
cout<<"Enter sum required\n";
cin>>s;
cout<<"Enter elements:\n";
for(int i=0;i<n;i++){
cin>>a[i];
}
cout<<subsetsum(a,n,s);
}
The output I obtained is below:
Enter number of elements:
3
Enter sum required
5
Enter elements:
1
4
2
0
I do not understand why I obtained 0 as the result when boolean should have been returned.
array is declared as follows:
bool T[n][s];
and return is done as:
return T[n][s];
Probably, you need to do as follows?
return T[n-1][s-1];
Array indexing in C++ starts from 0.
Related
this is the code which I tried
int main(){
int n;
cout<<"Type the number of elements\n";
cin>>n;
cout<<"Type the integars";
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
cout<<"This is the reversed array ";
for(int i=n;i>=0;i--){
cout<<arr[i]<<" ";
}
return 0;
}
this is the output pls explain why is 0 coming where did I go wrong?
The problem is that your reversed array starts at n, and arr[n] is invalid, because the array starts at 0.
for (int i = n - 1; i >= 0; i--) {
cout << arr[i] << " ";
}
Your array is of size 5, but you're accessing 6 elements instead 5. That is, you are accessing elements 5...0 (6 in total) when the array only has 4...0 (5 in total) elements.
Start the 2nd for loop with n-1 and that should solve your issue. That is, (for(int i=n-1;i>=0;i--)) instead of (for(int i=n;i>=0;i--))
Solution: To print use i = n - 1
int main(){
int n;
cout<<"Type the number of elements\n";
cin>>n;
cout<<"Type the integars";
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
cout<<"This is the reversed array ";
for(int i=n-1;i>=0;i--){
cout<<arr[i]<<" ";
}
return 0;
}
Arrays begin counting at zero through to n (n being the target size value), so you need to ignore the zero value and start printing after zero has been iterated through.
Something like:
counter = target -1
When printing the reverse of array make sure you remember the position of elements is always one less.
So you have to start with n-1. '0' is getting printed because it is the garbage value at that position it could have been any number or value.
int main(){
int n;
cout<<"Type the number of elements\n";
cin>>n;
cout<<"Type the integars";
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
cout<<"This is the reversed array ";
// When printing the reverse of array make sure you remember the position of elements is always one less.
// So you have to start with n-1. '0' is getting printed because it is the garbage value at that position it could have been any number or value.
for(int i=n-1;i>=0;i--){
cout<<arr[i]<<" ";
}
return 0;
}
I wrote a program of Insertion sort in c++ but output screen does not showing any Statement.I am facing this problem in few another program.
This is the code:
#include<iostream>
using namespace std;
int main()
{
int n;
int arr[n];
cout<<"Enter the number of the element of the Array:";
cin>>n;
cout<<"Enter the element of the Array:";
for(int i=0;i<n;i++){
cin>>arr[i];
}
for(int i=0;i<n;i++){
int current=arr[i];
int j=i-1;
while(arr[j]>current && j>=0){
arr[j+1]=arr[j];
j--;
}
arr[j+1]=current;
}
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}cout<<endl;
return 0;
}
You are using the value of n before it has a value. Change your code to this
int n; // n does not have a value
cout<<"Enter the number of the element of the Array:";
cin>>n; // n gets value here
int arr[n]; // n is used here
You cannot use any variable before it has a value. That's a fundamental rule of C++.
You cannot creat variable C-style arrays during run time.
You should either create them with the new operator or switch to STL containers like std::vector.
I am testing the ability to use functions to input values in 2 arrays and also to perform addition of the values. I wish to input the different values in the 2 arrays using a function inputArray(A1, A2, size) using a For loop.
I also used a function sumArray(A1, A2, size) to perform the addition of the 2 values in the arrays.
But the issue is with the input function as when i am running the program to input different values in the 2 arrays, the first array is also been attributed the value of the second array.
void inputArray(int Array1[], int Array2[], int n)
{
for(int i=0; i<n; i++)
{
cout<<"Array 1:"<<endl;
cin>>Array1[i];
cout<<" Array 2: "<<endl;
cin>>Array2[i];
cout<<endl;
//test
cout<<"\n\t A1[0]= "<<Array1[0]<<endl;
cout<<"\n\t A2[0]="<<Array2[0]<<endl;
}
}
I tried to use 2 different functions to input the values and this worked.But then again when i used a FOR loop for the Addition function sum = sumArray(A1, A2, size), both arrays A1 and A2 were being attributed the value of the second array.
int sumArray(int Array1[], int Array2[], int n)
{
int sum= 0;
for(int i=0; i<n; i++)
{
sum+= ( Array1[i] + Array2[i] );
cout<<"The total sum so far for values till position "<<i<<" is "<<sum<<endl;
}
//to monitor if the program is performing well with the addition
//test
cout<<"\n\t A1[0]= "<<Array1[0]<<endl;
cout<<"\n\t A2[1]="<<Array2[2]<<endl;
return sum;
}
#include<iostream>
using namespace std;
void inputArray(int Array1[], int Array2[], int n)
{
for(int i=0; i<n; i++)
{
cout<<"Array 1:"<<endl;
cin>>Array1[i];
cout<<" Array 2: "<<endl;
cin>>Array2[i];
cout<<endl;
//test
cout<<"\n\t A1[0]= "<<Array1[0]<<endl;
cout<<"\n\t A2[0]="<<Array2[0]<<endl;
}
}
//To sum all the values in the 2 arrays//not asked in the question
int sumArray(int Array1[], int Array2[], int n)
{
int sum= 0;
for(int i=0; i<n; i++)
{
sum+= ( Array1[i] + Array2[i] );
cout<<"The total sum so far for values till position "<<i<<" is "<<sum<<endl;
}
//to monitor if the program is performing well with the addition
//test
cout<<"\n\t A1[0]= "<<Array1[0]<<endl;
cout<<"\n\t A2[1]="<<Array2[2]<<endl;
return sum;
}
int main()
{
int size;
int A1[size];
int A2[size];
size= 3;
cout<<"Input "<<size<<" values in the first and second array: "<<endl;
inputArray( A1, A2, size); //do not write square bracket when calling a function
int sum = sumArray(A1, A2, size);
cout<<"The sum of the total values is : "<<sum<<endl;
//test
cout<<"\n\t A1[0]= "<<A1[0]<<endl;
cout<<"\n\t A2[1]="<<A2[2]<<endl;
return 0;
}
Here you use a non-initialised variable for the size of the two arrays.
int size;
int A1[size];
int A2[size];
This is in C++ wrong in more than one way.
You need to define C-like arrays with a constant size.
Safer would be to use C++ containers, e.g. std::vector for anything without predictable size.
Even in C, where VLAs are possible, creating them like you did and LATER reading in a new value for size will not change anything, especially not the size of the arrays.
Also, but that is already inside undefined behaviour and purely speculative,
if the compiler understands that as A1 and A2of size 0, then the start of both arrays is the same and writing to one writes also to the other - AND be totally forbidden because it will access beyond any arrays size.
To demonstrate that, try
int A1[5];
int A2[5];
This is a syntax of a program that I made of hackerrank to reverse an array. The location of the question is Practice < Data Structures < Arrays < Arrays - DS
the program seems to work fine on an online compiler but is showing error over hackerrank.
can anyone guide me where I went wrong?
#include<iostream>
using namespace std;
int main(){
int n,i;
int arr[n];
cin>>n;
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
for(int i=n-1;i>=0;i--)
{
cout<<arr[i];
}
}
You are creating an array with n elements before you read n, i.e n is having garbage value. Here is correct working code.
#include <iostream>
using namespace std;
int main(){
int n,i;
cout<<"Enter number of elements"<<endl; // Comment it if you don't want debug console outut
cin>>n;
int arr[n];
cout<<"Enter "<<n<<" elements"<<endl; // Comment it if you don't want debug console outut
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
for(int i=n-1;i>=0;i--)
{
cout<<arr[i];
}
}
I changed your solution and it work fine now , i will tell you some notes for your future:
1 - You have to read size of array first before declare array as mentioned in comments by #HolyBlackCat , otherwise it will give garbage value.
2 - this command int arr[n]; won't work on all Compilers and may give you an Error. Is there a solution to declare array without specific size ? Yes , you can use Vector like that vector<int> arr(n);but you have to include library of vector first #include<vector>
3 - In C and C++ programs the main function is of type int and therefore it should return an integer value. The return value of the main function is considered the "Exit Status" of the application. On most operating systems returning 0 is a success status like saying "The program worked fine". So you have to write return 0; in the end of your program.
#include<iostream>
#include<vector> // must include to use Vectors
using namespace std;
int main(){
int n;
cin >> n; // Must read size of array first , otherwise it will give it garbude value
vector<int> arr(n); // instead of int arr[n]
for(int i = 0 ; i < n ; i++)
cin >> arr[i];
for(int i = n-1 ; i >= 0 ; i--)
cout << arr[i] << " ";
cout << endl; // to print new line as mentioned in the problem statement
return 0; // Prefer to use it always
}
Hope that helps you and Good Luck.
The exercise of Hacker Rank needs that you reverse an array of integers, might you have a problem with the variable size array, cause some compilers support this characteristic but others not.
And Hacker Rank do not support some properties in other languages like JS , Hacker Rank do not support prompt() for take an input.
In any case for fix this error you can use vector class.
#include<iostream>
#include<vector>//Include library vector for dynamic size of array
using namespace std;
int main(){
int n;
cin>>n;
vector<int> arr(n);//Declare the vector with a max size n
for(int i=0;i<n;i++){
cin>>arr[i];
}
for(int i = n-1 ;i>=0;i--){
cout<<arr[i]<<" ";
}
return 0;
}
This code has passed al test cases of hackerRank
All the test on hackerRank had passed
this is my code, PLease help me ! im using xcode.. i want to generate a sequence for a polynomial and the terms are xor'ed and made a feedback to the first input bit since it is 8 bit it is done 2^8-1 times.Alternate code will also be helpful Thanks in advance
#include "32bit.h"
#include<iostream>
using namespace std;
int main()
{
bool input[8];
int n;
bool out=0;
cout<<"Enter the no of terms ";
cin>>n;
int temp1[n];
int gen=0;
bool store[255];
cout<<"Input power of x in increasing order, Omit x^0";
for(int i=0;i<n;i++)
cin>>temp1[i];
cout<<"Enter key to generate ";
cin>>gen;
for(int m=0;m<255;m++)
{
store[m]=input[gen];
bool temp2[n];
int var=0;
for(int j=0;j<n;j++)
{
var=temp1[j];
temp2[j]=input[var];
}
int c=0;
for(int k=0;k<n;k++)
{
if(temp2[k]%2==1)
c++;
}
if(c%2==1)
out=1;
else
out=0;
for(int l=0;l<8;l++)
input[l+1]=input[l];
input[0]=out;
}
for(int p=0;p<255;p++)
cout<<store[p];
}
There is an out of bounds array access here:
for(int l=0;l<8;l++)
input[l+1]=input[l];
since input is only of size 8 and you are trying to write to input[8] (i.e. the non-existent 9th element) on the last iteration of this loop. I'm guessing it should probably be:
for(int l=0;l<7;l++)
input[l+1]=input[l];