Hello people :)

My teacher gave me a new project to work on.
I need to write an application that output all the letters of the alphabet in a random order.
I've made some applications before but im still kinda new to this.
Could any of you give me a hint or put me in the right direction?

Kind regards,
Jelmund

PS: i already searched on the internet

Recommended Answers

All 12 Replies

Hello people :)

My teacher gave me a new project to work on.
I need to write an application that output all the letters of the alphabet in a random order.
I've made some applications before but im still kinda new to this.
Could any of you give me a hint or put me in the right direction?

Kind regards,
Jelmund

PS: i already searched on the internet

What do you have so far?

I will get you started at least. First, create an array to hold all of the characters of the alphabet. Next, use the rand function to return a random sequence. Last, run the array through a for loop and print out the results.

If you need help with the code, post what you already have done.

>>PS: i already searched on the internet

Then obviously you haven't searched at daniweb.

Anyhow, do you first know how to generate a number from a min to max?

Also:
you need to know variable type casting. Specifically how to convert between the int and char types. A table that shows the relationship between integer and characters (for English alphabet) is here:
http://www.asciitable.com
an example for assign lowercase 'a': char letterA = (char) 97
You need this because the random variable generator will be numerical based.

Using the ascii table you can setup a range for a random number generator function as well such that the result can be directly cast to a letter.

The trickier part I think will be making sure that no duplicate letters are made when outputting the random alphabet. You need to find a way after getting the random character, to compare it to all previously placed random characters, before adding it to the random alphabet output.

Im working on it now and i will post my progress later on,
but first i just got to thank you for your help!
I will do as much as i can to contribute to this nice community

Jelmund

Hello guys,

Im trying to fix the application for a few days now, and i just cant get to the answer.
I also asked other people on school and we could find the answer together.
Can any of you help me just a little bit more?
This is just my first assignment and i cant figure out how to make the application work successfully.

Greeds Jelmund

It isn't all that hard:
-- declare a variable to hold the first letter
-- make a loop that has 25-26 iteration (depends on how you design the loop)
-- print the variable
-- increment the variable
-- return 0 :)
-- simple

I will write the program out, but it really is not that hard. This is the source code:

#include <stdio.h>
int main() {
char letters[26] = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};

for (int i=0; i<26; i++) {
char n = rand(letters[i]);
sprintf(letters[n], "%s");
}

I am rusty with C so if anyone wants to look over the code and check that it is right, appreciate it.

I will write the program out, but it really is not that hard. This is the source code:

#include <stdio.h>
int main() {
char letters[26] = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};

for (int i=0; i<26; i++) {
char n = rand(letters[i]);
sprintf(letters[n], "%s");
}

I am rusty with C so if anyone wants to look over the code and check that it is right, appreciate it.

Did you try that out ? I see problems with that.

got this so far:

#include <stdio.h>
int main() {
char letters[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',};
 
char i;
for(i=0 i<26 i++); 
{
char i = rand(letters[i]);
printf("%d", &letters[i]);
                   } 
}

got one error left:
syntax error before "i"

can i ask for your help one more time:)?

Jelmund

You have 1 extra comma in your array initialize list.

And this code :

char i = rand(letters[i]);

is wrong. rand does not take anything it its parameter.

This is how to use it.

int randomNumber = rand();

Also there is a subtle bug in your program.

1.
#include <stdio.h>
2.
int main() {
3.
char letters[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',};
4.

5.
char i;
6.
for(i=0 i<26 i++);
7.
{
8.
char i = rand(letters);
9.
printf("%d", &letters);
10.
}
11.
}

There is an extra comma at the end of the array list as was pointed out by firstPerson. You also need to initialize the 'i' in your for loop. For the last part, you want to use a seed random number generator so that the random value is different every time the program is run.

#include <iostream>
#include <cstdlib>
const char Letters[] = "abcdefghijklmnopqrstuvwxyz";
srand(time(0) );
int rand_value = rand() % 26; // generate random value between 0 and 25
  
for ( int i(0); i != 5; ++i )
{
      std::cout << Letters[rand()%26];
}
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.