Introduction to Natural Numbers
Introduction to Natural Numbers
Fundamentals of Natural Numbers
Introduction
When you declare, you are asking the compiler to reserve an area in the computer memory to store a value. To make this happen, you must provide two pieces of information, a name and a data type. The data type specifies the amount of memory that will be necessary to store the value.
To store a value in its memory, the computer's memory uses something that looks like a series of cells. Each cell-like can contain only one value or it doesn't contain anything. The tiny value that this cell-like object can store is called a bit. A bit can assume only one of two values: either 1 (which means it has a value) or 0 (which indicates that it doesn't have a value).
The computer memory uses many (millions or billions of) combinations of bits to store information. Each data type uses a fixed combination of bits depending on the type of data. A type of data is a natural numbers. In reality, there are various types of natural numbers.
To represents the various data types that a computer can use, the .NET Framework provides a structure for each type. A data type that is represented by one those structures is called a primitive type. Each of those structures provide the characteristics that make a data type behave like a structure (or class). Some characteristics are common to many of the structures while some other aspects are unique to some types. To support the routine operations of regular variables, each structure that represents a primitive type is equipped with some properties and methods.
Practical Learning: Introducing Integers
@media screen and (min-width: 768px) { .jumbotron { padding-top: 48px; padding-bottom: 48px; } .container .jumbotron { padding-right: 60px; padding-left: 60px; } .jumbotron h1 { font-size: 63px; } }
body { padding-top: 50px; padding-bottom: 20px; } .top-banner { top: 0; width: 100%; height: 75px; position: fixed; background-color: white; } .heading-format { font-weight: 800; font-family: Cambria, Cochin, Georgia, Times, Times New Roman, serif; } .heading1 { color: maroon; font-size: 2.52em; line-height: 2.05em; } .heading2 { color: #b71f1f; margin-top: 20px; font-size: 1.52em; line-height: 1.15em; } .navbar-fixed-top { top: 75px; } .navbar-inverse { background-color: #581405; border-top: 3px solid black; border-bottom: 3px solid black; } .jumbotron { padding-top: 80px; background-color: white; border: 1px solid burlywood } .contents { margin: auto; margin-top: 80px; } .content2Parts { width: 300px; } .content3Parts { width: 350px; } .left-column { width: 140px; } .tbx-format { width: 50px; } .lead { margin-bottom: 0; } .pic-holder { text-align: center; } .contents { width: 300px; margin: auto; margin-top: 40px; } .left-column { width: 140px; } .tbx-format { width: 40px; }
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>OFRE :: @ViewBag.Title</title> @Styles.Render("~/Content/css") @Scripts.Render("~/bundles/modernizr") <link rel="stylesheet" type="text/css" href="~/Content/FundRaising.css" /> </head> <body> <div class="top-banner"> <p class="heading1 heading-format text-center">Organization for Fundraising Events</p> </div> <div class="navbar navbar-inverse navbar-fixed-top"> <div class="container"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> @Html.ActionLink("OFRE", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" }) </div> <div class="navbar-collapse collapse"> <ul class="nav navbar-nav"> <li>@Html.ActionLink("Home", "Index", "Home")</li> <li>@Html.ActionLink("About OFRE", "About", "Home")</li> <li>@Html.ActionLink("Contact Us", "Contact", "Home")</li> </ul> </div> </div> </div> <div class="container body-content"> @RenderBody() <hr /> <footer> <h4 class="text-center">© @DateTime.Now.Year :: Organization for Fundraising Events</h4> </footer> </div> @Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/bootstrap") @RenderSection("scripts", required: false) </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace OFRE2.Controllers { public class CampaignsController : Controller { // GET: Campaigns public ActionResult Index() { return View(); } // GET: TwoWay public ActionResult TwoWay() { return View(); } // GET: ThreeWay public ActionResult ThreeWay() { return View(); } } }
@{ ViewBag.Title = "Two-Way Campaign Distribution"; } <p> </p> <p> </p> <div class="contents"> <p class="heading2 text-center heading-format">Pledge Distribution</p> <form name="frmDistribution" method="post"> <table> <tr> <td class="left-column">Amount to Allocate:</td> <td><input type="text" name="txtAllocation" value="" class="tbx-format" /></td> </tr> </table> <table> <tr> <td class="left-column">In the Ratio:</td> <td><input type="text" name="txtPortion1" class="tbx-format" value="" /></td> <td>:</td> <td><input type="text" class="tbx-format" name="txtPortion2" value="" /></td> <td><input name="btnAllocate" type="submit" value="Allocate" /></td> </tr> </table> <hr /> <table> <tr> <td class="left-column">Part 1 Receives:</td> <td><input type="text" name="txtPart1" value="" /></td> </tr> <tr> <td>Part 2 Receives:</td> <td><input type="text" name="txtPart2" value="" /></td> </tr> </table> </form> </div>
@{ ViewBag.Title = "OFRE Home"; } <div class="jumbotron"> <h1>OFRE</h1> <p class="lead"> The Organization for Fundraising Events, or OFRE, is a group of people who assist organizations, communities, associations, schools, causes, religious institutions, etc, to plan, conduct, promote, and run campaigns to raise money for any goal (cause, health issues, communities, school, politics, ceremony, religion, catastrophe, business startup, etc). The Organization can provide a complete service, including promoting the event, collecting, and delivering the funds. OFRE is a private, independent, and a not-for-profit organization. </p> </div> <div class="row"> <div class="col-md-4"> <p class="pic-holder"><img src="~/Images/Campaign.png" alt="Plan With Us" width="248" height="137" /></p> <h2>Plan With Us</h2> <p>Are you ready for a memorable experience in fundraising? You reaached the right place. Do you lack experience in fundraising? Don't take a chance. Don't worry about a thing. Let us run your next campaign. Give us the opportunity to put our vast experience at your disposal. You will enjoy the whole adventure.</p> </div> <div class="col-md-4"> <p class="pic-holder"><img src="~/Images/TwoWay.png" alt="Two-Way Campaign" width="248" height="136" /></p> <h2>Two-Way Campaign</h2> <p>This is an already completely created program to run a campaign to raise funds for two organizations or causes. This program consists of starting a compaign, collecting funds, appropriately distributing them, and delivering a complete legal/business/financial report of the campaign.</p> @Html.ActionLink("Two-Way Campaign", "TwoWay", "Campaigns", new { DontTryToFindOutWhatThisIs = "" }, new { @class = "btn btn-default" }) </div> <div class="col-md-4"> <p class="pic-holder"><img src="~/Images/ThreeWay.png" alt="Three-Way Distribution" width="248" height="136" /></p> <h2>Three-Way Distribution</h2> <p>In this program, the Organization runs a campaign to raise funds for three different organizations or causes. The ratios of distribution are stated any way a group wants. An effective computer application is intuitively ready to process all types of requests.</p> @Html.ActionLink("Three-Way Campaign Distribution", "ThreeWay", "Campaigns", new { ThisIsJustAPlaceHolder = "" }, new { @class = "btn btn-default" }) </div> </div>
An Integer
An integer is one or a combination of digits (0, 1, 2, 3, 4, 5, 6, 7, 8, and 9). The most common way to use integers in C# is with a data type named int. You can use it to declare a variable for a natural number. You can also use the var or the dynamic keyword if you are ready to initialize the variable. To give a value to the variable, assign the desired number to it. Here are examples:
@{
int score = 3907;
var feel = 95;
dynamic face = 794800;
}
<ul>
<li>Score: @score</li>
<li>Feeling: @feel</li>
<li>Face: @face</li>
</ul>
The .NET Framework represents the int data type with a structure named Int32. Here is an example of declaring and using an Int32 variable:
@{
int score = 3907;
var feel = 95;
dynamic face = 794800;
Int32 ambiance = 9284;
}
<ul>
<li>Score: @score</li>
<li>Feeling: @feel</li>
<li>Face: @face</li>
<li>Ambiance: @ambiance</li>
</ul>
The int or Int32 data type is for a variable that can hold a natural number between -2,147,483,648 and 2,147,483,647.
Introductory Numeric Characteristics
Signed and Unsigned Numbers
A number is referred to as unsigned and it is considered positive. To re-enforce this, you can start a number with the + operator. Examples are +20, +842, or +95097584.
To express a negative number, you must start its value with the - operator. Examples are -33, -7, or -509204995097584. When a number displays an operator (+ or -), the number is said to be signed. Because either of these symbols can be applied, a number that is said to be signed can be positive or negative.
Because the number of an int or Int32 data type can be negative or positive (or 0 of course), the type is referred to as a signed integer.
Hexadecimal Numbers
One way to express a number is by using a combination of the first 6 letters of the English alphabet (a, b, c, d, e, and f or A, B, C, D, E, or F) and the 10 digits. This is referred to as the hexadecimal format. The number starts with 0x. Here is an example:
@{
var number = 0xE295AA49;
}
Introduction to Integers
Bytes
A byte is a small unsigned integer whose value is between 0 and 255. To support this type, the C# language provides a data type named byte. To support this type, the .NET Framework provides a structure named Byte. Here is an example that uses the byte data type:
@{
byte age = 14;
}
Alternatively, you can also use the var keyword to declare the variable and initialize it with a small number.
You can initialize a byte variable with a hexadecimal value. When doing this, make sure the decimal equivalent is less than 255. Here is an example:
@{
var number = 0xFE;
}
A Signed Byte
A byte number is referred to as signed if it can hold a negative or a positive value that is between -128 and 127. To support it, the C# language provides the sbyte data type. The equivalent structure in the .NET Framework is named SByte. Here is an example:
@{
sbyte roomTemperature = -88;
}
Short Integers
A short integer is a natural number between -32768 and 32767. To let you declare a variable that can support such numbers, C# provides the short dat type. The equivalent type in the .NET Framework is Int16. Here are examples of varibles of this type:
@{
short numberOfPages;
short temperature;
numberOfPages = 842;
temperature = -1544;
}
Of course, you can declare the variable for such a number using the var keyword. Here is an example:
@{
var schoolEffective = 1400; // Number of Students
}
Because a short integer handles numbers that are larger than the signed byte, any variable you can declare for a signed byte can also be used for a short variable.
An unsigned short integer is a positive and relatively small number that is between 0 and 65535. To let you declare a variable for such a number, C# provides the ushort data type. The equivalent structure in the .NET Framework is named UInt16. You can also use the var keyword. Here are examples:
@{
// These variables must hold only positive integers
ushort numberOfTracks;
ushort musicCategory;
numberOfTracks = 16;
musicCategory = 2;
}
Signed Integers
As seen in the introduction of this lesson, a signed integer is a natural number between -2,147,483,648 and 2,147,483,647. To declare a variable for it, you can use the C's int data type or the C#'s var keyword. Here is an example:
@{
var population = 72394475;
}
To support the signed integer, the .NET Framework provides a structure named Int32.
Unsigned Integers
An unsigned integer is a positive natural number between 0 and 4,294,967,295. To support this type, the C# language provides a data type named uint. Its structure in the .NET Framework is UInt32. You can use one of them or the var keyword to declare a variable that can hold small or large numbers. You can then assign the desired value to the variable. To indicate that the value must be treated as an unisgned integer, add u or U to the left of the value.
A long integer is a number between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807. To support such numbers, the C# language provides a data type named long. If you want to use a variable that can hold very large numbers, you can declare it using that type or the var keyword. This type is available in the .NET Framework as the Int64 structure.
If you declare an integer variable using the var keyword and initialize it with a value between 2,147,484,647 and 9,223,372,036,854,775,807, the compiler concludes that the variable is a long integer:
If you initialize the var variable with a value higher than 9,223,372,036,854,775,807, which is too large, the compiler would present an error:
As mentioned for other integral types, you can initialize a long variable with a hexadecimal value.
Although the long data type is used for large numbers, it mainly indicates the amount of space available but you don't have to use the whole space. For example, you can use the long keyword to declare a variable that would hold the same range of numbers as the short, the int, or the uint data types.
If you declare a variable as long but use it for small numbers, the compiler would allocate the appropriate amount of space to accommodate the values of the variable. If you insist on using memory for a long integer, when assigning a value to the variable, add an L suffix to it. Here is an example:
@{
long countryArea;
countryArea = 5638648L;
}
Therefore, keep in mind that an int, a uint, a short, or a ushort can fit in a long variable.
An unsigned long integer is a (very large) positive integer. To let you declare a variable for such a number, C# provides the ulong data type. In the .NET Framework, this type is defined as the UInt64 structure. A variable declared as ulong or UInt64 can handle extremely large positive numbers that range from 0 to 18,446,744,073,709,551,615.
Topics on Integers
The Issue of the Integral Data Types
As you may be aware, there are many languages that use or target the .NET Framework. Each language may have some internal functionalities that the other languages don't necessarily support. As a result, if you work with a group of programmers who use different languages to develop a common project, it is not impossible to have some conflicts. As a result, you should avoid using features that are particular to your language of choice. To start, refrain from using unsigned integers: ushort (and UInt16), uint (and UInt32), ulong (and UInt64).
When it comes to integral data types, we saw that each has a minimum and a maximum values. As a matter of fact, the number-based types are incremental when it comes to the memory they are using. As a result, the memory used by a byte (or Byte) variable is smaller than the memory required for a short variable. The amount of space that a short variable needs is lower than that of an int (and Int32). Therefore, instead of always spending your time speculating what data type to use for a natural number variable, it is recommended that you always use the int data type, unless you are a clear and particular reason to use one of the other types.
Operations on Integers
Integers support all of the four regular operations: addition, subtraction, multiplication, and division. When it comes to the division:
<!DOCTYPE html>
<html>
<head>
<title>Basic Algebra</title>
</head>
<body>
@helper Calculate()
{
@(240 / 1000)
}
<p>240 / 1000 = @Calculate()</p>
</body>
</html>Line
This would produce:
<!DOCTYPE html> <html> <head> <title>Basic Algebra</title> </head> <body> @helper Calculate() { @(1000 / 240) // In Microsoft Excel = 4.166666667 } @helper Divide() { @(1226 / 317) // In Microsoft Excel = 3.867507886 } <p>1000 / 240 = @Calculate()</p> <p>1226 / 317 = @Divide()</p> </body> </html>This would produce:
Practical Learning: Performing Operations on Integers
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace OFRE3.Controllers
{
public class CampaignsController : Controller
{
// GET: Campaigns
public ActionResult Index()
{
return View();
}
// GET: TwoWay
public ActionResult TwoWay()
{
return View();
}
// GET: ThreeWay
public ActionResult ThreeWay()
{
return View();
}
public int DistributeFirstPart(int amount, int portion1, int portion2)
{
int totalRatios = portion1 + portion2;
int eachPart = amount / totalRatios;
int result = eachPart * portion1;
return result;
}
public int Subtract(int first, int second)
{
return first - second;
}
}
}
Converting an Integral Type
As you may know already andas we have seen in previous lessons, by default, the value that a user selects from a control or types in a text box is an object. To retrieve it, you can pass the name of its control in the double-quotes of Request[""]. The value produced by Request[""] must be converted to the intended purpose. So far, to convert the value to an integer, we applied (or called) the AsInt() method.
To support value conversion for each type, the static Convert class provides a method for each type. They are:
C# Data Type | .NET Type/Structure | Method Used to Convert |
byte | Byte | Convert.ToByte() |
sbyte | SByte | Convert.ToSByte() |
short | Int16 | Convert.ToInt16() |
ushort | UInt16 | Convert.ToUInt16() |
int | Int32 | Convert.ToInt32() |
uint | UInt32 | Convert.ToUInt32() |
long | Int64 | Convert.ToInt64() |
ulong | UInt64 | Convert.ToUInt64() |
Practical Learning: Converting a Value to an Integral Type
@{ ViewBag.Title = "Two-Way Campaign Distribution"; } @{ int amtPledged = 0; int ratio1 = 1; int ratio2 = 1; if (IsPost) { amtPledged = Convert.ToInt32(Request["txtAllocation"]); ratio1 = Convert.ToInt32(Request["txtPortion1"]); ratio2 = Convert.ToInt32(Request["txtPortion2"]); } } <p> </p> <p> </p> <div class="contents content2Parts"> <p class="heading2 text-center heading-format">Pledge Distribution</p> <form name="frmDistribution" method="post"> <table> <tr> <td class="left-column">Amount to Allocate:</td> <td><input type="text" name="txtAllocation" value=@amtPledged /></td> </tr> </table> <table> <tr> <td class="left-column">In the Ratio:</td> <td><input type="text" name="txtPortion1" class="tbx-format" value=@ratio1 /></td> <td>:</td> <td><input type="text" class="tbx-format" name="txtPortion2" value="@ratio2" /></td> <td><input name="btnAllocate" type="submit" value="Allocate" /></td> </tr> </table> <hr /> <table> <tr> <td class="left-column">Part 1 Receives:</td> <td><input type="text" name="txtPart1" value="" /></td> </tr> <tr> <td>Part 2 Receives:</td> <td><input type="text" name="txtPart2" value="" /></td> </tr> </table> </form> </div>
To let you convert a value of a primitive type to a string, each structure of a primitive type is equipped with a method named ToString. This method is overloaded with various versions. One of the versions of this method takes no argument. The syntax of this method is:
public override string ToString();
Another version of this method takes as argument a string. This string holds an expression used to format the value of the variable that called the method. The syntax of this method is:
public string ToString(string format);
You can pass the desired string to format the value to be displayed.
Practical Learning: Converting Integral Values to Strings
@{ ViewBag.Title = "Two-Way Campaign Distribution"; } @{ int amtPledged = 0; int ratio1 = 1; int ratio2 = 1; int portion1 = 0; int portion2 = 0; if (IsPost) { amtPledged = Convert.ToInt32(Request["txtAllocation"]); ratio1 = Convert.ToInt32(Request["txtPortion1"]); ratio2 = Convert.ToInt32(Request["txtPortion2"]); OFRE3.Controllers.CampaignsController cc = new OFRE3.Controllers.CampaignsController(); portion1 = cc.DistributeFirstPart(amtPledged, ratio1, ratio2); portion2 = cc.Subtract(amtPledged, portion1); } } <p> </p> <p> </p> <div class="contents content2Parts"> <p class="heading2 text-center heading-format">Pledge Distribution</p> <form name="frmDistribution" method="post"> <table> <tr> <td class="left-column">Amount to Allocate:</td> <td><input type="text" name="txtAllocation" value=@amtPledged /></td> </tr> </table> <table> <tr> <td class="left-column">In the Ratio:</td> <td><input type="text" name="txtPortion1" class="tbx-format" value=@ratio1 /></td> <td>:</td> <td><input type="text" class="tbx-format" name="txtPortion2" value="@ratio2" /></td> <td><input name="btnAllocate" type="submit" value="Allocate" /></td> </tr> </table> <hr /> <table> <tr> <td class="left-column">Part 1 Receives:</td> <td><input type="text" name="txtPart1" value="@portion1.ToString()" /></td> </tr> <tr> <td>Part 2 Receives:</td> <td><input type="text" name="txtPart2" value="@portion2.ToString()" /></td> </tr> </table> </form> </div>
Amount to Allocate: 6540 Portion 1: 5 Portion 2: 3
Parsing a String
To let you retrieve a value from a text-based control and convert it to the desired value, each structure of a primitive data type is equipped with a static method named Parse. The syntaxes of the types are:
byte |
public static byte Parse(string s); |
sbyte |
public static sbyte Parse(string s); |
short |
public static short Parse(string s); |
ushort <=> UInt16 |
public static ushort Parse(string s); |
int <=> Int32 |
public static int Parse(string s); |
uint <=> UInt32 |
public static uint Parse(string s); |
long <=> Int64 |
public static long Parse(string s); |
unsigned long <=> UInt64 |
public static ulong Parse(string s); |
When calling this method, if a bad value is passed to the Parse() method, the program would produce an error. To assist you with this type of problem, each structure of a primitive type is equipped with a method named TryParse. This method is overloaded with two versions that each returns a Boolean value. One of the versions of this method takes two arguments: a string and a variable passed as an out reference. The syntaxes of this method are:
byte |
public static bool TryParse(string s, out byte result) |
sbyte |
public static bool TryParse(string s, out sbyte result) |
short |
public static bool TryParse(string s, out short result) |
ushort <=> UInt16 |
public static bool TryParse(string s, out ushort result) |
int <=> Int32 |
public static bool TryParse(string s, out int result) |
uint <=> UInt32 |
public static bool TryParse(string s, out uint result) |
long <=> Int64 |
public static bool TryParse(string s, out long result) |
unsigned long <=> UInt64 |
public static bool TryParse(string s, out ulong result) |
Based on this, if an int variable calls this method, the first argument is passed as a string and the second argument is passed as an out int object. This means that the second argument is returned from the method also.
When the TryParse() method is called:
Comparing the Values of Primitive Types
One of the most common operations performed on variables consists of comparing their values: to find out whether they are equal or to know whether one is higher than the other. These operations can be performed using the Boolean operators we know already. The Boolean operations are part of the C# language. To formally implement them, each structure of a data type is implements the IComparable interface that we reviewed inthe previous lesson. As a result, each structure of a primitive type is equipped with a method named CompareTo that is overloaded with two versions.
One of the implementations of the CompareTo() method compares a value or the value of a variable of the same type, with the variable that called the method. This method takes one argument that is the same type as the variable that is calling it. The syntaxes of this method are:
byte |
public int CompareTo(byte value) |
sbyte |
public int CompareTo(sbyte value) |
short |
public int CompareTo(short value) |
ushort <=> UInt16 |
public int CompareTo(ushort value) |
int <=> Int32 |
public int CompareTo(int value) |
uint <=> UInt32 |
public int CompareTo(uint value) |
long <=> Int64 |
public int CompareTo(long value) |
unsigned long <=> UInt64 |
public int CompareTo(ulong value) |
The method returns an int value. For example, imagine you have two int variables named variable1 and variable2, you can call the CompareTo() method of the first variable to compare its value to that of the second variable. This would be done as in:
int result = variable1.CompareTo(variable2);
The end result would be as follows:
Here is an example:
@{ int Variable1 = 248; int Variable2 = 72937; int result = Variable1.CompareTo(Variable2); }
Another version of the CompareTo() method allows you to compare the value of a variable with a variable whose type is not the same as the variable that called it. Since all types and classes of C# are based on object, this second version takes as argument an object object. The method returns an int value. The syntax of this version is:
public int CompareTo(object value);
Practical Learning: Ending the Lesson
|
||
Previous | Copyright © 2001-2019, FunctionX | Next |
|