Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PROBLOM IN PROGRAMMING code 2011
#1
RESPECTED SIR THIS PROGRAM IS CODED IN OUR TEXT BOOK
SIR I HAD DONE THIS EXAMPLE AND TJIS EXAMPLE RUN SUCCESSFULLY
SIR ONE QUESTION RISES IN MINE MIND THAT WE HAVE DECLARED
THE SIZE OF ARRAY IS GIVEN AS 7 IT MEANS THAT ONLY SEVEN
VALUES CAN BE STORED BUT HERE WE HAD STORED ALL THE VALUES
OF THE NUMBERS GENERATED BY THE RAND FUNCTION

HOW DOES IT HAPPENED KINDLY TELL ME THAT WHAT DOES THIS
LOOP IS DOING
for (roll=1;roll<=600000;roll++)
frequency[1+rand()%6]++;
THE CODE IS SNIPPED THERE

Code:
#include<iostream.h> #include<conio.h> #include<iomanip.h> using std::setw; #include<cstdlib> using std::rand; using std::srand; #include<ctime> using std::time; main() { int roll; int face; const int arraysize=7; int frequency[arraysize]={}; for (roll=1;roll<=600000;roll++) frequency[1+rand()%6]++; cout<<"FACE"; cout<<setw(13); cout<<"FREQUENCY"; cout<<endl; for ( face=1;face<arraysize;face++) { cout<<setw(4); cout<<face; cout<<setw(13); cout<<frequency[face]; cout<<endl; } cout<<getch(); }
Reply
#2
Dear Student,
This program is just like a game where a Die is rolled. We know that a Die consists of 6 numbers from 1 to 6 on its each different face. Here in this program, the objective is to find the number of times each face will come up when that die is rolled 600000 times. Let us look at the logic behind the loop which you are finding dificult to understand.

Code:
for (roll=1;roll<=600000;roll++) frequency[1+rand()%6]++;


In the loop declaration, you can see that loop limit is less than equal to 600000, which means that this loop will be executed 600000 times. In other words, this limits describes the number of times the Die will be rolled. Now lets come to second statement of the above code. We know that a die can only have 6 faces. when the 1+rand()%6 is executed, it always returns a value less than or equal to 6 becuase this value will represent the value of each face of the die.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ASCII code is binary numbers of 0 and 1 in C++ 2012 Vuhelper 0 2,282 01-04-2012, 10:41 PM
Last Post: Vuhelper
  int x[10]; x[0]= 1; x[1]=2; if following code is executable in C++ 2012 Vuhelper 0 1,938 01-04-2012, 10:17 PM
Last Post: Vuhelper
  array project code c++ 2011 Vuhelper 0 1,282 12-10-2011, 06:32 PM
Last Post: Vuhelper
  factorial in C++ code 2011 Vuhelper 0 957 12-10-2011, 06:31 PM
Last Post: Vuhelper
  stack during function call in C++ 2011 Vuhelper 0 2,033 11-05-2011, 05:05 PM
Last Post: Vuhelper
  What is the Thing Queue? in c++ 2011 Vuhelper 0 1,787 11-05-2011, 05:02 PM
Last Post: Vuhelper
  ASCII to char conversion code in C++ 2011 Vuhelper 0 1,727 11-05-2011, 05:01 PM
Last Post: Vuhelper
  How to write a summation in a sequence? C++ 2011-12 Vuhelper 2 1,362 09-19-2011, 04:53 AM
Last Post: Xpert
  GetPixel problem in c++ 2011-12 Vuhelper 0 2,512 09-18-2011, 07:12 PM
Last Post: Vuhelper
  C++ with loop code 2011-12 Vuhelper 0 983 09-18-2011, 07:09 PM
Last Post: Vuhelper

Forum Jump:


Users browsing this thread: 1 Guest(s)