I'm writing a trivial program that doubles the amount of rice grains per each square on a chess board. I'm trying to work out the amount of squares required for at least 1 000 000 000 grains of rice. The problem is, what ever I try, the second if statement gets skipped even though 'test' is false after first iteration.
I've tried an else after the if statement, but the else is skipped when 'test' variable is false.
constexpr int max_rice_amount = 1000000000;
int num_of_squares = 0;
int rice_grains = 0;
bool test = true; // so we can set rice_grains amount to 1
for (int i = 0; i <= max_rice_amount; i++)
{
if (test == true)
{
rice_grains = 1; // This will only happen once
test = false;
}
else if (test == false)
rice_grains *= 2;
++num_of_squares;
std::cout << "Square " << num_of_squares << " has " << rice_grains << " grains of rice\n";
}
The else causes the issue. But C++ is more powerful than you can imagine. Rework your loop to
for (
int rice_grains = 1, num_of_squares = 1;
rice_grains <= max_rice_amount;
rice_grains *= 2, ++num_of_squares
){
with
std::cout << "Square " << num_of_squares << " has " << rice_grains << " grains of rice\n";
as the loop body; and weap at the beauty.
Looking at the problem description, this looks a good fit for a while loop or a do-while loop.
#include <iostream>
void f()
{
constexpr int max_rice_amount = 1000000000;
int num_of_squares = 1;
int rice_grains = 1;
while (rice_grains < max_rice_amount)
{
std::cout << "Square " << num_of_squares << " has " << rice_grains << " grains of rice\n";
rice_grains *= 2;
++num_of_squares;
}
}
Code at compiler explorer
Here you recognize 3 big blocks:
Initialization
The 'until' condition in the while
The manipulation
Related
I'm not sure why but the program stops decrementing at Number = -12.75. The program should run until the Number = -50.0.
#include <iostream>
// create variables to be used in the loops
float Number = 50.0; // starting Number
int Counter1 = 0; // counter for .25 decrements
int Counter2 = 0; // counter for 1 decrements
std::cout << '\n' << Number << '\n';
// loop to decrement Number down from 50.0 to -50.0
for (int i = 50; i >= -50; --i)
{
// if statement to determine which loop to use
if ((Counter1 + Counter2) % 2 == 0)
{
Number = Number - 0.25;
std::cout << Number << '\n';
++Counter1;
}
else
{
Number = Number - 1.0;
std::cout << Number << '\n';
++Counter2;
}
}
int TotalCounter = Counter1 + Counter2;
std::cout << "\nCombined trips through loops: " << TotalCounter << '\n';
If your goal is to stop when Number reaches -50, then you should modify the condition at which the for-loop should stop. This should work:
#include <iostream>
// create variables to be used in the loops
float Number = 50.0; // starting Number
int Counter1 = 0; // counter for .25 decrements
int Counter2 = 0; // counter for 1 decrements
std::cout << '\n' << Number << '\n';
// loop to decrement Number down from 50.0 to -50.0
for (int i = 50; Number >= -50; --i)
{
// if statement to determine which loop to use
if ((Counter1 + Counter2) % 2 == 0)
{
Number = Number - 0.25;
std::cout << Number << '\n';
++Counter1;
}
else
{
Number = Number - 1.0;
std::cout << Number << '\n';
++Counter2;
}
}
int TotalCounter = Counter1 + Counter2;
std::cout << "\nCombined trips through loops: " << TotalCounter << '\n';
for getting the correct result you have to change the for loop condition that you have used i<=-50.Use while loop instead as : " while(Number>-50) " Because it takes more iterations to reach Number to -50 then 101 iterations you are giving in for loop.
hope u got this.
When I run the code it should only make the win condition = true after either all dragons have no health or all ships have no health.
But when it runs, it seems the do while loop exits after the first iteration, even though the condition is set to only leave while win condition is true.
I cannot seem to figure out why it wont continue running, I even assigned winCondition = false when declaring the variable.
Any help would be appreciated.
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
struct ship //Creates type ship
{
float shipHealth;
int numOfPeople;
bool capturedDragon;
};
struct dragon //Creates type dragon
{
string riderName;
string dragonName;
float dragonHealth;
bool isCaptured = false;
};
int main()
{
srand(time(NULL));
const int MAX_SHIPS = 7;
const int MAX_RIDERS = 5;
int dragonHit;
int dragonDamage;
int dragonRemoveMenNumber;
int shipDamage;
int shipHit;
bool winCondition = false;
ship hunterShips[MAX_SHIPS]; //creates array of ships
dragon dragonRiders[MAX_RIDERS]; //creates array of dragon/riders.
for (int i = 0; i <= 4; i++)
{
cout << "Rider Name: " << endl;
cin >> dragonRiders[i].riderName;
cout << "Dragon Name: " << endl;
cin >> dragonRiders[i].dragonName;
dragonRiders[i].dragonHealth = rand() % (20 - 15 + 1) + 15;
}
for (int i = 0; i <= 6; i++)
{
hunterShips[i].shipHealth = rand() % (40 - 30 + 1) + 30;
hunterShips[i].numOfPeople = rand() % (15 - 10 + 1) + 10;
}
do
{
for (int i = 0; i <= 4; i++) //Dragons turn
{
if(dragonRiders[i].dragonHealth > 0 && dragonRiders[i].isCaptured == false) //Dragon is alive
{
dragonHit = rand() % 10 + 1;
if (dragonHit <= 7) //Dragon hits target
{
if(hunterShips[i].shipHealth > 0 || hunterShips[i].numOfPeople > 0)
{
dragonDamage = rand() % (10 - 5 + 1) + 5; //Amount of Damage done
cout << dragonRiders[i].dragonName << " hit and dealt " << dragonDamage << " damage. " << endl;
if (dragonHit <= 3) //Dragon takes men out
{
dragonRemoveMenNumber = rand() % (3 - 2 + 1) + 2;
cout << dragonRiders[i].dragonName << "Took out " << dragonRemoveMenNumber << " men" << endl;
hunterShips[MAX_SHIPS].numOfPeople = hunterShips[MAX_SHIPS].numOfPeople - dragonRemoveMenNumber; //Ships lose people
}
hunterShips[MAX_SHIPS].shipHealth = hunterShips[MAX_SHIPS].shipHealth - dragonDamage;
}
else
{
cout << "All ships are destroyed, dragons win!" << endl;
winCondition = true;
}
}
else //Dragon misses target
cout << dragonRiders[i].dragonName << " missed" << endl;
}
else //Dragon is dead
cout << dragonRiders[i].dragonName << " is dead or captured" << endl;
}
for (int i = 0; i <= 6; i++) //Ships turn
{
if(hunterShips[i].shipHealth > 0 || hunterShips[i].numOfPeople > 0) //ship is afloat
{
shipHit = rand() % 10 + 1;
if (shipHit >= 7) //40% chance to hit
{
if(dragonRiders[i].dragonHealth > 0 && dragonRiders[i].isCaptured == false)
{
shipDamage = rand() % (5 - 4 + 1) + 4; //Damage done
cout << "Ship dealt " << shipDamage << " damage" << endl;
dragonRiders[MAX_RIDERS].dragonHealth = dragonRiders[MAX_RIDERS].dragonHealth - shipDamage;
}
else
{
cout << "All dragons are dead, ships win!" << endl;
winCondition = true;
}
}
else
cout << "Ship missed. " << endl;
}
else
cout << "Ship is sunk or out of men" << endl; //ship is sunk
}
} while (winCondition == true);
return 0;
}
Your code is
bool winCondition = false;
/* ... */
do {/* ... */}
while (winCondition == true);
and you wonder "Loop keeps exiting even though winCondition = false". You also explain "the condition is set to only leave while win condition is true".
I conclude that you misunderstood the semantics of a do-while loop to be "loop until condition is met". Instead it is however "loop while the condition is met". This means that exiting when winCondition evaluates to false is the intended behaviour.
You are initialising the winCondition = false to start with. The do..while loop will run through one iteration and evaluate that the iterate condition winCondition == true is not satisfied, and therefore exit.
In addition, if the do..while loop ever continued, there is no statement inside it where winCondition is ever set to false, so the loop will never exit.
Did you mean to close the do..while loop with winCondition == false?
I'm writing a code to swap integers in an array and I want to know how I can exit a loop without using a break statement and keeping my logic consistent. Here is my code below:
int swapped = 0;
if (arrays[0][first] % 2 == 0)
{
cout << arrays[0][first] << " is odd " << endl;
for (int i = 1; i < arraycount; ++i)
{
for (int j = 1; j < arrays[i][0] + 1; ++j)
{
if (arrays[i][j] % 2 != 0)
{
int temp = arrays[i][j];
cout << "Array #" << 1 << " value " << arrays[0][first] << " swapped with "
<< "Array #" << i << " value " << temp;
arrays[i][j] = arrays[0][first];
arrays[0][first] = temp;
swapped = 1;
break;
}
}
if (swapped) {
break;
}
Use goto [I'll be bashed because of this].
if (arrays[0][first] % 2 == 0)
{
cout << arrays[0][first] << " is odd " << endl;
for (int i = 1; i < arraycount; ++i)
{
for (int j = 1; j < arrays[i][0] + 1; ++j)
{
if (arrays[i][j] % 2 != 0)
{
int temp = arrays[i][j];
cout << "Array #" << 1 << " value "
<< arrays[0][first] << " swapped with "
<< "Array #" << i << " value " << temp;
arrays[i][j] = arrays[0][first];
arrays[0][first] = temp;
goto done;
}
}
done:
something;
for (int i = 1; i < arraycount && !swapped; ++i)
{
for (int j = 1; j < arrays[i][0] + 1 && !swapped; ++j)
{
if(arrays[i][j] % 2 != 0)
int temp = arrays[i][j];
cout << "Array #" << 1 << " value " << arrays[0][first] << " swapped with " << "Array #" << i << " value " << temp;
arrays[i][j] = arrays[0][first];
arrays[0][first] = temp;
swapped = 1;
}
}
}
this will do the same thing you have in inner loop.
Using a Break statement does not necessarily make your codes logic inconsistent and breaks are often useful to improve the readability of your code. But in answer to your question this can be achieved by utilizing while loops and logical boolean operators. A modified version of your code is below, I have tried to modify it as little as possible so you can still see your code within the example. There are a few logical errors in your code that I have left in the example below that you might want to look into. In particular the line below will print "is odd" when in fact the number would be even. If you where wanting to check if the number arrays[0][first] is odd then the following if statement would be needed if (arrays[0][first] % 2 != 0) instead of if (arrays[0][first] % 2 == 0).
Logical Error
if (arrays[0][first] % 2 == 0)
{
cout << arrays[0][first] << " is odd " << endl;
This is the code without using breaks.
bool swapped = true;
if (arrays[0][first] % 2 == 0)
{
cout << arrays[0][first] << " is odd " << endl;
int i = 1;
while ( (i < arraycount) && swapped)
{
int j = 1;
bool if_odd = true;
while ((j < arrays[i][0] + 1) && if_odd)
{
if (arrays[i][j] % 2 != 0)
{
int temp = arrays[i][j];
cout << "Array #" << 1 << " value " << arrays[0][first] << " swapped with "
<< "Array #" << i << " value " << temp;
arrays[i][j] = arrays[0][first];
arrays[0][first] = temp;
swapped = false;
if_odd = false;
}
j++;
}
i++;
}
}
I‘m having trouble with my for loop. I have a basic 2 times table that goes up from 1 to 10. (see below). I'm trying to add the result so that after every loop, allResult displays the values of all results added. it needs to show all results added after every loop. and I don't know how to go about it. A better description is the desired outcome commented in the code.
my Code...
int main(){
int allResult;
for( int i = 1; i < 11; i++)
{
int result = 2*i;
cout << 2 << " times " << i << " = " << result << endl;
// I want allResult to store results added. so after first loop, result = 2 so allResult = 2
// after second loop, result = 4. so allResult should be 6. (results from 1 and 2 added)
// after third loop, result = 6 so allResult should be 12. (results from 1, 2 and 3 added)
// I'm trying to apply this to something else, that uses random numbers, so i cant just * result by 2.
}
system("pause");
}
int allResult = 0;
for( int i = 1; i < 11; i++)
{
allResult += 2*i;
cout << 2 << " times " << i << " = " << 2*i << endl;
}
int allResult = 0;
for( int i = 1; i < 11; i++)
{
int result = 2*i;
cout << "2 times " << i << " = " << result << endl;
allResult += result;
cout << "allResult: " << allResult << endl;
}
int main()
{
int allResult=0;
for( int i = 1; i < 11; i++)
{
int result = 2*i;
allResult=allResult+result;
/*Initially allResult is 0. After each loop iteration it adds to its previous
value. For instance in the first loop it was allResult=0+2,
which made allResult equal to 2. In the next loop allResult=2+4=6.
Third Loop: allResult=6+6=12. Fourth loop:allResult 12+8=20 and so on..*/
cout << 2 << " times " << i << " = " << result << endl;
}
system("pause");
}
I am making a clock-driven simulation program and, among other issues, my main while loop, while(jobsCompleted < jobsToComplete) is looping more times than expected/wanted. For example, if I were to assign 500 to jobsToComplete, the output at the end of the program would tell me that there were 505 jobs completed. I have tried to debug this one issue for at least an hour now, but to no avail. Any help is appreciated. Thanks!
#include <iostream>
#include <string>
#include <stdlib.h>
#include <queue>
#include <fstream>
#include "job.cpp"
using namespace std;
int main()
{
ofstream cpuSim;
cpuSim.open("cpuSim.out.txt");
int clock = 0, jobsCompleted = 0, jobsToComplete = 0, probUser = 0, probability, id = 0;
jobType_t job_type;
int inWQ, outWQ, inCPUQ, outCPUQ, required, given, jobTypeInt, timeSpentInCPUqueue = 0, timeSpentInWaitQueue = 0, CPUidle = 0;
queue<job> CPUqueue, waitQueue;
int numIO = 0, numCPU = 0;
srand(time(NULL));
cout << "Enter how many jobs need to be completed: ";
cin >> jobsToComplete;
cout << endl << "Enter the probability that a new job is created: ";
cin >> probUser;
cout << endl;
while(jobsCompleted < jobsToComplete)
{
clock++;
probability = rand() % 100 + 1;
if(probability > probUser)
{
for(int i=0; i<jobsToComplete; i++)
{
id = rand() % 1000 + 1;
jobTypeInt = rand() % 100 + 1;
if(jobTypeInt >= 50)
job_type = IO_bound;
else
job_type = CPU_bound;
required = rand() % 10;
job *newJob = new job(id, job_type, inWQ, outWQ, inCPUQ, outCPUQ, required, given);
waitQueue.push(*newJob);
}
while((CPUqueue.size() <= 10) && waitQueue.empty() == false)
{
waitQueue.front();
job temp = waitQueue.back();
waitQueue.pop();
temp.setTimeExitedWQueue(clock);
temp.setTimeEnteredCPUQueue(clock);
CPUqueue.push(temp);
}
double oneSecond = 1.0, timeSpent = 0;
while((oneSecond > 0.0) && (!CPUqueue.empty()))
{
job top = CPUqueue.front();
CPUqueue.pop();
if(top.getJobType() == IO_bound)
{
top.setTimeGiven(top.getTimeGiven() + .1);
timeSpent = .1;
numIO++;
}
else
{
top.setTimeGiven(top.getTimeGiven() + .2);
timeSpent = .2;
numCPU++;
}
if(top.getTimeRequired() <= top.getTimeGiven())
{
top.setTimeExitedCPUQueue(clock);
jobsCompleted++;
timeSpentInWaitQueue += (top.getTimeExitedWQueue() - top.getTimeEnteredWQueue());
timeSpentInCPUqueue += (top.getTimeExitedCPUQueue() - top.getTimeEnteredCPUQueue());
}
else
CPUqueue.push(top);
oneSecond -= timeSpent;
if((clock%60 == 0) && (clock > 600)) //every 60 seconds after the first 10 minutes
{
cout << "After the first 10 minutes:" << endl;
cout << "Time: " << clock << endl;
cout << "Number of jobs in the wait queue: " << waitQueue.size() << endl;
cout << "Number of jobs in the CPU queue: " << CPUqueue.size() << endl;
job temp1 = waitQueue.front();
job temp2 = CPUqueue.front();
cout << "Job number of front wait job: " << temp1.getID() << endl;
cout << "Job number of front CPU job: " << temp2.getID() << endl;
}
else
{
cout << "Job Number: " << jobsCompleted << endl;
cout << "Job ID: " << top.getID() << endl;
cout << "Job Type: " << top.getJobType() << endl;
cout << "Time in CPU Queue: " << timeSpentInCPUqueue << endl;
cout << "Time Entered CPU Queue: " << top.getTimeEnteredCPUQueue() << endl << endl;
}
}
if((oneSecond > 0) && (CPUqueue.empty()))
CPUidle += oneSecond;
}
}
cout << "I/O_bound jobs: " << numIO << endl;
cout << "CPU_bound jobs: " << numCPU << endl;
cout << "*****JOBS COMPLETED: " << jobsCompleted << " *****" << endl << endl;
return 0;
}
And as a less pertinent question, I cannot get my enumerated data types to print out correctly nor my IDs at the very begging to pass into *newJob correctly...
Let's say it's been looping for a while and now jobsCompleted is 499 (and your jobsToComplete is 500). Okay, so this is the last loop right? Yes! But the incrementing of jobsCompleted happens within another nested while loop. So if that nested loop occurs 6 times, jobsCompleted will be 505 and then the outer while loop will end, leaving you with a total number of jobs completed at 505.
Telling you how to fix it would require understanding the logic of your code, but there's a bit too much for me to figure out. Maybe this will help you.
By looking at the code it's clear that this happens because you have a situation like the following
while (x < y) {
...
while (condition) {
...
if (condition) {
++x;
}
}
}
This means that for every outer iteration it may happen that you are incrementing x more than once, so you enter the last iteration (x == 499) and then increment it 6 times while inside the inner loop. You should debug that part of code to understand why it happens, explicitly you should check these two conditions:
while((oneSecond > 0.0) && (!CPUqueue.empty()))
if(top.getTimeRequired() <= top.getTimeGiven())
because on last iteration the are both true at least 6 times.