Language C: How to store information in WHILE and present the entered data in a list?

Asked

Viewed 267 times

-2

I would like to know how do I save the data and present it at the end; I have not yet been taught to do so. How do I resolve item C of the question

Consider that, for each of the farm hotels of the region, the name of the hotel has been registered, its distance from the city center (DA), the average number of visitors in the last holiday (V) and the type of access to the hotel (AC being 0 - access not asphalted; 1 - access asphalted). Build a program that reads this data and at the end inform:

a) The number of hotels more than 15km from the centre;

b) The average number of visitors on the last holiday, in hotels with unpaved access;

c) The name and distance from the centre in Km, of all the paved access hotels that had fewer than 1,000 visitors.

Input example :

Hotel A, DA=10, V=100, AC=0

Hotel B, DA=20, V=50, AC=1.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){

char hotel[20], w[1];
int V, AC, DA, centro=15, q1=0, q2=0, q3=0, q4;

printf("REGISTRO DE HOTEIS\n\n");

do{
    printf("\nHotel                      :");       scanf("%s",&hotel);
    printf("Distancia do centro em km    : ");      scanf("%d",&DA);
    printf("Visitantes no ultimo feriado : ");      scanf("%d",&V);
    printf("Acesso ao hotel\n Digite \n"
           "0 - acesso não asfaltado\n"
           "1 - acesso asfaltado         : ");      scanf("%d",&AC);

    if(DA>centro)
        q2++; //vai guardar quantos hoteis tem longe do centro

    if(AC==0){
        q1++;   //vai contar as repetições, nesse caso o numero de hoteis cadastrados
        q3 += V; //vai guardar todos os visitantes do ultimo feriado de todos os hoteis
    }
    if( V<1000 && AC==1)
        printf("\nHotel %s Distancia %d km. Asfaltado com menos de 1.000 visitantes.",hotel,DA);
        //c) O nome e a distância do centro em Km, 
        //de todos os hotéis de acesso asfaltado que tiveram menos de 1.000 visitantes.

    printf("\n\nNovo Registro S(sim) N(nao)? "); scanf("%s",w);

}while(strcmpi(w,"S")==0); //W = WHILE

q4 = q3/q1; //media de visitantes de todos os hoteis

/*c) O nome e a distância do centro em Km, 
de todos os hotéis de acesso asfaltado que tiveram menos de 1.000 visitantes.*/
if( V<1000 && AC==1)
    printf("\nHotel %s Distancia %d km. Asfaltado com menos de 1.000 visitantes.",hotel,DA);

//a) O número de hotéis que distam mais de 15km do centro;
printf("\nHoteis a 15km do centro                               : %d\n",q2);

//b) A quantidade média de visitantes no último feriado, nos hotéis com acesso não asfaltado;
printf("A media de visitantes nos hoteis com acesso sem asfalto : %d\n",q4);

return 0;
}
  • In C a string containing a single character occupies 2 positions: the character itself and the terminator character ' 0'. If it is only a character declare only char w;and instead of double quotes (") use single quotes(') and compare directly: w == 'S' and use %creading. I don’t know the function strcmpi maybe it’s strcmp. Here: scanf("%s",&hotel); doesn’t have this &: scanf("%s", hotel);.

  • strcmpi code is to accept so many capital letters of minuscules. Thank you so much for the tips

1 answer

0


So, there are several ways that you could save the hotels and present them at the end, one option would be to create a list (data structure), but by the level of the exercise I believe it is a somewhat advanced implementation to be used as a solution. In my opinion the simplest way to do this would be to use a vector, but there are two situations:

1. The amount of user entries is previously known, that is, if the question has defined how many entries the user will give.

2. The user will enter the hotels until he decides to stop.

To the 1 you can set this limit n (quantity of entries) as the size of the hotel vector and initialize it,e ai, always within this condition:

 if( V<1000 && AC==1)

You store the hotel in a vector position. For this you need a variable i to represent the index.

At the end you create a loop for that goes from 0 until i and ready.

As for the 2, you will need to work with memory allocation, ie you will also create a vector, but it will be created with dynamic memory allocation, with a pre-set size, which will be reallocated whenever you reach the limit.

For this solution, I suggest a read on this topic: Handling of malloc() and realloc()

I hope I’ve helped!

Remarks: the scanf made for vector char hotel does not need the & since somehow you’re working with a "string".

  • Thanks, I’ll take a look and try to learn. In the course we are still in repetition system so I think this issue is running away a little from what we were taught.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.