C++ Variable Zero Correction - c++

The variable "probability" always comes out to zero even though the outputs of the other variables are in the same placement area as the probability variable at the end of the program. I have a feeling it has to do with placement, but it could be another initializing problem. The probability should never come out to zero.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
int main() {
int die1,
die2,
sum,
turns,
win=0,
loss=0;
double probability=0;
int thepoint, rolls;
srand(time(0));
cout<<"How many turns would you like? ";
cin>>turns;
for(int i=0; i<turns; i++)
{
sum=0;
die1=rand()%6;
die2=rand()%6;
sum=die1+die2;
//cout<<"\nFirst die is a "<<die1<<endl;
//cout<<"Second die is a "<<die2<<endl;
//cout<<"\n\n>>>Turn "<<i<<": You rolled "<<sum;
switch (sum){
case 2: case 3: case 12:
//cout<<"You have lost this turn with 2 3 or 12!"<<endl;
loss++;
break;
case 7:
//cout<<"\nYea! You won this turn with a 7 on the first roll!"<<endl;
win++;
break;
case 11:
//cout<<"You won this turn ith 11!"<<endl;
win++;
break;
default:
//cout<<"\nRolling again!"<<endl;
thepoint=sum;
rolls=1;
sum=0;
//cout<<"\nRoll 1 - Your point is "<<thepoint<<endl;
while (sum != thepoint)
{
//srand(time(0));
die1=rand()%6;
die2=rand()%6;
sum=die1+die2;
rolls++;
//cout<<"Roll "<<rolls<<". You rolled "<<sum<<endl;
if (sum == thepoint)
{
//cout<<"You won this turn in the while with a point match!"<<endl;
win++;
break;
}
if (sum == 7)
{
loss++;
//cout<<"You lost this turn in the while with a 7"<<endl;
break;
}
}
}
}
probability = win/turns;
cout<<"No. of Turns: "<<turns<<"\n";
cout<<"No. of Wins: "<<win<<"\n";
cout<<"No. of Loses: "<<loss;
cout.precision(6);
cout<<"\nExperimental probability of winning: "<<fixed<<probability;
cin.get();
cin.get();
return 0;
}

Since the variables win and turns are of datatype int, and probability is a real number (i.e. double here) you would need to cast at least one of them to double before you perform the division.
probability = (double)win/turns;
BTW, there is no harm in casting both win and turns too if required but not really needed.

The mod 6 operation % 6 will give you a number in the range 0..5 (the remainder after dividing by 6). You need to add 1 to that.
change
die1=rand()%6;
die2=rand()%6;
to
die1=1+rand()%6;
die2=1+rand()%6;
Update: Although this is a bug, it is not the root cause of the probability printing zero, the real problem was pointed out by #Tuxdude.

Related

Getting an output of Prime Number for 15,25,35 even though i know it is not

If number is like 25,35 output is "This number is prime",even though I know it's not a prime number. Why am I getting that output? With several other casual numbers it works.
#include<iostream>
using namespace std;
bool isPrimeNumber(int number){
bool isPrimeFlag=true;
for(int i=2;i<number;i++){
if(number % i == 0)
isPrimeFlag=false;
break;
}
return isPrimeFlag;
}
int main(){
int number;
cout<<"Number: ";
cin>>number;
bool isPrimeFlag = isPrimeNumber(number);
if(isPrimeFlag)
cout<<"Prime Number"<<endl;
else
cout<<"Not Prime Number"<<endl;
}
for(int i=2;i<number;i++){
if(number % i == 0)
isPrimeFlag=false;
break;
}
C++ is not Python: indentation does not affect your program's semantics - what matters are braces, and you have a missing brace for the inner if so the break instruction is always executed even if the if( number % i ) statement is false.
Whenever writing in C, C++, and other easy-to-shoot-yourself-in-the-foot languages it helps to use a linter tool that will force you to always use braces.
for(int i=2;i<number;i++){
if(number % i == 0) {
isPrimeFlag=false;
break;
}
}

Different Output while using and not using 'break' in switch case in c++

I was solving the a question at HackerRank -
Question Link - https://www.hackerrank.com/challenges/maximum-element/problem
In one solution I used 'break' statement in switch-case, in another solution I didn't.
Solution was wrong when I didn't use break statement. What is the reason behind this ?
Input -
10
1 97
2
1 20
2
1 26
1 20
2
3
1 91
3
With break Statement -
#include <bits/stdc++.h>
using namespace std;
int main() {
int noOfTestCases;
cin>>noOfTestCases;
vector <int> st;
for(int x=0; x<noOfTestCases; x++){
int query;
cin>>query;
switch (query) {
case 1:
int number;
cin>>number;
if(st.empty()){
st.push_back(number);
}
else if(number > st[st.size()-1]){
st.push_back(number);
}
else{
st.push_back(st[st.size()-1]);
}
break;
case 2:
if(!st.empty()){
st.pop_back();
}
break;
case 3:
cout<<st[st.size()-1]<<endl;
}
}
}
//Output -
//26
//91
Without break statement -
#include <bits/stdc++.h>
using namespace std;
int main() {
int noOfTestCases;
cin>>noOfTestCases;
vector <int> st;
for(int x=0; x<noOfTestCases; x++){
int query;
cin>>query;
switch (query) {
case 1:
int number;
cin>>number;
if(st.empty()){
st.push_back(number);
}
else if(number > st[st.size()-1]){
st.push_back(number);
}
else{
st.push_back(st[st.size()-1]);
}
case 2:
if(!st.empty()){
st.pop_back();
}
case 3:
cout<<st[st.size()-1]<<" "<<query<<endl;
}
}
}
//Output -
//0
//0
//0
//0
//0
//0
//0
//0
//0
//0
Consider the following code
switch (x) {
case 1:
std::cout << "one\n";
case 2:
std::cout << "two\n";
break;
case 3:
std::cout << "three\n";
}
If x is 1 it will print both one and two. It will then exit the switch block due to the break statement. Note that x will not be compared with 2 after printing "one", it will directly fall-through to printing "two".
So I had a misconception about switch case statement -
What I thought -
I thought that in switch case if the expression is equal to any case D, then further cases - E, F, G, will not be executed, irrespective of the break statement.
Reality - If an expression is equal to any case D, then further cases - E, F, G will also be executed if we don't use break statement.
Thanks Jeffrey and Paul Sanders to clarify this problem.

How to use randomly generated number in switch statement?

i am making a simple program that displays the behaviour of the person from the sentences that i write in switch statement cases. I want to randomly generate a number between 1 and 10 and use it in "switch(this place)". So far, i have written the following code and stuck here..
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int rand_range(int low, int high)
{
return rand()%(high-low) + low;
}
int main()
{
srand(time(NULL));
cout<<"Hi!, this program tells you about your behaviour"<<endl;
cout<<"press enter to continue";
cin.get();
switch( )
{
case 1:
{
cout<<"you are rude !";
}
case 2:
{
cout<<"you are smart";
}
case 3:
{
cout<<"you try to prove yourself special all the time";
}
case 4:
{
cout<<"you are good in sarcasm";
}
case 5:
{
cout<<"you look dumb, but deep inside you are intelligent";
}
case 6:
{
cout<<"you are always ready for a debate with someone";
}
case 7:
{
cout<<"you are very lazy";
}
case 8:
{
cout<<"you are dumber than what you look";
}
case 9:
{
cout<<"you always try to help others";
}
case 10:
{
cout<<"you have good decision making capabilities";
}
default:
{
cout<<"something wrong";
}
}
}
In your switch statement in the parentheses you want (rand() % 10) + 1. After you have seeded the random number generator the rand() function will return a number between 0 and RAND_MAX. RAND_MAX is a large number.
To get a random number in a range a standard trick is to use the % operator. rand() % 10 returns a number between 0 and 9 inclusive. Add 1 to get your range of 1 through 10.

Program must decide positive, negative, or zero

I need to desigen a program which it must display positive, negative or zero when user enter the number with a function name called numtest. when i enter number 0 it still appears as, number negative. Can anyone help me on this. Thank you.
#include <iostream>
using namespace std;
float numtest(float a, float b, float c)
{
if(a>1)
return a;
if(b<1)
return b;
if(c=0)
return c;
}
int main()
{
float num;
cout<<"Please enter number";
cin>>num;
if(num>1)
cout<<"number is positive";
if(num<1)
cout<<"number is negative";
if(num=0)
cout<<"number is zero";
return 0;
}
First of all, when checking equality you must use == instead of =.The = sign assigns the R.H.S. to the L.H.S. Eg. c=0 changes the value in c to be 0, so instead it should be if(c==0).
Now, if you want to check positive numbers, the condition should be if(num>0), and for negative numbers, it should be if(num<0).
Lastly, try to use else if and else instead of three ifs.
Looks like you've made the age old problem of assigning a 0 to num when you want an equality comparison.
if(num == 0){ ... }
Also, think of what happens when you enter 0.5 or -0.5. And in your question you mention using your function (numtest) to do something, but your not even calling it in your main function
I believe your issue would be solved if you changed your parameters to:
if(a>0)
if(a==)
if a<0)
Because they way you have it set now you are not accounting for the infinite amount of numbers between 0 and 1 (.10 , .0000005 , etc). And we want to use the "==" sign to so equality.
Hope that helps!
Your program is not using the numtest() function and unwanted variable used and for testing number is +ve or -ve you should use
For Positive: if(num>0) or if(num>=1) in case of floatif(num>=0.001)
For Negative: if(num<0) or if(num<=-1) in case of floatif(num>=-0.01)
For Zero: if(num==0)
Try the below program:
#include <iostream>
#include<conio.h>
void numtest(float num)
{
if(num>0)
cout<<"number is positive";
if(num<0)
cout<<"number is negative";
if(num==0)
cout<<"number is zero";
}
void main()
{
float n;
cout<<"Please enter number";
cin>>n;
numtest(n);
getch();
}

Prime factors output - what's the issue?

I am entering the UVA online programming competition, and am working on a solution for UVA 583 (Prime Factors).
I recently made a Java solution for this that got accepted. When I tried translating it to C++, it always got WA ("wrong answer") even though for each test case I make, both programs output the same answer.
Can anyone point out what's wrong?
#include <iostream>
#include <string>
#include <cmath>
#include <stdio.h>
using namespace std;
int primes [4792];
void factorize(int x1){
int c = 0;
for(int i = 0;i<4792;i++){
int x2 = primes[i];
while(x1%x2==0){
if(c!=0)
cout<<" x ";
cout<<x2;
c++;
x1/=x2;
}
}
if(x1>1 && c!=0){
cout<<" x "<<x1;
}
if(c==0)
cout<<x1;
cout<<endl;
}
int main(){
primes[0]=2;
primes[1]=3;
int count = 2;
for(int i=5; i<46340;i+=2){
if(i%6 != 1 && i%6 != 5)
continue;
int limit = (int)sqrt((double)i);
bool isPrime = true;
for(int j=0;j<count;j++){
if(primes[j]<limit){
if(i%primes[j]==0){
isPrime = false;
break;
}
}
}
if(isPrime){
primes[count]=i;
count++;
}
}
int x = 0;
cin>>x;
while(x!=0){
string out;
cout<<x<<" = " ;
int x1 = x;
if(x<0){
cout<< "-1 x ";
x1*=-1;
}
factorize(x1);
cin>>x;
}
return 0;
}
while((double)x1/(double)x2 == (double)(x1/x2)){
That is almost always a bad idea. Due to the limited precision of floating point operations, you can end up with cases where in the mathematical sense the two are exactly equivalent, but for which the test above yields false.
in your factorize(int x1), just above while, add if (x2*x2 > x1) break;.
in your main(), if(primes[j]<limit){ should be using <= and it should have else clause with {break;} in it. With < in place of <= I'm surprised it worked for you in Java.
As it is, with < there, your code does not recognize the top 46 primes below 46340 - it puts them past the array end1 where they remain out of reach. Writing past array's end is bad in itself.
1 that is because it falsely recognizes the squares of primes as prime numbers, and there are 46 such squares between 5 and 46340.