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.
#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);
usingnamespace std;
int main()
{
//variables used
constshort 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;
}
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.