Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 months ago.
Improve this question
I had tried to write a program to find the maximum sum subarray, I am able to take the output correctly in a certain scenario but if I want to change it the output is not as desired. So anyone can help me?
#include<iostream>
using namespace std;
int main(){
int minValue,n;
int a[n]={4,-2,-3,4,-1,-2,1,5,-3};
int max_so_far = minValue;
int max_ending_here=0;
int start=0, end=0, s=0;
for(int i=0; i<n;i++){
max_ending_here = max_ending_here+a[i];
if(max_so_far < max_ending_here){
max_so_far = max_ending_here;
start=s;
end = i;
}
if(max_ending_here<0){
max_ending_here=0;
s = i+1;
}
start=s;
}
cout<<"maximum sum subarray is: "<<max_so_far;
cout<<"\nstart "<<start;
//cout<<"\nend "<<end;
return 0;
}
The output of this code is:
maximum sum subarray is: 7
start 3
But if I try to print the value of end as well as shown in the following program:
#include<iostream>
using namespace std;
int main()
{
int minValue,n;
int a[n]={4,-2,-3,4,-1,-2,1,5,-3};
int max_so_far = minValue;
int max_ending_here=0;
int start=0, end=0, s=0;
for(int i=0; i<n;i++)
{
max_ending_here = max_ending_here+a[i];
if(max_so_far < max_ending_here){
max_so_far = max_ending_here;
start=s;
end = i;
}
if(max_ending_here<0){
max_ending_here=0;
s = i+1;
}
start=s;
}
cout<<"maximum sum subarray is: "<<max_so_far;
cout<<"\nstart "<<start;
cout<<"\nend "<<end;
return 0;
}
The output becomes as:
maximum sum subarray is: 32760
start 3
end 0
Can anybody point out the actual error for me?
Here I made 2 changes:
Used INT_MIN
Used vector instead of array definition.
#include<iostream>
using namespace std;
int main()
{
vector<int> a ={4,-2,-3,4,-1,-2,1,5,-3}; // <------------ here
int minValue=INT_MIN, n= a.size(); // <--------- here
int max_so_far = minValue;
int max_ending_here=0;
int start=0, end=0, s=0;
for(int i=0; i<n;i++)
{
max_ending_here = max_ending_here+a[i];
if(max_so_far < max_ending_here){
max_so_far = max_ending_here;
start=s;
end = i;
}
if(max_ending_here<0){
max_ending_here=0;
s = i+1;
}
start=s;
}
cout<<"maximum sum subarray is: "<<max_so_far;
cout<<"\nstart "<<start;
cout<<"\nend "<<end;
return 0;
}
OUTPUT:
maximum sum subarray is: 7
start 3
end 7
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Greeting everyone, I'm trying to solve 0/1 Knapsack problem using the Dynamic Programming Top-Down Approach. I'm pretty sure that most of my logic is correct, my code is compiling successfully. But, it's not giving the proper/correct output that is needed.
For Instance, suppose weight[] has inputs as 10,20,30 and it's corresponding value[] has 60,100,120. The max weight that the Knapsack can hold onto is 50. The max profit should be 220, but my code is giving me the answer 280 instead. Please help me, here's my piece of code:-
#include<bits/stdc++.h>
using namespace std;
void knapsack(vector<int>& weight, vector<int>& value, int w, int n){
vector<vector<int>> t;
for(int i=0;i<n+1;++i){
vector<int> temp;
for(int j=0;j<w+1;++j){
int x =0;
temp.push_back(x);
}
t.push_back(temp);
temp.clear();
}
for(int i=1;i<n+1;++i){
for(int j=1;j<w+1;++j){
if(weight[i-1]<=w){
t[i][j] = max(value[i-1]+t[i-1][w-weight[i-1]], t[i-1][j]);
}
else{
t[i][j] = t[i-1][j];
}
}
}
cout<<"Max Profit: "<<t[n][w];
// return final;
// vector<int> oneDimVector;
// for(int i = 0; i < n+1; i++){
// for(int j = 0; j < w+1; j++){
// oneDimVector.push_back(t[i][j]);
// }
// }
// vector<int>::iterator maxElement;
// maxElement = max_element(oneDimVector.begin(), oneDimVector.end());
// cout<<"Max Profit: "<<*maxElement;
}
int main(){
int n;
int w;//Total weight of knapsack
cin>>n;
cin>>w;
vector<int> weight;
vector<int> value;
for(int i=0;i<n;++i){
int x;
cin>>x;
weight.push_back(x);
}
for(int i=0;i<n;++i){
int x;
cin>>x;
value.push_back(x);
}
knapsack(weight,value,w,n);
}
I again debugged my code, I had to change one variable which I had written wrong in the following line of code:-
t[i][j] = max(value[i-1]+t[i-1][w-weight[i-1]], t[i-1][j]);
here, it should be:-
t[i][j] = max(value[i-1] + t[i-1][ j - weight[i-1]], t[i-1][j]);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have added bits/stdc+.h and vector both.
Still this error is coming .
Can anyone tell me why this is happening.
#include <bits/stdc++.h>
#include<vector>
void rotate(int arr[], int n);
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
int a[n] , i;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
rotate(a, n);
for (i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
}
return 0;
}
// } Driver Code Ends
//User function Template for C++
void rotate(int arr[], int n)
{
vector<int> a;
a[0] = arr[n-1];
for(int i = 0 ; i<n-1 ;i++)
{
a.insert(a.back(), arr[i]);
}
for(int j : a)
cout<<j;
}
main.cpp:30:5: error: ‘vector’ was not declared in this scope
vector<int> a;
^~~~~~
Follow these: (EDITED)
(SOLUTION TO YOUR PROBLEM) Use, using namespace std as it means if the compiler finds something that is not declared in the current scope then it will go and check std.
Don't mix c and c++ syntax. Either use printf or cout.
Also check the 1st comment on this answer, as there is something you should know about "using namespace std" and "cout/cin".
No need to work two times, you can also declare and define your function at once.
Solution (but have an error in other parts)
#include <bits/stdc++.h>
using namespace std;
void rotate(int arr[], int n)
{
vector<int> a;
a[0] = arr[n - 1];
for (int i = 0 ; i < n - 1 ; i++)
{
a.insert(a.back(), arr[i]); // ITS YOUR SYNTAX, CONSIDER TO UPDATE IT
}
for (auto &it : a)
cout << it;
}
int main()
{
int t;
cin>>t;
while (t--)
{
int n;
cin>>t;
int a[n] , i;
for (i = 0; i < n; i++)
cin>>a[i];
rotate(a, n);
for (i = 0; i < n; i++)
cout<<a[i];
cout<<"\n";
}
return 0;
}
CHECK LINE NO 9 line,
and see if it's correct or not.
a.insert(a.back(), arr[i]); WRONG
You are doing something wrong there. Check this statement
error: ‘vector’ was not declared in this scope
Solved by using namespace
If you liked this answer. Please consider ticking this answer.:)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am a beginner in C++, in this assignment, I've made a program that sorts the given array in ascending order using the bucket sort algorithm. I have encountered numerous errors regarding the use of the vector library and when expressing the variables used to contain the array.
Is there a simpler implementation of the bucket sort of natural numbers? The examples I found were using bucket sort to sort alphabets decimals...etc which made it confusing for me to understand the basics when applied to positive whole numbers.
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
//function declaration
void display(int arr[], int size);
int getMax(int arr[], int size);
void bucketSort(int arr[], int size);
using namespace std;
void display(int arr[], int size) {
int i, m;
for(i = 0; i < size; i++) {
cout << m; {
m = arr[i];
}
}
return m;
}
void bucketSort(int arr[], int size) {
//variables
int max, bucket = 10, divider, i, j, k;
//10 buckets
vector B[bucket];
//find max and min
max = getMax(arr, size);
divider = ceil(float(max + 1) / bucket);
//insert element into bucket
for(i = 0; i < size; i++) {
j = floor( arr[i] / divider );
B[j].push_back(arr[i]);
}
//sort elements in the buckets
for(i = 0; i < bucket; i++) {
sort(B[i].begin(), B[i].end());
}
//append back the elements from the buckets
k = 0;
for(i = 0; i < bucket; i++) {
for(j = 0; j < B[i].size(); j++) {
arr[k++] = B[i][j];
}
}
}
int main(void) {
//unsorted elements
int arr[] = {22,45,12,8,10,6,72,81,33,18,50,14};
//size of the array
int n = sizeof(arr)/sizeof(arr[0]);
//output unsorted elements
display(arr, n);
//sort the elements
bucketSort(arr, n);
//display sorted elements
display(arr, n);
return 0;
}
You can't just ask for a vector... you have to tell it, a vector of what?
In your case it looks like you want an array of vectors of ints, so:
vector<int> B[bucket];
Your next problem is that you can't have an array of variable size.
Please refer to your C++ book for more information on all of this stuff.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to find all the divisors of a number (n) and to add to the array those divisors that are at the 1st power (that appear only once), but I get in output just zeros, what's wrong with my code?
#include<iostream>
using namespace std;
int k,A[100000],n,p,d=2,pozitia=0;
int main()
{
cin>>n;
while(n>1)
{
p=0;
while(n%d==0)
{
p=p+1;
n=n/d;
}
if (p==1) { A[pozitia]=d; pozitia++; }
d=d+1;
}
for (int i=0;i<=pozitia;i++) cout<<A[pozitia]<<" ";
return 0;
}
You print always the same value:
for (int i=0;i<=pozitia;i++)
cout<<A[pozitia]<<" ";
It should be
for (int i=0;i<pozitia;i++)
cout<<A[i]<<" ";
Also pay attention that it should be i<pozitia and not i<=pozitia because you increment pozitia each time you insert a new value so at the end pozitia will point to a not initialized value in A.
I couldn't follow your logic for computing the divisors. It seems to be much simpler than you make it out to be.
int stop = n/2 + 1;
for ( ; d < stop; ++d )
{
if ( n % d == 0 )
{
A[pozitia]=d;
pozitia++;
}
}
Here's a program that uses that logic.
#include<iostream>
using namespace std;
void printDivisors(int A[], int pozitia)
{
for (int i=0;i<pozitia;i++) cout<<A[i]<<" ";
}
void fun(int n)
{
int A[100000];
int d = 2;
int pozitia=0;
int stop = n/2 + 1;
for ( ; d < stop; ++d )
{
if ( n % d == 0 )
{
A[pozitia]=d;
pozitia++;
}
}
printDivisors(A, pozitia);
}
int main()
{
int n;
cin>>n;
fun(n);
return 0;
}
The output for input of 100:
2 4 5 10 20 25 50
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
in this code i try to divide the a[5] into two arrays on the criterion of stored data in the array .......
and store the index of the array a[5] to other arrays to show these index contain different data elements
but i doesn't work for me
#include <iostream>
using namespace std;
void printarray(int b[], int count)
{
int i;
for(i=0;i<count;i++)
cout<<b[i]<<endl;
}
void main()
{
int a[5]={1,0,0,1,1};
int array[5][5]=
{
{0,5,0,4,0},
{0,0,6,0,7},
{0,0,0,0,8},
{0,0,0,0,10},
{0,0,0,0,0}
};
int count=0;
int counti=0;
int C1=0;
int i;
for(i=0;i<5;i++)
{
if(C1==a[i])
{
count++;
}
else
{
counti++;
}
}
int *b= new int[count];
int *c= new int[counti];
for(i=0;i<5;i++)
{
if(C1==a[i])
{
b[i]=i;
}
else
{
c[i]=i;
}
}
printarray(b,count);
}
the code display the grabage values... plz help me
its show the following result
-842151450
1
The first i was 1 , so b will contain {1, 2}. where ( b[1] = 1, b[2] = 2 )
when you loop through b to print all elements you start from index 0 although b started from index 1.
you can fix index using (j, k instead of i).
int *b = new int[count];
int *c = new int[counti];
int j, k;
j = k = 0;
for (i = 0; i<5; i++)
{
if (C1 == a[i])
{
b[j++] = i;
}
else
{
c[k++] = i;
}
}
printarray(b, count);