#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;
}
Related
#include<iostream>
#include<climits>
using namespace std;
int main(){
int n;
cin>>n;
int a[n];
for(int i=0;i<n;++i){
cin>>a[n];
} //array instillisation
int cursum=0;
int maxsum=INT_MIN;
for(int i=0;i<n;++i){
cursum+=a[i];
if(cursum<0){
cursum=0;
}
maxsum=max(cursum,maxsum);
}
cout<<maxsum<<endl;
return 0;
}
//this code is for maximum subarray problem using kadane's algo.My compiler is retrurning wrong output
In cin you are doing incorrect operation it should be
cin>> a[i];
what you are doing is taking the value of a[n]
The function should return 156 with a "Z" input but it outputs 0.
If the letter's U-Z, the function finds the largest factor of the letters numeric value other than the value itself and multiplies it by 12.
Tried putting cout in the function and it works somehow. Don't know where's the problem. Someone help.
#include<iostream>
#include<string>
#include<ctype.h>
using namespace std;
int uz(int n);
int main(){
string letter, alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int alphSize=alphabet.size(), l=0, total=0;
cin>>letter;
int letterSize=letter.size();
for(int i=0;i<letterSize;i++){
for(int j=0;j<alphSize;j++){
if(toupper(letter[i])==alphabet[j])
l=j+1;
}
if(l>20&&l<=26){
l=uz(l);}
total+=l;
}
cout<<total<<endl;
return 0;
}
int uz(int n){
int size=1;
int arr[size];
for(int i=1;i<=n;i++){
if(n%i==0){
size++;
arr[size]+=i;
}
}
n=12*arr[size-1];
//cout<<n;
return n;
}
#include <iostream>
using namespace std;
int num(int n){
for(int i= 1 ; i<=n ; i++){
int sum=0;
sum += i;
cout<<sum;
}
}
i guess this part is clear.
int main()
{
int x;
cout<<"enter the value of x ";
cin>>x;
int answer=num(x);
cout<<"the total sum of the first n integer is "<<answer;
return 0;
}
ive tried looking it up but no results found....
ive always had some trouble with the loops.
You are throwing away what is calculated by previous iterations.
You are not returning what is calculated.
To fix, get the declaration and initialization of i out of the loop and return the result.
#include <iostream>
using namespace std;
int num(int n){
int sum=0;
for(int i= 1 ; i<=n ; i++){
sum += i;
cout<<sum;
}
return sum;
}
Try moving the definition and initialization of the variable sum to outside of the for loop, preferably before it. Do not forget your return value from the function.
The function num() should return the variable sum and variable Sum should be defined outside for loop.
Your code should be
#include <iostream>
using namespace std;
int num(int n){
int sum=0; //sum should be declared outside for loop
for(int i= 1 ; i<=n ; i++){
sum += i;
// cout<<sum; //dont print sum here it will be printed in main()
}
return sum; //you missed this
}
int main()
{
int x;
cout<<"enter the value of x ";
cin>>x;
int answer=num(x);
cout<<"the total sum of the first n integer is "<<answer;
return 0;
}
I am trying to calculate the standard deviation of an array but my answer is returning as 0. I think the problem is stemming from the "count" getting messed up. The array I am receiving the data from is simply four numbers of 1,4,6,7. The code is outputting the answer as 0 but that is incorrect. Any help would be much appreciated.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <string>
using namespace std;
double mean(double *mydata, double N);
double standard_dev(double *mydata, double m, int N);
int main()
{
int N(0);
char filename [100];
double m, stdev;
double next;
int count=0;
cout<<"Enter name of file: ";
cin>>filename;
ifstream myfile;
myfile.open(filename);
while(myfile>>next)
{
count++;
}
N=count;
double *mydata;
mydata=new double[N];
for(int i=0; i<N; i++)
{
myfile>>mydata[i];
}
m = mean(mydata, N);
stdev = standard_dev(mydata, m, N);
cout<<"The standard deviation is:" <<stdev<<endl;
myfile.close();
delete[] mydata;
return 0;
}
double mean(double *mydata, double N)
{
double sum(0), m;
for(int i=0; i<N; i++)
{
sum +=mydata[i];
}
m=(sum/(double)N);
return (m);
}
double standard_dev(double *mydata, double m, int N)
{
double *mydata2= new double [N];
for(int i=0; i<N; i++)
{
mydata2[i]= pow((mydata[i]-m),2);
}
double sum(0), S, X;
for(int i=0; i<N; i++)
{
sum+=mydata2[i];
}
X=sum/N;
S=sqrt(X);
return (S);
}
The code for the array containing the 4 numbers is just:
1
4
6
7
They are vertical in coding.
You are reading in the data wrong. First, you read in all the data to next to increment count. Then you try to read in the nonexistent data with you for loop. I recommend that you use std::vector to avoid the problem of dynamic memory allocation;
vector<double> mydata;
while(myfile>>next)
{
mydata.push_back(next);
}
The other easiest solution is just to put the number of elements in the file. Ex:
File:
4 1 4 6 7
And read N from the file and use your for loop
I make function to remove data of array which same in c++, so as make the process like this:
input:4
input:
25.50
64.25
64.25
25.50
output:
5.50
64.25
but the function error like this image
here the code
#include <iostream>
using namespace std;
void removeSame(double a[int x]){
int index[x];
for(int z=0; z<x; z++){
jum[z]=0;
for(int c=0; c<x; c++){
if(a[c]==a[z]){
index[z]=c;
}
}
}
for(int z=0; z<x; z++){
if(z==index[z]){
cout<<a[z]<<endl;
}
}
}
int main(){
int n,x;
cin>>n;
double a[n];
for(x=0; x<n; x++){
cin>>a[x];
}
removeSame(a[x]);
return 0;
}
then when I change the code like this, an error occurs again
void removeSame(double a[], int x){
...
}
the error like this:
cannot convert 'double' to 'double*' for argument '1' to 'void hapusygsama(double*, int)'
please help me
UPDATE. thx for you all who are reply my thread
#include <iostream>
using namespace std;
void hapusygsama(double a[], int len){
int index[len];
for(int z=0; z<len; z++){
for(int c=0; c<len; c++){
if(a[c]==a[z]){
index[z]=c;
}
}
}
cout<<endl;
for(int z=0; z<len; z++){
if(z==index[z]){
cout<<a[z]<<endl;;
}
}
}
int main(){
int n,x;
cin>>n;
double a[n];
for(x=0; x<n; x++){
cin>>a[x];
}
hapusygsama(a, n);
return 0;
}
Your code is illegal C++. C++ does not currently support run-time arrays. All array dimensions must include constant bounds. In addition, C++ does not permit any kind of declaring size in parameter like double a[int x]. You must pass the size as a separate parameter.
A much elegant way to achive the wished behaivour would be to use std::unique and std::resize
The code would be:
int main() {
std::vector<float> myvector;
int n;
cin >> n;
float b;
for (int i=0; i<n; i++){
cin >> b;
myvector.push_back(b);
}
std::sort(myvector.begin(), myvector.end());
myvector.resize(std::distance(myvector.begin(),std::unique (myvector.begin(), myvector.end())));
return 0;
}
Also in your question you have as input flaoting point numbers, while for the code you use int, I think you should change that too.
You don't pass the parameters correctly to the function :
#include <iostream>
using namespace std;
void removeSame(double a[], int x){
// ^^^
int index[x];
for(int z=0; z<x; z++){
jum[z]=0;
for(int c=0; c<x; c++){
if(a[c]==a[z]){
index[z]=c;
}
}
}
for(int z=0; z<x; z++){
if(z==index[z]){
cout<<a[z]<<endl;
}
}
}
int main(){
int n,x;
cin>>n;
double a[n];
// ^^^^ ERROR : not permitted, n is not a constant
// it would not compile.
for(x=0; x<n; x++){
cin>>a[x];
}
removeSame(a, x);
// ^^^
return 0;
}
You want to receive an array in removeSame to remove values who are the same. In you code, you try to pass a double value only.
As I said, it would compile because n is not a constant when you declare a in the main function.
EDIT : It seems it is an extension in gcc who permits this kind of syntax. But I suggest to use something more common...
You should do :
int main() {
int n, x;
cin >> n;
double* a = new double[n];
...
delete[] a;
return 0;
}
In that case, the prototype of removeSame should be :
void removeSame(double* a, int x);
And last thing, you should verify the values entered by the user, one of the most important rule in development is : Never trust the users input !