Reputation: 13
Hello I'm a beginner for C++ I don't know many syntax.
My question is how to store data found from int A,B,C.
A B C
generate random value.
How to make store input value from random int like table I meant :
| A | B | C | | - | - | - | | 8 | 2 | 4 | | 7 | 5 | 4 |
I try to use this :
#include<iostream>
#include<string>
#include <vector> using namespace std;
int main() {
int A, B, C; //in this case random A = 8 , Random B = 2, Random C = 4; and A,B,C is looping continues random int;
int arr[] = {A,B,C};
std::vector<int> myvec (arr, arr + sizeof(arr) / sizeof(int) );
for (int i: myvec)
std::cout << i << ' ';
}
Result from code like this :
| 8 | 2 | 4 |
Question 1: how to make continues loop table data like this:
| 8 | 2 | 4 |
| 7 | 5 | 4 |
Question 2: how to make if A generate random 8 and its already from table its skipped (not will be stored)
Question 3: how to pick int value from table A example:
int CheckSame;
if (CheckSame == A) { //if user input number int
// CheckSame same from store data then will be displayed
cout << "Equal with A : " << CheckSame;
}
I hope you understand my question really I'm stuck, I'm new to C++
First quote its my first question but I think no one know what I meant maybe because I'm bad making question
So i have declare int number as random;
#include <array>
#include <iostream>
#include <random> // for random numbers
#include <vector>
using namespace std;
int main()
{
int A = rand();
int B = rand();
int C = rand();
std::vector<int> myvec{A,B,C};
for (const auto& i: myvec){
std::cout << i << ' ';
}
}
Output :
23 11 43
what I want its like this :
23 11 43
9 21 55
37 21 45
1.main program check if A generate random number and different from row myvec so myvec will insert to next row "9 21 55" is the new row or adding new row or the second row
2.what a code if I want to make simple check
//I know this bad or wrong code, but i want something check like this one
int i = 23; int result;
if (i == myvec[A]){
result = i;
std::cout << "found same number from data A and i : " << i << "B number is : " << cout myvec[B] << "C number is :" << myvec[c];
}
return result;
so output like this :
found same number from data A and i : 23 B number is : 11 C number is : 43
i hope some one can help me.
Upvotes: 1
Views: 69
Reputation: 13076
Example for you, which is probably a lot different from most online information you will find (because most online examples are out of date and C++ has been improved in the meantime)
#include <algorithm> // for generate
#include <array>
#include <iostream>
#include <random> // for random numbers
#include <vector>
int GetRandomNumber()
{
// static local variable : only create on first call to function, reuse in all other calls
static std::random_device rd; // a source of true randomness (you should never use time() even if that's shown in almost all online "C" examples)
static std::mt19937 generator{rd()}; // a pseudorandom number generator (starting from a true random value), reading random_device is very slow, this is not.
static std::uniform_int_distribution distribution(0,9); // generate a random number between 0-9 (inclusive)
return distribution(generator);
}
int main()
{
//std::vector<int> values(3); // dynamically allocate memory (heap) for 3 integers
std::array<int,3> values; // statically allocate memory (stack) for 3 integers
// No raw loops (for loops with indices can be buggy, easy to make mistakes
// and access arrays out of the memory area).
std::generate(values.begin(),values.end(),GetRandomNumber);
// Range based for loop, the const means that value can only be read from
for(const auto value : values)
{
std::cout << value << "\n";
}
return 0;
}
Upvotes: 1