Thanachat Thanomkulabut

Slides:



Advertisements
Similar presentations
1 Programming Structures COMP102 Prog. Fundamentals, Structures / Slide 2 2 Structures l A Structure is a collection of related data items, possibly.
Advertisements

Structures Spring 2013Programming and Data Structure1.
Using Classes to Store Data Computer Science 2 Gerb.
Structures. An array allows us to store a collection of variables However, the variables must be of the same type to be stored in an array E.g. if we.
Structures COMP104 Structs / Slide 2 Motivation: a new type * Structures hold data that belong together. * Examples: n Student record  student id, name,
Thanachat Thanomkulabut
METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass.
INPUT/OUTPUT STATEMENT ADDITION SLIDES. Console.ReadLine() – Use to get the input (String) from user Convert string to other data type – int.Parse() 
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Iteration (Loop) partI Thanachat Thanomkulabut. Consider the following program! using System; Namespace SimPleExample { class SimPleC { class SimPleC.
Classes and Object-Oriented Programming in C# Computers and Programming ( )
STRUCT Thanachat Thanomkulabut 1. Array Review 2  Group multiple items of the same type into one "variable" double[] score; score = new.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
C# Basic Concept Thanachat Thanomkulabut. Naming Rules  Letters, digits and underscores(_)  First character  letter or _  Up to 63 characters long.
Array, Structure and Union
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Struct Data Type Computers and Programming ( )
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Interfaces and Inner Classes
METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Type of Method No Returned value Returned value No Parameter Pass by value refout Pass by reference Parameter.
Struct COMP104 Struct / Slide 2 Motivation * Structures hold data that belong together. * Examples: n Student record  student id, name, major, gender,
Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.
Multiple Items in one Data Object Arrays are a way to store more than one piece of data in a data object, provided that all the data is of the same type.
INPUT/OUTPUT STATEMENT ADDITION SLIDES. Console.ReadLine() – Use to get the input (String) from user Convert string to other data type – int.Parse() 
1 st Semester Module 6 C# Methods – Part II อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Matthew Small Introduction to Object-Oriented Programming.
Classes - Intermediate
Enum,Structure and Nullable Types Ashima Wadhwa. Enumerations, Enumerations, or enums, are used to group named constants similar to how they are used.
1 st Semester Module11 Struct Type in C# Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department.
1 st Semester Module 7 Arrays อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department.
Computer Programming II
Andrew(amwallis) Classes!
C# Basic Syntax, Visual Studio, Console Input / Output
C# Basic Syntax, Visual Studio, Console Input / Output
Programmer Defined Types and Classes
Examples of Classes & Objects
BİL527 – Bilgisayar Programlama I
C Programming Tutorial – Part I
Concepts and Basics of C++ Programming
Classes.
Programming Structures.
Arrays Computer & Programming Group
C# Programming Arrays in C# Declaring Arrays of Different Types Initializing Array Accessing Array Elements Creating User Interfaces Using Windows Standards.
Concepts and Basics of C++ Programming
Interfaces.
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Structures Lesson xx In this module, we’ll introduce you to structures.
Structures A Structure is a collection of related data items, possibly of different types. A structure type in C++ is called struct. A struct is heterogeneous.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Structure ការណែនាំអំពី Structure
Structures.
Classes & Objects: Examples
Thanachat Thanomkulabut
EGR 2261 Unit 12 structs Read Malik, Chapter 9.
Module8 Multi-dimensional Array
Objectives In this chapter, you will: - Learn about records (structs) - Examine operations on a struct - Manipulate data using a struct - Learn about the.
Module 8 & 9 Method :Part I & Array :Part II
Arrays in Java.
Programming Language C Language.
Iteration (Loop) partII
C Programming Lecture-13 Structures
Structures In C Programming By Rajanikanth B.
Programming Structures.
Chapter 9 Objects and Classes Part 01
Thanachat Thanomkulabut
Programming Structures.
Structures in c By Anand George.
Chapter 10 C Structures and Unions
Programming Fundamental
Presentation transcript:

Thanachat Thanomkulabut Struct Thanachat Thanomkulabut

Array Review Group multiple items of the same type into one "variable" double[] score; score = new double[7]; score Every items is double 1 2 3 How can we do if we want to keep a few things that are of different types together? 4 5 6

Structure Preview StudentInfo IcecreamInfo Name: Thanachat Code: C4564 string string Dept: CPE Flavor: Chocolate string string Age: 18 Weight: 500 (g) int int Gender: M Price: 65.50 B char double

Structure Allows multiple items of varying types to be kept in one place Somewhat similar to an array In C#, a structure is known as struct A struct is defined by programmer

Using struct Type 1. Define struct 2. Create struct 3. Access struct

Defining a Struct StudentInfo Name: Thanachat struct StudentInfo { Must use "struct" keyword Every struct needs a name Name: Thanachat struct StudentInfo { public string name; public string dept; public int age; public char gender; } string Dept: CPE string Age: 18 int Members (or properties) of struct Gender: M char Protection level – always use "public" for now

Defining a Struct IcecreamInfo struct IcecreamInfo { public string code; public string flavor; public int weight; public double price; } Code: C4564 string Flavor: Chocolate string Weight: 500 (g) int Price: 65.50 B double

Defining a Struct (cont'd) Where do we put the struct definition? Inside a class and outside a method E.g., using System; class StructTest { static void Main() { : } struct StdInfo { public int id; public string name; public string dept; }

Defining a Struct ComplexNumber Real: 3.7 Imag: -10.5 using System; class StructTest { struct ComplexNumber { public double real; public double imag; } static void Main() { : Real: 3.7 double Imag: -10.5 double

Defining a Struct (Test I) MovieInfo using System; class StructTest { struct MovieInfo { public string name; public string genre; public int minute; } static void Main() { : Name: Avatar string Genre: Sci-Fi string Minute: 165 int

Creating a Struct Or Syntax: <struct_name> <var_name>; <struct_name> <var_name> = new <struct_name>(); <struct_name> <var_name>;

Creating a Struct (Example) <struct_name> <var_name>= new <struct_name>(); using System; class StructTest { static void Main() { } StudentInfo struct StdInfo { public string name; public string dept; public int age; public char gender; } Name: string Dept: string Age: int StdInfo student Gender: = new StdInfo (); char

Creating a Struct (Example) <struct_name> <var_name>; using System; class StructTest { static void Main() { } StudentInfo struct StdInfo { public string name; public string dept; public int age; public char gender; } Name: string Dept: string Age: int StdInfo student StdInfo Student; Gender: = new StdInfo (); char

Creating a Struct (Test) 1 <struct_name> <var_name>= new <struct_name>(); 2 <struct_name> <var_name>; IcecreamInfo class StructTest { static void Main() { } struct IcecreamInfo { Code: public string code; string public string flavor; Flavor: public int weight; string public double price; Weight: } int Price: IcecreamInfo ice1 double IcecreamInfo ice1; = new IcecreamInfo();

Accessing Struct Variable Syntax <var_name>.<member_name> Student1 StudentInfo using System; class StructTest { static void Main() { } Monitor Mc name Name: Mc struct StdInfo { public string name; public string dept; public int age; public char gender; } string dept Dept: age 18 string gender Age: int StdInfo student1; student1.name = "Mc"; Gender: student1.age = 18; char Console.Write(student1.name);

Accessing Struct Variable Monitor z1 + z2 = (7, 4) z1 Real Imag ComplexNumber Real: Imag: double 5 class StructExample { static void Main() { } struct ComplexNumber { public double real; public double imag; } 1 z2 Real Imag 2 ComplexNumber z1,z2,z3; z1.real = 5; z1.imag = 1; 3 z2.real = 2; z2.imag = 3; z3.real = z1.real + z2.real; z3 Real Imag z3.imag = z1.imag + z2.imag; Console.Write(("z1 + z2 = ({0}, {1})" ,z3.real,z3.imag); 7 4

Test II From test I extend the Main method to read name, genre and minute of your favorite movie and show them to the monitor using System; class StructTest { struct MovieInfo { public string name; public string genre; public int minute; } static void Main() { : MovieInfo Name: Genre: Minute: string int

Test II static void Main() { MovieInfo myMovie; myMovie.name = Console.ReadLine(); myMovie.genre = Console.ReadLine(); myMovie.minute = int.Parse(Console.ReadLine()); Console.WriteLine ("Movie name = {0}",myMovie.name); Console.WriteLine ("Movie genre = {0}",myMovie.genre); Console.WriteLine ("Movie minute = {0}",myMovie.minute); }

Example: Struct & Method How can I replace this code with Method ReadInfo() static void Main() { MovieInfo myMovie; myMovie.name = Console.ReadLine(); myMovie.genre = Console.ReadLine(); myMovie.minute = int.Parse(Console.ReadLine()); Console.WriteLine ("Movie name = {0}",myMovie.name); Console.WriteLine ("Movie genre = {0}",myMovie.genre); Console.WriteLine ("Movie minute = {0}",myMovie.minute); } static void Main() { MovieInfo myMovie; myMovie = ReadInfo(); } static MovieInfo ReadInfo () { MovieInfo M; M.name = Console.ReadLine(); M.genre = Console.ReadLine(); M.minute = int.Parse(Console.ReadLine()); return M; }

Example: Struct & Method How can I replace this code with Method ShowInfo() static void Main() { MovieInfo myMovie; myMovie = ReadInfo(); Console.WriteLine ("Movie name = {0}",myMovie.name); Console.WriteLine ("Movie genre = {0}",myMovie.genre); Console.WriteLine ("Movie minute = {0}",myMovie.minute); } ShowInfo(myMovie); } static void ShowInfo (MovieInfo M) { Console.WriteLine("Movie name = {0}",M.name); Console.WriteLine ("Movie genre = {0}",M.genre); Console.WriteLine ("Movie minute = {0}",M.minute); }

Array of Structs 1 2 Array of integers Struct of student info Array of structs of student info 7 10 3 5 17 8 2 1 4 6 9 11 5010000 Aum CPE ID: Name: Dept: 5010000 Aum CPE ID: Name: Dept: 5020000 Nat IE ID: Name: Dept: 1 5010000 Top EE ID: Name: Dept: 2

Array of Structs Array declaration double[] score; struct StdInfo { public int id; public string name; public string dept; } Array declaration Array of Struct declaration double[] score; score = new double[4]; score 1 2 3 StdInfo[] student; student = new StdInfo[4]; id name dept id name dept id name dept id name dept student 1 2 3

Array of Structs Array Accessing score[2] = 5; Array of Struct Accessing score[2] = 5; score 5 1 2 3 student[1].dept = "CPE"; student[3].id = 713; id name dept id name dept id name dept id name dept 713 student CPE 1 2 3

Example : Read 50 Student’s Information class StructTest { struct StdInfo { public int id; public string name; public string dept; } static void Main() { } } StdInfo ID: Name: Dept: int string StdInfo[] student = new StdInfo[50]; for(int i=0;i<50;i++){ Console.Write("Input ID: "); student[i].id = int.Parse(Console.ReadLine()); Console.Write("Input Name: "); student[i].name = Console.ReadLine(); Console.Write("Input Department: "); student[i].dept = Console.ReadLine(); }

Example Display name of students of CPE department for(int i=0;i<50;i++){ if(student[i].dept == "CPE") Console.WriteLine(student[i].name); }

Any question?