C - Array of indeterminate size

Asked

Viewed 144 times

-2

How to print x | fi correctly?
If the user informs...
0 2 1 2 3 1 2 2 3 4
He should order so...
0 1 1 2 2 2 3 3 4
And print...
X | fi
0 | 1
1 | 2
2 | 4
3 | 2
4 | 1
That is to say:

0 appears 1 time
1 appears 2 times
2 appears 4 times
3 appears 2 times
4 appears 1 time

The code I started with...
Which print the table without any problem, and using gets to capture the input.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (){

int arr[10] = {0};
char num[200] = {'\0'};
size_t i;

printf("Informe um numero: ");
gets(num);

for ( i = 0; i < strlen(num); i++ )
    arr[ num[i] - '0' ]++;

printf("\nEste numero possui:\n\n");

for ( i = 0; i < 10; i++ )
    if (arr[i])
        printf("%d digitos %d\n", arr[i], i);

return 0;
}

Here where the defects begin, when trying to sort linearly the table type (x | fi) stops working and using scanf("%[ n]s", n) instead of gets. Because when using gets with Sort, the vector ordered in a linear way was with a lot of 0 and the correct table. But when I use scanf or even fgets the linear ordered vector is correct and the table misses.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void sort(int *array, int size);

void sort(int *array, int size){
int x, y, value;
for(x = 1; x < size; x++){
    value = array[x];
    for(y = x - 1; y >= 0 && array[y] > value; y--){
        array[y+1] = array[y];
    }
    array[y+1] = value;
}
}
int main (){
const int BFF = 200;
char num[BFF]={'\0'};
int arr[BFF]={0};
int tam, l;
size_t i;
// Inicializa os vetores
memset(arr, 0, BFF * sizeof(int));
memset(num, '{BARRA}0', BFF * sizeof(char));
// Pega os dados
printf("Informe a amostra: \n");
scanf("%[^\n]s", num);

// Preenche o vetor com elementos válidos
for (int i = 0; i < strlen(num); ++i) {
    if (isdigit(num[i])) {
        // Converte char para int
        arr[tam] = num[i] - '0';
        tam += 1;
    }
 }
sort(arr,tam);
// Exibe algumas informações
printf("\n");
printf("Tamanho: %d \n", tam);
printf("Amostra ordenada: \n");

// Imprime o vetor já ordenado
for(l=0;l<tam;l++)
        printf("%d ",arr[l]);/*
for ( i = 0; i < tam; i++ )
    arr[ num[i] - '0' ]++;*/

printf("\nX | fi\n");
for ( i = 0; i < 10; i++ )
    if (arr[i])
        printf("%d | %d\n", i, arr[i]);
}

Here with fgets

#include <stdio.h>
#include <stdlib.h>

int main ()
{
const int BFF = 200;
char num[BFF]={'\0'};
int arr[BFF]={0};
size_t i = 0;

printf("Informe a amostra: ");
fgets(num, BFF, stdin);
printf("\n Amostra ordenada:\n");
for(i=0;i<BFF;i++)
        printf("%d ",arr[i]);
for ( i = 0; i < BFF; i++ )
    arr[ num[i] - '0' ]++;

printf("\nX | fi\n");
for ( i = 0; i < 10; i++ )
    if (arr[i])
        printf("%d | %d\n", i, arr[i]);
}

I was told to use the Strtok function. First reading everything in a string, then separating the numbers read using Strtok and then to convert the number that is string to integer. But I have no idea how to do that.
Please help me.

  • Why do you treat your input numbers as a string? Where is the ordering code?

  • Not to have to inform several scanf and capture everything in one input. Wow, no void Sort(); and the two is from main?

1 answer

0

1- Since the vector has no given size, use the malloc() or calloc function().

2- You can read and sort the vector. But you can also read and then sort the vector. Some algorithms are: Bubble Sort and Selection Sort. Here’s an example: https://www.cs.usfca.edu/~Galles/Visualization/Algorithms.html.

3-Your reading should be of integer numbers (by which I etendi). But you are using string.

4- Now yes you count the numbers.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.