Array with random numbers

Hello all. I am just learning about arrays now, and I have a small problem. I have to create a C++ program that will:
- Declare an array named rNumb of 50 components of type int.
- The values of all components are randomly generated numbers in the range [0..100];
- Prints out all components which values are odd numbers.

This is the code I have so far.

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
#include <iostream>
#include <ctime>
using namespace std;
int input (int []);
int output (int []);
int main ()
{
int rNumb[50];
input (rNumb);
output (rNumb);
}

int input (int x[])
{
	srand(time(0));
	for(int i=0; i<50; i++) 
        x[i] = (rand()%100)+1; 
        cout << x[i] << endl;
	return x[i];
}

int output (int x[])
{
	for (int i = 0; i < 10; i++)
	{
    if (x[i] % 2 == 1)
         cout << x[i] << " ";
	}
	return x[i];
}


I do get some errors, not many, but some. Any suggestions?
Last edited on
And the errors are?
A logical error perhaps: in your input function you are ouputting. However you are only outputting one element in the array. Also, you are only returning one element in the array.

Also, on line 17 you don't need the brackets as % has a higher precedence than +, so it will execute the same.
Okay, sorry. The errors I was receiving were undeclared variable i and then time_t to unsigned int possible loss.

okay so for the code i should remove that output statement from input.

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
#include <iostream>
#include <ctime>
using namespace std;
int input (int []);
int output (int []);
int main ()
{
int rNumb[50];
input (rNumb);
output (rNumb);
}

int input (int x[])
{
	srand(time(0));
	for(int i=0; i<50; i++) 
        x[i] = rand()%100+1; 
}

int output (int x[])
{
	for (int i = 0; i < 10; i++)
	{
    if (x[i] % 2 == 1)
         cout << x[i] << " ";
	}
	return x[i];
}


Does that look correct? Unfortunately, I am not able to download my compiler at work so I can't test it.
Last edited on
Okay, I played around with that code for a little bit.
I was able to download my compiler, so I can test my code now.

It appears to be working correctly.

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
#include <iostream>
#include <ctime>
using namespace std;
int input (int []);
int output (int []);
int main ()
{
int rNumb[50];
input (rNumb);
output (rNumb);
}

int input (int x[])
{
	int i = 0;
	srand(time(0));
	for(int i=0; i<50; i++) 
    x[i] = rand()%100+1; 
	return x[i];
}

int output (int x[])
{
	int i = 0;
	for (int i = 0; i < 10; i++)
	{
    if (x[i] % 2 == 1)
         cout << x[i] << " ";
	}
	return x[i];
}
well for one you can use NULL in replace of 0, if you get that unsigned error, or.
srand((unsigned)time(NULL)); but your example, and the 2 i just listed work fine, it's only some compilers complain. I'm sure there's a valid explanation for this but I am unsure of it.

additionally I would use a constant for 100, and also for 50 as that way if you or someone else later on down the track want to change the amounts then they don't have to hard code it into everything, just a simple change of const value and bob's your uncle.

I'll write some sample code to demonstrate:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

#include <iostream>
#include <ctime>
using namespace std;

const int arraySize=50;
const int range=100;

int main()
{
    int array[arraySize];
    for (int i=0;i<arraySize-1;i++)
    {
        array[i]=getRand();
    }
    return 0;
}

int getRand()
{
    return 1+rand()%range;
}


EDIT: I actually didn't notice you have for loops in your last code because it was tabbed so poorly. you need to watch your indentation, also in your for loop you are saying while i less than 50, which means i starting at 0 has 50 elements so up to 49, but your asking for it to count to 50, which is actually counting 51 elements in the array. when you declare an array just remember the last element is always -1. for example if i declare an array of 99 elements then the last element is 98. because 0 is 1st, 1 is 2nd, 2 is 3rd, etc. well, 0 is actually the zeroth, 1 is the 1th(oneth), 2 is the 2th(twoth) and so on :P which is confusing way to look at it, it is much easier to just remember to minus one for whatever the declaration is.

goodluck
Last edited on
Okay, now I'm a little bit confused, lol.

I have
1
2
3
4
5
6
7
8
9
10
11
int input (int x[])
{
	int i = 0;
	srand(time(0));
	
                for(int i=0; i<50; i++) 
                x[i] = rand()%100+1; 

	return x[i];
}


Okay so for the for loop, I have the i < 50. I understand that I want it to go up to 50, but if i leave it like this it will only go up to 49. So to make it count up to 50, should I make it i <=50? See, I tried that too, and I received an error.

Edit: Also, why would I put i < 50 -1.. wouldn't that equal 48? Ah! I'm so lost.
Last edited on

why would I put i < 50 -1.. wouldn't that equal 48

your right my bad, I keep switching languages and confusing myself. Sorry for the confusion was 2 am when I posted and wasn't thinking clearly. :S

but yea that indentation is still off, basically you indent wherever you would put a curly brace, or wherever a method length is wrapped to next line.

1
2
3
4
5
6
7
8
9
10
11
12
int input (int x[])
{
    int i = 0;
    srand(time(0));
    
    for (int i=0; i<arraySize; i++)
    {
        x[i] = rand()%range+1;
    }

    return x[i];
}


1
2
3
4
5
6
int main()
{
    cout <<  "indenting to the next line because the cout statement is too long"
            <<  x[6]   <<  " is our 7th element"   <<  endl;
    return 0;
} 


there's no need to return the array as it is passed by reference anyway, the method can simply change it (assign random nums to it) and that will reflect, it is not like java in that respect.
Also good idea not to seed srand in the same method you are using rand(), I know that sounds really strange, but it doesn't quite work properly if it is close to it, it would more than likely work in your example. But just to be sure the numbers are random stick it somewhere else.
I just stick it in your variable declarations at the top of int main() or something.

Last edited on
Topic archived. No new replies allowed.