functions with pointers

I am working on functions with pointers that was assigned to me. I think I am missing something with the code. If anyone can help me I would appreciate it.

thanks
R.


Here are the instructions:

Create a program with the following variables, all created in main:
a const short, shMAX containing the value 10
a short, shCnt
an array of long containing 10 elements, lArr
a double variable to store the answer returned by functions you write, dAnswer
a string containing your name, sName
a 2 dimensional array of double, 2 rows and 3 columns, d2Arr

In main we will always store the values returned by non void functions in the variable provided for answers, dAnswer, so the form of all function calls, except void functions, will be:

dAnswer = functionname( .....

The variable will then be printed to the screen using a function called PrintData, which you create below. An example is below:

PrintData( "Average1 returned" , dAnswer);

PrintData and dAnswer are not used for void functions.

Before creating any of the functions below create the function PrintData with the following prototype:

void PrintData( char *sLabel, double dAnswer );
or
void PrintData( char sLabel[], double dAnswer );


Create the following functions and call each from main:

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.

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.

Here is my 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
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
  #include <iostream>
#include <string.h>
#include <time.h>

//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 = "randy";
	double d2Arr[2][3];


//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] << " ";
	}
}


void FillArray(long array[], long size)
{
	for(int count = 0; count < size; count++)
	{
		array[count] = rand();
	}
}


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;

}
closed account (iAk3T05o)
Interesting string.h headers. Remove the .h.
Nathan:
Thanks for your reply but taking the .h off does not make a difference. The code still has problems.

R.
Topic archived. No new replies allowed.