Why this simple C++ program is giving unexpected result? [closed] - c++

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 8 years ago.
Improve this question
I made a simple C++ program. I expect a floating point output of 5/9, but is is zero. Can someone comment, why output is unexpected i.e. zero?
#include<iostream>
using namespace std;
void fun(double *ptr);
int main (int argc, char* argv[])
{
double a;
fun(&a);
cout<<a<<endl; // why not floating point 5/9??
}
void fun (double *ptr)
{
*ptr=(5/9);
}

5/9 will result in 0 because it is int division.
You would need to do
void fun (double *ptr)
{
*ptr = (5.0 / 9.0);
}
You need to use the correct type literals:
5 // int
5.0 // double
5.0f // float
5u // unsigned int
5l // long
5ul // unsigned long

Related

Why can't the global variable delta be used in method of class? [closed]

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'm a beginner of C++. In this sample, I want to use the global variable delta in method update_v() of class neuron. But it can't be used. Could you tell me why if you know?
#include<iostream>
#include<cmath>
using namespace std;
unsigned long nextt=1;
long clock=0;
long delta=0;
class neuron{
public:
double a,b,c,d;
double current_v,current_u;
double previous_v,previous_u;
double accumulate;
void update_v(){
current_v=previous_v+delta(0.04*pow(previous_v,2)+previous_v)+accumulate;
}
void update_u(){
current_u=previous_u+delta*a*(b*previous_v-previous_u);
}
};
In void update_v(){, you do delta(0.04*pow(previous_v,2)+previous_v), so it makes the compiler thinks that you are calling a function named delta. But there's none, so it throws a error.
It looks like you forget to use the * operator:
void update_v(){
current_v = previous_v + delta * (0.04 * pow(previous_v,2) + previous_v) + accumulate;
}

In C++, why can int initialize a variable using new operator but double cannot? [closed]

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 3 years ago.
Improve this question
#include <iostream>
using namespace std;
int main()
{
int* i = new int(75);
double* d = new double(3.14159);
printf("%d\n",*i);
printf("%d\n",*d);
}
In the above code i returns a value of 75 however, d returns 1.
I tried explicitly initializing it as
*d = 3.14159
But the value is still returned as 1.
Can anyone explain what I am doing wrong here?
Use this for printing.
cout<<*i;
cout<<*d
"%f" is the (or at least one) correct format for a double if you want to use printf for printing the value of the double in C++.

Trying to create a cos(x) calculating function [closed]

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 4 years ago.
Improve this question
I am coding a cos(x) function in C++ but the result I am getting is infinity, except that it should be -1/3.
Here is my code:
#include <iostream>
#include <math.h>
using namespace std;
The factorial function:
int factorial(unsigned int n)
{
unsigned long factorial = 1;
for(int o=1;o<=n;o++)
{
factorial *= o;
}
}
int main()
{
double x;
double answre;
double input;
cin>>input;
for(int i=0;i<2;i++)
{
double y=2*(i)+2;
I declared y here instead of implementing it's value directly, since I thought it is dividing by factorial instantaneously and that is the reason for all the parentheses as well.
x=((pow(input,2*(i)+2))/(factorial(y)))*(pow(-1,(i)+1));
x=+x;
}
answere=1+x;
cout<<answere<<endl;
return 0;
}
You are not returning anything in factorial
x=((pow(input,2*(i)+2))/(factorial(y)))*(pow(-1,(i)+1));
x=+x; // ???
If you want to add to x, just write x += .... What you did was assign one (!) summand to x and then set x to itself. You are never adding anything.

expected identifier before numeric constant in a Vector [closed]

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 4 years ago.
Improve this question
I am making a simple program, please take a look
#include<iostream>
using namespace std;
int main(int argc,char* argv[])
{
int op=0;
int v[20]=[1, 0];
float Ma=0;
if (argv[1]==1)
{
float S=0;
for(int i=0;i<=20;i++)
{
S=S+v[i];
}
Ma=(double)(S/20);
}
cout<<"Media aritmetica pentru elementele din vector este "<<Ma<<endl;
return 0;
}
I get this error Program.cpp:10:13: error: expected identifier before numeric constant
int v[20]=[1, 0];
^
I am using the gcc from ubuntu for compiling and I'm not really sure if there's anything to it that may cause this. I am a bit new into this.
You probably meant to define an array of 20 int's and initialize its first 2 elements to 1 and 0 respectively.
Well, instead of writing:
int v[20] = [1, 0];
you should have written:
int v[20] = {1, 0};
which means what you want it to mean. Note, however, that the term "vector" has a different meaning typically in C++ - the name of the std::vector container class in the standard library.
Use curly braces instead.
int v[20]={1,0};

Confusion about passing arrays [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
Can someone help me with my program? I need to pass the three arrays into the function called calc_volts and then calculated the volts and then display the values. I keep getting errors that say "unreferenced local variable" or "undeclared idebtufier" for the variables; i, j, k, and volts.
#include <iomanip>
#include <iostream>
#include <cmath>
using namespace std;
double calc_volts(double, double, double, int);
int main()
{
const int max = 10;
int i; double current[max] = {10.62,14.89,13.21,16.55,19.62,9.47,6.58,18.32,12.15,3.98};
int j; double volts[max];
int k; double resistance[max] = {4,8.5,6,7.35,9,15.3,3,5.4,2.9,4.8};
}
double calc_volts(double current[],double volts[], double resistance[], int max)
{
for (j = 0, j<max, j++)
volts[j] = current[i]*resistance[k];
return volts[j];
}
You have many problems:
Your function declaration is wrong:
double calc_volts(double, double, double, int);
It should be:
double calc_volts(double[], double[], double[], int);
You must invoke the function in order to use it:
int main()
{
const int max = 10;
double current[max] = {10.62,14.89,13.21,16.55,19.62,9.47,6.58,18.32,12.15,3.98};
double volts[max];
double resistance[max] = {4,8.5,6,7.35,9,15.3,3,5.4,2.9,4.8};
calc_volts(current, volts, resistance, max); // call the function to execute it
}
The variables i, j and k do not exist inside calc_volts because they were declared inside main. Variables declared inside a function can only be used inside that function.
To fix the problem just put the declarations inside calc_volts.