Você está na página 1de 2

Practice Exercises Decisions and Loops; Flowcharts 1. Write a program that simulates a simple login screen.

. In order to do that, proceed as follows: First, declare two arrays of strings: one in which you will hardcode few usernames and one in which you will hardcode corresponding passwords (so, for example, if the first element in your array storing usernames is testUser, then the password of this user will be stored in the first position of the array storing passwords). Once you have that, ask the user to enter a username and a password (note that you should use the C# function Console.ReadLine() for reading the values and you should declare two string variables). Then, check if the username and password exist in the arrays defined previously: if they form a matching pair, inform the user that the login was successful; otherwise, display an appropriate error message.

2. Imagine that we have a doctor who makes the following decisions: If a patient has a cancer and is rich, the doctor sends the patient to a surgery. If a patient has a cancer and is not rich, the doctor advises the patient to make a public call for obtaining financial help. If a patient has a cold and is rich, the doctor takes special care for the patient. If a patient has a cold and is not rich, the doctor sends the patient home, advising him/her to drink tea while he/she has the cold. There are no other possibilities that can happen.

Suppose that a patient cannot have both a cancer and a cold. In your program, ask the user to enter the illness of the patient (cancer or cold) and the financial status (rich or poor) and display an appropriate decision (surgery, make a public call, take special care, send home) based on the input values.

3. Declare an array of 50 integer values as follows: int[] numbers = new int[50]; Then, write a for loop to fill in the array elements with the numbers from 1 to 50 (recall that arrays in C# are zero-based). Once you do that, go through all the array elements and sum them. Display the sum on screen. (Hint: you can take the number of elements in an array if you type {theNameOfYourArray}.Length).

Practice Exercises Decisions and Loops; Flowcharts 4. You can pass an array to a function just as you would pass any other parameter; however, the function definition should accept the parameter as follows: returnType nameOfFunction(arrayType[] arrayName) Repeat problem number 4, this time calculating the sum of the array elements in a function called Sum (note that this function should return an integer value).

5. Write a program that asks the user to enter a number between 1 and 12 and displays the name of the month associated with the number. To make things more interesting, however, you should write a while loop that runs as long as the user input is between 1 and 12 (i.e. dont just end the program after the first input). When the number entered is outside this range, end the program.

6. Draw flowcharts of the programs that you wrote in exercises 1-5.

Você também pode gostar