tag:blogger.com,1999:blog-48481720675985140722024-03-12T17:03:38.102-07:00Compsciuon.Blogspot.com<b>Made and managed by BERNARD BANTA</b>
<br>This blog generally talks about the course of Bachelor of Computer science and technology in general.www.blogspot.ibanta.comhttp://www.blogger.com/profile/11217750836776573004noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-4848172067598514072.post-26186959874631727442010-04-12T00:35:00.000-07:002010-04-12T00:35:44.836-07:00Lec 1 | MIT 6.046J / 18.410J Introduction to Algorithms (SMA 5503), Fall 2005<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/JPyuH4qXLZ0&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/JPyuH4qXLZ0&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>www.blogspot.ibanta.comhttp://www.blogger.com/profile/11217750836776573004noreply@blogger.com0tag:blogger.com,1999:blog-4848172067598514072.post-14466656168815694862010-04-09T06:25:00.001-07:002010-04-09T06:30:25.907-07:00Lecture - 1 Introduction to Data Structures and Algorithms<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/zWg7U0OEAoE&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/zWg7U0OEAoE&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>www.blogspot.ibanta.comhttp://www.blogger.com/profile/11217750836776573004noreply@blogger.com0tag:blogger.com,1999:blog-4848172067598514072.post-51444153549403070972010-04-01T03:48:00.000-07:002010-04-01T03:48:51.007-07:00C++ PROGRAM TO ADD, SUBTRACT AND MULTIPLY TWO COMPLEX NUMBERS USING OPERATOR OVERLOADING/*C++ program to add, subtract and multiply two complex numbers using operator overloading*/<br />
#include<iostream.h><br />
#include<conio.h><br />
class complex<br />
{<br />
float x,y;<br />
public:<br />
complex() {}<br />
complex(float real,float img)<br />
{<br />
x=real; y=img;<br />
}<br />
complex operator+(complex);<br />
complex operator-(complex);<br />
complex operator*(complex);<br />
void display()<br />
{<br />
cout<<x<<" + "<<y<<"i"<<endl;<br />
}<br />
};<br />
complex complex::operator+(complex c)<br />
{<br />
complex temp;<br />
temp.x=x+c.x;<br />
temp.y=y+c.y;<br />
return(temp);<br />
}<br />
complex complex::operator-(complex d)<br />
{<br />
complex temp;<br />
temp.x=x-d.x;<br />
temp.y=y-d.y;<br />
return(temp);<br />
}<br />
complex complex::operator*(complex e)<br />
{<br />
complex temp;<br />
temp.x=x*e.x+y*e.y*(-1);<br />
temp.y=x*e.y+y*e.x;<br />
return(temp);<br />
}<br />
void main()<br />
{<br />
clrscr();<br />
complex c1(5,3),c2(3,2),c3=c1+c2,c4=c1-c2,c5=c1*c2;<br />
c1.display();<br />
c2.display();<br />
cout<<"Addition"<<endl;<br />
c3.display();<br />
cout<<"Subtraction"<<endl;<br />
c4.display();<br />
cout<<"Multiplication"<<endl;<br />
c5.display();<br />
getch();<br />
}www.blogspot.ibanta.comhttp://www.blogger.com/profile/11217750836776573004noreply@blogger.com0tag:blogger.com,1999:blog-4848172067598514072.post-7650628478348865732010-03-09T00:24:00.000-08:002010-03-09T00:25:29.249-08:00How To Change a Windows XP Limited User Account Into a Computer Administrator � Raymond.CC Blog<a href="http://www.raymond.cc/blog/archives/2008/07/12/how-to-change-a-windows-xp-limited-user-account-into-a-computer-administrator/">How To Change a Windows XP Limited User Account Into a Computer Administrator � Raymond.CC Blog</a>www.blogspot.ibanta.comhttp://www.blogger.com/profile/11217750836776573004noreply@blogger.com0tag:blogger.com,1999:blog-4848172067598514072.post-56002427921440025892009-12-02T04:46:00.001-08:002009-12-02T04:46:50.276-08:00William Stallings Computer Organization and Architecture<b>Chapter 2 <br />
Computer Evolution and <br />
Performance <br />
Key Points </b><br />
<br />
<br />
• The evolution of computers has been characterized by increasing processor speed, decreasing component size, increasing memory size and increasing I/O capacity and speed <br />
• Processor speed is as a result of shrinking factor in the processor components; reducing the distance between the components hence, increasing speed. True cause is the organization of the processor e.g. pipelining and parallel execution techniques <br />
• Critical issue here is the balancing of the performance of the various elements <br />
<br />
<b>A<b> Brief History of Computers </b></b><br />
• First generation – vacuum tubes<br />
ENIAC The<br />
Von Neumann Machine<br />
Commercial computers <br />
• Second generation – Transistors <br />
The IBM 7094 <br />
Third generation – Integrated Circuits <br />
Later generations<br />
<br />
ENIAC - background <br />
• Electronic Numerical Integrator And Computer <br />
• By John Presper Eckert and Prof. John Mauchly at the <br />
• University of Pennsylvania<br />
• World’s first general-purpose electronic digital computer<br />
• Started 1943<br />
• Trajectory tables for weapons <br />
• Finished 1946<br />
Too late for war effort<br />
• Used until 1955<br />
<br />
ENIAC - details <br />
• Decimal (not binary) <br />
• 20 accumulators, each capable of holding 10 digits each <br />
• Programmed manually by switches <br />
• 8,000 vacuum tubes <br />
• 30 tons <br />
• 15,000 square feet <br />
• 140 kW of power consumption <br />
• 5,000 additions per second <br />
<br />
Von Neumann/Turing <br />
• 1945-first publication of the idea; the EDVAC (Electronic Discrete Variable Computer) <br />
• The manual programming was too tedious <br />
• Stored Program concept by John Von Neumann; Alan Turing developed the idea at around the same time<br />
• Main memory storing programs and data -a for suitable for storing in memory alongside data<br />
• ALU operating on binary data <br />
• Control unit interpreting instructions from memory and executing <br />
• Input and output equipment operated by control unit <br />
• Work on the computer begun at the Princeton Institute for Advanced Studies. The computer was named, IAS <br />
• Computer and was completed in 1952 <br />
• This is the prototype of all subsequent general-purpose computer <br />
<br />
Von Neumann/Turing Components <br />
• A main memory that stores both data and instructions <br />
• An ALU (Arithmetic and Logic Unit) capable of operating on binary data <br />
• A control Unit which interprets the instructions in memory an causes them to be executed <br />
• Input and output (I/O) equipment operated by the control unit <br />
<br />
Structure of von Neumann <br />
<br />
<br />
IAS - details <br />
• 1000 storage locations (words) of 40 bits each; both data and instructions are stored here <br />
• Everything must be in Binary number <br />
• Each number is represented by a sign bit and a 39- bit value (see figure 2.2 on page 19 for the William’s book) <br />
• 2 instructions per word of 20 bits each<br />
<br />
<br />
IAS - details <br />
• Set of registers (storage in CPU) <br />
• Memory Buffer Register (MBR) contains a word to be stored in memory or is used to receive word from memory <br />
• Memory Address Register(MAR) – specifies the address in memory of the word to be written from or read into the MBR <br />
• Instruction Register(IR) – contains the 8-bit opcode instruction being executed <br />
• Instruction Buffer Register (IBR) – employed to hold temporarily the right-hand instruction from a word memory <br />
• Program Counter (PC)- contains the address of the next instruction-pair to be fetched from memory <br />
• Accumulator (AC) and Multiplier Quotient (MQ) – employed to hold temporarily operands and results of ALU operations; e.g. a*b=c(80bits) so the most significant 40 bits are stored in the <br />
• AC and the least significant stored in MQ<br />
<br />
Structure of IAS - detail <br />
<br />
Commercial Computers <br />
• 1947 -Eckert-Mauchly Computer Corporation <br />
• UNIVAC I (Universal Automatic Computer) <br />
• US Bureau of Census 1950 calculations <br />
• Became part of Sperry-Rand Corporation<br />
• Late 1950s -UNIVAC II <br />
• Faster <br />
• More memory <br />
<br />
• IBM <br />
• Punched-card processing equipment <br />
• 1953 - the 701 <br />
IBM’s first stored program computer <br />
What does IBM stand for? <br />
Scientific calculations <br />
• 1955 - the 702 <br />
Business applications <br />
• Lead to 700/7000 series<br />
<br />
Transistors <br />
• Replaced vacuum tubes <br />
• Smaller <br />
• Cheaper <br />
• Less heat dissipation <br />
• Solid State device <br />
• Made from Silicon (Sand) <br />
• Invented 1947 at Bell Labs <br />
• William Shockley et al. <br />
<br />
Transistor Based Computers <br />
• Second generation machines <br />
• NCR (?) & RCA (?) produced small transistor machines <br />
• IBM 7000 <br />
• DEC -1957 <br />
Produced PDP-1 <br />
<br />
Microelectronics <br />
• Literally - “small electronics” <br />
• A computer is made up of gates, memory cells and interconnections <br />
• These can be manufactured on a semiconductor<br />
<br />
Generations of Computer <br />
• Vacuum tube -1946-1957 <br />
• Transistor -1958-1964 <br />
• Small scale integration -1965 on <br />
Up to 100 devices on a chip <br />
• Medium scale integration -to 1971 <br />
100-3,000 devices on a chip <br />
• Large scale integration -1971-1977 <br />
3,000 -100,000 devices on a chip <br />
• Very large scale integration -1978 to date <br />
100,000 -100,000,000 devices on a chip <br />
• Ultra large scale integration <br />
Over 100,000,000 devices on a chip <br />
<br />
Moore’s Law <br />
• Increased density of components on chip <br />
• Gordon Moore - cofounder of Intel <br />
• Number of transistors on a chip, will double every year <br />
Since 1970’s development has slowed a little <br />
• Number of transistors doubles every 18 months <br />
• Cost of a chip has remained almost unchanged <br />
• Higher packing density means shorter electrical paths, giving higher performance <br />
• Smaller size gives increased flexibility <br />
• Reduced power and cooling requirements <br />
• Fewer interconnections increases reliability<br />
<br />
Growth in CPU Transistor Count <br />
<br />
<br />
<br />
IBM 360 series <br />
• 1964 <br />
• Replaced (& not compatible with) 7000 series <br />
• First planned “family” of computers <br />
Similar or identical instruction sets <br />
Similar or identical O/S <br />
Increasing speed <br />
Increasing number of I/O ports (i.e. more terminals) <br />
Increased memory size <br />
Increased cost <br />
• Multiplexed switch structure<br />
<br />
DEC PDP-8 <br />
• 1964 <br />
• First minicomputer (after miniskirt!) <br />
• Did not need air conditioned room <br />
• Small enough to sit on a lab bench <br />
• $16,000 <br />
$100k+ for IBM 360 <br />
• Embedded applications & OEM <br />
• BUS STRUCTURE <br />
• <br />
<br />
DEC -PDP-8 Bus Structure<br />
<br />
Intel <br />
• 1971 - 4004 <br />
First microprocessor <br />
All CPU components on a single chip <br />
4 bit <br />
• Followed in 1972 by 8008 <br />
8 bit <br />
Both designed for specific applications <br />
• z1974 - 8080 <br />
Intel’s first general purpose microprocessor<br />
<br />
Designing for Performance <br />
• The cost of computers continue to drop while the performance and capacity continue to rise <br />
• However, the basic building blocks for today’s computers are virtually the same as those of the IAS computer from over 50 years ago! <br />
• The key idea is therefore the techniques for squeezing performance out of the material.<br />
<br />
Microprocessor Speed-<br />
Speeding it up <br />
• Pipelining <br />
• On board cache <br />
• On board L1 & L2 cache <br />
• Branch prediction <br />
• Data flow analysis <br />
• Speculative execution <br />
<br />
Performance Mismatch <br />
• Processor speed increased <br />
• Memory capacity increased <br />
• Memory speed lags behind processor speed <br />
<br />
Solutions <br />
• Increase number of bits retrieved at one time <br />
Make DRAM “wider” rather than “deeper” <br />
• Change DRAM interface <br />
Cache <br />
• Reduce frequency of memory access <br />
More complex cache and cache on chip <br />
• Increase interconnection bandwidth <br />
High speed buses <br />
Hierarchy of buses<br />
<br />
Internet Resources <br />
• http://www.intel.com/ <br />
Search for the Intel Museum <br />
• http://www.ibm.com <br />
• http://www.dec.com<br />
• Charles Babbage Institute <br />
• PowerPC <br />
• Intel Developer Homewww.blogspot.ibanta.comhttp://www.blogger.com/profile/11217750836776573004noreply@blogger.com0tag:blogger.com,1999:blog-4848172067598514072.post-69212464628332115282009-12-01T06:30:00.001-08:002009-12-01T06:30:51.234-08:00<b>ICS 113: Programming <br />
<br />
<br />
Structures in C </b><br />
<br />
<br />
Introduction to structures <br />
n Arrays are data structures whose elements are of the <br />
same type. <br />
n Structure is “a data structure ” in which the individual <br />
elements can di ffer in type. <br />
n A structure is a data type that consists of a collection of <br />
several variables stored together. A structure can also be <br />
referred to as a record. <br />
n The individual structure elements are known as <br />
elements.<br />
<br />
Defining a structure <br />
<br />
• <br />
A structure must be def ined in terms of its indi vidual <br />
members. In general terms, a structure may be def ined <br />
as: <br />
struct tag{ <br />
member 1; <br />
member 2; <br />
member 3; <br />
........ <br />
member m; <br />
}; <br />
<br />
<br />
Structure members <br />
<br />
<br />
<br />
• <br />
The individual members can be ordinary var iables, <br />
pointers, arrays or other structures. <br />
• <br />
The member names wi thin a particular structure must be <br />
distinct from one another, though a member name can be <br />
the same as the name of a variable that is defined <br />
outside the structure. <br />
• <br />
Individual members cannot be initiali zed within a <br />
structure type declarati on. <br />
• <br />
A storage class cannot be assigned to an individualmember. <br />
<br />
Declaring a structure <br />
n Once the composition of the structure has been defined, individual <br />
structure type variables can be declared as follows: <br />
storage_class struct tag variable1, variable2, variable n; <br />
– Storage_class is an optional storage class specifier <br />
e.g. static, external e.t.c. <br />
– struct is a required keyword, <br />
– tag is the name that appeared in the structure <br />
declaration and <br />
– variable1 to variable n are structure variables of type <br />
tag.<br />
<br />
Example 1 <br />
<br />
struct account{ <br />
int acct_no; <br />
char acct_type; <br />
char name[80]; <br />
float balance; <br />
<br />
}; <br />
<br />
• <br />
We can now declare the structure variables customer1 <br />
and customer2 as follows: <br />
struct account customer1, customer2; <br />
<br />
• <br />
Thus, customer1 and customer2 are variables of type <br />
account. In other word s, customer1 and customer2 are <br />
structure-type variables whose composition i s identified <br />
by the tag account. <br />
<br />
Combining structure and variable declarations <br />
<br />
storage_class struct tag{ <br />
<br />
member 1; <br />
<br />
member 2; <br />
<br />
member 3; <br />
<br />
......... <br />
<br />
<br />
member m; <br />
}variable1, variable2, ..., variable n; <br />
<br />
• <br />
The tag is optional in this situation. <br />
<br />
Example 2 <br />
<br />
<br />
<br />
struct account{ struct { <br />
int acct_no; int acct_no; <br />
char acct_type; char acct_type; <br />
char name[80]; char name[80]; <br />
float balance; float balance; <br />
<br />
} customer1, customer2; } customer1, customer2; <br />
<br />
<br />
Case 1 and 2 serve the same purpose <br />
<br />
<br />
Embedded structures <br />
<br />
<br />
<br />
• <br />
A structure variable can also be a def ined as a member <br />
of another structure. <br />
• <br />
In such situations, the declaration of the embedded <br />
structure must appear bef ore the declaration of the outer <br />
structure. <br />
<br />
Example 3 <br />
<br />
struct date{ <br />
<br />
int month; <br />
<br />
int day; <br />
<br />
int year; <br />
}; <br />
<br />
struct { <br />
<br />
int acct_no; <br />
<br />
char acct_type; <br />
<br />
char name[80]; <br />
<br />
float balance; <br />
<br />
struct date lastpayment; <br />
} customer1, customer2; <br />
<br />
<br />
Structure initialization <br />
n The members of a structure can be assigned initial <br />
values in much the same manner as the elements of an <br />
array. <br />
n The initial values must appear in the order in wh ich they <br />
will be assigned to thei r corresponding structure <br />
members, enclosed in braces and separated by <br />
commas. The general form is: <br />
storage_class struct tag variable ={value1, <br />
value2, value3, …., valuem}; <br />
n N.B. A structure variable, like an array, can be initialized <br />
only if its storage class is either external or static.<br />
<br />
Example 4 <br />
struct date{ <br />
int month; <br />
int day; <br />
int year; <br />
}; <br />
struct { <br />
int acct_no; <br />
char acct_type; <br />
char name[80]; <br />
float balance; <br />
struct date lastpayment; <br />
}; <br />
static struct account customer = {987654, ‘S’, “Will Obongo”, 102,589.30, 8, 12, <br />
2005}<br />
<br />
Array of structures <br />
<br />
<br />
• <br />
It is also possible to define anarray of structures, i.e. an <br />
array in which each element is <br />
a structure. <br />
struct date{ <br />
<br />
int month; <br />
<br />
int day; <br />
<br />
int year; <br />
}; <br />
struct { <br />
<br />
int acct_no; <br />
<br />
char acct_type; <br />
<br />
char name[80]; <br />
<br />
float balance; <br />
<br />
struct date lastpayment; <br />
}customer[100]; <br />
<br />
• <br />
In this declaration, <br />
customer is a 100 <br />
element array ofstructures.Hence, each <br />
element of customer is a <br />
separate structure of typeaccount i.e. each element <br />
of customer represents anindividual customer <br />
record. <br />
<br />
Initializing an array of structures <br />
<br />
<br />
struct date{ <br />
char name[80]; <br />
int month; <br />
int day; <br />
int year; <br />
<br />
}; <br />
<br />
static struct date bday[]={ <br />
{"Amy", 12, 30, 73}, <br />
{"Gail", 5, 13, 66}, <br />
{"Marc", 7, 15, 72}, <br />
{"Maria", 5 29, 67} <br />
}; <br />
<br />
struct date{ <br />
char name[80]; <br />
int month; <br />
int day; <br />
int year; <br />
<br />
}; <br />
<br />
static struct date bday[]={ <br />
"Amy", 12, 30, 73, <br />
"Gail", 5, 13, 66, <br />
"Marc", 7, 15, 72, <br />
"Maria", 5 29, 67 <br />
}; <br />
<br />
<br />
Naming members <br />
<br />
<br />
<br />
• <br />
Remember that each structure is a sel f contained entity <br />
with respect to member def initions. <br />
• <br />
Thus, the same member name can be used i n different <br />
structures to represent di fferent data, i.e. the scope of a <br />
member name is conf ined to the particular structure <br />
within which it is def ined. <br />
<br />
Processing a structure <br />
<br />
<br />
• <br />
The members of a structure are usually processed <br />
individually, as separate entities. Theref ore, we can <br />
access the individual members by writin g: <br />
variable.member <br />
<br />
• <br />
Where variable refers to the name of a structure -type <br />
variable, and member refers to the name of a member <br />
within the structure. <br />
• <br />
Notice the period (.) that separates the variable name <br />
from the member name. T his period is an operator; it is a <br />
member of the highest precedence group, and its <br />
associativity is left to right. <br />
<br />
Example 5 <br />
<br />
<br />
<br />
struct date{ • <br />
customer.acct_no; <br />
int month; • <br />
date.month; <br />
<br />
• <br />
customer.balance; <br />
int day; <br />
<br />
int year; <br />
}; <br />
struct { <br />
<br />
int acct_no; <br />
<br />
char acct_type; <br />
<br />
char name[80]; <br />
<br />
float balance; <br />
<br />
struct date lastpayment; <br />
}customer; <br />
<br />
<br />
(.) operator <br />
<br />
<br />
<br />
• <br />
Since this operator is a member of the highest <br />
precedence group, this operator wil l take precedence <br />
over the unary operators as well as the various <br />
arithmetic, relational, l ogical and assignment operators. <br />
• <br />
Thus ++variable.member = ++(variable.member) and <br />
&variable.member = &(variable.member) <br />
<br />
(.) expressions <br />
<br />
<br />
<br />
• <br />
More complex expressions invol ving the repeated use of <br />
the period operator may also be written. <br />
• <br />
For example, if a structure member is itself a structure, <br />
then a member of the embedded structure can be <br />
accessed by writing <br />
variable.member.submember <br />
<br />
• <br />
Where member refers to the name of the member within <br />
the outer structure, and submember refers to the name of <br />
the member withinn the embedded structure. <br />
<br />
Example 6 <br />
<br />
struct date{ <br />
int month; <br />
int day; <br />
int year; <br />
<br />
}; • <br />
customer.lastpayment.month <br />
<br />
struct { • <br />
++customer.lastpayment.month <br />
int acct_no; • <br />
customer.name[2] <br />
char acct_type; • <br />
&customer.name[2] <br />
char name[80]; <br />
float balance; <br />
struct date lastpayment; <br />
<br />
}customer; <br />
<br />
<br />
(.) with arrays of structures <br />
<br />
<br />
<br />
• <br />
The use of the period operator can be extended to arrays of <br />
structures, by writing: <br />
array[expression].member <br />
<br />
where array refers to the array name, and array[expression] refers <br />
to an individual array element (a structure variable). Therefore <br />
array[expression].member will refer to a specific member within a <br />
particular structure. <br />
<br />
<br />
Example 7 <br />
<br />
struct date{ <br />
int month; <br />
int day; <br />
int year; <br />
<br />
}; • <br />
Customer[13].acct_no <br />
<br />
• <br />
Customer[13].balance <br />
struct { <br />
<br />
• <br />
Customer[13].name <br />
int acct_no; <br />
<br />
• <br />
Customer[13].name[6] <br />
char acct_type; <br />
<br />
• <br />
Customer[13].lastpayment.month <br />
char name[80]; <br />
<br />
• <br />
++Customer[13].lastpayment.day <br />
<br />
float balance; <br />
struct date lastpayment; <br />
}customer[100]; <br />
<br />
<br />
Processing structure members <br />
<br />
<br />
• <br />
Structure members can be processed in the same <br />
manner as ordinary va riables of the same data type. <br />
• <br />
Single-valued structure members can appear in <br />
expressions, they can be passed to functions, and they <br />
can be returned f rom functions as though they were <br />
ordinary single-valued variables. <br />
• <br />
Complex structure members are processed i n the same <br />
way as ordinary data items of that same type. E.g. a <br />
structure member that is an array can be processed in <br />
the same manner as an ordinary array, and with the <br />
same restrictions. <br />
<br />
User defined data types (typedef) <br />
<br />
<br />
• <br />
The typedef feature allows users to define new data-<br />
types that are equivale nt to existing data types. <br />
• <br />
Once a user-defined data type has been established, <br />
then new variables, arr ays, structures etc can be <br />
declared in terms of thi s new data type. <br />
<br />
typedef <br />
<br />
<br />
• <br />
In general terms, a new data type is def ined as: <br />
typedef type new-type <br />
where type refers to an existing data type (either a <br />
standard data type, or previous user -defined data type), <br />
and new-type refers to the new user -defined data type. <br />
<br />
• <br />
It should be understood however, that the new data <br />
type will be new in name only. In reality, this new data <br />
type will not be fundamentally different f rom one of the <br />
standard data types. <br />
<br />
typedef int age; <br />
<br />
thus <br />
<br />
age male, female; <br />
<br />
Is equivalent to <br />
<br />
int male, female; <br />
<br />
Example 8 <br />
<br />
The following declarations: <br />
<br />
• <br />
typedef float height[100]; <br />
• <br />
Height men, women; <br />
define height as a 100-element floating <br />
point array type, hence men and <br />
women are 100 element floating point <br />
arrays. Another way to express this is <br />
<br />
• <br />
typedef float height; <br />
• <br />
height men[100], women[100]; <br />
<br />
typedef and structures <br />
<br />
<br />
<br />
• <br />
The typedef feature is particularly convenient when defining <br />
structures, since it eliminates the need to repeatedly write struct tag <br />
whenever a structure is referenced. <br />
• <br />
In addition, the name given to a user-defined structure type often <br />
suggests the purpose of the structure within the program. <br />
• <br />
In general terms, a user defined structure type can be written as: <br />
typedef struct{ <br />
member1; <br />
member2; <br />
....... <br />
membern; <br />
<br />
<br />
}new_type; <br />
<br />
<br />
Example 9 <br />
<br />
<br />
typedef struct { <br />
int acct_no; <br />
char acct_type; <br />
char name[80]; <br />
float balance; <br />
<br />
}record; <br />
<br />
record oldcustomer, <br />
newcustomer; <br />
<br />
<br />
typedef struct{ <br />
int month; <br />
int day; <br />
int year; <br />
<br />
}date; <br />
<br />
typedef struct { <br />
int acct_no; <br />
char acct_type; <br />
char name[80]; <br />
float balance; <br />
date lastpayment; <br />
<br />
}record; <br />
<br />
record customer[100]; <br />
<br />
<br />
Example 10 <br />
<br />
<br />
<br />
typedef struct{ <br />
int month; <br />
int day; <br />
int year; <br />
<br />
}date; <br />
<br />
typedef struct { <br />
int acct_no; <br />
char acct_type; <br />
char name[80]; <br />
float balance; <br />
date lastpayment; <br />
<br />
}record[100]; <br />
<br />
record customer; <br />
<br />
typedef struct{ <br />
int month; <br />
int day; <br />
int year; <br />
<br />
}date; <br />
<br />
typedef struct { <br />
int acct_no; <br />
char acct_type; <br />
char name[80]; <br />
float balance; <br />
date lastpayment; <br />
<br />
}customer [100]; <br />
<br />
<br />
Structures and pointers <br />
<br />
<br />
• <br />
The beginning address of a structure can be accessed in the <br />
same manner as any other address, through the use of the <br />
address (&) operator. <br />
• <br />
Thus if variable represents a structure-type variable, then <br />
&variable represents the starting address of that variable. <br />
• <br />
Moreover, we can declare a pointer vari able for a structure by <br />
writing <br />
type *ptvar; <br />
where type is a data type that identifies the composition of the <br />
structure and ptvar represents the name of the pointer variable. <br />
<br />
• <br />
We can then assign the beginning address of a structure variable <br />
to this pointer by writing <br />
ptvar = &variable; <br />
<br />
<br />
Example 11 <br />
<br />
<br />
typedef struct { <br />
int acct_no; <br />
char acct_type; <br />
char name[80]; <br />
float balance; <br />
<br />
}account; <br />
<br />
account customer, *pc; <br />
pc = &customer; <br />
<br />
typedef struct { <br />
int acct_no; <br />
char acct_type; <br />
char name[80]; <br />
float balance; <br />
<br />
}customer, *pc; <br />
<br />
pc = &customer; <br />
<br />
<br />
<br />
Accessing individual members using <br />
pointers <br />
<br />
<br />
• <br />
An individual structure member can be accessed in <br />
terms of its corresponding pointer variable by writin g <br />
ptvar->member <br />
<br />
• <br />
ptvar refers to a structure -type pointer variable and the <br />
operator -> is comparable to the period (.) operator. Thus <br />
the expression ptvar->member is equivalent to writing <br />
variable.member where variable is a structure -type <br />
variable. <br />
• <br />
The operator falls into the highest precedence group, like <br />
the period operator and its associativity is also left to <br />
right. <br />
<br />
(.) and -> <br />
n The –> operator and the period operator can be <br />
combined to access a submember within a structure. <br />
Hence the submember can be accessed by writing <br />
ptvar->member.submember <br />
n Similarly, the -> operator can be used to access an <br />
element of an array that is a member of a structure.. <br />
ptvar->member[expression]<br />
<br />
Example 12 <br />
<br />
<br />
• <br />
customer.acct_no <br />
• <br />
pc->acct_no <br />
• <br />
(*pc).acct_no <br />
typedef struct{ <br />
int month; <br />
int day; <br />
int year; <br />
<br />
}date; <br />
<br />
typedef struct { <br />
int acct_no; <br />
char acct_type; <br />
char name[80]; <br />
float balance; <br />
date lastpayment; <br />
<br />
}customer, *pc=&customer; <br />
<br />
<br />
The parentheses are <br />
required in the last <br />
expression because <br />
the period operator <br />
has a higher <br />
precedence than the <br />
*. <br />
<br />
<br />
Pointers as structure members <br />
<br />
<br />
<br />
• <br />
A structure can also include one or more pointers <br />
as members. <br />
• <br />
If ptmember is a pointer and a member of <br />
variable, then *variable.ptmember will access the <br />
value to which ptmember points. <br />
<br />
Passing structures to functions <br />
<br />
<br />
• <br />
Individual structure members can be passed to a <br />
function as arguments in the function call, and a <br />
single structure member can be returned via the <br />
return statement. <br />
<br />
Example 13 <br />
<br />
<br />
float adjust(char name[], int acct_no, <br />
float balance) <br />
main(){ <br />
typedef struct{ <br />
int month; <br />
int day; <br />
int year; <br />
}date; <br />
typedef struct { <br />
int acct_no; <br />
char acct_type; <br />
char name[80]; <br />
float balance; <br />
date lastpayment; <br />
}customer; <br />
<br />
customer.balance = <br />
adjust(customer.name, <br />
customer.acct_no, <br />
customer.balance); <br />
<br />
} <br />
<br />
float adjust(char name[], <br />
int acct_no, float <br />
balance) <br />
<br />
{ <br />
float newbalance; <br />
newbalance=.....; <br />
return(newbalance); <br />
<br />
} <br />
<br />
<br />
Passing structures to functions <br />
<br />
<br />
• <br />
A complete structure can be transferred to a function by <br />
passing a structure -type pointer as an argument. <br />
• <br />
However, we must use explicit pointer notation to <br />
represent a structure that is passed as an argument. <br />
• <br />
A structure passed in this manner is passed by reference <br />
rather than by value. <br />
<br />
Example 12 <br />
<br />
#include <stdio.h> <br />
typedef struct { <br />
<br />
void adjust(record *pt) <br />
<br />
char *name; <br />
<br />
{<br />
<br />
char acct_type; <br />
int acct_no; pt->name="John <br />
float balance; W."; <br />
<br />
<br />
}record; <br />
<br />
pt->acct_type='S'; <br />
<br />
void adjust(record *pt); <br />
<br />
pt><br />
acct_no=34569; <br />
<br />
main() <br />
{ pt->balance=0.0; <br />
static record cust = <br />
<br />
}<br />
<br />
{"smith",'C',333,34765}; <br />
printf("%s %c %d %.2f\n", cust.name, <br />
<br />
<br />
cust.acct_type, cust.acct_no, <br />
cust.balance); <br />
adjust(&cust); <br />
printf("%s %c %d %.2f\n", cust.name, <br />
<br />
<br />
cust.acct_type, cust.acct_no, <br />
cust.balance) <br />
}www.blogspot.ibanta.comhttp://www.blogger.com/profile/11217750836776573004noreply@blogger.com0