Upper Posted November 1, 2009 at 06:36 PM Report Share #294286 Posted November 1, 2009 at 06:36 PM Oi boas Tou com uma duvida preciso de criar um programa que receba um vector de x elementos do tipo float e faça a soma desses mesmos elementos de momento tenho este codigo: #include <stdio.h> int main(){ int n; int ind; printf("Quantos elementos e que sao: "); scanf(" %d", &n); int v[10]; for(ind = 0; ind < n; ind ++){ v[ind]=0; printf("Elemento %d = ",(ind+1)); scanf(" %d", &v[ind]); } float total=0; for(ind = 0 ; ind < n; ind++); { total += v[ind]; } printf(" %f", total); } So que ele nao efectua bem a soma do total. ALguem me pode ajudar? Link to comment Share on other sites More sharing options...
bruno1234 Posted November 1, 2009 at 06:43 PM Report Share #294288 Posted November 1, 2009 at 06:43 PM Talvez esteja a converter este resultado para int: total += v[ind]; experimenta fazer assim: total = (float) (total + v[ind]); é a unica coisa q me ocorre neste momento. Matraquilhos para Android. Gratuito na Play Store. https://play.google.com/store/apps/details?id=pt.bca.matraquilhos Link to comment Share on other sites More sharing options...
Upper Posted November 1, 2009 at 06:48 PM Author Report Share #294291 Posted November 1, 2009 at 06:48 PM depois dessa alteração experimentei fazer com 1 elemento e sendo esse o 3 o total apareceu.me 1.000000 Link to comment Share on other sites More sharing options...
pedrosorio Posted November 1, 2009 at 06:50 PM Report Share #294292 Posted November 1, 2009 at 06:50 PM Parece-me evidente que o erro é estares a pedir um inteiro quando devias pedir um float no scanf (i.e. scanf(" %f",&v[ind])) Não respondo a dúvidas por mensagem. Link to comment Share on other sites More sharing options...
Upper Posted November 1, 2009 at 06:51 PM Author Report Share #294293 Posted November 1, 2009 at 06:51 PM continua a fazer a mesma coisa Link to comment Share on other sites More sharing options...
pedrosorio Posted November 1, 2009 at 06:54 PM Report Share #294294 Posted November 1, 2009 at 06:54 PM continua a fazer a mesma coisa Já agora mete scanf(" %f", &(v[ind])); e não precisas de pôr o vector a zeros porque estás a pedir os valores ao utilizador. Não respondo a dúvidas por mensagem. Link to comment Share on other sites More sharing options...
bruno1234 Posted November 1, 2009 at 06:55 PM Report Share #294295 Posted November 1, 2009 at 06:55 PM Mas o array v é suposto ser inteiros ou floats? Tens o array declarado como int v[10]. Matraquilhos para Android. Gratuito na Play Store. https://play.google.com/store/apps/details?id=pt.bca.matraquilhos Link to comment Share on other sites More sharing options...
Upper Posted November 1, 2009 at 06:57 PM Author Report Share #294296 Posted November 1, 2009 at 06:57 PM #include <stdio.h> int main(){ int n; int ind; printf("Quantos elementos e que sao: "); scanf(" %d", &n); float v[10]; for(ind = 0; ind < n; ind ++){ printf("Elemento %d = ",(ind+1)); scanf(" %f", &v[ind]); } float total=0; for(ind = 0 ; ind < n; ind++); { total = (float) (total + v[ind]); } printf(" %f", total); } Continua com o mesmo erro, mesmo depois das duas modificacoes Link to comment Share on other sites More sharing options...
pedrosorio Posted November 1, 2009 at 06:57 PM Report Share #294297 Posted November 1, 2009 at 06:57 PM Mas o array v é suposto ser inteiros ou floats? Tens o array declarado como int v[10]. Fail da minha parte em não ter reparado nisso. São esses dois erros, o %d e o facto de declares o array como sendo de inteiros. Não respondo a dúvidas por mensagem. Link to comment Share on other sites More sharing options...
Upper Posted November 1, 2009 at 07:01 PM Author Report Share #294299 Posted November 1, 2009 at 07:01 PM Mas eu ja corrigi esses erros como esta explicito no meu post anterior e o erro e o mesmo Link to comment Share on other sites More sharing options...
pedrosorio Posted November 1, 2009 at 07:04 PM Report Share #294300 Posted November 1, 2009 at 07:04 PM Mas eu ja corrigi esses erros como esta explicito no meu post anterior e o erro e o mesmo E já recompilaste? xD Não respondo a dúvidas por mensagem. Link to comment Share on other sites More sharing options...
Upper Posted November 1, 2009 at 07:06 PM Author Report Share #294302 Posted November 1, 2009 at 07:06 PM E já recompilaste? xD Claro!! Link to comment Share on other sites More sharing options...
pedrosorio Posted November 1, 2009 at 07:11 PM Report Share #294303 Posted November 1, 2009 at 07:11 PM Não deve ser mas mete no scanf: scanf(" %f", v + ind); Não respondo a dúvidas por mensagem. Link to comment Share on other sites More sharing options...
Rui Carlos Posted November 1, 2009 at 07:12 PM Report Share #294304 Posted November 1, 2009 at 07:12 PM Tens um ; a mais depois do segundo ciclo for. Rui Carlos Gonçalves Link to comment Share on other sites More sharing options...
Upper Posted November 1, 2009 at 07:13 PM Author Report Share #294305 Posted November 1, 2009 at 07:13 PM Ja vi era o segundo ; Obrigado a todos Link to comment Share on other sites More sharing options...
Upper Posted November 1, 2009 at 07:22 PM Author Report Share #294306 Posted November 1, 2009 at 07:22 PM Ja agora outra duvida sobre o mesmo programa: Agora quero passar a parte que efectua a soma para uma funcao #include <stdio.h> float soma(int n); int main(){ int n, ind; printf("Quantos elementos e que sao: "); scanf(" %d", &n); float v[10]; for(ind = 0; ind < n; ind ++){ printf("Elemento %d = ",(ind+1)); scanf(" %f", &v[ind]); } soma(n); } float soma(int n){ int ind; float v[10]; float total=0; for(ind = 0 ; ind < n; ind++) { total = (float) (total + v[ind]); } printf(" %f", total); } so que agora quando executo o programa este da lixo no resultado do total. O que me falta? Sera que o n que esta em soma(n) e o erro? Link to comment Share on other sites More sharing options...
Rui Carlos Posted November 1, 2009 at 07:23 PM Report Share #294307 Posted November 1, 2009 at 07:23 PM Não está a passar os valores que leste anteriormente para a nova função. Em vez disso estás a declarar um novo vector v, que obviamente terá lixo. Rui Carlos Gonçalves Link to comment Share on other sites More sharing options...
Upper Posted November 1, 2009 at 07:26 PM Author Report Share #294308 Posted November 1, 2009 at 07:26 PM #include <stdio.h> float soma(int n, float v[]); int main(){ int n, ind; printf("Quantos elementos e que sao: "); scanf(" %d", &n); float v[10]; for(ind = 0; ind < n; ind ++){ printf("Elemento %d = ",(ind+1)); scanf(" %f", &v[ind]); } soma(n,v[10]); } float soma(int n, float v[]){ int ind; float v[10]; float total=0; for(ind = 0 ; ind < n; ind++) { total = (float) (total + v[ind]); } printf(" %f", total); } E assim que o vector esta bem declarado? Link to comment Share on other sites More sharing options...
bruno1234 Posted November 1, 2009 at 07:26 PM Report Share #294309 Posted November 1, 2009 at 07:26 PM Tens q passar float* v (q é igual a float v[], podes deixar como está). E tirar a nova declaração do método soma. Matraquilhos para Android. Gratuito na Play Store. https://play.google.com/store/apps/details?id=pt.bca.matraquilhos Link to comment Share on other sites More sharing options...
Upper Posted November 1, 2009 at 07:28 PM Author Report Share #294310 Posted November 1, 2009 at 07:28 PM float soma(int n, float *v[]) aqui e que fica o * ? e soma(n,&v[10]); fica assim? 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