11. C-sharp 5 01-10-2013

Created Saturday 23 November 2013

** O. Deitel !!!!!
** You have to read 700+- pages a week! ^)
** Code complete !!!!!
** Shildt (-reference, -'manuals') !!!
** Data structures and algorithms
** Module 2 Lessons 1-2 (already should know)
*** Variables - complex tasks require modeling implementation of real-life objectives in memory
*** Data structures - compound variables, consisting of other variables, grouping them together
**** There are predefined data structures, and also abilities to create custom data structures
**** Collections
***** Make possible to group simple vars in group to be processed together
***** Static
****** Consist of fixed amount of simple variables
****** + speed, simplicity, reliability
***** Dynamic
****** Consist of variable amount of simple variables
****** + flexibility
***** Both dynamic and static may be:
***** Typed
****** Preferable over non-typed (speed, reliability)
***** Non-typed
***** Array
****** Static collection, may be typed and non-typed
****** Example
#+BEGIN_SRC C#
Array a = Array.CreateInstance(typeof(Int32),5);
a.SetValue(123456789,0);
for(int i = 0; i < a.Length; i++) {
Console.WriteLine(a.GetValue(i));
}
#+END_SRC
****** 2124 Module 6
****** [] - postfix index operator
****** All elements of static collections are placed in adjacent memory cells (!!!!!!)
***** Struct(ure) - Non-typed static data structure: can be created:
****** Example
#+BEGIN_SRC C#
struct Student {
public int ID;
public string name;
public DateTime BirthDate;
public int[] marks;
} // Structure (new type) definition
Student st; // Declaration of type 'Student' variable st
// Next: fill data by name of element
st.ID = 1234567;
st.name = "Pupkin";
st.BirthDate = new DateTime(1978,10,29);
st.marks = new int[7];
st.marks[0] = 90;
Student[] classMCSD = new Student[20];
classMCSD[0].ID = 9393939393;
classMCSD[0].marks = new int[7];
classMCSD[0].marks[0] = 56;
#+END_SRC
***** Associated array emulation:
#+BEGIN_SRC C#
// Students
const int PUPKIN = 0;
const int ZALKIND = 1;
const int MALKIN = 2;
// Courses
const int NUMBER_OF_COURSES = 7;
const int c2667 = 0;
const int c2609 = 1;
const int c10265 = 3;
classMCSD[ZALKIND].marks[c2609] = 56;
classMCSD[ZALKIND].ID = 123412345;
classMCSD[ZALKIND].marks = new int[NUMBERS_OF_COURSES];
// Retrieve data by name
Console.WriteLine("{1} was born at {2}, have id={0}",st.ID,st.name,st.BirthDate);
#+END_SRC
***** Now always index arrays with constants!
***** Elegant way
#+BEGIN_SRC C#
enum EStudents {PUPKIN,GALKIN,MALKIN,ZALKIN}; //0,1,2,3
EStudents lastname = EStudents.GALKIN;
classMCSD[(int)EStudents.GALKIN].ID = 123412345;
#+END_SRC
** Homework
*** Описать и заполнить подходящие структуры данных для решения следующей задачи:
В классе 18 студентов. У каждого студента есть номер, фамилия, и 16 ячеек для
указания присутствия. Первые 4 ячейки заполнены значениями 0 - не проставлено,
1 - присутствовал, -1 - отсутствовал. Написать программу, которая вычисляет
процент посещаемости и хранит его в отдельной переменной внутри структуры студента.



Backlinks: