I am exploring to add an element, ASCII letters to my random generated 1D array to form new array. How can I use <cstring> header to achieve my result.

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

const int MAX = 10; 


void constructSet1 (char*, int);


int main ()
{

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

	srand (time(NULL));
	
	cout << "Element A: ";
	constructSet1 (a, 10);
	
	cout << endl;
	
        cout << "Enter a ASCII letter: ";
        
        //cin <<

        cout << endl;
        cout << "Final Array: ";
}

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

	
void insertElement ()
{





}

I am not sure whats your problem? Do you want to generate random
character, if so then use something like this :

//returns a "random" letter
char getRandomChar(){
 return rand() % ('z' - 'a') + 'a'
}

I wanted to insert an element using insertElement () function to
my already generated char array, constructSet1 () function.

User will get a prompt to cin the ASCII value (Eg, B)

I am not sure whats your problem? Do you want to generate random
character, if so then use something like this :

//returns a "random" letter
char getRandomChar(){
 return rand() % ('z' - 'a') + 'a'
}

Take a look at the example on this page. Is that what you are trying to do, except with digits?

Something similar. If you compile my code, you will see something like this:

ElementA: {A,N,B,X,J,}

I wanted to add in another char in ElementA provided the element does not exist in it.

User will key in a letterto be inserted to ElementA.

The result will be if user enter 'C':

ElementFinal: {A,N,B,C,X,J}

Take a look at the example on this page. Is that what you are trying to do, except with digits?

I'm assuming you pick a random position? There may be a shorter way but you probably have to use strcat with something like strncpy, etc. I don't want to steer you incorrectly, so try it and see what you can come up with and someone will probably be able to help you optimize it.

(1) My program generate random list of letters.
(2) Prompt user to enter a letter to be included in the (1)
(3) Output the final set of char array.

so you need a function of this sort :

//char *Array is the array that will be inserted with value
// const int Size is the maximum size of the Array
//const int where is the index where the char value will be inserted
//const char value is the actual value that will be inserted
void insert(char *Array, const int Size,const int where, const char value){
 //logic goes here
}

Looks like it. How do I get started?

so you need a function of this sort :

//char *Array is the array that will be inserted with value
// const int Size is the maximum size of the Array
//const int where is the index where the char value will be inserted
//const char value is the actual value that will be inserted
void insert(char *Array, const int Size,const int where, const char value){
 //logic goes here
}

Well try it on paper. Draw picture and see what you have to do. Then
report back.

Since you are using an array to emulate a dynamically sized container, I would definitely suggest you use a string type or container type from the standard template library. This way, you can be sure that you won't overrun your array, and you don't have to allocate large storage and re-allocate when you run out

#include <deque>
using namespace std;
int main(){
    deque<char> container;
    int n = 1000000;
    for( int i=0; i<n; i++ )
        container.push_front( ( rand() % ( 'z' - 'a' ) ) + 'a' );
}

This will stuff one million random characters into the dynamically sizing deque. You could use the simpler vector or string types, but they only support push_back(), so you would have to reverse the container before you could print its contents. If you are really going to use c++ to its full potential, you should definitely check out the standard template library. There are a bunch of goodies in there that can save you all kinds of time and seg faults!

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.