Chapter 1 Array and Structure
Chapter 1 Array and Structure
1
Objectives
int ar [4];
arr=ar;//not allowed
● Note: when initializing an array, we can provide fewer values than the
array elements. E.g. int a [10] = {10, 2, 3}; in this case the compiler sets
the remaining elements to zero.
mystring[0] = 'H';
mystring[1] = 'e';
mystring[2] = 'l';
mystring[3] = 'l';
mystring[4] = 'o';
mystring[5] = '\0';
● But, this does not seem to be a very practical method.
#include <iostream>
#include<conio.h>
void namecopy(char dest[], char source[])
{ int c = 0; void main() {
while(source[c] != ‘\0’) {
char name[10],dest[10];
dest[c] = source[c];
c++;
cout<< “\n enter your
} name : ”;
dest[c] = ‘\0’; cin>>name;
cout<< “\n your name after copying : namecopy(dest,name);
”<<dest; }
}
○ returns negative int(if the first is less than the second string)
○ returns positive int (if the first is greater than the second string
● It is arrays of arrays.
● Ex. a bi-dimensional array can be
imagined as a bi-dimensional table of a
uniform concrete data type
type multid_array_name[row_size][col_size];
#define HEIGHT 4
structureVariableName.memberName;
● An example Chapter1:
is shown Arrays and (next
Structurers slide) 44
here is an example:
#include<iostream> //initialize members here
.... strcpy(cd1.title,"Red Moon Men");
strcpy(cd1.artist,"Sams");
void main() cd1.num_songs= 12;
cd1.price = 11.95;
{ strcpy(cd1.date_purchased,"22/12/02");
struct cd_collection{ //print the data
cout<<"\nHere is the info"<<endl;
char title[25]; cout<<"Title : "<<cd1.title<<endl;
cout<<"Artist : "<<cd1.artist<<endl;
char artist[20]; cout<<"Songs :
int num_songs; "<<cd1.num_songs<<endl;
cout<<"Price : "<<cd1.price<<endl;
float price; cout<<"Date purchased“
<<cd1.date_purchased;
char date_purchased[9];
}cd1; }
Chapter1: Arrays and Structurers 45
Arrays of Structures
● Arrays of structures are good for storing a complete employee file,
inventory file, or any other set of data that fits in the structure
format.
● Consider the following structure declaration:
struct Company {
int employees;
int registers;
double sales;
}store[1000];
● Declare 1,000 store structures with the definition of the Company
structure, each one containing three members.
● NB. Be take care of resource limitation.(imagine RAM 64MB)
Chapter1: Arrays and Structurers 46
Array of structures can be
void main()
declared after the
{
declaration of the struct Company
structure. store[1000]; /*the variable
store is array of the structure
struct Company { Company*/
…
int employees; }
int registers;
double sales;
}; /*no structure variables
defined yet*/
Chapter1: Arrays and Structurers 47
Referencing the array structure
● The dot operator (.) works the same way for structure
array element as it does for regular structure.
● If the number of employees for the fifth store (store[4])
increased by three, you could update the structure
variable like this:
store[4].employees += 3;
● Unlike in the case of arrays, where the whole content of
an array could not be copied to another one using a
simple assignment statement, in structures, you can
assign complete structures to one another by using array
notation.
● To assign all the members of the 20th store to the 45th
store, you would Chapter1:
do this:
Arrays and Structurers 48
th th
use of array of structures,
and how to pass and void disp_menu(void);
struct inventory enter_data();
return structures to void see_data(inventory disk[125],int
functions. num_items);
#include<iostream.h>
void main()
#include<conio.h> { inventory disk[125];
#include<stdio.h> int ans;
#include<iomanip.h> int num_items = 0; //total number of
items in the inventory
struct inventory { do {
long storage; do {
disp_menu();
int accesstime;
cin>>ans;
char vendorcode; }while(ans<1 || ans>3);
float cost;
float price; };
Chapter1: Arrays and Structurers 49
switch(ans)
{ case 1:
disk[num_items] = enter_data();
num_items++;
break;
case 2:
see_data(disk,num_items);
break;
default :
break;
}
}while(ans != 3);
return;
}//end main