This question already has answers here:
Why does dividing two int not yield the right value when assigned to double?
(10 answers)
Closed 7 years ago.
my code doesn't return double value z, instead returns only 1, why?
#include <iostream>
#include <fstream>
using namespace std;
double road(int s, int v, int max )
{
double t;
t = (s/v);
return t;
}
int main()
{
int s[2]={0};
int v[2]={0};
int max;
double z; // result of function
ifstream fd;
fd.open("u1.txt");
fd >> max;
for (int i = 0; i < 2; i++)
{
fd >> s[i] >> v[i];
z = road( s[i], v[i], max );
cout << z << " ";
}
fd.close();
return 0;
}
Try to change your method like
double road(int s, int v, int max )
{
double t;
t = (s/(double)v);
return t;
}
int/int will result in a integer. So you need to cast the numerator or denominator as double.
Related
I am doing a code in c++ where I am supposed to be finding the series and I build the function for the series myself yet and I call the function I don't find my answer
here is my code
#include <iostream>
#include <cmath>
using namespace std;
double harmonicSeries(int n);
int main() {
int n;
cout << "Enter n" << endl;
cin >> n;
harmonicSeries(n);
}
double harmonicSeries(int n) {
for (int i = 1; i <= n; i++) {
float s;
float sum = 0.0;
s = 1 / n;
sum += s;
return sum;
}
}
I will be thankful for any help
See I have made the changes in your code,this works fine in this finding numbers and adding to get their sum.You should use return outside the function and basically harmonic series is of form 1/n which can be any float number or double number so I use s as double and i has float(which by this).
s=1/i(double=1/float,gets converted to double)
#include <iostream>
#include <cmath>
using namespace std;
double harmonicSeries(int n);
int main() {
int n;
cout << "Enter n" << endl;
cin >> n;
cout<<harmonicSeries(n);
}
double harmonicSeries(int n) {
double sum=0.00;
double s;
for (float i = 1; i <= n; i++) {
s = 1 / i;
sum += s;
}
return sum;
}
If you find anything wrong do ask for sure:)
I attempted a Hackerearth tutorial question and was able to solve it correctly using following code:
#include <iostream>
using namespace std;
int main() {
const long int MOD = 1000000007;
const long int SIZE = 100001;
long int t, n;
long int cache[SIZE];
cache[0] = 1;
for (long int i = 1; i < SIZE; i++) {
cache[i] = ( i * cache[i-1] ) % MOD;
}
cin >> t;
while (t--) {
cin >> n;
cout << cache[n] << endl;
}
return 0;
}
However when using scientific notation to write MOD or SIZE, online judge reports incorrect answers. What am I missing here?
#include <iostream>
using namespace std;
int main() {
const long int MOD = 10e9+7;
const long int SIZE = 100001;
long int t, n;
long int cache[SIZE];
cache[0] = 1;
for (long int i = 1; i < SIZE; i++) {
cache[i] = ( i * cache[i-1] ) % MOD;
}
cin >> t;
while (t--) {
cin >> n;
cout << cache[n] << endl;
}
return 0;
}
Scientific notation means “constant multiplied by ten to the power of x”. If you want 1000000007 you need 1e9, meaning “one followed by nine zeroes.” Now you use 10e9 which is “ten followed by nine zeroes” or “one followed by ten zeroes” so you’re off by a factor of ten.
MY CODE along with the output
The following code is not working. It has no errors, am doing some mistake in the logic I think. I want to find power of a number using functions. How to make this code work?
The code:
#include<iostream>
using namespace std;
int pow(int);
int main()
{
int x,p,ans;
cout<<"Enter a number";
cin>>x;
cout<<"Enter the power of the number";
cin>>p;
ans=pow(x);
cout<<ans;
return 0;
}
int pow(int)
{
int a=1,i,p,x;
for(i=0;i<=p;i++)
{
a=a*x;
}
return a;
}
Here is working code:
#include<iostream>
using namespace std;
int pow(int, int);
int main()
{
int x,p,ans;
cout<<"Enter a number";
cin>>x;
cout<<"Enter the power of the number";
cin>>p;
ans=pow(x, p);
cout<<ans;
return 0;
}
int pow(int x, int p)
{
int a=1,i;
for(i=0;i<=p;i++)
{
a=a*x;
}
return a;
}
Ideone
You have to pass the local variables into the function instead of defining new ones with the same name. What you are doing should give you warnings about unused variables (x and p in main) and it also invokes undefined behavior in pow because of ininitialized reads of the variables defined there.
Also your function was wrong. You were just multiplying 1 with a value a bunch of times, which stays 1 forever.
Your function must have the parameters names specified (not just the types):
int pow(int) -> int pow(int b, int p)
You iterate once more than necessary:
for (i = 0; i <= p; i++) -> for (i = 0; i < p; i++)
You can shorten some arithmetic operations:
a=a*x -> a *= x;
The final function:
int pow(int b, int p)
{
int a = 1, i;
for (i = 0; i < p; i++)
a *= b;
return a;
}
You call it by passing the variables precedently declared:
pow(x, p)
So your final code be like:
#include <iostream>
int pow(int b, int p)
{
int a = 1, i;
for (i = 0; i < p; i++)
a *= b;
return a;
}
int main()
{
int x, p, ans;
std::cin >> x >> p;
ans = pow(x, p);
std::cout << ans << std::endl;
return 0;
}
I am making a program that solves integrals. So far I managed to make a simple function integral (a,b) f(x)dx.
Now I need to make a Multithreading program in parallel programming using MPI.
This is how i see it. Need to make an array of elements for a,b,n(how precise i want my function solved) and then taking each combination of function elements for one thread.
Question : Am I understanding this problem correctly?
This is my code for functions and my epic fail trying to change it to array. Please help!
#include "stdafx.h"
#include <iostream>
#include "conio.h"
#include <stdio.h>
using namespace std;
void WaitForEnter(void)
{
printf("Press Enter to continue: ");
fflush(stdout);
while ( _getch() != '\n' ) ;
}
double fun (double x){return x;}//changing f(x)dx
double sumIntegral(double lowBound, int n, double dx)
{
double rectangleArea;
double cumSum=0;
for (int i=0; i<n; i++)
{
double xi = lowBound+i*dx;//x1,x2...
double funValue = fun(xi);
double rectangleArea = dx*funValue;
cumSum += rectangleArea;
}
return cumSum;
}
int main()
{
double lowBound = 4;//a
double upBound = 7 ;//b
int n = 5;//how precise is solution
double dx;
dx = (upBound-lowBound)/n;
double result = sumIntegral(lowBound , n, dx);
cout<<"rez je = "<<result;
WaitForEnter();
return 0;
}
//epic fail
#include "stdafx.h"
#include <iostream>
#include "conio.h"
#include <stdio.h>
using namespace std;
void WaitForEnter(void)
{
printf("Press Enter to continue: ");
fflush(stdout);
while ( _getch() != '\n' ) ;
}
double fun (double x){return x;}//menja funkciju
double sumIntegral(double lowBound[], int n, double dx[],int numBound)
{
double rectangleArea;
double cumSum;
double sumIntegralAll[] = {NULL};
for(int j=0;j<numBound;j++)
{
cumSum=0;
for (int i=0; i<n; i++)
{
double xi = lowBound[j]+i*dx[j];//x1,x2...
double funValue = fun(xi);
rectangleArea = dx[j]*funValue;
cumSum+=rectangleArea;
cout<<cumSum;
}
//sumIntegralAll[j]=cumSum;
}
for(int i=0;i<numBound;i++){cout<<sumIntegralAll;}//pise
return cumSum;
}
int main()
{
int numBound = 2;//broj do
double lowBound[] = {4,4};//a
double upBound[] = {7,7} ;//b
int n = 5;//broj intervala, preciznost
double dx[]={NULL};
for(int i=0;i<=numBound;i++){
dx[i] = (upBound[i]-lowBound[i])/n;
}
double result = sumIntegral(lowBound , n, dx,numBound);
//cout<<"rez je = "<<result;
WaitForEnter();
return 0;
}
This question already has answers here:
Why is my HelloWorld function not declared in this scope?
(11 answers)
Closed 8 years ago.
I am working on a C++ project and while compiling, I am receiving the error messages:
error: mean was not declared in this scope
error: standard_dev was not declared in this scope
My code is
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <string>
using namespace std;
int main()
{
int N(0);
char filename[100];
double m, stdev;
string temp;
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);
}
Those functions have not been seen yet when you try to use them; the compiler doesn't know what they are which leads to the error. Either move them before main() or prototype them, such as:
double mean(double * mydata, double N);
double standard_dev(double * mydata, double m, int N);
int main()
{
...
This will give the compiler its expectations for those symbols, so when it sees them in use it knows what to do with them.
Any name must be defined before its using (with rare exceptions relative to template). You use name mean in statement
m = mean(mydata, N);
but name mean is not yet defined.
Place function declarations before main
double mean(double *mydata, double N);
double standard_dev(double *mydata, double m, int N);
int main()
{
//...
Or before their usage.
int main()
{
double mean(double *mydata, double N);
double standard_dev(double *mydata, double m, int N);
//...
m = mean(mydata, N);
//...
You need to have prototypes for your functions mean() and std_dev() at the top of your file. Otherwise, they don't 'exist' in the main function. Either copy the entire functions to the space above your main function, or make prototypes for them. Good luck!