C++ :: Randomly Generating Probabilities - Function Stuck
Oct 29, 2014
I've written some code to randomly generate probabilities:
int Queue::car_per_hr() {
srand( time( NULL ));
int prob = rand() % + 101; // Probability in percentage from 0 to 100
int cars; //no of cars per hr
[Code] ....
So it checks through the range of possibilities: 0-20%, 21-40% etc. I then have this to implement it:
#include <iostream>
#include <ctime>
#include "queue.h"
using namespace std;
int main(){
Queue car_wash;
int NO_HOURS;
[Code] .....
For some reason though, everytime I call the car_per_hr() function, the probability just stays the same. It's output is always locked to the same thing it had to begin with?
View 3 Replies
ADVERTISEMENT
May 1, 2014
How would I generate nodes inside a function for example I have a head node
Node * head = new Node;
Now I want to generate nodes for as long as the for loop is running
void CreateList(Node * h) {
for(int i = 0; i < 5; i++) {
Node * n1 = new Node;
head -> next = n1;
} }
Something like this but it should keep adding nodes to the list and change the head each time so how would I do this...
View 1 Replies
View Related
Nov 14, 2014
for (int i=0; i<15; i++)
{
nx[i]=rand()%8+1;
printf("%d",nx[i]);
}
I want to the function of timer "srand(time(NULL))" to generate seed for random numbers. By running this for loop,I think I should expect random numbers ranging from 1 to 8.However, I get some wried numbers from the console window like 88,044,077,066,088,088,066,022,044,044,088,022,033,66814990522,-156026525933,1606416712. One more thing,I think I am going to have 15 outputs, but why I get 16 instead every time.
View 1 Replies
View Related
Jul 19, 2013
I'm writing a card game and I'm having trouble getting the first part of it right, which is picking out a random card from the deck and displaying it to screen. So it's supposed to display a different card every time i run the function in the program. The problem is, I keep getting the same output every time "Nine of Hearts".
This is what I have so far,
#include <iostream>
using namespace std;
//declare two string arrays
//one holds the "suit" of the cards, the other hold the "value"
string suit[] = {"Diamonds", "Hearts", "Spades", "Clubs"};
string faceValue[] = {"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ace", "King", "Queen", "Jack"};
[Code] ......
This is the output I keep getting every time I run the program:
Nine of Hearts
This "random selection" function I'm trying to do is meant to work as a means of shuffling the cards in the beginning of every round in the game and displaying only the top card from the deck.
View 1 Replies
View Related
Oct 26, 2013
I have a struct called Array and I'm to create a function to create a dynamic array that's fill with randomly generated integers from 0 to 50 (inclusive) and a function to destroy the array for freeing its memory. Below the code that I have written so far.
Code:
* Struct */
typedef struct {int *pArray; //the dynamic array
int length; //the size of the dynamic array}Array;
/* Function to create a dynamic array */
Array *initializeArray (int length) {int i;
}
[code]....
View 7 Replies
View Related
Mar 2, 2015
so my program is quite simple and it works fine when i execute it but when i try to run through it with gdb i get an error. so this is what i get from gdb
Code:
Breakpoint 1, main () at fun.c:4
4 {
(gdb) watch a
Hardware watchpoint 2: a
(gdb) next
6 a=0;b=0;c=0;
(gdb) next
[code]....
View 6 Replies
View Related
Mar 27, 2013
Alright I have a program that needs to calculate different rates of commission for a particular sale
I'm stuck in a loop and I'm not sure how to update the final cout statements as well.
#include <iostream>
#include <cmath>
using namespace std;
[Code].....
and yes those are the actual rates i need to calculate and they are in increments of 5%
View 2 Replies
View Related
Feb 15, 2015
how to ask for the input (className) without getting stuck in the loop. When I remove
char className;
cout << "Enter the class name: " << endl;
cin >> className;
it works fine. When I leave it how it's displayed below it goes in an infinite loop. By the way I think "char" is not the appropriate way to declare className. If I want to output it later, should I changed it to "string"?
#include <iostream>
using namespace std;
int main()
{
[Code].....
View 2 Replies
View Related
Feb 24, 2015
my code seems to enter an infinite loop should a user mistakenly enter a character other than a digit. The code is about selecting from a preset number of options, numbered 1- 4. If the user mistakenly enters a letter for example, instead of a number, the infinite loop kicks in...
View 1 Replies
View Related
Apr 4, 2013
I have a school project where I have to create a class with 2 attributes that default to 1. To verify that they are at 1, I have a cout before any user inputs or value manipulation. It works for the most part. What happens is that if I change the defaults to 17, the cout will retain the old values until I have an unsuccessful build (remove a semicolon from the end of the line). When I have a successful build (put that semicolon back), the cout displays the changed values. Change the values back to 1 and the same thing happens again until I have an unsuccessful build then a successful build. Are the old values getting stuck in memory somewhere?
// Rectangle.h
// Chapter 9_Problem 9.11_Page 412
#include <iostream>
using namespace std;
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle // Rectangle class {
public:
Rectangle(double = 17, double = 17); // default constructor setting values to 1
[code]....
View 10 Replies
View Related
Feb 7, 2015
I want to create a loop that that generates a random number set and gives the user the option to run the loop again for a new number. I have that working but when the loop ends the variable value does not leave the loop. I tried a posttest and pretest loop.
Is it possible to declare/create/alter a variable in a loop and have that value exit the loop to use in the code later? I am still learning about global variables.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
int oneDiceSix() // Roll 1d6
{int oneDiceSix = 1+(rand()%6);
return oneDiceSix;} //end function
[code]....
View 2 Replies
View Related
Apr 18, 2015
So this is my first attempt at actually writing code, I have a little basic core functionality set up and I'm 99% sure I'm doing something very fundamentally wrong with pointers.
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
struct Creature {
int pow;
int tou;
[code] ....
Error generated:
magic.c: In function "main":
magic.c:71:26: warning: initialization from incompatible pointer type [enabled by default]
struct Creaturedb *db = Initialize_creaturedb;
^
magic.c:72:22: warning: initialization from incompatible pointer type [enabled by default]
struct Player *pl = Initialize_player;
^
View 3 Replies
View Related
Jul 18, 2013
Code:
#include<stdlib.h>#include<stdio.h>
#include<unistd.h>
#include<math.h>
int main(void)
{
double N, NG, LG, epsilon, root; // setting all variables to type double
[Code] .....
The goal is to create a program to calculate the square root of a number provided by the user to an error tolerance 0.005
Looking around i fond the Code: fabs(NG - LG) < epsilon); section that was very similar to what i was using, but if this is better im down for that.
My issue, from what i can see, is the updating of the values of LG. if the test for error tolerance fails then LG needs to take on the value of the results of NG.
Now I am not 100% that is the point of failure due to the fact the script does calculate the root properly, but it never exits the program once it reaches the tolerance level.
Code: imac:ENG-3211 user$ ./hw_4_1
Please enter the number you wish to find the square root: 4
4.00 2.500
4.00 2.050
4.00 2.001
4.00 2.000
4.00 2.000
4.00 2.000
4.00 2.000
4.00 2.000
4.00 2.000
And it continues forever until i manually break the program even though the answer was found, in this example, on the 4th loop.
View 8 Replies
View Related
Sep 9, 2012
I currently have a hangman game in the making. Which is giving me debugging issues when I go to pick a letter, it will keep asking for a letter, if I place a break; within the loop it asks for a letter and says you've won. I know all I should need is a couple extra lines somewhere within the code.
/// Play game
public static string playGame() {
Words words = new Words();
[Code].....
View 2 Replies
View Related
Mar 2, 2013
in order to calculate some logarithm approximation I need to make a variable, say s, search through the whole integer numbers.
Code:
int s;
for {;;s++){
}
[Code] ....
since the variable needs to be initialized right? how to go through all the numbers?
View 3 Replies
View Related
Feb 25, 2014
Here is my code and everything works except i can not get the highest array number to be output. it always says 10.
My instructions are:
(1) Create a 10-integer array called data
(2) Set a pointer ptr to point to array data
(3) Output the elements in array data using pointer ptr
(4) Find the largest element in the array data using pointer ptr
#include <iostream>
#include <cstdlib>
using namespace std;
[Code].....
View 4 Replies
View Related
Aug 28, 2014
Working on a project for a c++ class and got a bit stuck.
What I am trying to do is include a class for calculating home square footage in a program I have written for my last project. When I do this, I get errors in codeblocks, on lines 74 and 87, "expected ; before 'box', and 'box' was not declared, respectively. Unsure what my snafu is... I had these two programs separate and both run without errors until I combine the two into one program.
View 2 Replies
View Related
Mar 18, 2014
I'm having trouble getting my loop to work correctly. If I iterate the for loop once it works as expected and displays proper output. When I try to iterate two times or more the program gets stuck in an infinite loop.
testData8.dat:
Code:
12 9
13 756
View 3 Replies
View Related
Oct 21, 2013
I am making a math game program that simple and need to have a user input a name and then that name.txt is brought up and use variables from the text to add to their score. heres my code kinda long.
#include <iostream>
#include <cmath>
#include <cctype>
#include <string>
#include <ctime>
#include <cstdlib>
[Code] ....
View 10 Replies
View Related
May 10, 2014
I am making a MFC application with VC++2012 with receiving data from Serial Port and the data is stored in a Listbox, then after processing (convert into numbers) and get these data to draw a sine wave on the chart. (I intend to make a virtual oscilloscope).
Everything works ok at the beginning, but after receiving about 300 items or above (in the Listbox) the value seem stucking (or lagging) and work very slowly.
I receive data by the SerialPort_DataReceived() method, and process of converting and drawing chart also in this method, I think this makes the system overload!!
View 1 Replies
View Related
Jan 8, 2014
I'm trying to keep track of the size of blocks of memory that a pointer points to. No matter what I do, this code below always outputs the integer 8.
If I change 1000 to 5, I still get 8. If I change it to 0, I get 8... If I change it to -1, I get 8. If I change int *a to double *a, I get 8. If I take away the & symbol, I get 8. If I use *& instead, I get 8.
Why? I want it to output 1000. If I change that to 500, I want it to output 500.
int *a;
a = malloc(1000 * sizeof(int));
int j = sizeof(&a);
printf("%d", j);
I want to build my skills where I can allocate, inspect and change memory sizes.
View 4 Replies
View Related
Jan 1, 2013
The program I have is from a tutorial where the user enters two points on a line, and then the program calculates the mid-point and slope.
I want to modify it from it's initial form so that co-ordinates can be input in (x,y) fashion. Right now the user has to enter the x-coordinate, enter a space, and then enter the y-coordinate (lame...)
I saw people using SStream and using that to either write functions to ignore the comma or similar things for converting one file into an array, but not quite what I am trying here.
// program to determine slope of a line
#include <ios>
#include <iostream>
#include <istream>
#include <limits>
using namespace std;
void myflush ( istream& in ) {
[Code] .....
View 2 Replies
View Related
Mar 9, 2013
I have this program for generating subsets, I need to run it with input n=23. It has been running for the past 5 hours, is it normal???
Code:
/*generate subsets */
int subsets(vector < bool > sub, int i)
{
if (i > n) {
return 0;
[Code] .....
View 2 Replies
View Related
Oct 19, 2014
I have a program that generates random numbers. After the random number is generated, the program asks if you want to generate another random number. However, if you generate another random number, it is always the same as the first random number. How can I fix this?
View 5 Replies
View Related
Feb 16, 2013
I want to generate big random numbers in C(not C++ please).By "big" I mean integers much bigger than srand(time(NULL)) and rand() functions' limit(32767).
I tried writing: (note:I am not able to see "code" tag button in this editor,so I am not using it)
//****
int randomnumber;
srand( time(NULL) );
randomnumber = (( rand() % 33 ) * ( rand() % 33 ) * ( rand() % 33) * ( rand() * 33) * (rand() % 33 )) + 1
//****
But I have doubts about it's randomness quality.Also there is another problem,the program can't know the maximum random number it should use before user input,so maximum random number may need to use much smaller maximum random number according to user input.
Is there a better algorithm to create big random numbers in C?
View 2 Replies
View Related
Jun 9, 2013
I'm creating a game in C++ and need to generate random numbers. I know about
int main()
{
srand(time(NULL)); //Initialises randomiser or sum' like that
int x=rand%10; //Generates from 0-9
cout<<x;
}
Now, I need the best way to generate random numbers. Do I call "srand(time(NULL));" every time I want to randomise? What is the best method to generate a nearly perfect random number?
I may need to call a randomiser more than once a second, so taking second as seed (I believe that's what srand(time(NULL)); does).
View 3 Replies
View Related