Sum of positions of a cycle for c

2

I made a program using functions in which you requested the age of the person, and added some conditions that if it is less than 12 years old it is childish and the cost is 300, if it is less than 18 years old it is considered childish and the cost is 500 and if it is over 18 years it is considered adult and the cost is 800. The program allows the entry of 10 people, however, I am not adding the positions I want for each condition, what I need is print how many people entered each category but it is not working the right way. Excuse me if you do not explain well.

#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <stdio.h>    

void main ()
{
    int edad[10];
    int x,y,r;    

    printf ("BIENVENIDOS AL EVENTO\n");

    for (x=0;x<=9;x++) //Guarda 10 edades
    {
        printf ("Ingrese las edades");
        scanf (" %d",&edad[x]);
    }
    for (x=0;x<=9;x++)
    {
        printf (" %d",edad[x]);
    }    

     r=evento(edad);  //Llamando funcion
    printf ("El total de los boletos es de %d",r);
}

void evento (int edad[]) //Recibe funcion
{
    int suma=0, suma2=0, suma3=0;
    int i,o = 300,x,h = 500,t = 800,p,q;

    for (i=0; i < 10;i++) // Suma las posiciones que son menor a 13, aqui es donde no me esta dando, igual que en las otras condiciones. 

    {
        if (edad[i] < 13)   
        {
            suma = suma+i;
        }
    }
        printf ("En infantil ingresaron %d personas\n",suma);    

            for (i=0;i<10;i++)  // Suma las posiciones que son menor a 18
            {
                if (edad[i]<18)
                {
                    suma2 = suma2+i;
                }
            }
            printf ("En juvenil ingresaron %d personas\n",suma2);    

    for (i=0;i< 10;i++)   // Suma las posiciones que son mayor a 18 
            {
                if (edad[i]>18)
                {
                suma3=suma3+i;
                }
            }
            printf ("Ingresaron %d adultos\n",suma3);

    //q=h+o+t;
    //return q;
}
    
asked by Arturo 10.08.2018 в 01:07
source

1 answer

1

Reading a bit I think that what you need is just to add the number of people for each category and not the costs of each ticket, then it would be like this:

#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <stdio.h> 



void main ()
{
    int edad[10];
    int x,r;



printf ("BIENVENIDOS AL EVENTO\n");

for (x=0;x<=9;x++) //Guarda 10 edades
{
    printf ("Ingrese las edades");
    scanf (" %d",&edad[x]);
}
for (x=0;x<=9;x++)
{
    printf (" %d",edad[x]);
}    

r=evento(edad);  //Llamando funcion
printf ("El total de los boletos es de %d",r);
}

void evento (int edad[]) //Recibe funcion
{
int sumas=0;
int sumas1=0;
int sumas2=0;
int j,k,q;

// con un solo ciclo recorremos todo el arreglo y discriminamos por categoria
    for (j=0; j < 10;j++) // Suma las posiciones que son menor a 13
    {
    if (edad[j] <13)   
    {
        sumas = sumas+1;
    }
    else 
    if (edad[j] >= 13 && edad[j]<18)
    {
        sumas1 = sumas1+1;//alamacenamos la suma de cada persona que son menor a 18

    }
    else
    if (edad[j]>18 )
    {
        sumas2 = sumas2+1;// alamacenamos la suma de cada persona que son mayor a 18
    }
}

printf ("En infantil ingresaron %d personas\n",sumas);    

printf ("En juvenil ingresaron %d personas\n",sumas1);    

printf ("En adultos ingresaron %d personas\n",sumas2);

q=1*k;
return q;
}

NOTE If you want to add the values of each ticket, it would also be the same ...

I hope you serve and marques xD ... ReNiceCode ...

    
answered by 10.08.2018 / 02:46
source