1 / 10

CSC 1401 S1 Computer Programming I

CSC 1401 S1 Computer Programming I. Hamid Harroud School of Science and Engineering, Akhawayn University h.harroud@aui.ma http://www.aui.ma/~H.Harroud/CSC1401 Spring 2009. Structures. Lecture 10. Data Structure. Simple data types use a single memory cell to store a single data item.

may-ratliff
Download Presentation

CSC 1401 S1 Computer Programming I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 1401 S1Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University h.harroud@aui.ma http://www.aui.ma/~H.Harroud/CSC1401 Spring 2009

  2. Structures Lecture 10

  3. 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.

  4. 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. 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. John Doe 12345 92.1 Structure

  5. 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};

  6. 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 = 123456; • stud1.grade = 92.1; Initializes the structure variable stud1

  7. Accessing Members of a Structure • 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 = 123456; • 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

  8. /*************************************************/ // Exploring structures in C. /*************************************************/ #include <stdio.h> 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", 123456, 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 = 789012; 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 = 345678; 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 = 901234; 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 = 567890; (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); }

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

  10. /**********************************************/ // Program that manages a class list. /*********************************************/ #include <stdio.h> #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 = 789012; 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 = 345678; 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; }

More Related