free geoip

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PROBLOM IN PROGRAMMING code 2011
VU Help • House Banner VU Help Virtual University Students - Assignments, Solved Papers, GDB & Exam Help Student Help Forum • Collaborative • Free Support
#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,320 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,972 01-04-2012, 10:17 PM
Last Post: Vuhelper
  array project code c++ 2011 Vuhelper 0 1,319 12-10-2011, 06:32 PM
Last Post: Vuhelper
  factorial in C++ code 2011 Vuhelper 0 992 12-10-2011, 06:31 PM
Last Post: Vuhelper
  stack during function call in C++ 2011 Vuhelper 0 2,068 11-05-2011, 05:05 PM
Last Post: Vuhelper
  What is the Thing Queue? in c++ 2011 Vuhelper 0 1,821 11-05-2011, 05:02 PM
Last Post: Vuhelper
  ASCII to char conversion code in C++ 2011 Vuhelper 0 1,765 11-05-2011, 05:01 PM
Last Post: Vuhelper
  How to write a summation in a sequence? C++ 2011-12 Vuhelper 2 1,411 09-19-2011, 04:53 AM
Last Post: Xpert
  GetPixel problem in c++ 2011-12 Vuhelper 0 2,540 09-18-2011, 07:12 PM
Last Post: Vuhelper
  C++ with loop code 2011-12 Vuhelper 0 1,022 09-18-2011, 07:09 PM
Last Post: Vuhelper

Forum Jump:


Users browsing this thread: 2 Guest(s)
VU Help • House Banner VU Help Virtual University Students - Assignments, Solved Papers, GDB & Exam Help Student Help Forum • Collaborative • Free Support