So basically I am trying to create a loop that fills a matrix with random numbers. I need to make it so every column has a different range, unique to it.
//Variables
int lsx = 3;
int lsy = 10;
int lust[lsy][lsx];
int i = 0;
int l = 0;
int shpp = 7;
//List setup
for (i = 0; i < lsy; i++)
{
for (l = 0; l < lsx; l++)
{
lust[i][l] = 0;
}
}
while (true)
{
//List generator
for (i = 0; i < lsy; i++)
{
for (l = 0; l < lsx; l++)
{
//Column 1
if (i == 0)
{
lust[i][l] = rand() % flx;
cout << lust[i][l] << '\n';
}
//Column 2
if (i == 1)
{
lust[i][l] = rand() % fly;
cout << lust[i][l] << '\n';
}
//Column 3
if (i == 2)
{
lust[i][l] = rand() % shpp;
cout << lust[i][l] << '\n';
}
}
cout << "Endline reached! \n \n";
}
for (i = 0; i < lsy; i++)
{
for (l = 0; l < lsx; l++)
{
cout << lust[i][l] << " ";
}
cout << "\n";
}
}
}
This only generates 3 lines. Does anyone have any ideas on why this could happen?
I tried changing some stuff around but only got weirder results that wouldn't fill the array in completely eitherThis is what the program displays when I try and run it
for (l = 0; l < lsx; l++)
{
Column 1
if (i == 0)
}
lust[i][l] = rand() % flx;
cout << lust[i][l] << '\n';
}
Column 2
if (i == 1)
}
lust[i][l] = rand() % fly;
cout << lust[i][l] << '\n';
}
Column 3
if (i == 2)
{
lust[i][l] = rand() % shpp;
cout << lust[i][l] << '\n';
}
}
cout << "Endline reached! \n \n";
You're using i (the line iterator) to evaluate what you're going to fill. Which means your code will only concern itself with lines 0, 1 and 2. Instead, shift that i to l - your column iterator. It should work.
Also, consider removing the while true loop. Not only is it redundant, it's also pretty dangerous considering there's no break condition - in this case it's safe, since you'll be around to shut it down, but as a good practice stay out of while(true) unless you can't write your break condition as a boolean expression
Related
School assignment for coding a cows and bulls game. Final scoring loops not working and I am not sure what the reason is.
I have tried renaming the vectors, changing iterators, changing where in the code the vectors are declared/initialized (still not sure exactly the difference)
//Get Number to Guess
if (numlen == 0) {
cout << "Enter the number to guess: ";
cin >> num;
cout << "Enter the number of digits in code: ";
cin >> numlen;
numstr = to_string(num);
if (numstr.length() < numlen) {
int diff = numlen - numstr.length();
addz = (diff, "0");
for (int z = 1; z <= diff; ++z) {
numstr = addz + numstr;
}
num = stoi(numstr);
}
vector<int> numvct(numlen, 0);
max1 = 1;
for (l = 1; l < numlen; ++l) {
max1 = max1 * 10;
}
for (j = max1, k = 0; j >= 1, k < numlen; j = j / 10, ++k) {
int addval1 = num / j;
num = num - (addval1 * j);
numvct.at(k) = addval1;
}
cout << "Number to guess: ";
for (r = 0; r < numlen; ++r) {
if (r == (numlen - 1)) {
cout << numvct.at(r) << endl;
}
else {
cout << numvct.at(r) << "-";
}
}
}
else {
//Fill vector to pick from
for (i = 0; i <= 9; ++i) {
pickvct.push_back(i);
}
//Pull to random number
vector<int> numvct(numlen);
for (k = 0; k < numlen; ++k) {
tempnum1 = rand() % (pickvct.size() - 1);
numvct.at(k) = pickvct.at(tempnum1);
pickvct.erase(pickvct.begin() + tempnum1);
}
cout << "Number to guess: ";
for (r = 0; r < numlen; ++r) {
if (r == (numlen - 1)) {
cout << numvct.at(r) << endl;
}
else {
cout << r << "-";
}
}
}
//Get guess
do {
do {
cout << "Enter guess: ";
cin >> guess;
guessstr = to_string(guess);
guesslen = guessstr.length();
if (guesslen < numlen) {
int diff = numlen - guesslen;
addz = (diff, "0");
for (int z = 1; z <= diff; ++z) {
guessstr = addz + guessstr;
}
guess = stoi(guessstr);
guesssame = true;
}
if (guesslen == numlen) {
guesssame = false;
}
while (guesslen > numlen) {
cout << "You can only enter " << numlen << " digits." << endl;
cout << "Enter guess: ";
cin >> guess;
guessstr = to_string(guess);
guesslen = guessstr.length();
}
for (s = 0; s < guesslen; ++s) {
for (t = s + 1; t < guesslen; ++t) {
if (guessstr.at(s) == guessstr.at(t)) {
guesssame = true;
}
else {
guesssame = false;
}
}
}
if (guesssame == true) {
cout << "Each number must be different." << endl;
guesssame = true;
}
} while (guesssame == true);
vector<int> guessvct(guesslen, 0);
max2 = 1;
for (m = 1; m < guesslen; ++m) {
max2 = max2 * 10;
}
for (n = max2, o = 0; n >= 1, o < guesslen; n = n / 10, ++o) {
addval2 = guess / n;
guess = guess - (addval2 * n);
guessvct.at(o) = addval2;
}
//Check the guess
for (p = 0; p < guesslen; ++p) {
guessdigit = guessvct.at(p);
cout << "Guess digit at " << p << ": " << guessdigit << endl;
for (q = 0; q < guesslen; ++q) {
numdigit = numvct.at(q);
cout << "Num digit at " << q << ": " << numdigit << endl;
if (numdigit == guessdigit && q == p) {
bulls = bulls + 1;
if (bulls == numlen) {
win = true;
break;
}
cout << bulls << " bulls" << endl;
}
else {
if (numdigit == guessdigit && q != p) {
cows = cows + 1;
cout << cows << " cows" << endl;
}
}
}
}
} while (win == false);
To make sure the loop was working right I added the cout statements but it is printing the first one only:
Enter guess: ####
Guess digit at 0: #
Program finished
//Get Number to Guess
if (numlen == 0) {
...
}
else {
//Fill vector to pick from
...
}
Within this if else block, you have the following two lines:
vector<int> numvct(numlen, 0);
and
vector<int> numvct(numlen);
These lines declare and initialize vectors which pass out of scope when the program leaves their respective if / else blocks. You use this vector numvct again later, though, so I assume you also declared and initialized it before any of the shown code, and it probably starts off empty. Because those two numvct vectors within the if / else block pass out of scope, all the work you do to them goes away aswell. That means that when you try to use numvct again later, you're working with the (presumably) empty one that you declared at the very beginning of the program.
Instead of redeclaring numvct, try just resizing it:
//vector<int> numvct(numlen);
numvct.resize(numlen);
and
//vector<int> numvct(numlen, 0);
numvct.resize(numlen, 0);
Also you might want to try cutting down on the number of variables you're using here, and try to declare them only within the code blocks where they are really needed. It will make it easier to keep track of what's going on.
Edit: I just want to add that I suggest resizing the vectors, as opposed to any other operation you could perform on a vector, because I don't really know what your exact intended use for them was. I ran your code with these changes, as well as with the addition of ~25 other variable declarations that you did not include in your post, and it did things that seemed reasonable i.e. allowed me to choose a number, guess its digits, print number of cows and bulls etc.
I'm writing a code for projecteuler.net's 7th problem and I kind of succeeded but i need to find the 10001st prime number and what happens is my program crashes if i try to find higher number than 2262 so I'm unable to get the answer I need. I've tried changing int to other data types such as long but that doesn't seem to be a problem here and I'm just stuck now. What do I need to do for my program to not crash when I try to go beyond 2262nd prime number?
int main()
{
int numbersList[10000], prime[10000], rez;
int where1=1, where=0;
bool remainder;
prime[0] = 2;
for(int i = 2; i < INT_MAX; i++)
{
numbersList[where] = i;//2
where++;
for(int j = 0; j < where1; j++)
{
if(i % numbersList[j] != 0)
{
remainder = true;
}
else
{
remainder = false;
break;
}
}
if(remainder)
{
prime[where1] = i;
where1++;
}
if(where1==2262)// Which primary number you want. More or equal to 2263 crashes.
{
rez = prime[where1-1];
break;
}
}
cout << endl << where1 << " primary number: " << rez << endl;
return 0;
}
I think it has a small typo.
Modulus operation will be done using prime array instead of numberList array
int main()
{
int numbersList[1000000], prime[100000], rez;
int where1=1, where=0;
bool remainder;
prime[0] = 2;
for(int i = 2; i < 1000000; i++)
{
numbersList[where] = i;//2
where++;
remainder = true;
for(int j = 0; j < where1; j++)
{
if(i % prime[j] != 0)
{
remainder = true;
}
else
{
remainder = false;
break;
}
}
if(remainder)
{
prime[where1] = i;
where1++;
}
if(where1==10001)//104743
{
rez = prime[where1-1];
break;
}
}
cout << endl << where1 << " primary number: " << rez << endl;
return 0;
}
I am running first for-loop till 10^6 which will be enough to give first 10001st prime. There are 78489 prime from 1 to 1,000,000 numbers.For more details on numbers of prime you can refer this link
#include "../../../std_lib_facilities.h"
int main()
{
vector <int> nmb;
vector <int> rep;
vector <int> prt;
int flag = 0;
int temp = 0;
int br = 0;
int max = -1;
int ind = 0;
cout << "Enter as much integers as you like\n";
while (cin >> temp)
{
if (nmb.size() == 0)
{
nmb.push_back(temp);
prt.push_back(temp);
++rep[br];
++br;
}
else
{
for (int i = 0; i < nmb.size(); ++i)
{
if (temp == nmb[i])
{
++rep[i];
flag = 1;
}
}
if (flag == 0)
{
nmb.push_back(temp);
prt.push_back(temp);
++rep[br];
++br;
}
else if (flag == 1)
{
flag = 0;
prt.push_back(temp);
}
}
}
cout << "You've entered numbers\n";
for (int j = 0; j < prt.size(); ++j)
cout << prt[j] << " ";
for (int k = 0; k < rep.size(); ++k)
if (rep[k] > max)
{
max = rep[k];
ind = k;
}
cout << "\n\nMost repeated number is " << nmb[ind] << endl;}
My task is to write what number has been entered max times. I know it's probably not the best idea but it was the first "good" one I had so I went with it. It compiles fine but gives me that error from that title when running. I tried cout << in few places and it seems that problem starts at the beginning of while loop.
You try to access the first element of rep, which is an empty vector.
You have to actually add elements before you may access them. Right now you're reading from and writing to memory that is not yours.
I have a serious problem that I just can't seem to solve. I apologize if it's a really newbie-like problem.
srand(117);
//enumeration type to keep track of which fish is which
enum CellState {EMPTY, FISH, SHARK};
//Creating the grid
CellState fishGrid[MAX_X][MAX_Y];
for(int m = 0; m < MAX_X; ++m)
{
for(int n = 0; n < MAX_Y; ++n)
{
int num = rand() % 2;
if(num == 0)
fishGrid[m][n] = EMPTY;
else
{
num = rand() % 2;
if(num == 0)
fishGrid[m][n] = SHARK;
else
fishGrid[m][n] = FISH;
}
}
}
//calculate how many on grid
if(stepsTaken % 5 == 0)
{
int fishAmount = 0;
int sharkAmount = 0;
for(int m = 0; m < MAX_X; m++)
{
for(int n = 0; n < MAX_Y; n++)
{
if(fishGrid[m][n] = FISH)
fishAmount++;
else if(fishGrid[m][n] = SHARK)
sharkAmount++;
}
}
cout << "FISH: " << fishAmount << endl
<< "SHARKS: " << sharkAmount << endl << endl;
For some reason, the output is always:
FISH: 100
SHARKS: 0
I don't necessarily understand. Would someone be able to please help me? Thanks.
= designates assignment operator in C++. Which means that what you have here
if(fishGrid[m][n] = FISH)
fishAmount++;
else if(fishGrid[m][n] = SHARK)
sharkAmount++;
uses assignment operator inside if conditions. By the above you override your entire grid with FISH values.
Equality comparison in C++ is performed by == operator. But you already know that, since you already use == in your num comparisons. Why are you using = in these grid comparisons then?
This question already has answers here:
How can I print a list of elements separated by commas?
(34 answers)
Closed 7 years ago.
LANGUAGE: C++
Hello, in the following function (code block) i have written a line to print an space between characters but i don't want print spaces after last characters. How can i solve this problem?
bool perfecto(int n)
{
int suma, i;
suma = 0;
for (i = 1; i < n; i++)
{
if (n % i == 0)
{
suma += i;
cout << i << " ";
}
}
if (suma == n)
return true;
else
return false;
}
Best regards.
Ángel Manuel.
The simplest way would be to turn the problem around: if you only print spaces before printing the number (and not after) then it becomes how not to print the first time, which is much easier.
I'll let you figure it out :)
bool perfecto(int n)
{
int suma, i;
suma = 0;
bool first = true;
for (i = 1; i < n; i++)
{
if (n % i == 0)
{
suma += i;
if ( !first )
{
cout << " ";
}
cout << i;
first = false;
}
}
if (suma == n)
return true;
else
return false;
}
You can either check if i is equal to n - 1 and not print it in that case, or something like
std::cout << "1";
for (int i = 2; i < n; ++i)
{
std::cout << " " << i;
}
In the second case, you have to watch for for a case where n is 1 or less
There are a variety of options. In this case, probably the easiest is to print spaces BEFORE elements except the first and use a flag to track the first element:
bool perfecto(int n)
{
int suma, i;
suma = 0;
bool first = true;
for (i = 1; i < n; i++)
{
if (n % i == 0)
{
suma += i;
if(!first)
{
std::cout << " ";
}
else first = false;
cout << i;
}
}
if (suma == n)
return true;
else
return false;
}
EDIT: Other popular alternatives are printing the first item and no delimiter outside the loop completely and then inside the loop you can always pre-print the item with no if-check at all. This approach wouldn't work as well with your loop though since you don't always know when the first item will print. You can also create wrapper ostream like classes that keep track of their internal printing state and know when to put the spaces.
Replace this line: cout << i << " ";
with:
cout << i;
if (i == n-1)
cout << endl;
else
cout << " ";