This question already has an answer here:
Integer division always zero [duplicate]
(1 answer)
Closed 7 years ago.
#include <iostream>
#include <math.h>
#include <ctype.h>
using namespace std;
int main()
{
int n=0,tot=0;
float sum = 0;
float average = 0;
float product = 1;
cout<<"Type an integer and press Enter:\n";
cin>>n;
/*
Your logic goes here
*/
for(int i=1;i<=n;i++){
cout<<sum<<endl;
sum= sum+(1/i);
product=product*1/i;
tot++;
}
cout<<"Sum, product and average of reciprocals are:\n";
cout<<sum<<endl;
cout<<product<<endl;
cout<<average<<sum/tot<<endl;
}
Anyone please tell me what I am doing wrong , my sum is always equal to one, i don't know why. I put cout and at each iteration it printout "1". My logic is right but there is some mistake which i can't find.
The following line
sum= sum+(1/i);
Does integer division, when i = 1 it will evaluate to 1, otherwise when i > 1, it will be 0. I would use 1.0/i to force floating point division
EDIT: I would make the change to your product update as well
1/i will be 0 for all i greater than 1. Integer division. You will want to fix this by replacing 1/i with 1.0/i.
See ideone here
Related
I'm trying to write a program that prints the factorial of a number:
#include <iostream>
using namespace std;
int main() {
int ans,fact=1, number;
cin>>number;
for (int i=1; i<=number; i++) {
fact = fact*i;
}
ans = fact%998244353;
cout <<ans<<endl;
return 0;
}
When I try to input the number 250, the program returns 0, but when I try to print a smaller number like 4, it returns the right number. Does anyone know why this occurs and how to fix this? Thanks.
Some hints for doing math in a finite field or ring while avoiding overflow.
Mathematically is true that:
(a*b)%c == ((a%c)*(b%c))%c
and also:
(a+b)%c == ((a%c)+(b%c))%c
But in the case a*b or a+b will overflow, you have to use the righthand expression because it is less likely to overflow.
Link to Problem -> https://atcoder.jp/contests/dp/tasks/dp_i
question Coins-I
Code I wrote gave wrong answer to TEST CASE 3
5
0.42 0.01 0.42 0.99 0.42
Expected output
0.3821815872
MY code output
0.382182
As the error is greater than 1e-9 it got WA
What I tried:
I made double to long double but still it doesn't give accuracy
is there any way to increase the accuracy when working with double in this question.
#include<bits/stdc++.h>
using namespace std;
long double p[3000];
long double dp[3001][1501];
long double solve(int i,int tail_count){
if(dp[i][tail_count]!=2){
return dp[i][tail_count];
}
long double ans=0;
if(i==1 && tail_count==0)return p[1];
else if(i==1 && tail_count>0)return (long double)1;
else if(tail_count==0)ans= p[i]*solve(i-1,tail_count);
else ans= (p[i]*solve(i-1,tail_count)+(1-p[i])*solve(i-1,tail_count-1));
dp[i][tail_count]=ans;
return ans;
}
int main(){
for(int i=0;i<3000;i++){
for(int j=0;j<1500;j++){
dp[i][j]=2;
}
}
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>p[i];
}
cout<<solve(n,(n-1)/2);
return 0;
}
The number of digits to be printed via output streams is controlled by their precision parameter. The default is 6 digits, if you want more you need to adjust it accordingly by std::setprecision :
std::cout << std::setprecision(num_digits) << solve(n,(n-1)/2);
Most of your indexing is off by one. Valid indices of an array with N elements are 0 till (including) N-1. dp is 3001 x 1501 but you only use 3000 x 1500 and p leaves the first element unused. Perhaps the code is still correct, but at least your way of using indices is confusing. Also in case you know the size of the array only at runtime you could use a std::vector.
Description:
For an array consists of several non-negative int,array nomalization means every element will be divides by the sum of the array.Presuming that the array consists of at least one element,and the sum of elements won't be beyond the max of int.
Input:
several non-negative int
Output:
the result of normalization.
Sample Input
1 2 3 4
Sample Output
0.10 0.20 0.30 0.40
I think it's an easy problem,but the Accept Rate is only 12/1352.This is my code.
#include <vector>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int a,t=0,sum=0;
vector<int> arr;
while(cin>>a)
{
arr.push_back(a);
t++;
}
for(int i=0;i<t;i++)
sum+=arr[i];
if(sum)
{
cout<<setiosflags(ios::fixed)<<setprecision(2)<<1.0*arr[0]/sum;
for(int i=1;i<t;i++)
{
cout<<setiosflags(ios::fixed)<<setprecision(2)<<" "<<1.0*arr[i]/sum;
}
}else
{
for(int i=0;i<t;i++)
if(i==0)
cout<<"0.00";
else
cout<<" 0.00";
}
return 0;
}
The output is the same to the sample output.But I get a wrong answer 75%,I don't know what tips I have not considered.
I figure it out,it's not a good problem,0/0 should output 1
Yes, indeed, you are right, it is an easy problem. Your output statement
cout<<setiosflags(ios::fixed)<<setprecision(2)<<" "<<1.0*arr[i]/sum;
formats the output with
setiosflags(ios::fixed)
setprecision(2)
Meaning, you are showing exactly 2 digits for the fraction. And everytime, when
(1.0*arr[i] / sum) < 0.01
The result will be shown as exactly 0.00
Hope this helps
This question already has answers here:
Strange behaviour of the pow function
(5 answers)
Closed 6 years ago.
I'm helping a friend with a C++ assignment. There is an issue with the folowing base converter function:
#include <iostream>
#include <cmath>
using namespace std;
int strToInt(string num, unsigned base){
int result = 0;
for (int i=0; i<num.length(); i++) {
if (num[i]>='0' && num[i]<='9')
result += (num[i]-'0')*pow(base,num.length()-i-1);
else if (num[i]>='A' && num[i]<='F')
result += (num[i]-'A'+10)*pow(base,num.length()-i-1);
else if (num[i]>='a' && num[i]<='f')
result += (num[i]-'a'+10)*pow(base,num.length()-i-1);
}
return result;
}
int main()
{
string number;
int base;
while(number.compare("exit")!=0){
cin>>number;
cin>>base;
cout<<strToInt(number,base)<<"\n\n";
}
return 0;
}
For some inexplicable reason every time I enter 3 and 5 digit decimals and chose base 10 I am getting the proper number -1.
E.g.
100
10
99
10000
10
9999
I've been going over this function for the last 5-6 hours and adding all types of debug code, but for the good of me I can't figure out what the hell is wrong.
Code style remarks are also very appreciated.
Cheers
std::pow does floating-point math. You're probably getting a round-off error somewhere. The usual way to accumulate values is to multiply and add each time through the loop:
result *= base;
result += ch - '0';
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C++ Random number homework question
Guys, I need help (yes, last minute homework help). I'm suppose to write a function named getTwoRandomNumbers that uses two parameters to return two different random numbers. The function is suppose to accept two parameters that specify the minimum and maximum values of the random number.
I need to write data validation code that ensures that two identical random numbers will never be returned. This is what I have so far: (ANY DIRECTION IS GREATLY APPRECIATED)
Ok, I made changes and now have this:
#include <iostream>
#include <iomanip>
#include <cmath>
#include <time.h>
using namespace std;
float getTwoRandomNumbers (int Min, int Max, int & number1, int & number2);
void main()
{
cout << "The two random numbers are " << getTwoRandomNumbers << endl;
}
float getTwoRandomNumbers (int Min, int Max, int & number1, int & number2)
{
int loopNumber, number;
for (loopNumber = 0; loopNumber <= 200 ; loopNumber ++)
{
number = rand();
if (loopNumber < 100 && number >= Min && number <= Max)
{
number1 = number;
}
if (loopNumber > 100 && number >= Min && number <= Max)
{
number2 = number;
}
return number2;
}
}
First off, you're on the right path with passing back both values through the parameters of the function. Then it seems like you're also trying to get the 2nd value back through the function return.
Since you need to get more than 1 value from the function, ignore the return value and just use the 2 parameters passed to get your return values.
Also, try thinking out the question in your head, what are you trying to do? Get 2 random values while number1 is not equal to number2
That should give you a decent start on the logic flow for getting the random numbers to pass back.
One thing for you: As I reformatted your code, it became apparent that you have a return within your for loop. That means getTwoRandomNumbers will always return after the first iteration. I'm 99.99% positive this is not what you have in mind.