-2
I have this activity:
Write a function that receives, as input, a vector of n > 0 elements of type Cell and return, as output, the sum, of the weights of all vector elements belonging to bands. For example, for the sequence "yyzzzya", if the weights of these elements are 10,2,5,4,5,4,3, their function must return the value 26.
I start it with the 7 elements, put the same char in the description and the int but when sum me the value of 21; here is the code
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char car;
int peso;
} Cell;
void SomaDosPesos (Cell vet[],int tam);
int main(){
int i,tam,a1;
do
{
printf("\nDigite a quantidades de elementos que ira inserir, tem de ser maior que Zero! :");
scanf("%d", &tam);
} while (tam < 0);
Cell vet[tam];
for(i = 0 ; i < tam; i++){
printf("\nDigite um caractere:");
scanf("%s",&vet[i].car);
printf("\nDigite o peso deste caracter:");
scanf("%d", & vet[i].peso);
}
SomaDosPesos(vet,tam);
}
void SomaDosPesos (Cell vet[],int tam){
int i,aux = 0;
for(i= 0; i < tam ; i++){
printf("\nFuncao:%c,%d",vet[i].car,vet[i].peso);
}
if(vet[0].car == vet[1].car){
aux += vet[0].peso;
}
for(i= 0; i < tam ; i++){
if(vet[i].car == vet[i+1].car){
aux += vet[i+1].peso;
}
}
printf("\nFuncao peso: %d",aux);
}
Your sum function is looking for characters that have an equal on their right (
i + 1
), then she ignores the latter characters of each track because they only have one character equal to the left! You can fix this by putting an Else if in the right place.– Krossbow
where I place?
– Eduardo Fabricio