I want count the elements in a srand generated array. The array is char array. Can anyone advise me on my void countElements (char* a, int size) function as it don't work? TIA.
#include <iostream>
#include <ctime>
#include <cstring>
#include <cstdlib>
using namespace std;
const int MAX = 10;
void constructSet1 (char*, int);
void countElements (char*, int);
int main ()
{
char x[MAX];
char* a = x;
srand (time(NULL));
cout << "Element A: ";
constructSet1 (a, 10);
countElements (a, 10);
cout << endl;
}
//This function don't work
//Count the no. of elements
void countElements (char* a, int size)
{
for (int i = 0; i < size; i++)
{
a[i] = *(reinterpret_cast <char*> (a [i]));
int count = sizeof(a [i])/sizeof(a [0]);
cout << "No of elements: " << count;
}
}
//Construct Element A
void constructSet1 (char* a, int size)
{
//Get random size of 2 - 10
size = rand () % 9 + 2;
cout << "{";
for (int i = 0; i < size; i++)
{
a[i] = static_cast <char> (rand () % 26 + 65);
cout << a [i];
if (i < size - 1)
cout << ",";
}
cout << "}";
}