Generate array of unique characters in C - c

I'm trying to generate an array of 10 random but unique characters. Sometimes when I run the code, characters are not unique.
I would appreciate any help. Thank you.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
srand(time(NULL));
char letters[10];
for (int i = 0; i < 10; i++) {
letters[i] = 97 + rand() % (122-97);
for (int j = 1; j < i; j++) {
if (letters[i]==letters[j]) {
letters[i] = 97 + rand() % (122-97); // continue
}
}
printf("%c\n", letters[i]);
}
}

The problem is that random can give the same number. It doesn't know you want a different number every time.
This is how to solve this, I am telling you the way, you will need to program it.
Decide what characters are participating, then create an array that holds these characters.
Then roll the dice to give a number within the array range. The number you get is an index to the array, you then take the character in that index.
Then you take the last characters in the array and put in that index, and the next time you roll the dice you limit the max number to one less the original size of the array.
You continue with this algorithm until you get all the required amount of characters.
This algorithm ensures that you get different characters.

Your code checks only if the character you just generated is different from its predecessor in your array ; you should try with a flag like that :
srand(time(NULL));
char letters[10];
int is_unique = 1, i=0,j ;
while (i<10){
is_unique = true;
letters[i] = 'a' + rand() % 26;
for(j=0;j<i;j++){
if (letters[i]==letters[j])
is_unique = false;
}
if (is_unique)
i++;
}
BTW, it's better to replace rand() % (122-97) by simply 26, it's more clear for anyone reading your code.
Another way would be :
srand(time(NULL));
char letters[10];
int j;
for (int i = 0; i < 10; i++)
{
letters[i] = 'a' + rand() % 26;
j=0;
while (j<i) {
if (letters[i]==letters[j]){
letters[i] = 'a' + rand() % 26;
j = 1;
} else
j++;
}
printf("%c\n", letters[i]);
}

Related

Replacing all duplicate numbers in an Array, so that every element is unique in C

The elements in the array are created using rand().
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(void){
int array[6] = { 0 };
srand(time(NULL));
for(int i = 0; i < 6; i++){
array[i] = rand() % 49 + 1;
}
/*
Code to check for duplicates
if duplicate found
duplicate = rand(49);
*/
for(int i = 0; i<6; i++){
printf("[%d]",array[i]);
}
return 0;
}
I don´t really want to sort the array if it makes it easier to find duplicates because the array is for a lottery ticket.
I have tried different methods, but all of them are inefficient and includes a lot of loops.
I had different approaches, but all of them didn´t really work, because what if, the newly created number, is yet again a duplicate? Or if the last number in the array is a duplicate.
So I came with the following approach: The algorithm will create as many areas, as long no number in the array is a duplicate
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define TRUE 1
int main(void) {
int a[6] = {0};
srand(time(NULL));
while (TRUE) {
int c = 0;
for (int i = 0; i < 6; i++) {
a[i] = rand() % 49 + 1;
}
for (int i = 0; i < 6; i++) {
for (int j = i + 1; j < 6; j++) {
if (a[i] == a[j]) {
c++;
}
}
}
if (c == 0) {
break;
}
}
for (int i = 0; i < 6; i++) {
printf("%d\n", a[i]);
}
return 0;
}
Any Ideas, how to make an easy, efficient, but not so complex algorithm for a beginner?
Thanks :)
There's two sort of problems here. Firstly, if you find a duplicate number, you wish it to be replaced by another number. However, it is also possible that the new number could be a duplicate to some other value. I'd suggest adding the following code to your program to check for duplicacy
for(int i = 0; i<6; i++)
{
for(int k=0; k<6; k++)
{
if(i==k)
{
continue;
}
if(array[i] == array[k])
{
array[i]=rand()%49+1;
}
}
}
There is an outer loop to rotate between the 6 elements of the array, and an inner loop to test that element against all other elements of that array. However, you should note that it is entirely possible that the elements 1-5 may end up being duplicated to some other value again.
Edit: The continue; statement iterates the loop for the next value. Its there to make sure no element is tested against itself.

Optimise a code of random number with no repetition in C

I do a code that will display to the screen 10 random numbers with no repetitions. I want to know if we can optimize the code or if you have a better and simple way in order to do this request.
Thanks !!
int main(){
int nbr = 0; srand(time(NULL));
int arr[10], i, j, flag;
for (i = 0; i < 10; i++)
{
do
{
nbr = rand() % 10 + 1;
flag = 1;
for (j = 0; j < i; j ++)
{
if (nbr == arr[j])
{
flag = 0;
break;
}
}
} while (!flag);
arr[i] = nbr;
}
for (i = 0; i < 10; i++)
{
printf("%5d", arr[i]);
}
system("PAUSE");
return 0;
}
So if i get what you're trying to do here it is:
generate an array of numbers between 1 and 10, in a random order (given rand() % 10 + 1)
instead of trial and error I'd suggest the following algorithm:
fill arr with 1...10
shuffle arr
this will run a lot faster
While I agree with the solution provided by Work of Artiz, this will result in the hard question to answer of when to stop shuffling.
To solve this you can use the following solution (which will use more memory, but less clock time):
1 Create an array temp having values 1..10 (ordered, not random)
2 Keep track of the length length of the array (10)
3 Generate a random index rand_i between 0 and length - 1
4 Copy temp[rand_i] to next position in your final array
5 Overwrite temp[rand_i] by temp[length-1]
6 Decrement length
7 Iterate Steps 3 - 6 until your array is filled (10 times)
This will both eliminate your excessive looping, and the problem of when to stop shuffling your array
EDIT: including code
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(){
int nbr = 0; srand(time(NULL));
int arr[10], i, j, flag;
int temp[10] = {1,2,3,4,5,6,7,8,9,10};
int length = 10;
for (i = 0; i < 10; i++)
{
nbr = rand() % length; // Generate random index between 0 and length - 1
arr[i] = temp[nbr]; // Copy value from random index in temp to next index in arr
// Now since temp[nbr] is already taken (copied to arr) we should replace it by last available value in temp (temp[lenght])
// and in the next iteration we will create a value between 0 and length -1. This will keep values not yet taken from temp in
// contiguous order
temp[nbr] = temp[length-1];
length--;
}
for (i = 0; i < 10; i++)
{
printf("%5d", arr[i]);
}
return 0;
}

c programming - printing sequence of sum of squared digits (as an array) for a potential happy number

I have this assignment for my intro to C programming class and part of my code has to find the sequence of the sum of square digits of a number in order to determine after if the given number is a happy number (sum of square digits = 1)
Here's part of my code:
#include <stdio.h>
#include <math.h>
// The sum of square digits function
int sqd (int x) {
int sum = 0;
while (x > 0) {
sum = sum + pow(x%10, 2);
x = x/10;
}
return sum;
}
// The search function
int search (int a[], int val, int size) {
int i;
for (i = 0; i < size; i++) {
if (a[i] == val) {
return 1;
}
}
return 0;
}
// The main program
void main () {
int a [1000] = {0};
int N;
int count = 1;
int j;
printf("Please enter the potential happy number:\n", N);
scanf ("%d", &N);
a[0] = N;
a[count] = sqd (N);
do {
a[count] = sqd (a[count-1]);
count++;
} while (search (a, a[count], count));
for ( j = 0; j <= count; j++) {
printf("%d\n", a[j]);
}
}
It only prints the first three sums in the sequence. I really don't know how to make it work.
Thank you in advance
This line
while (search (a, a[count], count));
makes sure that you break out of the loop after one round since a[1] is not equal toa[0]. You can change that line to be:
while (a[count-1] != 1);
You also need to add a clause to make sure that you stop when the limit of the array is reached. Update that line to be:
while (a[count-1] != 1 && count < 1000 );
And then, change the printing loop to use i < count, not i <= count. Using <= will result in accessing the array out of bounds when the user enters a sad number.
for ( j = 0; j < count; j++){
printf("%d\n", a[j]);
}
Update
After a bit of reading on happy numbers at Wikipedia, I understand why you had call to search in the conditional of the while. The following also works.
} while ( ! (a[count-1] == 1 || search(a, a[count-1], count-1)) );
That will search for the last number in the array but only up to the previous index.

Ordering numbers in an array

# include <stdio.h>
# include <stdlib.h>
# include <time.h>
int main(){
int i, n, A[10] = {0}, B[10] = {0};
srand((unsigned)time(NULL));
printf("Shift how many elements? ");
scanf("%d", &n);
for (i = 0; i < 10; i++){
A[i] = rand()%10;
printf("%d ", A[i]);
}
printf("\n\n\n");
for (i = 0; i < 10; i++){
B[i + n] = A[i]; *******
}
for (i = 0; i < 10; i++){
printf("%d ", B[i]);
}
return 0;
}
I'm trying to write a code that shifts the numbers in the array by a value entered by the user but I really don't know how to deal with the indexing, if the number is at the end and you add number to it it might go outside the array so how do i prevent that. Pay attention
to the line with asterisks. If you can, please give me a brief explanation of array indexing while coding, or hints about what I can do to fix this. I know my mistake however, I just don't know how to fix it.
This language is C and I'm using code::blocks. Thank you!
You can use modulo (%) operator to get the correct indexing:
for (i = 0; i < 10; i++){
B[ (i + n) % 10 ] = A[i];
}
I have used 10 directly. In general, you should use sizeof(B) or actual number of elements in the array if it's less than size of the array.
You can use a wraparound condition for this.
for (i = 0; i < 10; i++){
B[i] = A[(i+n) % 10]; // to shift elements to the left by n
//B[(i+n) % 10 ] = A[i]; // to shift right
}

Nested for/while loops and arrays, filtering out numbers from an array

int main(void)
{
int i,j=0,k; //initialization
char equation[100]; //input is a string (I think?)
int data[3]; //want only 3 numbers to be harvested
printf("Enter an equation: ");
fgets(equation, 100, stdin); //not so sure about fgets()
for (i = 0; i < equation[100]+1; i++) { //main loop which combs through
//"equation" array and attempts
//to find int values and store
while (j <= 2) { //them in "data" array
if (isdigit(equation[i])) {
data[j] = equation[i]
j++;
}
}
if (j == 2) break;
}
for (k = 0; k <= 2; k++) { //this is just to print the results
printf("%d\n", data[k]);
}
return 0;
}
Hello! This is my program for my introductory class in C, I am trying to comb through an array and pluck out the numbers and assign them to another array, which I can then access and manipulate.
However, whenever I run this I get 0 0 0 as my three elements in my "data" array.
I am not sure whether I made an error with my logic or with the array syntax, as I am new to arrays.
Thanks in advance!!! :)
There are a few problems in your code:
for (i = 0; i < equation[100]+1; i++) { should be something like
size_t equ_len = strlen(equation);
for (i = 0; i < equ_len; i++) {
Whatever the input is, the value of equation[100] is uncertain, because char equation[100];, equation only has 100 element, and the last of them is equation[99].
equation[i] = data[j]; should be
data[j] = equation[i];
I suppose you want to store digit in equation to data.
break; should be deleted.
this break; statement will jump out of the while loop, the result is you will store the last digit in equation to data[0] (suppose you have switched data and equation, as pointed out in #2).
If you want the first three digits in equation, you should do something like
equ_len = strlen(equation);
j = 0;
for (i = 0; i < equ_len; i++) {
if (j <= 2 && isdigit(equation[i])) {
data[j] = equation[i];
j++;
}
if (j > 2) break;
}
printf("%d\n", data[k]); should be printf("%c\n", data[k]);
%d will give the ASCII code of data[k], for example, if the value of data[k] is character '1', %d will print 50 (the ASCII code of '1') instead of 1.
Here is my final code, based on the OP code:
#include <ctype.h>
#include <string.h>
#include <stdio.h>
int main(void)
{
int i,j,k;
char equation[100];
int data[3];
int equ_len;
printf("Enter an equation: ");
fgets(equation, 100, stdin);
equ_len = strlen(equation);
j = 0;
for (i = 0; i < equ_len; i++) {
if (j <= 2 && isdigit(equation[i])) {
data[j] = equation[i];
j++;
}
if (j > 2) break;
}
for (k = 0; k <= 2; k++) {
printf("%c\n", data[k]);
}
return 0;
}
Tested with:
$ ./a.out
Enter an equation: 1 + 2 + 3
1
2
3

Resources