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.
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;
};
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);
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;
}
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]();