Algorithm with structure PARA and SE

Asked

Viewed 4,588 times

6

An algorithm that reads the name and sex of fifty-six people and states her name and whether she is male or female. In the end report the total of men and women. I need the show to run on Visualg.

I made this code but it’s not working:

// Função :
// Autor :
// Data : 29/09/2015
// Seção de Declarações 
var
nome , sexo , feminino, masculino: literal
i : inteiro
inicio
escreva ("Digite seu nome: ", nome)
leia (nome)
escreva ("Digite seu sexo: ", sexo)
leia (sexo)
para i <- 1 ate 56 faca
se (sexo = feminino) entao
  escreva ("Seu nome é: ",nome "e seu sexo é: feminino", feminino)

fimse
fimpara
fimalgoritmo

I need that in addition to reading the name and sex of 56 people enter the name and if she is male or female and in the end inform the total of men and women, but I can not make the code work.

  • 3

    Welcome to Sopt. Here we answer questions, we don’t do your college work. Click [Dit] and put your question, about something specific. If you want to understand how the site works recommend to do the [tour] take a look at [help]. Also see How to create a Minimum, Complete and Verifiable example.

  • 2

    Yeah, Cristina, here you find a giant file of information, just look it up. And to contribute by asking questions and receiving retribution in the form of an accurate answer, you will need to do more when writing the question. It’s just [Edit] and add details of what you tried, what went wrong and what exactly is your programming doubt.

3 answers

5


I’ve never used Visualg, but I think your code should look like this

var 
nome , sexo: literal
i, c, fem, masc : inteiro

inicio
  para c <- 1 ate 56 faca

    nome := ""
    sexo := "" 

    escreval ("Digite seu nome:")
    leia (nome)
    escreval ("Digite seu sexo (masculino/feminino):")
    leia (sexo)

    se(sexo = "feminino")
      fem := fem + 1
    senao
      masc := masc + 1
    fimse

    escreval("Seu nome é ", nome, " e seu sexo é ", sexo)
  fimpara    

escreval("foram cadastrados ", fem, " mulheres e ", masc, " homens")

fimalgoritmo

There are some things that can be improved, for example, validate not to accept different entries from masculine and feminine in sex, but it’s already much better than the question code.

  • this worked almost well, for example if I put the name : Ana and female sex he keeps appearing only ana and female can not put another name or other sex

  • How do I make the field clean ? for example put ana and female sex and I want to appear to me to type more that does not appear there, what to do?

  • the program is already 98% working but has a problem when I run it appears like this :" your name is André and your gender is: male type your name " all in the same line as I do to enter your name in the next line

  • Exchange the escreva for escreval which will print each command on a line

  • Thank you so much :) the code is now 100%

4

It can get better, but it works:

var
nome, sexo: caractere
i, m, f : inteiro
inicio
m <- 0
f <- 0
para i <- 1 ate 56 faca
  escreva("Digite seu nome: ")
  leia(nome)
  escreva("Digite seu sexo (m/f): ")
  leia(sexo)
  se (sexo = "f") entao
    escreval("Seu nome é: ", nome, " e seu sexo é: feminino")
    f <- f + 1
  senao
    escreval("Seu nome é: ", nome, " e seu sexo é: masculino")
    m <- m + 1
  fimse
fimpara
escreval("Total de mulheres: ", f)
escreval("Total de mulheres: ", m)
fimalgoritmo

I put in the Github for future reference.

Among the various problems including language syntax, I wasn’t taking the data 56 times as the description required, so I added a loop in this part. And I printed it out when it was male.

  • this syntax doesn’t work, just puts name and sex and then stops running

  • Give more information. I don’t have Visualg so I can’t test it. But syntax error is easier to find when you know what it is.

  • Starts to make an error from this typing("Your name is: ", name "and your gender is: female") f <- f + 1

  • I had this fixed. Now I got another thing that I saw that is not using vector, I am so used to these exercises having that I did as if I had. Since you don’t have the solution is to make a single loop.

  • How do I erase a field? the code keeps repeating the same name and the same sex does not erase, example: if I type name Ana and female sex when it is for me to type the next appears ana and can not delete

  • I doubt if this happens, should not, there is something different in your code. If you want to insist on doing this you would have to put at the beginning or at the end of the loop the lines nome <- "" and sexo <- "". It has to be inside the loop. But reinforcement doesn’t need to do this. Even if you don’t know how to use Visualg :D

  • I think she was referring to my answer, which was actually printing the variables nome and sexo when I asked.

  • 1

    Yeah, that "question" is very confusing from end to end :)

  • @Kcristina copy the code the way it is and tell me if there’s anything wrong yet or if it’s all right.

  • I managed to find the error, it was in my code right after the to

  • @Kcristina I was able to test now and the code is running round doing everything described, without breaking, without repeating information, piling lines, counting right, etc.

  • Yes, thank you very much turned out great the code

Show 7 more comments

0

algoritmo "semnome"

// Função :

// Autor :

// Data : 09/02/2016

// Seção de Declarações 

var

nome, sexo, f, m: caracter

cntf, cntm, i, : inteiro

inicio

cntf := 0

cntm := 0

para i de 1 ate 56 faca

escreva ("Digite seu nome: ")

leia (nome)

escreva ("Digite seu sexo (f/ m): ")

leia (sexo)

se (sexo = f) entao

cntf := cntf + 1

senao

fimse

se (sexo = m) entao

cntm := cntm + 1

senao

fimse

fimpara

escreval ("Total de homens: ",cntm)

escreval ("total de mulheres: ",cntf)

fimalgoritmo
  • I don’t understand. What links?

Browser other questions tagged

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