Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 100 additions & 8 deletions ControlFlow/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,51 @@ public static class ControlFlow
// be validated). Negative numbers should return "Invalid".
public static string AreYouValid(int number)
{
return default;
string output = "Invalid";
if (number > 1 && number < 10)
{
output = "Valid";
return output;

}
return output;
Copy link
Member

@yurisim yurisim Sep 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are there two return output here? You don't need the one on line 22.

}

// 1.2 - Write a method which takes two integers and returns the maximum of the two. If they
// are the same, return either one.
public static int Maximum(int number1, int number2)
{
return default;
if (number1 > number2)
{
return number1;
}
else if (number2 > number1)
{
return number2;
}
else
{
return number1;
}
}

// 1.3 - Write a method which takes two integers, the width and height of an image. Then
// return a string to tell if the image is "Landscape" or "Portrait". Return "Square" if
// they are equal. Assume non-negative values.
public static string LandscapeOrPortrait(int width, int height)
{
return default;
if (width > height)
{
return "Landscape";
}
else if (height > width)
{
return "Portrait";
}
else
{
return "Square";
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having this many return statements just segregate a variable called "output" or something.

}

// 1.4 - Your job is to write a program for a speed camera. For simplicity, ignore the details
Expand All @@ -48,7 +77,28 @@ public static string LandscapeOrPortrait(int width, int height)
// Suspended" instead. Don't Worry about input validation.
public static string SpeedTrap(int speedLimit, int speedCar)
{
return default;
if (speedCar > speedLimit)
{
int demerits = 0;
int overLimit = speedCar - speedLimit;
if (overLimit >= 5)
{
while (overLimit > 0)
{
overLimit -= 5;
demerits++;
}
}
if (demerits >= 13)
{
return "License Suspended";
}
return string.Format("{0}", demerits);
}
else
{
return "Okay";
}
}

// Part 2, Control Flow. Come back to this section after completing the For/Foreach/While loops.
Expand All @@ -58,29 +108,71 @@ public static string SpeedTrap(int speedLimit, int speedCar)
// output should be 2.
public static int DivisibleByNumber(int minNumber, int maxNumber, int divider)
{
return default;
int divided = 0;
for (int i = minNumber - 1; maxNumber > i; i++)
{
int answer = 0;

answer = minNumber % divider;

if (answer == 0)
{
divided++;
}
minNumber++;
}
return divided;
}

// 2.2 - Write a method to calculate the sum of all the integers and return it. For example if
// the program enters (2, 3, 5, 1) then the return should be 11.
public static int SumIntegers(params int[] numbers)
{
return default;
int result = 0;
for (int i = 0; i < numbers.Length; i++)
{
result += numbers[i];
}
return result;
}

// 2.3 - Write a method to compute the factorial of an integer return it. For example, if the
// user enters 5, the program should calculate 5 x 4 x 3 x 2 x 1 and return it as 120. Make sure
// that 0! = 1.
public static int FindFactorial(int number)
{
return default;
if (number == 0)
{
return 1;
}

int result = number;
number--;

if (number > 0)
{
while (number != 0)
{
result = result * number;
number--;
}
}
return result;
}

// 2.4 - Write a method that takes an array of integers. Then find the maximum of the numbers
// and return it. For example, if the numbers are {5, 3, 8, 1, 4}, the program should return 8.
public static int MaximumNumber(params int[] numbers)
{
return default;
int maxNum = 0;
for (int i = 0; i < numbers.Length; i++)
{
if (numbers[i] > maxNum)
{
maxNum = numbers[i];
}
}
return maxNum;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Come see me when you are free and I'll tell you why this needs to be fixed.

}
}

Expand Down