My variables black1 and black2 giving me the wrong answers in c++? - c++

My professor is forcing me to use a program called RAPTOR, which preety much creates and executes flow charts and psuedocode. And I'm stuck converting my flowchart, insted of writing in the actual language. I finally got my program to compile, but now it wont give me the same results as in the RAPTOR program, and my black1 variable keeps returning 0, where as black2 keeps spitting out a number like 1,000. Anyways any help would be greatly appreciated thank you!
Here's the code:
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
{
int trials;
int first;
string bag;
int j;
float percent;
string temp;
int black2;
int runs;
int l;
int black1;
int firstdraw;
int c;
string possible;
int seconddraw;
srand(time(NULL));
l = bag.length();
int r = rand() % 4;
possible ="bw";
runs =0;
while (!(runs>9))
{
black1 =0;
black2 =0;
runs =runs+1;
trials =0;
while (!(trials==1000))
{
trials =trials+1;
bag ="bbwww";
l = bag.length();
c =1;
temp =" ";
while (!(c==5))
{
j = r ;
temp[1] = bag[c];
bag[c] = bag[j];
bag[j] = temp[1];
c =c+1;
}
c =1;
j = r;
firstdraw =bag[j];
if (firstdraw==possible[1])
{
black1 = black1+1;
first = 1;
}
else
{
first = 0;
}
while (!(j>l-1))
{
bag[j] = bag[j + 1];
j =j+1;
}
l =l-1;
j = r;
seconddraw = bag[j];
if (seconddraw==possible[1] && first==1)
{
black2 =black2+1;
}
else
{
}
}
percent = 100.0 * black2/black1;
cout << black2 << " " << black1 << endl;
cout << "Percentage for run #" << runs << " and " << trials << " trials: %" << percent << endl; }
return 0;
}

I'm sorry but I won't fix your whole code, I made it more readable, removed that ugly using namespace std; and left some comments for you. Check this out, fix your code, and take a look into some tutorials if needed. I've gotten a debug assertion over and over again with this code. Guessing you never runned any debugger and just had Release mode on.
Also don't forget to initialize the variables. You risk undefined behaviour.
These two strings walk into a bar and sit down. The bartender says, "So what'll it be?"
The first string says, "I think I'll have a beer quag fulk boorg jdk^CjfdLk jk3s d#f67howe%^U r89nvy~~owmc63^Dz x.xvcu"
"Please excuse my friend," the second string says, "He isn't null-terminated."
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <time.h>
#include <stdlib.h>
int main()
{
int trials = 0;
int first = 0;
int c = 0;
int j = 0;
int l = 0;
int black2 = 0;
int runs = 0;
int black1 = 0;
int firstdraw = 0;
int seconddraw = 0;
float percent = 0.0f;
std::string temp = "\0";
std::string bag = "\0";
std::string possible = "\0";
srand (time(NULL));
l = bag.length();
fflush(stdin); // You may want to do this to get random numbers from rand() otherwise it may use the same number over and over again
int r = 0;
r = rand() % 4;
possible = "bw";
runs = 0;
while (!(runs > 9))
{
black1 = 0;
black2 = 0;
runs = runs + 1; //Why no for loop ?
trials = 0;
while (!(trials == 1000))
{
trials = trials + 1; //Why no for loop ?
bag ="bbwww";
l = bag.length();
c = 1;
temp =" ";
while (!(c == 5))
{
j = r;
temp[1] = bag[c]; // temp[1] <- debug assertion, can't work
bag[c] = bag[j];
bag[j] = temp[1]; // temp[1] <- debug assertion, can't work either
c = c + 1;
}
c = 1;
j = r;
firstdraw = bag[j];
if (firstdraw == possible[1])
{
black1 = black1 + 1;
first = 1;
}
else
{
first = 0;
}
while (!(j > l - 1))
{
bag[j] = bag[j + 1]; //Will most likely cause a debug assertion too...
j = j + 1;
}
l = l - 1;
j = r;
seconddraw = bag[j];
if (seconddraw == possible[1] && first==1)
{
black2 = black2 + 1;
}
else
{
//Not needed !?!
}
}
percent = 100.0 * black2 / black1;
std::cout << black2 << " " << black1 << std::endl;
std::cout << "Percentage for run #" << runs << " and " << trials << " trials: %" << percent << std::endl;
}
return 0;
}

Related

How to add two arrays in C++ from two separate for loops

I am reasonably new to programming and I was wondering how I would add arrays 'adv' and 'rcd' together. I'm assuming that the issue is because of arrays 'adv' and 'rcd' are only recognised inside the respective 'for' loops. Any help would be greatly appreciated.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <new>
#include <ctime>
#include <string>
#include <sstream>
#include <vector>
const double pi = 3.1415926535897932384626433832795;
const double convToRad = pi/180.0;
int index1 = 180;
int index2 = 0;
int index3 = 0;
int o, u, w;
int main(){
double rcd[index1];
for (int o = index1; o >= 90; o--){
rcd[o] = o*convToRad;
//std::cout << rcd[o] << std::endl;
}
double adv[index2];
for (int u = index2; u <= 90; u++){
adv[u] = u*convToRad;
//std::cout << adv[u] << std::endl;
}
double car[index3];
for (int w = index3; w <= 90; w++){
car[w] = (adv[u]+rcd[o])/2;
std::cout << car[w] << std::endl;
}
return 0;
}
You've attempted to declare the array double car[index3] when index3 was set to zero, in other words, the array car[index3] is defined as: car[0]. Same with double adv[index2].
A correct example to do so:
#include <iostream>
const int MAX = 10;
int main(void) {
double arr1[MAX] {0};
double arr2[MAX] {0};
double arrSum[MAX] {0};
for (int i = 0; i < MAX; i++) {
// example
arr1[i] = i + 4;
// std::cout << arr1[i] << ' ';
}
std::cout << std::endl;
for (int i = 0; i < MAX; i++) {
// example
arr2[i] = i + 1;
// std::cout << arr2[i] << ' ';
}
std::cout << std::endl;
for (int i = 0; i < MAX; i++) {
arrSum[i] = (arr1[i] + arr2[i]) / 2;
std::cout << arrSum[i] << ' ';
}
std::cout << std::endl;
return 0;
}
Notice that it doesn't solves your exact problem since I only given a code which does the work you want, but yes, it'll guide you how to solve it correctly.
Sample Output:
2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5 10.5 11.5
// sum of arr1[1] + arr2[1] <space> arr1[2] + arr2[2] ...
// uncomment the cout syntax to see further output
Your code has multiple errors.
int index1 = 180;
int index2 = 0;
int index3 = 0;
When you are initializing arrays rcd is made of size 180, where as adv is of size 0.
for (int w = index3; w <= 90; w++){
car[w] = (adv[u]+rcd[o])/2;
std::cout << car[w] << std::endl;
}
In the loop values of u and o are constant.

generate random numbers in c ++ for small intervals

I'm working on a program to calculate the odds of a poker game, it's in process. I found how to generate random numbers but these random numbers depend on time and are not appropriate for generating random numbers in a small interval. I would like to know how I can generate random numbers without having to depend on computer time.
#include <cstdlib>
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
using namespace std;
int main() {
srand(time(NULL));
int N = 1000, T=100;
int j;
float tcouple = 0, ttrio = 0, tfull = 0, tpoker = 0, trien = 0;
struct Lettre{ int numero; char baton;};
Lettre lettre[5];
for(int a = 0; a < T; a++)
{
int couple = 0, trio = 0, full = 0, poker = 0;
for(int i = 0; i< N; i++){
int d = 0 ;
for(j = 0; j < 5; j++)
{
int r = 0;
lettre[j].numero = (1 + rand() % 13);
r = (1 + rand() % 4);
switch(r)
{
case 1:
lettre[j].baton = 'T';
break;
case 2:
lettre[j].baton = 'P';
break;
case 3:
lettre[j].baton = 'C';
break;
case 4:
lettre[j].baton = 'D';
break;
}
}
for(int l = 0; l < 4; l++)
{
for(int k = l + 1; k<5; k++)
{
if(lettre[l].numero == lettre[k].numero)
d = d + 1;
}
}
if (d == 1)
couple = couple + 1;
if (d == 3)
trio = trio + 1;
if(d == 4)
full = full + 1;
if(d==6)
poker = poker + 1;
}
tcouple = tcouple + couple;
ttrio = ttrio + trio;
tfull = tfull + full;
tpoker = tpoker + poker;
}
trien=(T*N)-(tcouple+ttrio+tfull+tpoker);
cout << "probabilite couple: " << tcouple/(T*N) <<endl;
cout << "probabilite trio: " << ttrio/(T*N) <<endl;
cout << "probabilite full: " << tfull/(T*N) <<endl;
cout << "probabilite poker: " << tpoker/(T*N) <<endl;
cout << "probabilite rien: " << trien/(T*N) << endl;
return 0;
}
You may want to keep a random numbers pool, which is filled at start or once a time period. It should be big enough so every time you get new random value from it it was a new one. In this case you may use uniform_int_distribution as Timo suggested or even rand.
struct RandPool
{
void Generate()
{
srand(time(nullptr));
for (int i = 0; i < 1000'000; ++i)
mNumbers.push_back(rand());
mIndex = 0;
}
int Next()
{
return mNumbers[mIndex++];
}
private:
int mIndex = 0;
std::vector<int> mNumbers;
};

Run Time Check failure # 2 - Stack around variable 'ary' was corrupted. Why?

I'm new, don't know what I'm doing.
The compile warnings are on and do not show any warnings. Executable pops up and alerts of Run Time Check Failure #2.
Help would be appreciated as to why this is happening.
#include <iostream>
#include <string>
using namespace std;
class romanType {
public:
string strg;
void inputRoman(int ary[]);
//void CalculateRoman(int ary[]);
//void outputRoman(int total);
};
int main()
{
int M = 1000;
int D = 500;
int C = 100;
int L = 50;
int X = 10;
int V = 5;
int I = 1;
romanType numerals;
int ary[50];
cout << "This is to convert your input of Roman numerals to a positiver integer" << endl;
cout << "When prompted, do as you're told" << endl;
numerals.inputRoman(&ary[50]);
// numerals.CalculateRoman(&input[50]);
return 0;
}
void romanType::inputRoman(int ary[])
{
string strg;
int array_size;
int i;
cout << "Input the an appropriate Roman Numeral value" << endl;
cin >> strg;
array_size = strg.length();
for (i = 0; i < array_size; i++)
{
ary[i] = strg[i];
}
}
/*
void romanType::CalculateRoman(int ary[])
{
int total = 0;
int i;
for (i=0; i < 50 ; i++){
if (ary[i] < (ary[i + 1])){
total = total + (ary[i + 1] - ary[i]);
}
else {
total = total + ary[i];
}
}
cout << "Your conversion should equal " << total << endl;
}
*/`
&ary[50] is the address of 51st element of ary, which means it points just after the last element of ary. Change it to ary:
numerals.inputRoman(ary);

Placing Random String values into an int Array

I am pretty newish at c++ and was wondering how I can place values randomly into an int array.What I want to do is use this function to place into a grid randomly for the purpose of creating a memory matching game.
#include "stdafx.h"
#define NOMINMAX
#include <iostream>
#include <iomanip>
#include <windows.h>
#include <time.h>
#include <string>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
void shuffleL(int [][8]);
int main()
{
//shuffles characters
shuffleS(charactersS);
for (int i = 0; i <= 8; i++)
{
cout << "---";
}
cout << endl;
//output grid
for (int r = 0; r < 4; r++)
{
cout << r + 1 << " | ";
for (int c = 0; c < 4; c++)
{
cout << " [VGC] ";
status[r][c] = false;
}
cout << endl;
}
cout << endl;
}
void shuffleL(int characters[][8])
{
string vgc[50] = { "PacMan", "Laura", "Mario", "Sonic", "Link", "Snake", "Drake", "Samus", "MegaMan", "Kratos",
"Isaac", "DK", "Dante", "Crash", "Spyro", "Kirby", "Ryu", "Yoshi", "Sora", "Strider",
"DigDug", "Lil_Mac", "Pit", "Booker", "Rayman", "Frogger", "Marcus", "Shepard", "Sly", "Ezio",
"Guybrush", "Leon", "Raz", "Ninten", "Ralph", "Crono", "MaxPayne", "Fox", "Simon", "Cole",
"Pheonix", "Corvo", "Parappa", "Faith", "Lucas", "Scorpion", "Gordon", "Roland", "Chell", "Olimar" };
string temp;
for (int s = 0; s <= 4; s++)
{
for (int x = 0; x<16; x++)
{
srand((unsigned)time(NULL));
int i = rand() % 15 + 1;
temp = vgc[x];
vgc[x] = vgc[i];
vgc[i] = temp;
}
}
int i = 0;
//Input of Values in Here
for (int r = 0; r < 50; r++)
{
for (int c = 0; c < 50; c++)
{
characters[r][c] = vgc[i]; //THIS VGC GIVES ME THE ERROR
cout << characters[r][c];
i = i + 1;
}
cout << endl;
}
}
It also gives me an error for one of the variableNames (vgc) saying
1 IntelliSense: no suitable conversion function from "std::string" to "int" exist
I am completely stumped on how to fix this solution.
Dont do int characters [] [8]; , but string characters [] [8]; . The type you specify at the start of the array declaration is the type of info the array will store.
Peace.
The problem is, you are trying to store string in your int type array.
for (int c = 0; c < 50; c++)
{
characters[r][c] = vgc[i]; //<- **Here**
cout << characters[r][c];
i = i + 1;
}

C++ generating a pascal triangle, wrong output

I have a problem with generating a pascal triangle in c++, same algorithm works good in java and in c++ it only works for the first two numbers of every line of the triangle in any other it generates way to big numbers. For example in java it generates:
1 5 10 10 5 1 and in C++: 1 5 1233124 1241241585 32523523500 etc
Here is code:
#include <cstdlib>
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
class Pascal {
private:
int* tab;
int prev1;
int prev2;
public:
Pascal(int n) {
tab = new int[n+1];
prev1=0;
prev2=0;
for(int i = 0; i <= n; i++) {
for(int k = 0; k <= i; k++) {
if (k == 0) {
tab[k] = 1;
prev2 = 1;
} else {
prev1 = tab[k-1] + tab[k];
tab[k-1] = prev2;
prev2 = prev1;
}
}
}
}
int wspolczynnik(int m) {
return tab[m];
}
};
int main (int argc, char* argv[]) {
int n = 0, m = 0;
n = atoi(argv[1]); // konwersja string na int
if (n >= 0)
for (int i = 2; i < argc; i++) {
Pascal *wiersz = new Pascal(n);
m = atoi(argv[i]);
int result = wiersz->wspolczynnik(m);
if (m < 0 || m > n)
cout << m << " - element poza zakresem" << endl;
else
cout << m << " : " << result << endl;
delete[] wiersz;
}
return 0;
}
See if initializing the tab array helps:
tab = new int[n+1]();