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

Recommended Answers

All 17 Replies

what in the world is line 41 supposed to do -- it makes no sense??? Line 42 is pretty much meaningless too.

Very simple to count the number of characters in the array

int count = 0;
for(int i = 0; i < size; i++)
{
    if( a[i]  != 0 ) // if not at end of string
         count++;
}

line 55 is also not going to do anything because variable size was passed by value, not by reference, so the value of that variable in main() will be unchanged when the ConstructSet1() returns.

For line 41 & 42, I am actually trying to perform count for char array.
line 55 is the no. of elements generated upon execution

Reattached code, something still wrong here.

#include <iostream>
#include <ctime>
#include <cstring>
#include <cstdlib>
using namespace std;

const int MAX = 10; 


void constructSet1 (char*, int);
int countElements (char*, int);


int main ()
{

	char x[MAX];
	char* a = x;

	srand (time(NULL));
	
	cout << "Element A: ";
	constructSet1 (a, 10);
	cout << endl;
	
	cout << countElements (a,10);
	
	cout << endl;
	

}



//Count the no. of elements
int countElements (char* a, int size)
{
	
	 
      int count = 0;
   
      for(int i = 0; i < size; i++)
   
      {
   
      if( a[i] != 0 ) // if not at end of string
   	
	
       count++;
  
  	  cout << 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 << "}";
}

what in the world is line 41 supposed to do -- it makes no sense??? Line 42 is pretty much meaningless too.

Very simple to count the number of characters in the array

int count = 0;
for(int i = 0; i < size; i++)
{
    if( a[i]  != 0 ) // if not at end of string
         count++;
}

line 55 is also not going to do anything because variable size was passed by value, not by reference, so the value of that variable in main() will be unchanged when the ConstructSet1() returns.

In your construct method add a null terminator to your string (so at the index after the last used space add '\0' then use a ! = '\0' instead of a !=0 ( = 0 might work, but I don't know for sure offhand). So essentially give your counting routine something to end on. It's probably a little bit safer too.

You have forgotten to return a value from the countElements() function. Isn't the compiler saying anything about that?

I had added. How do I call the function in main function?

#include <iostream>
#include <ctime>
#include <cstring>
#include <cstdlib>
using namespace std;

const int MAX = 10; 


void constructSet1 (char*, int);
int countElements (char*, int);


int main ()
{

	char x[MAX];
	char* a = x;

	srand (time(NULL));
	
	cout << "Element A: ";
	constructSet1 (a, 10);
	cout << endl;
	
	cout << countElements (a,MAX);
	
	cout << endl;
	

}



//Count the no. of elements
int countElements (char* a, int size)
{
	
	 
      int count = 0;
   
      for (int i = 0; i < size; i++)
   
      {
   
      if ( a[i] = 0) // if not at end of string
   	
	
       count++;
  	  
  	  //cout << 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++)
	{
	
		if ( a[i] != '\0');
	
		a[i] = static_cast <char> (rand () % 26 + 65);
		
				
		cout << a [i];
		
		if (i < size - 1)
		cout << ",";
		
	}
	
	
	cout << "}";
}

what in the world is line 41 supposed to do -- it makes no sense??? Line 42 is pretty much meaningless too.

Very simple to count the number of characters in the array

int count = 0;
for(int i = 0; i < size; i++)
{
    if( a[i]  != 0 ) // if not at end of string
         count++;
}

line 55 is also not going to do anything because variable size was passed by value, not by reference, so the value of that variable in main() will be unchanged when the ConstructSet1() returns.

In your construct method add a null terminator to your string (so at the index after the last used space add '\0' then use a ! = '\0' instead of a !=0 ( = 0 might work, but I don't know for sure offhand). So essentially give your counting routine something to end on. It's probably a little bit safer too.

delete line 70 -- yet another useless and sensless statement.

You have forgotten to return a value from the countElements() function. Isn't the compiler saying anything about that?

I am able to compile. Which value?

I am able to compile. Which value?

Well it's up to you, but wouldn't it be suitable to return the number of elements in the array?

I meant what is wrong. It is not displaying the no of elements in the array?

Well it's up to you, but wouldn't it be suitable to return the number of elements in the array?

I meant what is wrong. It is not displaying the no of elements in the array?

In the main() function, you have

cout << countElements (a,MAX);

What will it print, if you don't specify any return value in countElements() ?

>>I meant what is wrong.

What is wrong is that the function was declared to return an integer, but it does not return anything. If you don't want that function to return an int then change it to a void function void CountElements() , and if you do that then you can not use the function in the cout statement mentioned in the above post.

Well it's up to you, but wouldn't it be suitable to return the number of elements in the array?

In the main() function, you have

cout << countElements (a,MAX);

What will it print, if you don't specify any return value in countElements() ?

It will print 0

My code:

int countElements (char* a)
{
	
	  	
      int count = 0;
	  int size;
   
      for (int i = 0; i < size; i++)
   
      {
   
      if ( a[i] = 0) 

	
       count++;
  	  
  	  return count;
      }
}

>>I meant what is wrong.

What is wrong is that the function was declared to return an integer, but it does not return anything. If you don't want that function to return an int then change it to a void function void CountElements() , and if you do that then you can not use the function in the cout statement mentioned in the above post.

I want count the elements in a srand generated array.

For me, the basic premise is lacking. You don't need to count the members of an array because you know how many elements it has when you declare it. Or if it is dynamic, you know how many elements when you ask for them. Why bother counting?

Because instruction was given and I have to follow the rule.

For me, the basic premise is lacking. You don't need to count the members of an array because you know how many elements it has when you declare it. Or if it is dynamic, you know how many elements when you ask for them. Why bother counting?

Your loop goes exactly once because you return "every" iteration. Assignment is = , comparison is == .

for ( int i = 0; i < size; i++ )
   {
      if ( a[i] = 0 )
         count++;
      return count;
   }

So the if statement is always false, and you return immediately. Lemme guess -- the function always returns 0?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.