Arrays and Functions

Hopefully someone can help me with this problem I am having with functions and arrays.
I have to fill array with random numbers using a function and print out the array using the following function:
void PrintData( char *sLabel, double dAnswer );
or
void PrintData( char sLabel[], double dAnswer );

dAnswer is used for function calls.

The code does not run right. If anyone can help me I would appreciate it.

Thank you for your time.
R.

Below is my code.


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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  #include <iostream>
#include <string>
#include <ctime>

//prototype functions
void PrintData(char sLabel[], double dAnswer);
void FillArray(long array[], long size);
double Average1(long array[], long size);




using namespace std;

int main()
{
	//variables used
	const short shMAX  = 10;
	short shCnt;
	long lArr[10];
	double dAnswer;
	string sName = "bob";
	double d2Arr[2][3];

	srand((unsigned)time(NULL));


//FillArray called
FillArray(lArr, 10);

dAnswer = Average1(lArr, 10);

PrintData("lArr", dAnswer);


	system("pause");
	return 0;
}

void PrintData(char sLabel[], double dAnswer)
{
	for(int count = 0; count < dAnswer; count++)
	{
		cout << sLabel[count] << " ";
	}
}


//#1. FillArray - It will receive the lArr array, an array of long integers, 
//from main and a count of the number of elements in the array. 
//A random number will be assigned to each element of the array in a loop. 
//Use rand(). This function has no return value, a void function.
void FillArray(long array[], long size)
{
	for(int count = 0; count < size; count++)
	{
		array[count] = rand();
	}
}


//#2a Average1 - It will receive the array lArr and a count of the items. 
//It will add up the total and then return the average as a double. 
//Use subscript notation throughout. In main call the function PrintData 
//to print the average returned. This function receives 2 arguments and returns a double.
double Average1(long array[], long size)
{
	double total = 0.0;
	double average;

	for(int count = 0; count < size; count++)
	{
		total += array[count];
	}

	average = total / size;

	return average;

}
40
41
42
43
44
45
46
void PrintData(char sLabel[], double dAnswer)
{
    for(int count = 0; count < dAnswer; count++)
    {
        cout << sLabel[count] << " ";
    }
}

Not sure what you're trying to do here...
Thanks for quick reply:
Basically I have to fill out an array using a random fill function then print out the array using either of the following:
void PrintData( char *sLabel, double dAnswer );
or
void PrintData( char sLabel[], double dAnswer );

This is what was given to me to use. I do not understand how to use the char to print out the array. I have never seen anything like this before. If you have any thoughts on this I would appreciate it.

Thanks
R.
Topic archived. No new replies allowed.