(code fragments-ice)
Write a C# code fragment (just the relevant statements, not an entire program) that displays the square of the numbers 1 - 10 (inclusive) to the Console. Use a while loop. Declare and initialize all
...
(code fragments-ice)
Write a C# code fragment (just the relevant statements, not an entire program) that displays the square of the numbers 1 - 10 (inclusive) to the Console. Use a while loop. Declare and initialize all relevant variables in your code fragment. You may use magic numbers. Each line of output should be like:
The square of 5 is 25. - ANSWER int num = 1;
while (num <= 10)
{
Console.WriteLine("The square of " + num + " is " + (num*num));
++num; }
Write a C# code fragment (just the relevant statements, not an entire program) that creates and displays a tip table for meals costing $1 - $100 (inclusive) for 15%. Send your output to the Console. Use a for loop. Declare and initialize all relevant variables in your code fragment. You may use magic numbers. Each line of output should be like: Meal: $10.00, Tip: $1.50. - ANSWER double meal, tip;
for (meal = 1.0; meal <= 100.0; meal += 1.0)
{
tip = meal * 0.15;
Console.WriteLine("Meal: " + meal.ToString("C") + ", Tip: " +
tip.ToString("C"));
}
Write a C# code fragment (just the relevant statements, not an entire program) that displays the time for every minute from 1:00 PM to 11:59 PM (inclusive) to the Console using nested for loops. Declare and initialize all relevant variables in your code fragment. You may use magic numbers. Remember, to get leading zeros with the minute, use format code D2. - ANSWER for (int hour = 1; hour <= 11; ++hour)
for (int minute = 0; minute <= 59; ++minute)
Console.WriteLine(hour + ":" + minute.ToString("D2") + " PM");
(Quiz 4)
a ___ loop is one in which loop control variable/expression is tested AFTER the loop body executes. - ANSWER Posttest
the do-while repetition statement tests the conditions ___ the body of the loop executes. - ANSWER After
when loops are nested, each pair contains a ___ loop and an outer loop. - ANSWER inner.
how many iterations will the following loop complete?
for (int i = 0; i <= 10; ++i)
Console. WriteLine("i = " + i); - ANSWER 11
with a ___ loop, you can indicate the starting value for the loop control variable, the test condition that controls loop entry, and the expression that alters the loop control variable, all in one convenient place. - ANSWER For
what will the value of the variable TEST be after the following sequence of statements complete execution?
int x = 25;
int test;
test = x++ - ANSWER 25
when a variable is declared in the initialization expression of a FOR loop, the scope of the variable is limited to the loop itself. - ANSWER True
a WHILE loop is a ___ loop. - ANSWER pretest.
counter-controlled repetition is an example of: - ANSWER definite repetition.
when you know you want to perform some task at least one time, the ____ loop is the best choice. - ANSWER Do while
how many iterations will the following loop complete?
for (int i = 10; i <= 1; --i)
Console.WriteLine("i = " + i); - ANSWER 0
one execution of any loop is called a ____ - ANSWER iteration.
how many iterations will the following loop complete?
for (int i = 10; i >= 1; --1)
Console.WriteLine("i = " + i); - ANSWER 10
(code frag-ice):
following statement calls a PUBLIC method named ShowHalf. The ShowHalf method displays a value that is half that of its argument using a MessageBox with 1 decimal place of precision. Write the method definition based on the call.
METHOD CALL:
ShowHalf(50.0); //should show 25.0 - ANSWER METHOD DEFINITION:
public void ShowHalf (double num)
{
double half = num/2;
MessageBox.Show (num.ToString("n1"));
}
following statement calls a PUBLIC method named ShowNumbers. the ShowNumbers method displays a list of numbers from 1 to the value of its argument (inclusive) to the console, each on a seperate line. Write the method definition based on the call.
method call:
ShowNumbers(10); //should show 1, 2, 3, ..., 10 //on seperate lines. - ANSWER METHOD DEFINITION:
public void ShowNumbers (int num)
{
for (int count = 1; count <=num; ++count)
Console.WriteLine(count);
The following statement calls a public method named ConvertInchesToFeet. The ConvertInchesToFeet
method calculates and returns the number of feet in the given number of inches represented by its argument.
Remember, there are 12 inches in 1 foot. Write the method definition based on the call.
Method Call:
double feet = ConvertInchesToFeet(42.0); // Should result in 3.5 feet - ANSWER Method Definition:
public double ConvertInchesToFeet(double inches)
{
double feet = inches/12;
return feet;
}
(code frag-wiki):
Write a loop that displays every fifth number from 1 through 100 (5, 10, 15, etc.) to the console, each on a separate line. [Alternate: Display each value using a MessageBox.] - ANSWER for (x=5; x<= 100; x+=5)
{ console.writeline (x); }
Write a method named ShowRetailPrice that accepts two parameters, a double wholesaleCost and a double markupPercent and does not return a value (void method). The method should calculate the retail price (increase the cost by the given percentage) and display it using a MessageBox. - ANSWER private void ShowRetailPrice(double wholesaleCost, double markupPercent)
{
MessageBox.Show(wholesaleCost * (1 + markupPercent));
}
Write a method named ConvertInchesToCM that accepts a double parameter inches and returns a double value. The method should convert the specified number of inches into centimeters. Each inch is equivalent to 2.54 centimeters. - ANSWER double ConvertInchesToCM(double inches)
{
return inches * 2.54;
}
Write a C# code fragment (just the relevant statements, not an entire program) that defines a public method named Sum that returns a double and accepts three double parameters a, b, and c. The method should calculate the sum of the three values and return it. - ANSWER public double Sum(double a, double b, double c)
{
return a + b + c;
}
Write a C# code fragment (just the relevant statements, not an entire program) that defines a public method named SumArray that returns an int and accepts one parameter, an array of integers named arr. The method should calculate the sum of all the numbers in the array arr and return it. - ANSWER public int SumArray (array[] arr)
{
int sum = 0
foreach (int i in arr)
{ sum += i; }
return sum;
}
Write a C# code fragment (just the relevant statements, not an entire program) that defines a public method named Max that returns a double and accepts two double parameters a, and b. The method should return the value of the larger parameter, either a or b. - ANSWER public double Max(double a, double b)
{
if (a > b)
{
return a;
}
else
{
return b;
}
}
Write a C# code fragment (just the relevant statements, not an entire program) that defines a public method named Max that returns a double and accepts three double parameters a, b, and c. The method should return the value of the largest parameter, either a, b, or c. - ANSWER public double Max(double a, double b, double c)
{
if(a > b && a > c)
return a;
if(b > a && b > c)
return b;
else
return c;
}
Write C# code fragments (just the relevant statements, not an entire program) to accomplish the following:
a) Declare an array of ints named temps.
b) Allocate memory for the array to hold 10 elements.
c) Write a for loop to initialize all of the array elements to the value 72 . - ANSWER a. int [] temps = new int [];
b. int [] temps = new int [10];
c. for (i = 0; i < temps.length; i++;)
temps[i] = 72;
Write a C# code fragment that prints each element of an array of doubles named arr to the console, each on a separate line. You must use a foreach loop. [Alternate: Display each value using a MessageBox.] - ANSWER foreach (double i in arr)
{
Console.WriteLine(i);
}
Write a C# code fragment that creates an array of int values using an initializer list with the following values: 10, 14, 16, 21, 27 . Write a loop that displays the values from first to last, and then write another loop that displays them from last to first. DO NOT change the order of the elements in the array, simply step through the array from last element to first element. Display each value on a separate line using the console. [Alternate: Display each value using a MessageBox.] - ANSWER int[] numbersArray = new int[] {10, 14, 16, 21, 27};
for (i = 0; i < numbersArray.length; i++)
Console.WriteLine(i);
for (i = numbersArray.length; i >= 0; i--)
Console.Writeline(i);
(wiki-wright)
The switch statement may be used to replace nested if/else if statements under certain circumstances.
- Describe the circumstances in which this can be done.
- Be sure to mention any limitations of the switch statement that may limit its usefulness. - ANSWER can be used as an alternative to an if-else-if statement that tests the same variable or expression for equality against several different values
- Matching has to be done on a specific: Value, Type, Enumeration, or Other data.
- Cannot be a decimal or floating point number. Can't be used to used to test for a range of values. each value must be tested individually.
Explain how a switch statement works in your own words. Describe the flow of control from case to case and what happens when no case matches the test expression (governing expression). - ANSWER The switch statement lets the value of a variable or an expression determine which path of execution the program will take. It is a multiple-alternative decision structure. It can be used as an alternative to an if-else-if statement that tests the same variable or expression for equality against several different values
The testExpression is a variable or an expression that given an integer, string, or bool value. Yet, it cannot be a floating-point or decimal value.
Each case is an individual subsection containing one or more statements, followed by a break statement.
The default section is optional and is designed for a situation that the testExpression will not match with any of the cases.
Explain the difference between definite repetition and indefinite repetition. - ANSWER Definite/Finite Loops - Test for a predefined condition. Once condition no longer remains true, exit the loop.
Typical use of while loop - situations where the number of repetitions is unknown - indefinite repetition
We've looked at three primary loop statements in C# the (1) WHILE loop, the (2) DO-while loop, and the (3) FOR loop. All of these loops are similar and, in most ways, functionally equivalent.
- In what ways are they different (focus on when each's condition is tested)?
- Under what circumstances is the use of EACH loop preferred over the others? - ANSWER 1. while loop (pretest) has two parts:
- A Boolean expression that is tested for a true or false value.
- A statement or set of statements that is repeated a long as the Boolean expression is true. - while loop is commonly used with indefinite repetition.
2. The do-while loop is a posttest loop, which means it performs an iteration before testing its Boolean expression. -If body needs to execute at least once, do-while is best
3. The for loop is specially designed for situations requiring a counter variable to control the number of times that a loop iterates; - specify intialialization, test, & update.
-(pretest); - used to process the array. -for loop is commonly used with counter-controlled repetition
Explain how a for loop works in your words. Describe the three parts of the loop statement and the flow of control between them and the body of the loop. - ANSWER The for loop is specially designed for situations requiring a counter variable to control the number of times that a loop iterates
- You must specify three actions: Initialization: a one-time expression that defines the initial value of the counter, Test: A Boolean expression to be tested. If true, the loop iterates,
& Update: increase or decrease the value of the counter.
- pretest loop.
What does a break statement do inside a loop? What about a continue statement inside a loop? - ANSWER break: Can use in a loop to prematurely terminate it and control resumes with statement after body. Never necessary to include a break in a loop and is generally considered poor programming practice to do so. When a break statement is encountered within a loop, the loop is stopped and the statement right after the loop is executed next.
With a continue statement, the current iteration is interrupted and control moves directly to the test condition. Again, generally best to avoid. When a continue statement is encountered within a loop, the loop skips that step of the iteration and moves on to the next iteration.
Explain the process of top-down design. - ANSWER method for breaking down an algorithm into methods
The overall task that the program is to perform is broken down into a series of sub-tasks.
Each sub-task is examined to determine whether it can be broken down further into more subtasks. This is repeated until no more subtasks can be identified. Once all the subtasks are identified, they are written in code. It is called top-down design because the programmer begins by looking at the topmost level of tasks that must be performed and then breaks down those tasks into lower levels of subtasks.
What are preconditions and postconditions for a method? - ANSWER Preconditions and postconditions allow a programmer to specify what a method accomplishes without describing how the method accomplishes it.
What is the difference between a void method and a value-returning method? - ANSWER void method simply executes a group of statements and then terminates.
A value-returning method returns a values to the statement that called it
Explain the difference between a method's parameters and its arguments. Which is used in the method's definition? Which is used in the method's call? - ANSWER Argument is any piece of data that is passed into a method when the method is called WHILE a Parameter is a variable that receives an argument that is passed into a method.
Compare and contrast the use of pass by value against pass by reference when passing primitive data (like int and double) to a method. [Hint: Be sure you make clear in your answer which one sends only a copy of the argument's value and which one would allow the argument to be changed by the call.] - ANSWER When an argument is passed by value, only a copy of the argument's value is passed into the parameter variable. If the contents of the parameter variable are changed inside the method, it has no effect on the argument calling part of the program. It guarantees that the value of the variable will not be changed by the method its passing through.
When an argument is passed by reference to a method, he method can change the value of the argument in the calling part of the program.
using a reference parameter: ref.
Using an output parameter method: out.
[Show More]