-3
I’m having a problem with my code I can’t bring the data from one function to another!!
in the case of Function sum I cannot print it in function print.
Can someone help me
1 - Insert into a vector 2 - Sum vector values 3 - Print values and sum 4 - Quit
At any time you can request printout of the data, do not print out memory junk At any time you can request the sum, do not add unread values yet
#include <stdio.h>
#define TAM 5
int vetor[TAM];
int menu(){
int opcao;
printf("1 - Inserir\n");
printf("2 - Somar valores\n");
printf("3 - Imprimir\n");
printf("4 - Sair\n");
scanf("%d", &opcao);
return opcao;
}
//----------------------------------------------------INSERINDO OS DADOS
int inserir(int i){
int vt;
for (i = 0; i < TAM; i++){
scanf("%d", &vetor[i]);
}
return vt;
}
//-----------------------------------------------------------------------
//------------------------------------------------------------------SOMAR
int somar(int i){
int soma=0, total;
for (i=0; i<TAM; i++){
soma=soma+vetor[i];
}
total = soma;
printf("\n");
return total;
}
//-----------------------------------------------------------------------
//----------------------------------------------------IMPRIMINDO OS DADOS
void imprimir(int i){
printf("Imprimindo elementos\n");
for (int i=0; i<TAM; i++){
printf(" %d ", vetor[i]);
}
printf("%d\n", total); //IMPRIMIR DADO DA SOMA <----------------
printf("\n");
}
//-----------------------------------------------------------------------
int main(){
int op = 0, i=0;
i++;
while(op != 4){
op = menu();
switch (op){
case 1:
inserir(i);
break;
case 2:
somar(i);
break;
case 3:
imprimir(i);
break;
case 4:
printf("Saindo");
break;
}
}
return 0;
}
Perfectly, a thousand pardons
– MarioSantos
Mario if you want to leave a message making yourself available for help use the comments.
– Augusto Vasques