Home > Net >  How do I queue numbers in Numbers.txt? and if the tens digit is 0, how do I print 1 in the tens digi
How do I queue numbers in Numbers.txt? and if the tens digit is 0, how do I print 1 in the tens digi

Time:12-22

Random numbers are printed in "numbers.txt". "numbers.txt" exists as a single line. The values here will be taken as two digits and assigned to the queue. I'm having trouble with the while part.

When the numbers in the Numbers.txt file are separated by two digits, I want to make the 0 in the tens digit a 1.

Example

numbers.txt :

839186660286459132876040232609

Output:

two-digit
83 91 86 66 2 86 45 91 32 87 60 40 23 26 9.

As you can see 02 and 09 written as 2 and 9. i want to 12 and 19.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 30

typedef struct stack
{
int value;
} Stack;

int *first, *last, *queue;
void kuyrukList()
{
  printf("\nKuyruktaki Elemeanlar\n");
  int *temp = first;
  while (temp < last)
  {
    printf("%d ", *temp);
    temp  ;
  }
}

void insert(int value)
{
  *last = value;
  last  ;
}
int main()
{
//Random number.
srand(time(NULL));
int text[30] = {0};

FILE *dosyaYaz = fopen("numbers.txt", "w");
printf("\nOlusturulan number degeri:\n");
for (int i = 0; i < SIZE; i  )
{
    text[i] = (rand() % 10);

    printf("%d", text[i]);

    fprintf(dosyaYaz, "%d", text[i]);
}
fclose(dosyaYaz);
printf("\n ");
//***********************************

char ch;
int number = 0;
int counter = 1;

queue = (int *)malloc(sizeof(int) * SIZE);
first = queue;
last = queue;

FILE *dosyaAc = fopen("numbers.txt", "r");
if (dosyaAc == NULL)
{
    printf("\nDosya bulunamadi.\n");
    exit(0);
}
while ((ch = fgetc(dosyaAc)) != -1)
{
    if (counter % 2 == 1)
    {           
        number  = (ch - '0') * 10;
    }
    if (counter % 2 == 0)
    {
        number  = (ch - '0');
        
        insert(number);
        number = 0;
    }
    
    counter  ;      
}

fclose(dosyaAc);

kuyrukList();
return 0;
}

CodePudding user response:

So you are creating random numbers, but afterwards you modify them when they are smaller than 10? The easiest solution is to create random numbers who only vary from 10 to 99. This can be done as follows:

Imagine that double rand() generates a random number from 0 to 1 (both never being generated).
Then, 90 * rand() generates a random number from 0 to 90 (both never being generated).
Then, 10 90 * rand() generates a random number from 10 to 100 (both never being generated).
Then, (int)(10 90 * rand()) generates a random natural number, from 10 to 99 (both might be generated because of the rounding mechanism).

CodePudding user response:

It appears from your stated goals that an array of 15 numbers ranging from 10 - 99 is the need. If that is true, skip writing to a file, and reading them back as a intermediate step and just create an array of 15, two digit numbers directly.
To do this, consider using a function to accept range and offset parameters (upper and lower values) and an array sized for each of the values to use with configuring rand(),.

The following can serve as the core of what you are doing by generating an array of pseudo randoms in the range you specify:

//generate an array of pseudo random values between lower and upper values
void gen_rand(int lower, int upper, int count, int *arr) 
{ 
   int i;
   for (i = 0; i < count; i  ) 
   {
      arr[i] = (rand() % (upper - lower   1))   lower;
   }
}

int main(void)
{
    int arr[15];
    srand(time(NULL));
    gen_rand(10, 99, 15, arr);
    return 0;
}
  • Related