Dynamic Array

Hello, I'm trying to create a dynamic array of random numbers from 1 to c. I input b&c into main, but cut this down to make it easier to read. I'm trying to use a pointer, but can't get it syntax error free.

cpp:
1
2
3
4
5
6
7
8
9
10
int Class::array(int a[], int b, int c)
{
	int* a = new int [ b ];
	for(int i=0;i<b;i++)
	{
		srand(time(0));
		a[i]= 1+rand() % c;
	}
	cout<< a;
}



main:
1
2
Class object;
object.array(a[],b,c);


Thank you.
You only need to call srand() once.

Change object.array(a[],b,c); to object.array(a,b,c);
Thanks for the reply, still can't get it to run though, here's the whole code (.h, .cpp, main):

1
2
3
4
5
6
7
8
#include <string>
using namespace std;

class Class
{
public:
	int array1(int [], int, int);
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;

#include "classfile.h"

int Class::array1(int a[], int b, int c)
{
	int* a = new int [ b ];
	for(int i=0;i<b;i++)
	{
		srand(time(0));
		a[i]= 1+rand() % c;
	}
	cout<< a;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cstdlib>
using namespace std;

#include "classfile.h"

int main()
{
int b;
int c;

cout<< "Enter b" << endl;
cin>> b;
cout<< "Enter c" << endl;
cin>> c;

Class object;
object.array1(a,b,c);
}
Well you need to define something called 'a' within main. An array to be specific.
closed account (48T7M4Gy)
Try this:
You were creating (well sort of) an array in main and another in Class. The Class constructor was not working/correct also.

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
// classfile.h
class Class
{
public:
	Class(int, int);
	int* getArray();

private:
	int length, rNumber;
	int* array;
};

// classfile.cpp
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include "classfile.h"

Class::Class(int length, int rNumber)
{
	srand(time(0));

	array = new int[length];
	for (int i = 0; i < length; i++)
		array[i] = 1 + rand() % rNumber;
}

int* Class::getArray()
{
	return array;
}

// main
#include <iostream>
#include <cstdlib>
#include "classfile.h"

int main()
{
	int b;
	int c;

	std::cout << "Enter b" << std::endl;
	std::cin >> b;
	std::cout << "Enter c" << std::endl;
	std::cin >> c;

	Class object(b, c);	

	int* answer = object.getArray(); ///

	for (int i = 0; i < b; i++)
		std::cout << answer[i] << '\t';

	return 0;
}


Last edited on
Thanks that does work, but I need it in the format:
array1(int a[], int b, int c)

However, I think I'm supposed to be calling array0(int c) which is a random number into this function.
I will then need to call that function again to do a bunch of things to it.
closed account (48T7M4Gy)
Yeah, I might be missing something but I can't see the point of creating the array inside the class if you've already done it in main. But there ya go.

This might be of use.
http://www.tutorialspoint.com/cplusplus/cpp_passing_arrays_to_functions.htm

Cheers :)
Topic archived. No new replies allowed.