Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Using rand function to generate, Construct an array of upper case letters of size (at least 20) not more than 30.

This is what my program is.. to generate an element between the range from 65 to 91 and cast it, can get any letter from A to Z... After the generation, using only one loop, try to move all the letters smaller than letter 'M' to the left hand side of the array and all the letters greater than 'M' to the right side of the array..

the output should be:

Given the follwing array:
A D F Y H J I O L B G E M Y R W A Q (Random)

iterative swap of array
??????

Given the follwing array:
A D F Y H J I O L B G E M Y R W A Q (Random)

Recursive swap of array
????????

help me with my code below

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

void characters(char[]);
void printChar(char[]);
void swapChar(char[]);

int main ()
{
	char c[26];
	
	characters(c);
	cout << "Given the following array" << endl;
	printChar(c);
	swapChar(c);
	cout << "Iterative swap of array" << endl;
	printChar(c);

}

void characters(char c[])
{
	int seed;
	
	int sum = rand();
	srand(time(NULL));
	
	for(int i = 65; i <= 90; i++)
	{
		c[sum] = i;	   	   	   
		sum++;
	}

}

void printChar(char c[])
{
	for(int i = 0; i <= 25; i++)
	{
		
		cout << c[i] << " ";
	}	 
		cout << endl;
}

void swapChar(char c[])
{
	int temp;
	for(int i = 0; i <= 25; i++)
	{
		if(c[i] > c[12])
		{
			temp = c[i];
			c[i] = c[26-i-1];
			c[26-i-1] = temp;
		}
		
	}
}
Posted

I think error is in characters(char []) function.

Try this:
void characters(char c[])
{
	bool used[25];

	memset(used, 0, 25 * sizeof(bool));
	srand(time(NULL));
	
	for(int i = 65; i <= 90; i++)
	{		
		int index;

		do {
			index = rand() % 25;
		} while (used[index]);

		used[index] = true;
		c[index] = i;
	}

}
 
Share this answer
 
Comments
Mzbarz1986 16-Aug-10 12:28pm    
Will the whole program work???
Nyarost 16-Aug-10 12:30pm    
Well, didn't check all code. But you'll at least get correct randomized array. Your version heads to access violation. You can give it a try.
Mzbarz1986 16-Aug-10 12:31pm    
Did tried it out... but no output.... blank.. waiting.. somthing is wrong.. somewhere
without swapChar() it should look like this

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cstring>

using namespace std;

void characters(char[]);
void printChar(char[]);
void swapChar(char[]);

int main ()
{
	char c[26];
	
	characters(c);
	cout << "Given the following array" << endl;
	printChar(c);
	swapChar(c);
	cout << "Iterative swap of array" << endl;
	printChar(c);
	return 0;
}

void characters(char c[])
{
	bool used[26];

	memset(used, 0, 26 * sizeof(bool));
	srand(time(NULL));
	
	for(int i = 65; i <= 90; i++)
	{		
		int index;

		do {
			index = rand() % 26;
		} while (used[index]);

		used[index] = true;
		c[index] = i;
	}

}

void printChar(char c[])
{
	for(int i = 0; i <= 25; i++)
	{
		
		cout << c[i] << " ";
	}	 
	cout << endl;
}


The only thing left is this function. Homework? :)
 
Share this answer
 
v3
Comments
Mzbarz1986 16-Aug-10 12:42pm    
How to do the swapChar function to swap the first set of characters to iterative swap????

Nah.. trying out a program my friend sent me...
Nyarost 16-Aug-10 12:50pm    
Remember, you have to also think by yourself ;)
Mzbarz1986 16-Aug-10 12:52pm    
trying too.. lol... i'm not as great as my friend was.. he gave me some pointers.. but still dont understand the swap.. i've got learned bit of the swap using 26-i-1
Mzbarz1986 16-Aug-10 12:55pm    
I just need to complete this by tomorrow morning to show him.. this is how far as i can do
Some kind of this, I think:

void swapChar(char c[])
{
	int temp;
	int l_index = 0;	

	for(int i = 0; i <= 25; i++)
	{
		if (c[i] < 'M') {
			temp = c[i];
			c[i] = c[l_index];
			c[l_index++] = temp;
		}
	}
}
 
Share this answer
 
Rather than messing about with hand rolled loops why not try the magic of the standard C++ library?

-std::random_shuffle can give you a random vector of capital letters:

const char letters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
std::vector<char> stirred( letters, letters + 26 );
std::random_shuffle( stirred.begin(), stirred.end() );</char>


- std::partition can give you greater than 'M' stuff:

bool less_than_M( char c )
{
    return c < 'M';
}

std::partition( stirred.begin(), stirred.end(), less_than_M );

You really don't want to fiddle about with low level stuff like pointers and arrays when there are better ways of representing your data and doing the operations you want.

Cheers,

Ash

Edited as I got the sign in less_than_M the wrong way 'round, complete spanner that I am...
 
Share this answer
 
v2
Comments
Dalek Dave 16-Aug-10 18:05pm    
Good call.
Nuri Ismail 17-Aug-10 5:11am    
Reason for my vote of 5
Good demonstration of some standard C++ library features. :)
Sauro Viti 17-Aug-10 5:31am    
Reason for my vote of 5
Very good: the use of STL make the code short, safe and easy to understand

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900