Programming In C++ Spring Semester 2013 Lecture 9 Programming In C++, Lecture 9 By Umer Rana.

Slides:



Advertisements
Similar presentations
Spring Semester 2013 Lecture 5
Advertisements

1 Structures. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc) and other.
Programming and Data Structure
Structure.
Structures Spring 2013Programming and Data Structure1.
Structures in C.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Kernighan/Ritchie: Kelley/Pohl:
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#4)
BY Kamran Yousaf Application of Computer Graphics and Animation Using C++ Language.
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
CS100A, Fall 1997, Lectures 221 CS100A, Fall 1997 Lecture 22, Tuesday 18 November Introduction To C Goal: Acquire a reading knowledge of basic C. Concepts:
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
Guide To UNIX Using Linux Third Edition
‘C’ LANGUAGE PRESENTATION.  C language was introduced by Dennis Ritchie..  It is a programming language, which can make a interaction between user and.
C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
ICS312 Set 4 Program Structure. Outline for a SMALL Model Program Note the quiz at the next lecture will be to reproduce this slide.MODEL SMALL.586 ;
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
C Tokens Identifiers Keywords Constants Operators Special symbols.
PASSING VALUE TO A FUNCTION # CALL BY VALUECALL BY VALUE # CALL BY REFERENCECALL BY REFERENCE STORAGE CLASS # AUTOAUTO # EXTERNALEXTERNAL # STATICSTATIC.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Learners Support Publications Classes and Objects.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
Chapter 11: Pointers Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Pointers.
Introduction to Programming 3D Applications CE Lecture 12 Structure Pointers and Memory Management in C.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
Array, Structure and Union
Spring 2005, Gülcihan Özdemir Dağ Lecture 11, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 11 Outline 11.1.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
© Oxford University Press All rights reserved. CHAPTER 8 STRUCTURES.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
10H Interrupt. Option 0H – Sets video mode. Registers used: – AH = 0H – AL = Video Mode. 3H - CGA Color text of 80X25 7H - Monochrome text of 80X25 Ex:
Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
UNION UNIT -II. Unions A union is another compound datatype like a structure. So what is the properties of structure? Eg: Union exam { int roll_no; Char.
Computer Programming for Engineers
ROM BIOS Chapter 9. The ROM BIOS PC computer come with a set od built in routines collectively called the ROM BIOS. These routines are permanent part.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
STRUCTURES. Library system : Book’s name (a string), Book’s price (a float) number of pages in it (an int). SOLUTION- Construct individual arrays, one.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Programming Fundamentals Enumerations and Functions.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
Project Assignment Snake Game/Car Acceleration Meter Min 10 Pages 10 min Presentation Max 5 group members Submitting Date: lab 2:Dec 27, 2014 Lab 3: Dec.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
Arithmetic Expressions
Module 2 Arrays and strings – example programs.
DATA HANDLING.
Visit for more Learning Resources
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Classes and Objects.
CS100A Lecture November 1998 Prelim 3 Statistics Maximum 94
Java Programming Language
Course Outcomes of Programming In C (PIC) (17212, C203):
Chapter 11 Structure and Union Types.
Structures, Unions, and Enumerations
Presentation transcript:

Programming In C++ Spring Semester 2013 Lecture 9 Programming In C++, Lecture 9 By Umer Rana

Structures Programming In C++, Lecture 9 By Umer Rana A structure is a user defined data type. A structure is a collection of simple variables. Variables can be of different types. Data items in structure called members of the structure. A Structure consists of a number of data items, which no need to be of the same type but they grouped together. A Structure is a data type whose format is defined by the programmer

Structures-Declaring Programming In C++, Lecture 9 By Umer Rana Syntax struct structure-name { member_type1 member_name1; member_type2 member_name2; member_type3 member_name3;.. } ; struct class { int rollno; char name[25]; float totalmark; } ;

Structures-Defining Programming In C++, Lecture 9 By Umer Rana Once we declared the structure, we can define one or more variables to be structure type. struct class var1; Or struct class var1,var2,var3;

Accessing Structures Members Programming In C++, Lecture 9 By Umer Rana Structure use the “dot operator” (.), which is also called the “membership operator”.

Accessing Structures Members Programming In C++, Lecture 9 By Umer Rana struct class { int rollno; char name[25]; float totalmark; } ; struct class var1; var1.rollno=121; var1.name=‘A’; var1.totalmark=93.5;

Initializing Structures Members Programming In C++, Lecture 9 By Umer Rana struct class { int rollno; char name[25]; float totalmark; } ; struct class var1={121,’A’,93.5}; Structure members can be initialized at declaration, this much the same manner as the element of an array; The initial value must appear in the order in which they will be assigned to their corresponding structure members, enclosed in braces and separated by commas struct stucture_name var={val1,val2,val3…..};

Nested Structures Programming In C++, Lecture 9 By Umer Rana Structure that contain other structure. struct stud_Res {int rno; char std[10]; struct stud_Marks { char subj_nm[30]; int subj_mark; }marks; }result; int main() {printf("\n\t Enter Roll Number : "); scanf("%d",&result.rno); printf("\n\t Enter Standard : "); scanf("%s",result.std); printf("\n\t Enter Subject Code : "); scanf("%s",result.marks.subj_nm); printf("\n\t Enter Marks : "); scanf("%d",&result.marks.subj_mark); printf("\n\n\t Roll Number : %d",result.rno); printf("\n\n\t Standard : %s",result.std); printf("\nSubject Code : %s",result.marks.subj_nm); printf("\n\n\t Marks : %d",result.marks.subj_mark); getch(); }

Passing Structures to Function Programming In C++, Lecture 9 By Umer Rana Same way that it can be passed in an assignment statement, the value of a structure variable can also be passed as a parameter to a function. void printRec(struct data); struct data { float amount=10.20; string fname=“Test String”; string lname=“Preston”; } rec; printRec(rec); void printRec(struct data x) { printf(“Amount is %f”,x.amount); printf(“Fname is %s”,x.fname); printf(“Iname is %s”,x.Iname); }

Arrays Of Structures Programming In C++, Lecture 9 By Umer Rana It is possible to store a structure as an array element. i.e., an array in which each element is a structure. Although a structure contains many different types, the compiler never gets to know this information because it is hidden away inside a sealed structure capsule, so it can believe that all the elements in the array have the same type, even though that type is itself made up of lots of different types.

Arrays Of Structures Programming In C++, Lecture 9 By Umer Rana struct student { int rollno; char name[25]; float totalmark; } stud[100]; In this declaration stud is a 100- element array of structures. Hence, each element of stud is a separate structure of type student. An array of structure can be assigned initial values just as any other array. So the above structure can hold information of 100 students.

Arrays Of Structures Programming In C++, Lecture 9 By Umer Rana void main() { struct student { int rollno; char name[25]; int totalmark; }stud[100]; int n,i; printf("Enter total number of students\n\n"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter details of %d-th student\n",i+1); printf("Name:\n"); scanf("%s",&stud[i].name); printf("Roll number:\n"); scanf("%d",&stud[i].rollno); printf("Total mark:\n"); scanf("%d",&stud[i].totalmark); } printf("STUDENTS DETAILS:\n"); for(i=0;i<n;i++) { printf("\nRoll number:%d\n",stud[i].rollno); printf("Name:%s\n",stud[i].name); printf("Totel mark:%d\n",stud[i].totalmark); } }

TypeCasting Programming In C++, Lecture 9 By Umer Rana Type casting is a way to make a variable of one type to an other type. int main() { printf( "%c\n", (char)65 ); printf( "%d\n", (int)'A' ); getchar(); }

Some More C Function Programming In C++, Lecture 9 By Umer Rana Sizeof(): This function take a data type as an argument and returns the size in bytes that the data type occupies. e.g. Int a; printf( "%d\n", sizeof(a) ); output is 4. Sqrt(): This function return a square root, but argument sent to this function must be of type double. double x = 2.0, result; result = sqrt(x); printf("The square root of %1f is %1fn", x, result);

Union Programming In C++, Lecture 9 By Umer Rana Similar to a struct, but all members share a single memory location, and only one member of the union can be used at a time Declared using union, otherwise the same as struct Variables defined as for struct variables Allocates memory at declaration time Provides a way to look at the same data in several different ways. Uses only one memory location.

Union Programming In C++, Lecture 9 By Umer Rana Union is user defined data type used to stored data under unique variable name at single memory location. Union is similar to that of structure. Syntax of union is similar to structure. But the major difference between structure and union is 'storage.' In structures, each member has its own storage location, whereas all the members of union use the same location. Union contains many members of different types, it can handle only one member at a time. To declare union data type, 'union' keyword is used. Union holds value for one data type which requires larger storage among their members.

Union Programming In C++, Lecture 9 By Umer Rana MEMORY ALLOCATION :

Union Programming In C++, Lecture 9 By Umer Rana Syntax: union union_name { element 1; element 2; element 3; }union_variable; Example: union techno { int comp_id; char name; float sal; }tch;

Union Programming In C++, Lecture 9 By Umer Rana union techno { int id; char nm[50]; }tch; int main() { //clrscr(); printf("\n\t Enter developer id : "); scanf("%d", &tch.id); printf("\n\n\t Enter developer name : "); scanf("%s", tch.nm); printf("\n\n Developer ID : %d", tch.id); printf("\n\n Developed By : %s", tch.nm); getch(); }

ROM BIOS Programming In C++, Lecture 9 By Umer Rana Set of built-in routines written in assembly language and were designed to be called by assembly language programs. These routines are a permanent part of the machine. Our C programs can make use of these routines to perform a variety of input/output activities.

Advantages of Using ROM BIOS Programming In C++, Lecture 9 By Umer Rana Mostly handle input/output operations like keyboard, printer, diskette drives, user defined devised, the serial port etc… Most important in Graphics The Largest category of routines deals with video display.

Accessing the ROM BIOS Programming In C++, Lecture 9 By Umer Rana C compiler working on the PC generally provide a method to access these routines. When we use C to call a BIOS routine, the process is somewhat different. Instead of values being placed in an area of memory, they are placed in hardware devices called registers. Registers are the heart of the microprocessor, they are used to perform arithmetic and many other operations. There are a number of registers in the microprocessor; the ones we will be most concerned are four registers, AX,BX,CX and DX, consist of two bytes. Unlike C variables, however, the registers are fixed; they are always there, and they always have the same names.

Dual Role of Registers Programming In C++, Lecture 9 By Umer Rana Another difference between C variables and registers is that registers can be accessed in two different ways: either as four two-byte registers, or as eight one-byte registers as shown in Figure (AX,BX,CX and DX). one byte AL register BL register CL register DL register AH register BH register CH register DH register Registers as One-Byte Storage

Interrupt Numbers Programming In C++, Lecture 9 By Umer Rana The ROM BIOS routines are accessed through interrupts. [ An interrupt provides access to a group of ROM BIOS routines. The mechanism used to access a ROM BIOS routine is a C library function called int86( ). The “int” stand for “interrupt” and the “86” refers to the 80x86 family of chips in processor.

Finding the Memory Size Programming In C++, Lecture 9 By Umer Rana This function takes the interrupt number and two union variables as arguments. The first union represents the values in the registers being sent to the ROM routine, and the second represents the values in the registers being returned from the ROM routine to the C program. int86(INT, &inregs, &outregs)

Making the Cursor Disappear Programming In C++, Lecture 9 By Umer Rana You can make the cursor vanish if you set bit 5, in the byte placed in the CH register, to 1. What do we mean by bit 5? The bits in every byte are numbered from 0 to 7, with the last significant bit being 0. To set bit 5 on, we can place the hex number 20 in the CH register. Hex 20 is , which is a just configuration we need.

Setting The Cursor Size… Programming In C++, Lecture 9 By Umer Rana On the monochrome screen, the cursor consist of 14 short horizontal lines, numbered from 0 to 13 (reading from top to bottom). The default cursor, the one you are most used to seeing, uses only two of these lines, 12 and 13, at the bottom of the cursor position. To redefine the size of the cursor, we call the BIOS routine at interrupt 1(hex), with the AH register containing 1. We also place the starting cursor line number in the CH register, and the ending line number in the CL register.