I try to use the random function for different things.
For now it works but I get one problem is I want to generate randomly the F in my code but they need to become array after. Also I need to use it and if I need to do it 1 by 1 I think my code will be too long and messy.
Do you know how I can do this?
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main(){
srand(time(0));
//random numbers 1 to 10 for Time:
int t = rand() % 10 + 1 ;
cout << "There is "<< t << " Time;
int* F1 = new int [t];
int* F2 = new int [t];
int* F3 = new int [t];
int* F4 = new int [t];
int* F5 = new int [t];
cout << "Time per F: 0 Not available, 1 available;
//For F1
for(int i = 0; i < t; i++){
//random numbers 0 or 1:
F1[i] = rand() % 2 ;
}
cout << "The Time for F1 is ";
for(int a = 0; a < t; a++){
cout << " "<< F1[a] <<" ";
}
//For F2
for(int j = 0; j < t; j++){
//random numbers 0 or 1:
F2[j] = rand() % 2 ;
}
cout << "The Time slot for F2 is ";
for(int b = 0; b < t; b++){
cout << " "<< F2[b] << " ";
}
return 0;
}
Thank you
Edit: With the solution you give me helps to find the solution
I do int F[em][t];
Since you are using C++, and have access to the standard library, this could be written much cleaner as safer as a std::vector:
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
srand(time(0));
//Random number from 3 to 7
int numberOfVectors = (rand() % 7) + 3;
//Random number from 1 to 10.
int size = (rand() % 10) + 1;
std::cout << "There are " << numberOfVectors << " vectors." << std::endl;
std::cout << "Each vector has " << size << " elements." << std::endl;
std::vector< std::vector<int> > vectorOfVectorOfInt;
std::cout << "Value per element: 0 = Not available, 1 = available" << std::endl;
for(int vec = 0; vec < numberOfVectors; vec++)
{
//Create a new vector with 'size' elements.
std::vector<int> newVector(size);
for(int i = 0; i < size; i++)
{
//Generate a random value between 0 and 50
newVector[i] = (rand() % 50);
}
//Add the vector to our vector-of-vectors.
vectorOfVectorOfInt.push_back(newVector);
std::cout << "The values for Vector #" << (vec+1) << " is:";
for(int b = 0; b < size; b++)
{
int value = vectorOfVectorOfInt[vec][b];
std::cout << "\t" << value;
}
std::cout << std::endl;
}
return 0;
}
You can run it and see the results here: http://ideone.com/RWQhjO
Use an array of array:
#include <ctime>
#include <cstdlib>
using namespace std;
int main(){
srand(time(0));
//random numbers 1 to 10 for Time:
int t = rand() % 10 + 1 ;
cout << "Time per F: 0 Not available, 1 available";
const int f_num = 5;
cout << "There is "<< t << "Time";
int* F = new int*[f_num];
for(int i = 0; i < f_num; i++){
F[i] = new int[t];
cout << "The Time for F"<<i<<" is :";
for(int j = 0; j < t; j++){
//random numbers 0 or 1:
F[i][j] = rand() % 2 ;
cout << " "<< F1[i][j] <<" ";
}
}
}
BTW, your code is C-like. In C++, we usually use std::vector instead of plain array, and http://en.cppreference.com/w/cpp/numeric/random instead of rand() . Also, don't use using namespace std . This can cause name clashes.
I try to use the random function for different things.
Random things, presumably.
For now it works but I get one problem is I want to generate randomly the F in my code but they need to become array after.
You can generate this just as you generated your value for t.
const int number_of_fs = rand() % 10 + 1;
std::cout << "Working with " << number_of_fs << " Fs" << std::endl;
Also I need to use it and if I need to do it 1 by 1 I think my code will be too long and messy. Do you know how I can do this?
Your code is already too long and messy and has memory leaks. You should be using containers from the Standard Containers library, rather than pointers to pointers that never get deleted.
Here is how your code might look were you to use a std::map of std::vectors for instance:
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iterator>
#include <map>
#include <vector>
int main(){
srand(time(0));
//random numbers 1 to 10 for Time:
const int t = rand() % 10 + 1;
std::cout << "There is "<< t << " Time" << std::endl;
const int number_of_fs = rand() % 10 + 1;
std::cout << "Working with " << number_of_fs << " Fs" << std::endl;
std::map<int, std::vector<int> > f;
std::cout << "Time per F: 0 Not available, 1 available" << std::endl;
for (int f_slot = 1; f_slot <= number_of_fs; ++f_slot) {
for(int i = 0; i < t; i++){
//random numbers 0 or 1:
f[f_slot].push_back(rand() % 2);
}
std::cout << "The Time for F" << f_slot << " is " << std::endl;
std::copy(f[f_slot].begin(), f[f_slot].end(),
std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
}
}
See it run!
Population and printing of the contents of these containers should probably be extracted into separate functions, of course, but I didn't want to re-write your code completely.
Related
I am currently working on this program to roll 2 dice. My program works however, for some reason my expected output is coming out as 0.000% for all roll sums instead of what it should be. I am sure I am overlooking something but I have no idea what. Any help is much appreciated!
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
using namespace std;
int main() {
const int ROLLS = 36000;
const int SIZE = 13;
const int CW = 10;
// array 'expected' contains counts for the expected number of times
// each sum occurs in 36 rolls
int expected[SIZE]= {0,0,1/36,1/18,1/12,1/9,5/36,1/6,5/36,1/9,1/12,1/18,1/36};
int sum [SIZE] = {0};
int die1;
int die2;
srand(static_cast<unsigned>(time(nullptr)));
for (int i = 0; i <=ROLLS; ++i) {
die1 = 1 + rand() % 6;
die2 = 1 + rand() % 6;
sum[die1+die2]++;
}
cout << fixed << showpoint << setprecision(3);
cout << setw(CW) << "Sum" << setw(CW) << "Total"
<< setw(CW) << "Expected" << setw(CW) << "Actual" << endl;
for (int j = 2; j < SIZE; ++j) {
cout << setw(CW) << j << setw(CW) << sum[j]
<< setw(CW-1) << (100.0 * expected[j] / 36) << '%'
<< setw(CW-1) << (100.0 * sum[j] / ROLLS) << '%' << endl;
}
return 0;
}
The type of expected is int, which means it's an integer. The results of all of your divisions are truncated to the lowest integer, which is always 0.
You need to:
Declare expected as a real number type, such as float or double
Use the correct literals to make the division produce real numbers, e.g. 5.0f/36 or 5.0/36.
I am working on a homework assignment and when I run my program my calculations are being displayed as -7.40477e+61. I am using visual studio as my IDE and when I check my code on an online checker it displays just fine. I am not sure why everything is being printed in that format. Any advice would be great!
#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>
using namespace std;
int main()
{
double dArr[5];
long lArr[7] = { 100000, 134567, 123456, 9, -234567, -1, 123489 };
int iArr[3][5];
char sName[30] = "fjksdfjls fjklsfjs";
short cnt1, cnt2;
long double total = 0;
double average;
long highest;
srand((unsigned int)time(NULL));
for (int val : dArr) {
dArr[val] = rand() % 100000 + 1;
cout << dArr[val] << endl;
}
for (int count = 0; count < 5; count++) {
total += dArr[count];
average = total / 5;
}
cout << endl;
cout << "The total of the dArr array is " << total << endl;
cout << endl;
cout << "The average of the dArr array is " << average << endl;
cout << endl;
system("pause");
return 0;
}
The range-based for loop:
for (int val : dArr)
iterates val over the values of the collection dArr, not the indexes of that collection. So, when you attempt:
dArr[val] = rand() % 100000 + 1;
within said loop, it's unlikely to to give you the results you expect. Since dArr is local to main, it may have any values in it.
A better way would be to mirror your second loop, with something like:
for (int count = 0; count < 5; count++) {
dArr[val] = rand() % 100000 + 1;
cout << dArr[val] << endl;
}
Having said that, there appears to be no real reason why you're storing these numbers in an array at all (unless there's something about that in the problem statement that isn't shared in this question).
All you really need to do is keep the total, and the count so you can work out the mean. That could be as simple as (I've also changed the code to use Herb Sutter's AAA style, "almost always auto"):
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main() {
const auto count = 5U;
srand((unsigned int)time(NULL));
auto total = 0.0L;
for (auto index = 0U; index < count; ++index) {
const auto value = rand() % 100000 + 1;
cout << value << "\n";
total += value;
}
const auto average = total / count;
cout << "\nThe total of the dArr array is " << total << "\n";
cout << "The average of the dArr array is " << average << "\n\n";
return 0;
}
I am having this terminal error every time I finish the debug program.
What I am doing:
[this program is a simple Lottery Numbers Comparison between user input numbers with the non-repeated random lottery numbers. e.g. using what if it got 4 right of 6]
but it turns out that the program is not working or at least, be stable.
Here's my code:
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <time.h>
#include <ctime>
#include <algorithm>
using namespace std;
int main()
{
cout << "[La Loteria Electronica]\n";
cout << "Escoge 6 n" << char(163) << "meros del (1 al 49): \n";
int numberchoices[] = { 0 };
for (int w = 1; w < 7; w++)
{
cout << "N" << char(163) << "mero #" << w << ": ";
cin >> numberchoices[w];
} // user numbers
//lottery numbers
int i, j, k, nums[51];
srand((int)time(0));
for (i = 1; i < 50; i++) nums[i] = i;
for (i = 1; i < 50; i++)
{
j = (rand() % 49) + 1;
k = nums[i]; nums[i] = nums[j]; nums[j] = k;
}
cout << "The lottery numbers are: ";
for (i = 1; i < 7; i++) cout << nums[i] << " ";
if (numberchoices[i] = nums[i])
{
cout << "gud\n";
}
if (numberchoices == nums)
{
cout << "gud 2";
}
/**/
cout << "\n\n";
system("pause");
Please ?
int numberchoices[] = { 0 };
for (int w = 1; w < 7; w++)
{
cout << "N" << char(163) << "mero #" << w << ": ";
cin >> numberchoices[w];
} // user numbers
You're declaring an array of size 1 and then you use it up to position 6 ?
I am having this terminal error every time I finish the debug program.
I'm surprised that you're not having a terminal error every time you start debug.
The access of numberchoises at positions from 1 to 6 are UB (Undefined Behavior). That is: all can happens.
Solution: try with
int numberchoices[7] = { }; // initialize all elements to zero!
Another point
if (numberchoices == nums)
not sure that you get what do you expect.
Do you want compare the integer pointer corresponding to numberchoices (a int[1], suggested int[7]) with the one corresponding to nums (a int[51]) ?
I'm creating a small program that allows the user to input 3 names (or whatever string they want). The program should then display all three strings (which is working), then it should use the rand() function to randomly display one of the three strings. This is the part that isn't functioning properly.
#include <iostream>
#include <string>
using namespace std;
void display(string[], int);
const int SIZE = 3;
int main()
{
string names[SIZE];
for (int i = 0; i < SIZE; i++)
{
cout << i + 1 << ": ";
getline(cin, names[i]);
}
cout << endl;
display(names, SIZE);
int name = rand() % (2 + 1 - 0) + 0;
cout << names[name];
cin.get();
return 0;
}
void display(string nm[], int n)
{
int i = 0;
for (i; i < n; i++)
{
cout << "Name " << i + 1 << ": ";
cout << nm[i] << endl;
}
}
I had it set up differently before, and it gave me an error, but after changing it to what it is now, it always gives me the last element [2].
Is this a code error, or is it just that rand() always gives the same output on the same system?
After some discussion in the comments, it became apparent that the issue was that I was not seeding the rand() function. Below is part of the code that was not functioning, corrected.
(Also, as a sidenote, to use the time() function, <ctime> or <time.h> has to be included.)
srand(time(NULL));
int name = rand() % 3;
cout << names[name];
(Thanks to #manni66 for pointing out that it was useless to include an overly complicated calculation to get the range for rand(), as it just had to be a single integer.
seeding with current time works :
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cstdio>
using namespace std;
void display(string[], int);
const int SIZE = 3;
int main()
{
string names[SIZE];
for (int i = 0; i < SIZE; i++)
{
cout << i + 1 << ": ";
getline(cin, names[i]);
}
cout << endl;
display(names, SIZE);
srand(time(NULL)); // use current time as seed for random generator
int name = rand() % 3 ;
printf(" random %i \n", name);
cout << names[name];
cin.get();
return 0;
}
void display(string nm[], int n)
{
int i = 0;
for (i; i < n; i++)
{
cout << "Name " << i + 1 << ": ";
cout << nm[i] << endl;
}
}
I'm trying to show a ranking list of my array qt, which contains 5 numbers.
int i, j;
int qt[5] = {10,20,10,50,20};
int tempqt;
for (i=0; i<5; i++)
{
for(j=(i+1); j<5; j++)
{
if (qt[i] >= qt[j])
{
tempqt = qt[i];
qt[i] = qt[j];
qt[j] = tempqt;
}
}
}
for(i=0; i<5; i++)
{
cout << i+1 << ".number: " << qt[i] << endl;
}
normally, the 2 for-loops sort my array and the last for-loop displays my array ordered, so it looks like this:
1.number: 10
2.number: 10
3.number: 20
4.number: 20
5.number: 50
But I want to display the numbers with the same value as the same ranking position, so like this:
1.number: 10
1.number: 10
2.number: 20
2.number: 20
3.number: 50
The idea is to increase rank counter when meet different value in qt array.
i = 0;
int rank = 1, val = qt[i];
cout << rank << ".number: " << qt[i] << endl;
for(i=1; i<5; i++)
{
if (qt[i] != val) {
++rank;
val = qt[i];
}
cout << rank << ".number: " << qt[i] << endl;
}
Use std::sort to sort the array -- you won't get anywhere until the array is sorted.
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int qt[5] = { 10, 20, 10, 50, 20 };
sort(qt, qt + 5);
int count = 1;
for (int i = 0; i < 5; ++i)
{
if (i > 0)
{
if (qt[i] != qt[i - 1])
++count;
}
cout << count << ".number: " << qt[i] << endl;
}
}
Here is another solution using a map. This is more "lazy" in that there is no real "check if number already seen" logic involved. Just add numbers to a map, and print out the results in a loop.
If there are no memory constraints (you will need to create a map of the numbers, of course), and/or you need the array to remain stable (not sorted), then this could be an alternative.
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int qt[5] = { 10, 20, 10, 50, 20 };
std::map<int, int> IntMap;
// add entries to map, adding to a counter each time
for (int i = 0; i < 5; ++i)
IntMap[qt[i]]++;
// output the results.
int count = 1;
for (auto it = IntMap.begin(); it != IntMap.end(); ++it, ++count)
{
for (int i = 0; i < it->second; ++i)
cout << count << ".number: " << it->first << endl;
}
}
The map already sorts, so that's taken care of. Then the map is set up to count the number of times each number shows up, so that's taken care of. The only thing left is to write a loop that just goes through the map and prints the information.
See it here: http://ideone.com/q08SeX
I'd rather use a do while loop:
int p = 1, x = 0;
do
{
cout << p << ".number: " << qt[x++] << endl;
if (x < 5 && qt[x] != qt[x-1])
p++;
} while (x < 5);