c 1 introduction to programming and the c language

c 1 introduction to programming and the c language


DisciplinaFísica Básica I830 materiais10.237 seguidores
Pré-visualização50 páginas
and the important thing here is to note that one can use the index operator.
Comment
Of the collection classes that are mentioned above, is a List the most commonly used and it is applied 
basically when you do not have the ability to estimate the size of an array.
Exam68
Enter sale of products
The aim of this example is to show a further application of a List.
The task is to write a program where the user can enter information about a product sale. The user must 
enter some information (records) consisting of a monthly number, and an amount and it is repeated 
until there is no more information. As an example the user could enter:
Month Amount
5 800
8 1000
3 120
5 200
3 700
5 1500
....
This means that the numbers can come in any order for months, and there may be several amounts for 
the same month.
After entering, the program must print a listing that for each month shows a line for each product sale 
and the total sale \u2013 that is the sum of all item amounts for the current month.
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
195 
List<T>
How to
Basically, the task can be broken down into two sub-problems:
\u2022	 Entering of sales items
\u2022	 Print the result
A record consists of a monthly number and an amount, and I will therefore start with a type that can 
represent a single record:
struct Sale : IComparable<Sale>
{
public int month;
public double value;
public int CompareTo(Sale sale)
{
return month.CompareTo(sale.month);
}
}
As records come in any order, they must be stored somewhere, before the result can be counted together, 
and here I will use a List. Every time you enter a new record, I\u2019ll add it to the List. The list may be defined 
as follows:
static List<Sale> sales = new List<Sale>();
The first sub-problem can be described as:
repeat
{
enter mounth number
if month == 0 then terminate
enter amount
add a Sale object to the list
}
The entering routine can now be written in C# as:
static void AddSales()
{
while (true)
{
Sale sale;
sale.month = Enter.EnterInt(&quot;Enter 1, 2, ..., 12 for month or 0 for stop&quot;);
if (sale.month == 0) break;
if (sale.month >= 1 && sale.month <= 12)
{
sale.value = Enter.EnterDouble(&quot;Enter item amount&quot;);
sales.Add(sale);
}
else Console.WriteLine(&quot;Ulovlig værdi for måned...&quot;);
}
}
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
196 
List<T>
Then there is the other sub-problem. The numbers should be printed in order of the months, so I will 
start by sorting the content of the List for months, so I know when the list is traversed that one comes 
to numbers in the right order. Then the task is in principle simple and mainly consists in summarizing 
month totals and print product amounts. The hardest part is to print the totals at the right time:
let sum = 0
as long as there are more records in the list repeat
{
if the record is from the same month then
{
add the amount to sum
}
or
{
print the total for the month
start with a new month
}
print the product amount
}
The algorithm can be written in C# as follows:
static void Print()
{
int month = 0;
double sum = 0;
foreach (Sale sale in sales)
{
if (sale.month == month)
sum += sale.value;
else
{
if (month > 0) Console.WriteLine(&quot;Month {0, 2}{1, 12:F2}&quot;, month, sum);
sum = sale.value;
month = sale.month;
}
Console.WriteLine(&quot;{0, 20:F2}&quot;, sale.value);
}
Console.WriteLine(&quot;Month {0, 2}{1, 12:F2}&quot;, month, sum);
}
Then there is the Main() method, and here is the only one outstanding to sort the list, but the List class 
has a method for that purpose, that does it all, as long as the list\u2019s objects implements the IComparable 
interface, but that is precisely the case for the type of Sale:
static void Main(string[] args)
{
AddSales();
sales.Sort();
Print();
}
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
197 
List<T>
Explanation
Regarding the type of Sale there is not much to explain beyond, once again noting that it implements the 
IComparable interface so that Sale objects can be compared and thus sorted. The type is a struct instead 
of a class, and there is no specific justification for that only once again to remind you of a struct, and to 
show that a struct can implement an interface. Although that in this connection there is no particular 
advantage of using a struct, then Sale indeed is a good candidate for a struct as the type consisting solely 
of two simple variables.
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
198 
List<T>
The list itself is defined as a static variable in the class and you should primarily observe how the list is 
defined to contain Sale items. Also note how it is sorted in Main().
The most important in the method AddSales() is how to create Sale objects (as Sale is a struct, it is not 
necessary to create an object with new) and adds them to the list. Most of the method has to do with 
the input, using two input methods, which are not shown above. You can find them in the final code.
The method Print() has two local variables:
\u2022	 month to keep track of the number of the current month
\u2022	 sum to summing the total for a particular month
Otherwise, the method consists of a loop over the list\u2019s elements, and here it is important to keep track 
of whether there is a change of month. If applicable, the total is printed, and the two auxiliary variables 
must be initialized for a new month.
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
199 
Stack<T> and Queue<T>
25 Stack<T> and Queue<T>
A stack is a collection which has two basic operations
\u2022	 one can put an item on the stack, an operation that is called push
\u2022	 one can remove the element that was last placed on the stack, an operation that is called pop
For variables, I have several times mentioned the program stack. This is an example of a data structure that 
is a stack. You also sometimes call the data structure a LIFO (Last In First Out) structure, corresponding 
to that there only is access to the element that last are put on the stack. One think typical of the structure 
as a container where you can place an element at the top, where the arrow is pointing and where you 
can only remove the item that the arrow is pointing at \u2013 it stacks elements on top of each other and 
hence the name.
7
5
3
2
A concrete implementation of the type and that includes the type Stack<T> in C#, will usually also define 
other methods. For example a method to refer to the element at the top of the stack without removing 
it, and a method in order to test whether the stack is empty.
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
200 
Stack<T> and Queue<T>
Exam46
Stack of integers
As an example is shown a program that creates a stack of integers and places 8 numbers on the stack. 
Then the program empties the stack, and the numbers are printed on the screen:
static void Main()
{
Stack<int> s = new Stack<int>();
s.Push(2);
s.Push(3);
s.Push(5);
s.Push(7);
s.Push(11);
s.Push(13);
s.Push(17);
s.Push(19);
while (s.Count > 0) Console.WriteLine(s.Pop());
}
One should note that the numbers are printed in reverse order of how they are put on the stack \u2013 the 
number that was last placed on the stack is printed first.
By 2020, wind could provide one-tenth of our planet\u2019s 
electricity needs. Already today, SKF\u2019s innovative know-
how is crucial to running a large proportion of the 
world\u2019s wind turbines. 
Up to 25 % of the generating