So I'm doing a code which sums up all elements of field but I have to use pointers :( No matter what I tried I get 1 as output. Tried same code with multiplication but still nothing... Code:
#include <iostream>
#include <cmath>
using namespace std;
float vrat=0;
int suma (int pok2, int vel22){
for(int z=0; z<vel22; z++){
vrat+=pok2;
}
return vrat;
}
int main()
{
/// 2. zadatak
int vel2;
int *vel22=&vel2;
cout<<"Unesi broj elemenata koje hoces upisati"<<endl;
cin>>vel2;
int polje2[vel2];
cout<<"Kreni unosit elemente "<<endl;
for(int z=0; z<vel2; z++){
cin>>polje2[z];
}
int *pok2=&polje2[vel2];
suma(*pok2, *vel22);
cout<<"Suma elemenata je "<<suma<<endl;
return 0;
}
Thank you!
I think you meant to do something like this
#include <iostream>
#include <cmath>
using namespace std;
float vrat=0;
int suma (int *pok2, int vel22){
for(int z=0; z<vel22; z++, pok2++){
vrat+=*pok2;
}
return vrat;
}
int main()
{
/// 2. zadatak
int vel2;
int *vel22=&vel2;
cout<<"Unesi broj elemenata koje hoces upisati"<<endl;
cin>>vel2;
int polje2[vel2];
cout<<"Kreni unosit elemente "<<endl;
for(int z=0; z<vel2; z++){
cin>>polje2[z];
}
int *pok2= polje2;
suma(pok2, *vel22);
cout<<"Suma elemenata je "<< vrat << endl;
return 0;
}
this line
int *pok2=&polje2[vel2];
attempts to access memory outside of the bounds of polje2. Remember arrays are zero-indexed. Let me know if this isn't what you were intending to do.
Ah! It looks like at the very end you're printing the function suma rather than the result. Try this instead:
int result = suma(*pok2, *vel22);
cout<<"Suma elemenata je "<< result <<endl;
I'm not sure why printing a function results in "1", but printing the actual result should work here.
Related
The following is the code, i have tried al the other styles mentioned in the comments one by one but none worked to remove the garbage value, Although "jugaad" works but it's not performance wise good. Couldn't figure out what's wrong !
#include <bits/stdc++.h>
using namespace std;
int main() {
int n,m;
cin>>n>>m;
// Other styles
// int data[n+1][m+1] = {0,0};
// int data[n+1][m+1] = {0};
// int data[n+1][m+1] = {{0,0}};
int data[n][m] = {0};
// Jugaad start:
for(int i=0;i<m;i++){
data[0][i] = 0;
}
// Jugaad end
cout<<"\n";
for(int x=0;x<n;x++){
for(int y=0;y<m;y++){
cout<<data[x][y]<<"\t";
}
cout<<"\n";
}
cout<<"\n";
return 0;
}
Screenshot of code and output
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
data[i][j] = 0;
}
``}
This will set all values of your array to zero
#include <iostream>
using namespace std;
int main() {
int a=0,b=0;
cin>>a>>b>>endl;
for(int i=a;i<=b;++i)
cout<<i<<endl;
return 0;
}
I want to see the output is about the integers inclusive between a and b, but after entering two numbers, it shows no output..
#include <iostream>
using namespace std;
int main() {
int a=0,b=0;
cin>>a>>b>>endl;
for(int i=a;i<=b;++i)
cout<<i<<endl;
return 0;
}
First you can't use endl in cin
Second you wrote ++i inside your for loop which will increase value of by i means value of will become 1 from 0.
Therefore the condition will never be true as value of b is 0.
THE CORRECT WAY
#include <iostream>
using namespace std;
int main() {
int a=0,b=0;
cin>>a>>b;
for(int i=a;i<=b;i++)
cout<<i<<endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a = 0, b = 0;
cin >> a;
cin >> b;
for (int i = a; i <= b; i++)
cout << i << endl;
return 0;
}
EDIT: I removed something as it wasn't true :P Silly me.
Also 'endl' doesn't work with cin :)
The code is wrong because you have already got a and b equal to 0,and after that you are taking a and b as inputs.
If you wanna take them as inputs you should write int a,b. NOT int a=0,b=0
#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;
}
#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 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 !