Random number

I cant compile this code as Im at work and the computers are security protected, So i''l have to wait until i get home to test this, but im pretty sure im wrong. The problem is I have to create two vectors with 10 elements and input random numbers into it, then pick one of the elements of the second vector at random and append it to an element from the first vector at random. This has to be done 10 times and the im assuming i have to print the 10 results. This is what I have so far I feel like I really need some help this book doesnt really seem to cover it for me. Thank you in advance.


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
#include <cstdlib> 
#include <ctime> 
#include <iostream>

using namespace std;

int main() 
{ 
vector<int> random (10);
    srand((unsigned)time(0)); 
    int random_integer; 
    for(int i=0; i<10; i++){ 
        random_integer = (rand()%10)+1; 
        random_integer = random[i]; 
    } 
}

vector<int> random2 (10)
	srand((unsigned)time(0)); 
    int random_integer; 
    for(int i=0; i<10; i++){ 
        random_integer = (rand()%10)+1; 
        random_integer = random2[i];

 for (int i=0; i<10; i++){


sum = append (random[i], random2[i])
cout<< sum <<"\n"

return 0; 
There are online compilers to test your code:
http://www.compileonline.com/compile_cpp_online.php
Thanks modoran so I have tested and recieved alot of errors but i debugged them down to two.

1. error: expected ',' or ';' before 'srand'
srand ((unsigned)time(0));

2. 'append' was not declared in this scope
sum = append(random[i], random2[i]);

I have no idea what is wrong with the first error, the second one I think Im using the wrong formula so to speak to combine the two vectors. Any one have any pointer?


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
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int> random(10);
    {
    srand((unsigned)time(0));
    int random_integer;
    for (int i=0; i<10; i++){
        random_integer = (rand()%10)+1;
        random_integer = random[i];
    }
}
vector<int> random2 (10)
srand ((unsigned)time(0));
int random_integer;
for (int i=0; i<10; i++){
    random_integer = (rand()%10)+1;
    random_integer = random2[i];
    
}
for (int i=0; i<10; i++){
    int sum;
    sum = append(random[i], random2[i]);
    cout<<sum<<"\n";
    return 0;
}
}
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
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <vector>
#include <cmath>//<<<<<<<<<<<<<<<<<<<for pow()

using namespace std;

int int_append(int,int);

int main()
{
    srand((unsigned)time(0));
    vector<int> random(10);

    int random_integer;
    for (int i=0; i<10; i++)
        {
            random_integer = (rand()%10)+1;
            random[i] = random_integer;//<<<<<<<
        }

    vector<int> random2 (10);//<<<<<<<<<<semicolon
//srand ((unsigned)time(0)); don't use it twice
   // int random_integer;
    for (int i=0; i<10; i++)
    {
        random_integer = (rand()%10)+1;
        random2[i] = random_integer;

    }
    for (int i=0; i<10; i++)
    {
    //int sum;
    //sum = int_append(random[i], random2[i]);
        cout<<"random numbers "<<random[i]<<" - "<<random2[i]<<endl;
        cout<<"appended = "<<int_append(random[i],random2[i])<<endl;
    }
    return 0;
}
int int_append(int a,int b)
{
    int l=log10(b)+1;
    int output = a*pow(10,l)+b;
    return output;
}


then pick one of the elements of the second vector at random and append it to an element from the first vector at random.

Now it's your turn ;)
Line 37: you use the member function of cout: .operator<<()

Don't use commas when you use the cout object.

Example:
std::cout<<object[0]<< object[1];


I misread.
Last edited on
37 is a call to a function int_append(random[i],random2[i])
IWishYouKnew ;)
Sorry, I misread.
no problem my friend :)
Man I was way off I appreciate all the help I just have question about the last function int_append, Can you explain to me exactly what it does Chriscpp?
int l=log10(b)+1;
the length of b I mean how many digits
but think in this case it's not c++ but math :)
Topic archived. No new replies allowed.