0% found this document useful (0 votes)
27 views3 pages

Untitled

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

La Salle College

Algorithms & Programming (420AP1AS)


_______________________________________________________________________________________________

Michelle M. Khalifé
Assignment 1

#1 [datatypes and variable declaration]

There are many datatypes in C#.


The table shows some of the language’s primitive (value-based) data-types and the string reference type.

Byte (byte), 1bytes Single (float), 4bytes Char (char), 2bytes Boolean String, 4bytes address
Int16 (short), 2bytes Double (double), 8bytes (bool), 1byte
Int32 (int), 4bytes
Int64 (long), 8bytes
For whole numbers, For decimal/real For single letters, Truth values: For a sequence of
e.g.: -7, -5, 0, 3, 7, numbers, e.g.: -5.5, digits, special True, False characters (one or
11, 999 0.00, 10.9999 characters and/or more) e.g.: “algorithms
symbols, e.g., ‘a’, ‘b’, and programming”,
‘c’, ‘X’, ‘Y’, ‘Z’, ‘1’, ‘2’, “123456789”, “Joe
‘3’, ‘@’, ‘#’, ‘!,’ ‘?’ Smith”, “Jan-20”

Character values are String values are


surrounded by single surrounded by double
quotes quotes

Declaration Syntax

datatype varName; // declaration, reserves space in memory for that variable


or
datatype varName = some_value; // declaration and initialization, reserves space and stores the value

Convention:
Choose an informative name for your variable – typically, you would be looking at one or more nouns.
C# favors camelCase notation for naming local variables. Start with a lower-case letter, then every subsequent word
starts with an upper-case letter. E.g.: myVariable, myVar1, day, weekDay, dayOfTheWeek, hourlyWage, rate, ….

Declare a variable for each of the following:

a. the current temperature


b. a letter grade
c. your final AP average
d. the number of students in a class
e. your favorite colour
f. the temperature required for baking a banana cake
g. your middle initial
h. your 7-digit student ID
i. your 10-digit mobile number
j. PI
k. the name of your bank
l. a yes/no answer to a question

To do so, you must think of any possible value that represents this variable. In the case of the temperature, one
would think: -20, 0, 10, 25, but also 18.5, 7.5, … This will allow you to determine the type. Once you’ve got the type
and the name, you may go ahead assign values to these variables and print them.
#2 [explicit casting]

Casting is when the value of a variable changes type. For example, in int a = 5, the 5 is an integer. If we wish to turn
the 5 into 5.0, i.e., a float, then we need to prepend it with the desired type between parenthesis: (float)5. Seeing
that the 5 is contained in variable a, this becomes (float)a. This results in a float and should be assigned to a float
variable.
Casting syntax:

type1 var1 = some_value; // first, for an assigned &/or scanned value, declare a variable to contain it.
type2 var2 = (type2) var1; // then, cast this variable into another type, & save the result in a variable of that new type

Declare a(n) ________ variable and cast it into a(n) ________ variable. Print the latter. What do you observe in each
case?

a. integer, float
b. float, integer
c. float, double
d. char, integer
e. char, float
f. integer, char
g. float, char

#3 [implicit casting]

“Prompt the user” translates into a Console.WriteLine() function call, which allows you to communicate to the user
what you want them to enter, followed by a Console.ReadLine() function call, which allows you read their input.

1. a. Prompt the user for a float and save it in an integer. Print the integer. What do you observe?
b. Now, prompt the user to enter an integer or a float. This time, save it in the right variable type.
c. Print that variable. What do you observe?

2. a. Prompt the user for an integer and save it in a float. Print the float. What do you observe?
b. Repeat steps 1b and 1c.

#4 [logic]

A statement is either true or false, but it cannot be both at the same time. For instance:

Statement Truth Value


S1: 1 is strictly less than 5 True
S2: 0 is equal to 100 False
S3: “January is considered a winter month” True
S4: “We meet for algorithms on Fridays” False
S5: “2 is even and prime” True

The table on the left depicts the resulting truth value when two statements are joined with logic operators:

- && (and) , and


- || (or)
The table on the right depicts the truth value of a statement (or a logic operator) that is being negated.

S1 S2 && || S !S (not S)
T T T T T F
T F F T F T
F T F T && ||
F F F F || &&

1. Come up with 4 or 8 pairs of statements that would allow you to understand how && and || operate. For
example, the pair below illustrates the first row in the table:

S1: 2 is even
S2: 2 is prime
S1 && S2 => true and S1||S2 => true

2. Throw a third statement S3 in the mix. Let S3 be true, then let it be false. What happens?

#5 [function naming]

main() is a function. It is called main because it is the main point of entry & exit of the program. Other functions that
programmers create will be defined outside main(). Unlike variables, functions do not carry information: they DO
something. They perform an action. Therefore, they are best named with a verb followed by a noun. C# favors
PascalCase notation for function names. The first letter of every word is capitalized while the rest of the word is in
lower-case. E.g.: MakeBananaMangoSmoothie, MakeCappuccino, IsSunny, RunAnalytics, …

How would you simply name a function that:

a. adds a bunch of numbers


b. computes the value of a number raised to some power
c. tells you the day of the week
d. prints a new line
e. lets you know if a number is prime or not
f. converts a numeric average to the corresponding letter grade
g. calculates your BMI

You might also like