|
I am very new to the C++ programming scene and I am kind of lost on this one and need some help. I will post the code I have so far and maybe somebody can set me straight as to what I am doing wrong. The program should use a random number to simulate the first die and a second random number to simulate a second die. Then the program should calculate the sum of those two numbers. Each die can show an interger value from 1 to 6, so the sum of the two values will vary from 2 to 12, with 7 being the most frequent sum and 2 and 12 being the least frequent sums. The program should roll the dice 36,000 times. It should use a single-subscripted array to tally the numbers of times each possible sum appears. The results should be in tabular format. This one is really throwing me so if somebody could show me the right way to do this it would be a big relief.
#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
int _tmain()
{
Random *randomNumber = new Random();
int die1,
die2;
int frequency __gc[] = new int __gc[13];
double total;
//roll dice, calculate sum and display results
int RollDice(Random *randomNumber)
{
int die1, die2, workSum;
die1 = randomNumber->Next(1, 7);
die2 = randomNumber->Next(1, 7);
workSum = die1 + die2;
Console::WriteLine(S"Player rolled {0} + {1} = {2}",
die1.ToString(), die2.ToString(), workSum.ToString() );
return workSum;
} //end function RollDice
for (int roll = 1; roll <= 36000; roll++){
die1 = randomNumber->Next(2, 13);
++frequency[die1];
die2 = randomNumber->Next(2, 13);
++frequency[die2];
} //end of loop
total = 0;
for (int i = 1; i < 13; i++)
total += frequency[i];
Console::WriteLine(S"Face\tFrequency\tPercent");
//output frequency values
for (int x = 1; x < frequency->Length; x++)
Console::WriteLine(S"{0}\t{1}\t\t{2}%", x.ToString(),
frequency[x].ToString(),
(frequency[x]/total * 100).ToString("N") );
return 0;
} //end _tmain
|
|
|
|
|
bartman1437 wrote:
[Description of what seems to be homework snipped]
This one is really throwing me so if somebody could show me the right way to do this it would be a big relief.
What's exactly the problem you are experiencing? Don't expect anyone to do your homework. Post specific questions and somebody might help.
Do you actually have the entire RollDice function defined inside the _tmain function, or was just an error while pasting the code in your post? Where is RollDice being called?
--
jlr
http://jlamas.blogspot.com/[^]
|
|
|
|
|
I didn't ask anybody to do it for me. What I asked for was some help in doing it the right way. Not some egomaniac full of himself who just wants to make it hard on somebody trying to learn the right way.
Anyway, I did it myself, it was less aggravation than dealing with people like you.
#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
int _tmain()
{
//initializing variables
Random *randomNumber = new Random();
int face1;
int face2;
int sum;
const int ARRAY_SIZE = 13;
int frequency__gc[ARRAY_SIZE];
double total;
//simulating the rolling of the two dice
for (int roll = 1; roll<=36000; roll++)
{
face1 = randomNumber->Next(1,7);
face2 = randomNumber->Next(1,7);
sum = face1 + face2;
++frequency__gc[sum];
}
total = 0;
for (int i= 1; i < 13; i++)
total += frequency__gc[ i ];
Console::WriteLine(S"Sum:\tFrequency:");
//output the sum and frequency values
for (int x = 2; x < ARRAY_SIZE; x++)
Console::WriteLine(S"{0}\t{1}", x.ToString(),
frequency__gc[ x ].ToString());
return 0;
}
|
|
|
|
|
bartman1437 wrote:
I didn't ask anybody to do it for me. What I asked for was some help in doing it the right way.
Just to put this into context, what you did was to describe your homework assignment, along with 50+ lines of code and this phrase:
bartman1437 wrote:
This one is really throwing me so if somebody could show me the right way to do this it would be a big relief.
That is, no indication of what exactly was your problem, what errors you were getting, or any specific question; just a general "if somebody could show me the right way to do this."
My response was precisely pointing that out, and although in a shorter way, not very different than what is said in this FAQ - What's the best way to ask a question about code, so that I get a good answer?[^]. For the record, these were my exact words:
Jose Lamas Rios wrote:
What's exactly the problem you are experiencing? Don't expect anyone to do your homework. Post specific questions and somebody might help.
Concise and to the point. I don't think anyone would take offense to that; it wasn't my intention in any case.
After that, I nonetheless suggested you some things that, on a quick first sight, seemed wrong. I was even polite enough to allow for the possibility that the error was only at composing your post and not in the actual code:
Jose Lamas Rios wrote:
Do you actually have the entire RollDice function defined inside the _tmain function, or was just an error while pasting the code in your post? Where is RollDice being called?
And that was it. Nothing else was in my response to your post. Granted, I didn't take the time to cut and paste your post into a source file, compile it, fix whatever errors it had, and present it back to you as working code. But I don't think I was expected to do that, was I?
So, it was a big surprise for me to read the following in your reply:
bartman1437 wrote:
Not some egomaniac full of himself who just wants to make it hard on somebody trying to learn the right way. Anyway, I did it myself, it was less aggravation than dealing with people like you.
You did it yourself? Good! That was the idea in the first place. As for the rest, you won't get any further response. I don't get into name calling or flame fests.
Good luck,
--
jlr
http://jlamas.blogspot.com/[^]
|
|
|
|
|
in my app, there are many files. when I pause, it is not inside file1.cpp, but stoped in file2.cpp.
I think this matters. if it stoped in file1.cpp I can see this variable.
Best
|
|
|
|
|
What on earth are you talking about ?
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
I think it has to do with his other post below.
<font=arial>Weiye Chen
Life is hard, yet we are made of flesh...
|
|
|
|
|
Yeah, I guessed that was probably the case. It made no sense to me, and I'm sure a lot of other people, due to the nature of the post. He should comment under his current post, obviously.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Hello Graus,
Can you please provide me some suggestions regarding VC++, MFC as I want to learn more of C++ and MFC. I am already into VC++,MFC but wants to learn more so that I can help people in solving their problems. If possible, can you provide me ur MSN mail id so that when time permits I can learn from you.
Thanking you,
Regards,
Neelesh K J Jain.
|
|
|
|
|
Neelesh K J Jain wrote:
Can you please provide me some suggestions regarding VC++, MFC as I want to learn more of C++ and MFC.
There are tons of MFC articles on this site, just go to the articles section.
Neelesh K J Jain wrote:
If possible, can you provide me ur MSN mail id so that when time permits I can learn from you.
I answer about 20-40 questions a day. Can you imagine if I gave all those people my MSN ? But by all means, ask here and I'll be one of many people happy to help you.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Try breakpoints..?
Behind every great black man...
... is the police. - Conspiracy brother
Blog[^]
|
|
|
|
|
Sorry for my confusing question. And I am glad that smart people figured it out. Thank you for all your guys warm reply.
|
|
|
|
|
buf_balance is a globel variable. when defined in file1.cpp
int buf_balance;
then i run app in debug mode. I pause the app, want to check the value of buf_balance. but I got error in my watch list.
Why can't I see this variable?
Thank you in advance.
Richard Luo
|
|
|
|
|
I tried this in VC6, but it shows the value of global variables correctly...
did you pause the app or set a breakpoint? try with a breakpoint at the place you want to debug (or use DebugBreak() )
also try rebuilding everything and try again
- Indivara
"...This city desert makes you feel so cold.
It's got so many people but it's got no soul..."
- Gerry Rafferty, Baker Street
|
|
|
|
|
replying to your post in a separate thread above,
in my app, there are many files. when I pause, it is not inside file1.cpp, but stoped in file2.cpp.
I think this matters. if it stoped in file1.cpp I can see this variable.
that shouldn't make a difference. I could see global variables in the watch window no matter which file the debugger is stopped in
try restoring project settings to default in Project - Settings, C/C++ & Link tabs, settings related to debug information
(my OS is not in english, so I can't give the exact settings...)
- Indivara
"...This city desert makes you feel so cold.
It's got so many people but it's got no soul..."
- Gerry Rafferty, Baker Street
|
|
|
|
|
In don't think that you gave it global scope. Did you use an extern declaration in the headerfile? Did you include the definition in file2.cpp? The reason why the debugger can't see your variable is that it does not know its definition.
Behind every great black man...
... is the police. - Conspiracy brother
Blog[^]
|
|
|
|
|
Hi...
This feels like a stupid question, but does anybody know why it is better to use a loop like this to advance a pointer
int i;
BYTE *pointer; int count; int increment;
for (i = 0; i < count; i++) {
pointer += increment;
}
than simply advance the pointer in a single step
pointer += increment * count;
I found this in some code which used to run in both user mode and kernel mode. I think it was written by an experienced programmer, and probably isn't a mere ...
- Indivara
"...This city desert makes you feel so cold.
It's got so many people but it's got no soul..."
- Gerry Rafferty, Baker Street
|
|
|
|
|
I can see some differences between them:
1. (increment * count) might overflow in cases in which the for loop would work without problems
2. If count is negative, the for loop won't alter the pointer ; in the alternative, it will always be modified unless either increment or count is zero.
Still, even if I wanted to cover those cases, I wouldn't do it that way. Are you sure that's exactly how the code was written? Maybe if you give us some more context?
--
jlr
http://jlamas.blogspot.com/[^]
|
|
|
|
|
Jose Lamas Rios wrote:
1. (increment * count) might overflow in cases in which the for loop would work without problems
I don't think overflow is a problem. all values are well within range for all practical purposes
Jose Lamas Rios wrote:
2. If count is negative, the for loop won't alter the pointer; in the alternative, it will always be modified unless either increment or count is zero.
I didn't think of that. increment can in fact be either positive or negative (intentionally, to move the pointer forward or backward) but as you say, there are better ways, like using the absolute value of count
Jose Lamas Rios wrote:
Are you sure that's exactly how the code was written? Maybe if you give us some more context?
this is a part of code used to copy data from a bitmap. the pointer is the bitmap buffer, and increment is the number of bytes to move to the next line. count is used to skip certain lines.
and it was written like this
if (count != 0) {
if (count == 1) {
pointer += increment;
} else {
for (i = 0; i < count; i++) {
pointer += increment;
}
}
}
I skipped the first if conditions in the original post...
they are used because count is often 0 or 1 (I think), but why use the loop?
- Indivara
"...This city desert makes you feel so cold.
It's got so many people but it's got no soul..."
- Gerry Rafferty, Baker Street
|
|
|
|
|
Indivara wrote:
Jose Lamas Rios wrote:
1. (increment * count) might overflow in cases in which the for loop would work without problems
I don't think overflow is a problem. all values are well within range for all practical purposes
If you use bitmaps, you never know how large your data will be. One should make his / her code as robust as possible. There is one golden rule that programmers should honer always: user data is malicious until proven otherwise! So extreme caution should be taken when using pointers and data from outside the program.
Indivara wrote:
I skipped the first if conditions in the original post...
they are used because count is often 0 or 1 (I think), but why use the loop?
I wonder why the programmer used the if 's..? The following code does exactly the same:
for( int i = 0; i < count; i++ )
pointer += increment;
But indeed, maybe for performance reasons he used the if 's.
Behind every great black man...
... is the police. - Conspiracy brother
Blog[^]
|
|
|
|
|
Bob Stanneveld wrote:
If you use bitmaps, you never know how large your data will be. One should make his / her code as robust as possible. There is one golden rule that programmers should honer always: user data is malicious until proven otherwise! So extreme caution should be taken when using pointers and data from outside the program.
you are right about that, I've had countless problems due to assumptions about user data..
... however in this case, I doubt the programmer had that in mind when writing this code. if this were to overflow, the bitmap would be so large that the system would run out of memory long before...
there must be some other reason for the loop
Bob Stanneveld wrote:
But indeed, maybe for performance reasons he used the if's.
probably so. I was wondering whether the loop was there for the same reason, but can't figure out why it would be faster...
- Indivara
"...This city desert makes you feel so cold.
It's got so many people but it's got no soul..."
- Gerry Rafferty, Baker Street
|
|
|
|
|
Indivara wrote:
I was wondering whether the loop was there for the same reason, but can't figure out why it would be faster...
Take the following scenario where a loop would run exactly one time:
<list> Step 1: initialize the loop counter
Step 2: test the expression: i < count (0 < 1 == true)
Step 3: execute body
Step 4: increment i: i++ (i == 1)
Step 5: test the expression: i < count (1 < 1 == false)
Step 6: leave loop
If you do the test of iterating one time, you save steps 1, 4, 5 and 6. You still have to test once, so that uses the same CPU cycles as step and and the body is also executed once. So if count equals one often enough, it may save a lot of CPU cycles. If count is not equal to zero or one, it causes the loop run acutally slower.
Back in the old C days (especially with image processing and computer games) everybody was optimizing as much as they could. Nowdays the compiler often does a better job than we at optimizing.
Behind every great black man...
... is the police. - Conspiracy brother
Blog[^]
|
|
|
|
|
I'm sorry, I think I didn't write clearly...
what I meant was, I wondered whether the loop would be faster than simply adding count * increment to the pointer under certain conditions (like maybe in kernel mode?)
Bob Stanneveld wrote:
Back in the old C days
maybe this is left over from the old C days...
- Indivara
"...This city desert makes you feel so cold.
It's got so many people but it's got no soul..."
- Gerry Rafferty, Baker Street
|
|
|
|
|
That goes back to trusting the data...
Behind every great black man...
... is the police. - Conspiracy brother
Blog[^]
|
|
|
|
|
hi,
i have a dialog window with some controls on it. the programme changes the size and position of these controls. and there is my problem: if i can not see all the controls on the window, i want to scroll the dialog. but i don't know how. maybe some of you can help me.
thx, g0ju
|
|
|
|
|