| | |
for loop
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Mar 2009
Posts: 3
Reputation:
Solved Threads: 0
greetings everyone,
i seem to be having an issue with a function that has a reference n a value to do a loop. the nature of the prog, is to roll a dice (n) times n then count the number of time srand rolls an even num.
for the most part i think it is working--no errors, but inside a function im calling another function n then asking it to loop. And that is where im stuck @.
any suggestions are greatly appreciated.
i seem to be having an issue with a function that has a reference n a value to do a loop. the nature of the prog, is to roll a dice (n) times n then count the number of time srand rolls an even num.
for the most part i think it is working--no errors, but inside a function im calling another function n then asking it to loop. And that is where im stuck @.
C++ Syntax (Toggle Plain Text)
#include <cstdio> #include <cstdlib> #include <ctime> #include <iostream> using namespace std; int simDiceThrow(int, int&); int diceThrow(int&); bool isEvenSided(int&); const long MAX=6, MIN=1; int main () { int diceA, total=0, totalOne=0, pvalue, maxNumber=0; srand((unsigned)time(NULL)); cout <<" Dice Counter\n\n"; maxNumber = 100; simDiceThrow(maxNumber, pvalue); <div class="bbquote"> <div class="tlc"><div class="tli">•</div></div> <div class="blc"><div class="bli">•</div></div> <div class="trc"><div class="tri">•</div></div> <div class="brc"><div class="bri">•</div></div> <blockquote style="font-size:11px">not sure if error is here as the MaxNum should be getting passed to the loop inside the function</blockquote> </div> if (isEvenSided(pvalue) != false) {total++;} maxNumber = 1000; simDiceThrow(maxNumber, pvalue); <div class="bbquote"> <div class="tlc"><div class="tli">•</div></div> <div class="blc"><div class="bli">•</div></div> <div class="trc"><div class="tri">•</div></div> <div class="brc"><div class="bri">•</div></div> <blockquote style="font-size:11px">im wondering how will the if statement run if there is no loop outside??</blockquote> </div> if (isEvenSided(pvalue) != false) {totalOne++;} cout <<"100 x dice thrown " <<" " <<total <<" " <<pvalue; cout <<"\n1000 x dice thrown" <<" " <<totalOne <<" " <<pvalue; _flushall(); cin.get(); return 0; } int diceThrow (int &diceA) { return (diceA = (rand() % (MAX - MIN +1))+ MIN); } bool isEvenSided (int &pvalue) { return ((pvalue%2)== 0); } int simDiceThrow(int maxNumber, int &diceA) { int pvalue; for (int roll = 0; roll < maxNumber; roll++) {pvalue = diceThrow(diceA);} return pvalue; }
any suggestions are greatly appreciated.
C++ Syntax (Toggle Plain Text)
if (isEvenSided(pvalue) != false) {total++;}
How can you compute the total outside the loop ? you should
pass the total also as a reference. And inside the function you
can make it to zero first and add it if even.
First draw a structure chart. Then you can clearly model this.
Last edited by NicAx64; Mar 19th, 2009 at 2:01 am.
•
•
Join Date: Mar 2009
Posts: 3
Reputation:
Solved Threads: 0
alright so after getting some dinner, i decided to redo the whole thing. from the drawing board i came up with the following code, but something is fishie about the count. it runs the loop for the srand and from what i can tell moves the counter but the count is not always right.
that produces some like this:
C++ Syntax (Toggle Plain Text)
#include <cstdio> #include <cstdlib> #include <ctime> #include <iostream> #include <iomanip> using namespace std; int simDiceThrow(int, int&); int diceThrow(); bool isEvenSided(int&); int main () { int total=0, totalOne=0, maxNumber; srand((unsigned)time(NULL)); cout <<" Dice Counter\n\n"; maxNumber = 3; simDiceThrow(maxNumber, total); cout <<"total even for 100: " <<total <<endl; maxNumber = 5; simDiceThrow(maxNumber, totalOne); cout <<"total even for 1000: " <<totalOne <<endl; cin.get(); cin.get(); return 0; } int diceThrow () { int diceA = (rand()% 6+ 1); return diceA; } bool isEvenSided (int &pvalue) { return (pvalue %2 == 0); } int simDiceThrow(int maxNumber, int &total) { int pvalue; for (int roll = 1; roll <= maxNumber; roll++) { cout <<setw(5) <<diceThrow() <<endl; pvalue=diceThrow(); if (isEvenSided(pvalue)) {total++;} } }
that produces some like this:
•
•
•
•
Dice Counter
1
3
2
total even for 3: 1 //this one is correct
6
5
4
4
3
total even for 5: 4 //this one isnt, i am thinking something a zero but dont know where to eliminate such prob
In your simDiceThrow() loop you output the dice values, but then calculate the number of even values by throwing the dice again. So the number of even values has no relation to the numbers previously output. The logic you actually need is to throw the dice n times, remembering each value thrown. Calculate the number of even values, then output the number set and total.
good you have taken some courage .
if you still can't underestand why look at this code and
think why ?
if you still can't underestand why look at this code and
think why ?
C++ Syntax (Toggle Plain Text)
int simDiceThrow(int maxNumber, int &total) { int pvalue; for (int roll = 1; roll <= maxNumber; roll++) { //cout <<setw(5) <<diceThrow() <<endl; pvalue=diceThrow(); cout << setw(5) << pvalue << endl ; if (isEvenSided(pvalue)) {total++;} } }
Nothing like a kernel pannic !
•
•
Join Date: Mar 2009
Posts: 3
Reputation:
Solved Threads: 0
i now see where the error what and why it was, so here is my understanding of it.
num gets generated it then gets passed over to the bool and gets counted if return is true. my first issue was that the bool was not in the loop, then after that was corrected, the value was not passed over to the bool for count. here is the updated function. works great. thank you guys for pushing.
num gets generated it then gets passed over to the bool and gets counted if return is true. my first issue was that the bool was not in the loop, then after that was corrected, the value was not passed over to the bool for count. here is the updated function. works great. thank you guys for pushing.
C++ Syntax (Toggle Plain Text)
int simDiceThrow(int maxNumber, int &total) { for (int roll = 1; roll <= maxNumber; roll++) { int evalue=diceThrow(); cout <<setw(2) <<evalue; if (roll %25 == 0) cout <<endl; //spacing if (isEvenSided(evalue)) {total++;} } }
![]() |
Similar Threads
- Help with gui loop. (C)
- Loop...without the loop (Java)
Other Threads in the C++ Forum
- Previous Thread: Array subscript error: invalid types char[int] (?)
- Next Thread: Dynamic Array to Static Array
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





