NEW to arrays

I just had an array lesson with my professor but still sort of clueless on one of his assignment by using calling function of array. Some helps for hints would be so much appreciate.

Here's the part that I stuck on:

using calling statement:

int main()
{ int a[10];
//assign 10 random intergers between 1 and 100 to array a
Assign (a);
//Display array a
Display (a);
return 0;
}

I know for the random generate must be using the <ctime> but I am so lost while trying to putting them into a function. This is what I had so far, I know it's not right but I really tried my best to pulling every out on top of my mind on this small piece.
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
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
void Assign (int& a)
{	
	for (int i=0;i<10;++i)
	{
		srand(time(0));
		cout<<a[i]<<"  ";
	}
}
void Display (int a)
{
	cout<<a<<endl;
}
int main()
{
	int a[10];
	a[10]=rand()%100+1;
	//assign 10 random intergers between 1 and 100 to array a
	Assign (a[10]);
	//Display array a
	Display (a[10]);

	//Terminate
	system("pause");
	return 0;
}


I am deeply appreciate for the quick replies. A small hint also a great contribution that could help me open up my mind.
So here's the working code, and I'll explain it.

First, you need <cstdlib>, not <ctime> (or <iomanip>)

In your functions, you need to pass the entire array, not just one integer value.

You need to generate the random numbers in the for loop in your assign function, or else you will get the same value every time.

When passing arrays from your main function, you only need to pass the name, not the brackets as well.

For displaying the array, you need another for loop.


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 <iostream>
#include <cstdlib>
using namespace std;

void Assign (int a[])
{   srand(0);

	for (int i=0;i<10;++i)
	{
		int num = rand()%100+1;
		a[i] = num;
	}
}
void Display (int a[])
{
   for (int i=0; i< 10; ++i)
   {
       cout << a[i] << endl;
   }
}
int main()
{
	int a[10];

	//assign 10 random intergers between 1 and 100 to array a
	Assign (a);
	//Display array a
	Display (a);

	//Terminate
	system("pause");
	return 0;
}
Last edited on
I do not acquainted with <cstdlib> library base on lesson I had so far. Are there anyway to using <ctime> library for the legitimate knowledge base on the first beginning level of C++?
I do not acquainted with <cstdlib> library
You do not need detailed knowledge to use libraries. You merely need to know what libraries contain what information. For this problem you need to use <cstdlib>. For others, you can use <ctime>.

For more information, you can always use google :) or books or other resources.

See
http://www.cplusplus.com/reference/clibrary/cstdlib/
Last edited on
I had the explanation on the <cstdlib> and my professor also allowed me to use that since it contained most of the pseudo library needs.

Thank you so much.

I very deeply appreciate for your time of helping me. =)
Topic archived. No new replies allowed.