britoOo Posted March 21, 2015 at 03:23 PM Report Share #579927 Posted March 21, 2015 at 03:23 PM (edited) Pessoal estou com dúvidas num exercicio. o exercicio pede para introduzir um número e dizer quantos 1 tem em binário. por exemplo: 10/2 -> 0 5/2 -> 1 2/2 -> 0 1 ou seja tem 2 numeros 1. Eu fiz este código compila mas a soma não está a funcionar não sei porque, continua sempre com '0'. #include <stdio.h> int bitsUm(unsigned int n){ int aux,soma; while(n>=0){ if(n%2==1){ soma=soma+1; aux=n/2; n=aux; } else { aux=(n/2); n=aux; } return soma; } } int main() { int n,soma; printf("numero: \n"); scanf("%d",&n); printf("o numero em binario '%d' apresanta '%d' numeros 1 \n",n,bitsUm(soma)); return 0; } na consola dá isto numero: 10 o numero em binario '10' apresanta '0' numeros 1 Obrigado pela atenção! BritoO Edited March 21, 2015 at 05:13 PM by thoga31 Tags code + GeSHi Link to comment Share on other sites More sharing options...
pokker Posted March 21, 2015 at 03:41 PM Report Share #579928 Posted March 21, 2015 at 03:41 PM Só assim por alto, na função main só tas a ler a variavel "n" e a passar para a função bitsUm a variavel soma que não estás a atribuir valor nenhum Link to comment Share on other sites More sharing options...
britoOo Posted March 21, 2015 at 04:00 PM Author Report Share #579929 Posted March 21, 2015 at 04:00 PM Só assim por alto, na função main só tas a ler a variavel "n" e a passar para a função bitsUm a variavel soma que não estás a atribuir valor nenhum o valor da soma não devia de vir da função ? if resto der 1 soma 1? Link to comment Share on other sites More sharing options...
britoOo Posted March 21, 2015 at 04:43 PM Author Report Share #579936 Posted March 21, 2015 at 04:43 PM (edited) Obrigado pela ajuda já consegui fazer! #include <stdio.h> int bitsUm(unsigned int n){ int aux,soma=0; while(n>0){ if(n%2==1){ soma=soma+1; n=n/2; } else { n=n/2; } } printf("O numero de uns e: %d \n",soma); return soma; } int main() { int n; printf("numero: \n"); scanf("%d",&n); bitsUm(n); return 0; } Edited March 21, 2015 at 05:13 PM by thoga31 Tags code + GeSHi Link to comment Share on other sites More sharing options...
HappyHippyHippo Posted March 21, 2015 at 07:27 PM Report Share #579957 Posted March 21, 2015 at 07:27 PM para referência de quem procurar uma solução : #include <stdio.h> unsigned int bitsUm(unsigned int n){ unsigned int soma = 0; while(n) { soma += n % 2; n >>= 1; } return soma; } int main(void) { unsigned int n; printf("numero: \n"); if (scanf("%u", &n)) printf("O numero de uns e: %d\n", bitsUm(n)); return 0; } IRC : sim, é algo que ainda existe >> #p@p Portugol Plus Link to comment Share on other sites More sharing options...
Luís Paiva Posted March 4, 2019 at 02:16 PM Report Share #613929 Posted March 4, 2019 at 02:16 PM @HappyHippyHippo não seria suficiente isto: int bitsUm(unsigned x) { int r=0; while (x != 0){ if (x%2 == 1) r++; x = x/2; } return r; } eu fiz este programa mas retorna-me valor 1. Link to comment Share on other sites More sharing options...
HappyHippyHippo Posted March 24, 2019 at 01:50 PM Report Share #614099 Posted March 24, 2019 at 01:50 PM On 3/4/2019 at 2:16 PM, Luís Paiva said: @HappyHippyHippo não seria suficiente isto: int bitsUm(unsigned x) { int r=0; while (x != 0){ if (x%2 == 1) r++; x = x/2; } return r; } eu fiz este programa mas retorna-me valor 1. e o que achas que o código que apresentei faz (de uma forma mais simples) ? IRC : sim, é algo que ainda existe >> #p@p Portugol Plus Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now