Can you improve my code?

Asked

Viewed 59 times

-2

Hi, I’m Vitor, and I’m new to programming and Stack Overflow, and I’d like some tips on how to clean my code. ->Code to read 50 integers between 0 and 9 and count how many times each number was typed.

Note: Ignore code comments.

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

int main(int argc, char *argv[]) {

//Faça um programa que lê 50 valores inteiros entre 0 e 9 e 
//conte quantas vezes cada numero foi digitado.//

    setlocale(LC_ALL,"Portuguese");
    int contador = 0;
    int i = 0;
    int vetor[10];

    for(i = 0 ; i < 10 ; i++){

        vetor[i] = 0;

    }

    printf("Leitura do vetor com 50 números entre 0 e 9\n");

    for(i=0; i <= 10; i++){/*For para limitar numeros entre 0 e 9*/
        if(contador < 50){/*If para percorrer o máximo de vezes pelo laço obedecendo o for de que os numeros seja de 0 á 9*/

            printf("Digite um numero de 0 a 9: ");
            scanf("%d", &i);

            contador++;
            vetor[i]++;
        }
   }


        printf("\nImpressão/n");

        for(i = 0; i < 10; i++){

            printf("\nNúmero %d : %d vezes\n",i,vetor[i]);

        }

        system("pause");
}

1 answer

2

welcome to Stackoverflow.

Assuming you really are a beginner in programming, your code is relatively good. I don’t know where you are or why you joined the program (technical school or college, for example), but there are some ways to improve the code in terms of aesthetics.

I’m also a programming student and confess that I don’t have enough knowledge of algorithmic performance, but I will help in any way I can.

First of all, it is essential to use functions to better divide the problem into small parts. If you are well at first, you don’t have to worry so much, but it’s good to get used to the functions.

For me, his logic is good, just improving a little aesthetics. I’ll leave an example below for you to see how the structure of the same code is made with functions.

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

void readNumbers(int vet[]){
    int cont, num;
    for(cont=0;cont<5;cont++){
        printf("Digite um numero de 0 a 9: ");
        scanf("%d", &num);
        vet[num]++;
    }
    printf("\n");
}

void printTimes(int vet[]){
    int cont;
    for(cont=0;cont<10;cont++)
        printf("O numero %d foi digitado %d vezes.\n", cont, vet[cont]);
}

int main(){
    int typedTimes[10]={0};
    readNumbers(typedTimes);
    printTimes(typedTimes);
}

Remembering again that I’m no performance expert, but you don’t have to worry about that now.

Some changes I made:

  1. I transferred the reading and printout tasks to functions
  2. I removed the library "Math" because I saw no use for your code
  3. I removed the main() arguments, because as a beginner, it’s good you know what it is and what it’s for, but you don’t need to use it now.
  4. When creating the vector, I eliminated what you did to fill the vector with "0", doing all this in the declaration just by assigning the "0" between the { }.

If the idea of functions is too new for you, I recommend that you train a little more without them. Then just browse some content on youtube about the logic of the functions.

I recommend to train the logic of the site of Urionlinejudge, there you find several problems and can take a joke.

Good studies!

Browser other questions tagged

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