#include <iostream>
#include <cstdlib>
#include <array>
usingnamespace std;
int main()
{
array<int, 7> lottorad;// Ett heltalsfält som lagrar lottoraden
bool lika = false;
for (int i = 1; i < lottorad.size(); i++) {
lottorad[i] = rand() % 35 + 1; // randomise a number betwin 1-35
for (int j = 1; j < lottorad.size(); j++) { // loops throu if the number has been drawn before
if (lottorad[i] == lottorad[j]) {
lika = true;
}
if (lika == true) { // if the number has been drawn, go back and reroll
i--;
lika = false;
}
else { // if the number has not been drawn write it out.
cout << lottorad[i] << endl;
}
}
}
return 0;
}
hmm, explain more about this "set" never heard of it before :o how do i write it?
The thing is, they gave me a code with aloth of errors and misstake. i have fixed it up to this. but i am kinda stuck, and i think i dont have to use an array i can use whatever i want. just make it as simple and good as i can :)
#include <iostream>
#include <cstdlib>
#include <array>
usingnamespace std;
int is_present(int i, int array) {
bool lika = false;
for (int j = 1; j < lottorad.size(); j++) { // Loopar igenom de tidigare slumpade tal och kontrollerar om det aktuella talet redan har dragits
if (lottorad[i] == lottorad[j]) {
lika = true;
}
if (lika == true) { // Om det aktuella talet redan dragits så minskas i med 1. Man går alltså tillbaka ett steg i loopen och slumpar ett nytt tal
i--;
lika = false;
}
else { // Om det aktuella talet inte hade dragits tidigare så skrivs det ut
cout << lottorad[i] << endl;
}
}
}
int main()
{
array<int, 7> lottorad;// Ett heltalsfält som lagrar lottoraden
bool lika = false;
for (int K = 0; K<lottorad.size(); ++K) { //index goes from 0 to n-1
int number;
do {
number = rand() % 35 + 1;
} while (is_present(number, lottorad));
lottorad[K] = number;
}
return 0;
}
#include <iostream>
#include <cstdlib>
#include <array>
#include <time.h>
staticint lottorad[7];
usingnamespace std;
void randomNumber(int pos)
{
srand(time(NULL));
lottorad[pos] = rand() % 35 + 1;
for (int j = 1; j < lottorad[7]; j++) { // Loopar igenom de tidigare slumpade tal och kontrollerar om det aktuella talet redan har dragits
if (lottorad[pos] == lottorad[j]) {
pos--;
}
else{ // Om det aktuella talet inte hade dragits tidigare så skrivs det ut
cout << lottorad[pos] << endl;
}
}
}
int main()
{
{
for (int a = 0; a < lottorad[7]; a++)
{
randomNumber(a);
}
}
cout << lottorad[1] << endl;
cout << lottorad[7] << endl;
}
> How do i make the function take an array?
The same way that you would make it take any variable bool is_present(int value, const std::array<int, 7> &array);
Or you could use pointers
Your function should not do any output to the console.
for (int j = 1; j < lottorad[7]; j++)
¿what are you doing there?
¿why does `j' start in 1?
¿what are you accessing the 8th element of the array? (which doesn't exist)
if you say that your function returns a value, then there should be a return statement.