-4
Create an algorithm that given a sequence of n real numbers, determine the numbers that make up the sequence and the number of times each of them occurs in it.
Example: n = 8
Sequel:
-1.7,
3.0,
0.0,
1.5,
0.0,
-1.7,
2.3,
-1,7
Exit:
-1.7 ocorre 3 vezes
3.0 ocorre 1 vez
0.0 ocorre 2 vezes
1.5 ocorre 1 vez
2.3 ocorre 1 vez
I did it in the Visualg Code:
ALGORITMO "QUESTAO 9"
VAR
C,X,SEQ,I: INTEIRO
VSR: VETOR[1..100] DE REAL
SEQUENCIA: VETOR[1..100] de REAL
INICIO
ESCREVA("QUANTOS NÚMEROS REAIS COMPÕEM A SEQUÊNCIA? ")
LEIA(SEQ)
ESCREVAL("INFORME A SEQUÊNCIA: ")
PARA I DE 1 ATE SEQ FACA
LEIA(SEQUENCIA[I])
SE (SEQUENCIA[I])<>(VSR[I]) então
VSR[I]<-SEQUENCIA[I]
fimse
fimpara
para X de 1 ate SEQ faca
C <- 0
para I de 1 ate SEQ faca
se (VSR[X] = SEQUENCIA[I]) então
C <- C + 1
fimse
fimpara
escreval(VSR[X], " OCORRE ", C, " VEZES")
fimpara
escreval(" ")
escreval(VSR[X], " OCORRE ",SEQUENCIA[SEQ], " VEZES")
fimalgoritmo
Welcome to Stack Overflow. It seems your question contains some problems. We won’t do your college work or homework, but if you’re having difficulty somewhere specific, share your code, tell us what you’ve tried and what the difficulty is, so you increase your chances of getting a good answer. Be sure to read the Manual on how NOT to ask questions to have a better experience here.
– Leticia Rosa
This answers your question? Get how many times each element of a list repeats
– hkotsubo
Unless a typo has occurred the value -1.7 occurs twice, not three, in your list of 9 elements.
– anonimo
The method
Counter
of libcollections
fix it. See here– Paulo Marques
@Overpeas, it’s hard to see the code on
comments
. Update your post, please.– Paulo Marques