Im currently practicing coding hmmmm..
i dont know what went wrong here im just practicing using youtube.
wondering if anyone can help me on this? self study
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int main(void) {
int bol;
int nt, v, t, s, IsIn;
bol ;IsIn( nt )v, int t, int s) {
// iterate thru s first elements of t
for(int i = 0; i < s; i++)
if(t[] == v)
// equal element found - IsIn returns true
returntrue;
// no equal element found - IsIn returns false
returnfalse;
}
// get a random value from range 1..mb
int RandBall(int mb) {
rturn rand() % mb + ;
}
int main(void) {
int maxball;
int ballsno;
cout << "Max ball number? ";
cn >> maxball;
cout << "How many balls? "
cin >> bals;
srand(time(NULL));
int *balls = newint[ballno];
for(int b = 0; b < ballno; b+) {
// get next random value
int bl = RandBall(mxball);
// repeat until new value is unique in balls
whi(IsIn(bl, balls, b))
bl = RandBall(maxball);
// ok - it's unique - put it into balls
balls[b] = bl;
}
f(int b = 0; b < ballsno; b+1)
cout << ball[b] << " ";
cout << endl;
delete[] balls;
return 0
}
10 15 [Error] 'IsIn' cannot be used as a function
20 22 [Error] a function-definition is not allowed here before '{' token
23 16 [Error] a function-definition is not allowed here before '{' token
48 1 [Error] expected '}' at end of input
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
// function IsIn checks if v is equal to
// any of first s elements in vector t
bol IsIn(nt v, int t[], int s) {
// iterate thru s first elements of t
for(int i = 0; i < s; i++)
if(t[] == v)
// equal element found - IsIn returns true
return true;
// no equal element found - IsIn returns false
return false;
}
// get a random value from range 1..mb
int RandBall(int mb) {
return rand() % mb ++ ;
}
int main(void) {
int maxball;
int ballsno;
cout << "Max ball number? ";
cin >> maxball;
cout << "How many balls? ";
cin >> ballsno;
srand(time(NULL));
int *balls = new int[ballsno];
for(int b = 0; b < ballsno; b++) {
// get next random value
int bl = RandBall(maxball);
// repeat until new value is unique in balls
while(IsIn(bl, balls, b))
bl = RandBall(maxball);
// ok - it's unique - put it into balls
balls[b] = bl;
}
for(int b = 0; b < ballsno; b+1)
cout << ballsno[b] << " ";
cout << endl;
delete[] balls;
return 0;
}
woah! i think getting there?
7 1 [Error] 'bol' does not name a type In function 'int main()':
33 24 [Error] 'IsIn' was not declared in this scope
39 18 [Error] invalid types 'int[int]' for array subscript
ballsno is an int, you can't use it like an array.
Don't rush in writing your code, think about what's happening. Look at the line number of the error messages and try to figure out what's wrong; the mistakes are easy to spot.
hey guys its already running but i dont know what should be the output?
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
// function IsIn checks if v is equal to
// any of first s elements in vector t
bool IsIn(int v, int t[], int s) {
// iterate thru s first elements of t
for(int i = 0; i < s; i++)
if(t[i] == v)
// equal element found - IsIn returns true
return true;
// no equal element found - IsIn returns false
return false;
}
// get a random value from range 1..mb
int RandBall(int mb) {
return rand() % mb ++ ;
}
int main(void) {
int maxball;
int ballsno;
cout << "Max ball number? ";
cin >> maxball;
cout << "How many balls? ";
cin >> ballsno;
srand(time(NULL));
int *balls = new int[ballsno];
for(int b = 0; b < ballsno; b++) {
// get next random value
int bl = RandBall(maxball);
// repeat until new value is unique in balls
while(IsIn(bl, balls, b))
bl = RandBall(maxball);
// ok - it's unique - put it into balls
balls[b] = bl;
}
for(int b = 0; b < ballsno; b+1){
this is not helpful. you have a bunch of code that you do not know what it does, it has multiple C in C++ problems. Correcting it may have helped you a little (syntax reinforcement if nothing else), but that is all you probably want to get from this exercise. And, sticking with it and getting it to run indicates you want to work through it and learn, so that is good too. You have the right attitude but your approach will not teach you what you need to learn at this stage.
I highly advise you to find a problem, and then write your own code to solve it. Start simple. Learn about functions and objects and built in language tools like vector, string.
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
// function IsIn checks if v is equal to// any of first s elements in vector tbool IsIn(int v, int t[], int s) {
// iterate thru s first elements of tfor(int i = 0; i < s; i++)
if(t[i] == v)
// equal element found - IsIn returns truereturntrue;
// no equal element found - IsIn returns falsereturnfalse;
}
// get a random value from range 1..mbint RandBall(int mb) {
return rand() % mb ++ ;
}
int main(void) {
int maxball;
int ballsno;
cout << "Max ball number? ";
cin >> maxball;
cout << "How many balls? ";
cin >> ballsno;
srand(time(NULL));
int *balls = newint[ballsno];
for(int b = 0; b < ballsno; b++) {
// get next random valueint bl = RandBall(maxball);
// repeat until new value is unique in ballswhile(IsIn(bl, balls, b))
bl = RandBall(maxball);
// ok - it's unique - put it into balls
balls[b] = bl;
}
for(int b = 0; b < ballsno; b++){
cout << ballsno << b << endl;
cout << endl;
}
delete[] balls;
return 0;
}
replace the comment. As I said it is not correct.
I can't tell you what to change in the code, because I don't know what you want to do, if anything.
and check it. that ++ is odd, I took it to be mb++. I would have written (rand()%mb) +1 if I wanted 1 .. mb range. Perhaps it does this ... I did not run it... see who is right, me or the code/comment...
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
// function IsIn checks if v is equal to
// any of first s elements in vector t
bool IsIn(int v, int t[], int s)
{
// iterate thru s first elements of t
for (int i = 0; i < s; i++)
if (t[i] == v)
// equal element found - IsIn returns true
returntrue;
// no equal element found - IsIn returns false
returnfalse;
}
// get a random value from range 1..mb
int RandBall(int mb)
{
return rand() % mb++;
}
int main(void)
{
int maxball;
int ballsno;
cout << "Max ball number? ";
cin >> maxball;
cout << "How many balls? ";
cin >> ballsno;
srand(time(NULL));
int* balls = newint[ballsno];
for (int b = 0; b < ballsno; b++)
{
// get next random value
int bl = RandBall(maxball);
// repeat until new value is unique in balls
while (IsIn(bl, balls, b))
bl = RandBall(maxball);
// ok - it's unique - put it into balls
balls[b] = bl;
}
for (int b = 0; b < ballsno; b++)
{
cout << ballsno << b << endl;
cout << endl;
}
delete[] balls;
return 0;
}