1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

EASTERN MEDITERRANEAN UNIVERSITY EENG212 ALGORITHMS & DATA STRUCTURES Structures in C.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.
Data Structures Using C++ 2E
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Structure.
Structures Spring 2013Programming and Data Structure1.
Structures in C.
String in C++. String Series of characters enclosed in double quotes.“Philadelphia University” String can be array of characters ends with null character.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 25 Thanks for Lecture Slides: Dr. Sadaf Tanveer Dr. Sadaf Tanveer,
Kernighan/Ritchie: Kelley/Pohl:
Topic 11 – Structures and Unions. CISC105 – Topic 11 Introduction to Structures We have seen that an array is a list of the same type of elements. A structure.
CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT.
16/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Methods to manipulate a collection of values under one name. Arrays: Homogenous.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle.
1 CSE1301 Computer Programming Lecture 21 Structures (Part 2)
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Data Type. A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common.
Structs. Structures We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Functions, Pointers, Structures Keerthi Nelaturu.
Week 9 - Monday.  What did we talk about last time?  Time  GDB.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Array, Structure and Union
Arrays and Strings. Why? Consider a class of 30 students, each has a score for hw1  Do we want to have 30 variables with different names?  Would it.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
C Lecture Notes 1 Structures & Unions. C Lecture Notes Introduction Structures –Collections of related variables (aggregates) under one name Can.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
CPS120: Introduction to Computer Science Lecture 15A Structures.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
1 CSC103: Introduction to Computer and Programming Lecture No 24.
11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,
Struct Data Type in C++ What Are Structures?
CPS120: Introduction to Computer Science Data Structures.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Chapter 6 Structures Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Chapter 11 Structures, Unions and Typedef 11.1 Structures Structures allow us to group related data items of different types under a common name. The individual.
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
Introduction to programming in java Lecture 21 Arrays – Part 1.
CS150 Introduction to Computer Science 1
EECE.2160 ECE Application Programming
Introduction to Computer Programming Lecture 17 Structures (Part 2)
CS148 Introduction to Programming II
EECE.2160 ECE Application Programming
Objectives In this chapter, you will: - Learn about records (structs) - Examine operations on a struct - Manipulate data using a struct - Learn about the.
Structure.
Structs Adapted from Dr. Mary Eberlein, UT Austin.
C Programming Lecture-8 Pointers and Memory Management
C Programming Pointers
EECE.2160 ECE Application Programming
CPS120: Introduction to Computer Science
Introduction to Computer Programming Lecture 16 Structures (Part 1)
Structure (i.e. struct) An structure creates a user defined data type
Characters and Strings
EECE.2160 ECE Application Programming
Week 9 - Monday CS222.
Presentation transcript:

1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University Spring 2009

Structures Lecture 10

Data Structure Simple data types use a single memory cell to store a single data item. Data Structure: a collection of related data items stored under the same name. Array: a collection of data items of the same type. Structure (Record): a collection of related data items not necessarily of the same type.

Structures and Their Manipulation Structures may contain variables of different types as opposed to arrays for example, that are formed from elements of the same type. A structure is useful when we want to group data items that are related. For example, we may want to group a student’s first name, last name, student number and grade. In this example, the data items to be stored are of different types: the student names are strings, the student number is an integer and the student grade is a real number. John Doe Structure String (array of char ) to store the first name. String (array of char ) to store the last name. int variable to store the student number. double variable to store the grade.

Structure Type Definition Before a structure can be used, the format of its components must be defined. Several ways to define a structure, the most used way is to define a new data type using typedef: typedef struct { char first_name[SIZE]; char last_name[SIZE]; int number; double grade; } student; A variable declaration is required to allocate memory space for one student: student stud1; Student *studPtr; student stud2 = {“John”, “Doe”, 12345, 92.1};

Accessing Members of a Structure The members of a structure can be accessed using one of two possible operators: the structure member operator, often called the dot operator,. and, the structure pointer operator, often called the arrow operator, -> A member of a structure variable is accessed using the dot operator by quoting the name of the structure variable followed by the dot and the name of the member to be accessed: stud1.first_name = “ John ” ; stud1.last_name = “ Doe ” ; stud1.number = ; stud1.grade = 92.1; Initializes the structure variable stud1

The structure pointer operator is used with a pointer to a structure variable in order to access the structure variable’s members. studPtr = &stud1; stud1Ptr->first_name = “ John ” ; stud1Ptr->last_name = “ Doe ” ; stud1Ptr->number = ; stud1Ptr->grade = 92.1; We can also use the dereferencing operator * to access a structure variable’s member via its pointer: (*studPtr).first_name = “ John ” ; Initializes the structure variable stud1 Accessing Members of a Structure

/*************************************************/ // Exploring structures in C. /*************************************************/ #include void main() { // Definition of structures. typedef struct { char *first_name; char *last_name; long number; double grade;} student; // Variable declarations. student one_student = {"John", "Doe", , 92.1}; student csc1401[15], *studentPtr; // Print out the variable one_student. printf("First name: %s\n", one_student.first_name); printf("Last name: %s\n", one_student.last_name); printf("Student number: %ld\n", one_student.number); printf("Grade: %f\n\n", one_student.grade); // Access members of one_student using the dot operator. one_student.first_name = "Miles"; one_student.last_name = "Davis"; one_student.number = ; one_student.grade = 49.4; // Print out the variable one_student. printf("First name: %s\n", one_student.first_name); printf("Last name: %s\n", one_student.last_name); printf("Student number: %ld\n", one_student.number); printf("Grade: %f\n\n", one_student.grade); // Access members of one_student using the arrow operator. studentPtr = &one_student; studentPtr->first_name = "Billie"; studentPtr->last_name = "Holiday"; studentPtr->number = ; studentPtr->grade = 95.2; // Print out the variable one_student. printf("First name: %s\n", studentPtr->first_name); printf("Last name: %s\n", studentPtr->last_name); printf("Student number: %ld\n", studentPtr->number); printf("Grade: %f\n\n", studentPtr->grade); // Access members of an elment of array csc1401 // using the dot operator. csc1401[10].first_name = "Dave"; csc1401[10].last_name = "Brubeck"; csc1401[10].number = ; csc1401[10].grade = 80.0; // Print out element 10 of the array csc1401. printf("First name: %s\n", csc1401[10].first_name); printf("Last name: %s\n", csc1401[10].last_name); printf("Student number: %ld\n", csc1401[10].number); printf("Grade: %f\n\n", csc1401[10].grade); // Access members of an element of array csc1401 // using the arrow operator. (csc1401[11])->first_name = "Bill"; (csc1401[11])->last_name = "Evans"; (csc1401[11])->number = ; (csc1401[11])->grade = 85.1; // Print out element 11 of the array csc1401. printf("First name: %s\n", (csc1401[11])->first_name); printf("Last name: %s\n", (csc1401[11])->last_name); printf("Student number: %ld\n", (csc1401[11])->number); printf("Grade: %f\n\n", (csc1401[11])->grade); }

Output of the program on the previous program. Accessing Members of a Structure

/**********************************************/ // Program that manages a class list. /*********************************************/ #include #define CLASS_SIZE 2 #define TOTAL_LABS 5 // Placing structure definitions before main() // makes them global in the program. typedef struct { char *lastName; char *firstName; long number; double lab[TOTAL_LABS]; double labAverage;} student_rec; // Prototypes. void calc_lab_ave(student_rec *recordPtr); // Main program. void main() { // Variable declaration. int ctr; student_rec csc1401[CLASS_SIZE]; // Initialization. csc1401[0].firstName = "Miles"; csc1401[0].lastName = "Davis"; csc1401[0].number = ; csc1401[0].lab[0] = 80.2; csc1401[0].lab[1] = 60.2; csc1401[0].lab[2] = 71.5; csc1401[0].lab[3] = 90.3; csc1401[0].lab[4] = 85.1; csc1401[1].firstName = "Bill"; csc1401[1].lastName = "Evans"; csc1401[1].number = ; csc1401[1].lab[0] = 82.2; csc1401[1].lab[1] = 65.2; csc1401[1].lab[2] = 72.5; csc1401[1].lab[3] = 94.3; csc1401[1].lab[4] = 88.1; // Compute the average of the labs. for (ctr=0; ctr <= CLASS_SIZE-1; ctr++) calc_lab_ave(&csc1401[ctr]); // Print out the records. for (ctr=0; ctr <= CLASS_SIZE-1; ctr++) { printf("Student: %s %s\n", csc1401[ctr].firstName, csc1401[ctr].lastName); printf("Number: %ld\n", csc1401[ctr].number); printf("Lab Average: %f\n\n", csc1401[ctr].labAverage);} } /***************************************************/ // This function computes the lab average for // a student record. /***************************************************/ void calc_lab_ave(student_rec *recordPtr) { // Local variables. int ctr; double average=0.0; // Compute the average of the labs. for (ctr=0; ctr <= TOTAL_LABS-1; ctr++) average = average + recordPtr->lab[ctr]; recordPtr->labAverage = average/TOTAL_LABS; }