Hello Guys!

Pages: 12
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace 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
return true;
// no equal element found - IsIn returns false
return false;
}
// 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 = new int[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
}
Indent your code properly and you'll see some problems.

You should only have one main.

Other functions should be defined outside of main.

"return" has an 'e' in it.

"for" isn't just spelled "f".

Other idiotic misspellings abound.
Pay more attention. :)

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

I have new problem now XD
Change this to that, put those over there, and add in one of these.
Like this?

#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
Last edited on
What is a bol?

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.
Last edited on
I dont really know@ganado XD
i just copied that code and trying to fix it. because im practicing. :P
Show us what you're copying from

Is the code intentionally wrong? If so, it sounds like that's the whole point of the practice, is for you to figure out what's wrong, no?

Here is a hint, read this: http://www.cplusplus.com/doc/tutorial/variables/
Last edited on
I think yes. XD
im trying my best to figured it out.
Thank you for the hint sir! I will try to figured it out!
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){

cout << ballsno << " ";
cout << endl;
}
delete[] balls;
return 0;
}
its randomized, so we can't say exactly.

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.
Last edited on
its running can you check this out?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#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++){
cout << ballsno << b << endl;
cout << endl;
}
delete[] balls;
return 0;
}

Last edited on
// get a random value from range 1..mb
not quite. you are off by one on each end!

% is remainder, effectively, as in division.
it will be between 0 and mb-1 inclusive (what is 10%5?)
isn't it nice that c++ indexes arrays from 0?

I think the rest are all good.
Last edited on
what sould i replace ? i dont get that // get a random value from range 1..mb

should i replace the mb++ to m-1?
Last edited on
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...
Last edited on
The comment suggests you want to generate a random number in the range of 1 to mb.

16
17
18
19
20
// get a random value from range 1..mb
int RandBall(int mb)
{
   return rand() % mb + 1;
}


i dont know what went wrong here im just practicing using youtube.

Forget trying to learn C++ via youtube. Many of the videos are wrong or don't explain the details well.

There are several tutorial websites that make learning C++ much easier.

One is here at CPlusPlus (if it is a bit outdated):
http://www.cplusplus.com/doc/tutorial/

Another site that is more up-to-date, and is frequently updated:
https://www.learncpp.com/

Since your problem(s) happen from random number generation issues here's a "jump in feet first" with the page on random number generation:
https://www.learncpp.com/cpp-tutorial/59-random-number-generation/

Using Whitespace.....it makes reading source code easier.

Indenting and adding some spaces turns a wall of hard-to-read text into easier to digest source.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#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++)
   {
      cout << ballsno << b << endl;
      cout << endl;
   }

   delete[] balls;
   return 0;
}
Forget trying to learn C++ via youtube. Many of the videos are wrong or don't explain the details well.


Yes, at all times concentrate on Fury Guy and his balls.
Forget trying to learn C++ via youtube. Many of the videos are wrong or don't explain the details well.

Agreed.

Ideally, find a (reasonably current) textbook with favorable expert reviews. There are only a few.

See the ISO C++ Foundation website's FAQ. In particular:
https://isocpp.org/wiki/faq/how-to-learn-cpp#start-learning
And
https://isocpp.org/wiki/faq/how-to-learn-cpp#best-book
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <set>
using namespace std;

int main()
{
   int maxBall, numBall;
   cout << "Max ball number? ";   cin >> maxBall;   
   cout << "How many balls? ";   cin >> numBall;

   srand( time( 0 ) );
   set<int> balls;
   while( balls.size() < numBall ) balls.insert( 1 + rand() % maxBall );

   for ( int b : balls ) cout << b << ' ';
}
Max ball number? 10
How many balls? 4
2 6 8 10  
Pages: 12