SlideShare a Scribd company logo
1 of 20
Chapter 4
Arrays
Ragia A. Ibrahim, Ph.D. Student
1
‫القاهرة‬ ‫جامعة‬
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬
‫مركز‬‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
2
Arrays used to group of similar type data items. Accessed
via an index.
Array Declaration examples
int myarray[100];
Data
Type
Name
Size
Introduction
Brackets delimits array size
char name[30];
float salaries[10];
Array Elements-1
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
3
Array Elements-2
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
4
44
16
23
68
Age[0]
Age[1]
Age[2]
Age[3]
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
5
Insert 4 students ages into an array
#include<iostream.h>
Int main()
{
float age[4];
for(int i=0;i<4;i++)
{
cout<<“Enter an age”;
cin>>age[i];
}
/ / to print the array values
for(int i=0;i<4;i++)
cout<<“you Entered”<<age[i];
// why no {} in the second for?
Return 0;
}
Example - 1
Enter an age:44
Enter an age:16
Enter an age:23
Enter an age:68
You entered 44
You entered 16
You entered 23
You entered 68
Array element accessed twice.
The first time: insert a value into
array
cin>>age[j];
The second time: we read it
cout<< “you entered”<<age[i];
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
6
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
6
Sales: enter 6 sales values , calc avrg , sum.
#include<iostream.h>
Int main()
{
const int size=6; //size of array
double sales[size]; //array of 6 variable
cout<<“enter widget sales for 6 days n”;
for (int j=0; j<size; j++) //puts figures from array
{
cin<<sales[j];
total+=sales[j]; // to find total
}
// -------
double average = total/size; //to find average
cout<<“average= ”<<average<<endl;
return 0;
}
Example - 2
variables declared with ‘const’
added become constants
Enter widget sales for 6
days
352.64
867.70
781.32
867.35
746.21
189.45
Average = 634.11
Syntax of array initialization
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
By: Ragia Ibrahim 7
Assignment operator Intialize values
int coins [6]or[] = [1,5,10,25,50,100];
braces
Array size (optional)
commas
Initializing Array
days from start of year to date specified
#include<iostream.h>
Int main()
{
//shows days from start of year to date specified
int month, day, total_days;
int days_per_month[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
cout<<“n enter month(1 to 12):”; //get date
cin>>month;
cout<<“n enter day (1 to 31):”;
cin>>day;
total_days=days; //seprate days
for (int j=0;j<month-1;j++) //add days each month
total_days +=days_per_month[j];
cout<<“total days from start of year is:”<<total_days<<endl;
return 0;
}
days_per_month[0]
days_per_month[1]
days_per_month[2]
days_per_month[3]
days_per_month[4]
days_per_month[5]
days_per_month[6]
days_per_month[7]
days_per_month[8]
days_per_month[9]
days_per_month[10]
days_per_month[11]
Multidimensional array
• //salemon.cpp
• //display sales chart using 2.d array
• #include <iostream.h>
• #include <iomanip.h>
• Const int districts=4;
• Const int months=3;
How to represent Sales for each district per month ?
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
10
Defining multidimensional arrays
District 1
District 2
District 3
District 4
Month
1 2 3
Sales[0][0]
Sales[0][1]
Sales[0][2]
Sales[1][0]
Sales[1][1]
Sales[1][2]
Sales[2][0]
Sales[2][1]
Sales[2][2]
Sales[3][0]
Sales[3][1]
Sales[3][2]
double sales[districts][month];
#include <iostream>
const int district= 4;
const int month= 3;
Int main()
{
int d,m;
d=0; m=0;
double sales[d] [m] ;
for(int d=0; d<districts; d++)
for(int m=0; m<months; m++)
{
cout<<"Enter sales for district (" << d+1 <<')' ;
cout<<" and Enter month (" << m+1 <<") : " ;
cin>>sales[d][m];
} //put number in array }
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
12
// program continue
cout<<“nn”;
cout<<“ monthn”;
// can we display array values ?? when
for(int d=0; d<4; d++)
for(int m=0; m<3; m++)
{
for(int m=0;m<months;m++) //display array values
cout<<“n District ”<< d+1 << “Month:” <<m+1;
cout << sales[d][m];
} //end for
Cout <<endl;
return 0;
} //end main
cout<<setiosflags(ios::fixed) //not exponential
<<setiosflags(ios::showpoint) //always use point
<<setprecision(2) //digits to right
<<setw(10) //field width
cout <<sales[d][m]; //get number from array
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
13
sales[districts][months]= {
{1432.07,234.5,654.01},
{322.00,13838.32,17589.88},
{9328.34,934.00,449.30} ,
{12838.29,2332.63,32.93}};
//two-dimensional array definition with
initialization
Initialization for Multidimensional Array
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
14
String ??
A
M
A
N
U
E
N
S
I
S
0
Terminating 0 or null byte
representing by ‘0’
characters constant
Characters in string
string
String
buffer
str
Unused part of buffer
String stored in array of char’s variable
Str={‘A’, ‘M’, ‘A’, ’N’, ’U’, ’E,’…}
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
15
#include<iostream.h>
Int main()
{
char str[80];
cout<<“Enter String :”;
cin>>str;
return 0;
}
String - variable
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
16
//constant string
#include<iostream.h>
Int main()
{
char str[]=“farewell! Thou art too dear for my possessing.”;
cout<<str<<endl;
return 0;
}
String Constant
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
17
#include<iostream.h>
Int main()
{
char str1[]=“Welcome to ISSR.”;
char str2[80];
Int j=0;
while(str1[j]!=’0’;
{
str2[j]=str1[j];
j++;
}
Str2[j]=“0” //adding termination.
cout<< str2;
return 0;
}
String -Copying
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
18
#include<iostream.h>
Int main()
{
char weekDays[7][10] = {
“Monday “, “ Tuesday “, “ Wednesday“,
“Thursday“, “Friday“, “Saturday“, “Sunday“};
For ( int j =0; j< 7 ; j++)
cout<< weekDays[j]<<endl;
return 0;
}
Array of String
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
19
‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬
‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬
20
Q3: Write C++ prog. to reverse string.
#include<iostream>
using namespace std;
int main()
{
char str[10];
int i, len;
cout << "Enter a string: ";
cin>>str;
i=0; //intial value
while (str[i]!='0')
i++;
len=i;
// print in reversed order starting from string end to first..
for(i = len-1; i>=0; i--)
cout<<str[i];
cin.get();
return 0;
}

More Related Content

What's hot

حلقات التكرار
حلقات التكرارحلقات التكرار
حلقات التكرارmahaa6789
 
درس أدوات الادخال و الاخراج- الصف الأول الثانوي
درس أدوات الادخال و الاخراج- الصف الأول الثانويدرس أدوات الادخال و الاخراج- الصف الأول الثانوي
درس أدوات الادخال و الاخراج- الصف الأول الثانويhifakhaldi
 
الرياضيات للصف الثالث متوسط
الرياضيات للصف الثالث متوسطالرياضيات للصف الثالث متوسط
الرياضيات للصف الثالث متوسطAyad Haris Beden
 
matlab simulink مكتبة العناصر غير المستمرة
matlab simulink مكتبة العناصر غير المستمرةmatlab simulink مكتبة العناصر غير المستمرة
matlab simulink مكتبة العناصر غير المستمرةHasan Edrees
 
ملزمة الرياضيات للصف الثالث متوسط
ملزمة الرياضيات للصف الثالث متوسطملزمة الرياضيات للصف الثالث متوسط
ملزمة الرياضيات للصف الثالث متوسطAhmed Mahdi
 

What's hot (8)

الحل
الحلالحل
الحل
 
Arithmetic in c
Arithmetic in cArithmetic in c
Arithmetic in c
 
حلقات التكرار
حلقات التكرارحلقات التكرار
حلقات التكرار
 
درس أدوات الادخال و الاخراج- الصف الأول الثانوي
درس أدوات الادخال و الاخراج- الصف الأول الثانويدرس أدوات الادخال و الاخراج- الصف الأول الثانوي
درس أدوات الادخال و الاخراج- الصف الأول الثانوي
 
الرياضيات للصف الثالث متوسط
الرياضيات للصف الثالث متوسطالرياضيات للصف الثالث متوسط
الرياضيات للصف الثالث متوسط
 
البرمجة+ الستركجر
البرمجة+ الستركجرالبرمجة+ الستركجر
البرمجة+ الستركجر
 
matlab simulink مكتبة العناصر غير المستمرة
matlab simulink مكتبة العناصر غير المستمرةmatlab simulink مكتبة العناصر غير المستمرة
matlab simulink مكتبة العناصر غير المستمرة
 
ملزمة الرياضيات للصف الثالث متوسط
ملزمة الرياضيات للصف الثالث متوسطملزمة الرياضيات للصف الثالث متوسط
ملزمة الرياضيات للصف الثالث متوسط
 

Viewers also liked

R programming Fundamentals
R programming  FundamentalsR programming  Fundamentals
R programming FundamentalsRagia Ibrahim
 
Step by Step Guidance to Study Abroad
Step by Step Guidance to Study AbroadStep by Step Guidance to Study Abroad
Step by Step Guidance to Study AbroadSamaa Hazem Hosny
 
Palm oil processing machine line
Palm oil processing machine linePalm oil processing machine line
Palm oil processing machine lineMarymei
 
Che323 l1.1 palm oil milling &amp; refining mii
Che323 l1.1 palm oil milling &amp; refining miiChe323 l1.1 palm oil milling &amp; refining mii
Che323 l1.1 palm oil milling &amp; refining miisjobli74
 
2016-05-Malaysia Palm Oil Mill Value Map public
2016-05-Malaysia Palm Oil Mill Value Map public2016-05-Malaysia Palm Oil Mill Value Map public
2016-05-Malaysia Palm Oil Mill Value Map publicLennart Nilsson
 
Palm Oil Mill Waste Utilization (biogas, biomass briquette, biomass gasificat...
Palm Oil Mill Waste Utilization (biogas, biomass briquette, biomass gasificat...Palm Oil Mill Waste Utilization (biogas, biomass briquette, biomass gasificat...
Palm Oil Mill Waste Utilization (biogas, biomass briquette, biomass gasificat...Tara F Khaira
 
Sustainable Approach Of Recycling Palm Oil Mill Effluent Using Integrated Bio...
Sustainable Approach Of Recycling Palm Oil Mill Effluent Using Integrated Bio...Sustainable Approach Of Recycling Palm Oil Mill Effluent Using Integrated Bio...
Sustainable Approach Of Recycling Palm Oil Mill Effluent Using Integrated Bio...SAJJAD KHUDHUR ABBAS
 
Palm Oil, Palm Kernel Oil Process - Fractions, Derivatives and Product Uses
Palm Oil, Palm Kernel Oil Process - Fractions, Derivatives and Product UsesPalm Oil, Palm Kernel Oil Process - Fractions, Derivatives and Product Uses
Palm Oil, Palm Kernel Oil Process - Fractions, Derivatives and Product UsesGreenPalm
 
Version Control With GitHub & RStudio
Version Control With GitHub & RStudioVersion Control With GitHub & RStudio
Version Control With GitHub & RStudioRsquared Academy
 
R Programming: First Steps
R Programming: First StepsR Programming: First Steps
R Programming: First StepsRsquared Academy
 
R programming intro with examples
R programming intro with examplesR programming intro with examples
R programming intro with examplesDennis
 
Policy on Protection and Management of Peatland Ecosystem in Indonesia
Policy on Protection and Management of Peatland Ecosystem in IndonesiaPolicy on Protection and Management of Peatland Ecosystem in Indonesia
Policy on Protection and Management of Peatland Ecosystem in IndonesiaGlobalEnvironmentCentre
 
Peatlands, carbon and climate change
Peatlands, carbon and climate changePeatlands, carbon and climate change
Peatlands, carbon and climate changeecosystemsclimate
 
Wiring diagrams and ladder logic
Wiring diagrams and ladder logicWiring diagrams and ladder logic
Wiring diagrams and ladder logicCPX
 
R Programming: Introduction to Matrices
R Programming: Introduction to MatricesR Programming: Introduction to Matrices
R Programming: Introduction to MatricesRsquared Academy
 
Digital Mapping of Peat soils in Indonesia
Digital Mapping of Peat soils in IndonesiaDigital Mapping of Peat soils in Indonesia
Digital Mapping of Peat soils in IndonesiaBudiman Minasny
 
Introduction to palm oil processing
Introduction to palm oil processingIntroduction to palm oil processing
Introduction to palm oil processingCPX
 
Getting Up to Speed with R: Certificate Program in R for Statistical Analysis...
Getting Up to Speed with R: Certificate Program in R for Statistical Analysis...Getting Up to Speed with R: Certificate Program in R for Statistical Analysis...
Getting Up to Speed with R: Certificate Program in R for Statistical Analysis...Revolution Analytics
 

Viewers also liked (20)

R programming Fundamentals
R programming  FundamentalsR programming  Fundamentals
R programming Fundamentals
 
Step by Step Guidance to Study Abroad
Step by Step Guidance to Study AbroadStep by Step Guidance to Study Abroad
Step by Step Guidance to Study Abroad
 
Optimal palm oil mill plant layout
Optimal palm oil mill plant layoutOptimal palm oil mill plant layout
Optimal palm oil mill plant layout
 
Palm oil processing machine line
Palm oil processing machine linePalm oil processing machine line
Palm oil processing machine line
 
Che323 l1.1 palm oil milling &amp; refining mii
Che323 l1.1 palm oil milling &amp; refining miiChe323 l1.1 palm oil milling &amp; refining mii
Che323 l1.1 palm oil milling &amp; refining mii
 
2016-05-Malaysia Palm Oil Mill Value Map public
2016-05-Malaysia Palm Oil Mill Value Map public2016-05-Malaysia Palm Oil Mill Value Map public
2016-05-Malaysia Palm Oil Mill Value Map public
 
Palm Oil Mill Waste Utilization (biogas, biomass briquette, biomass gasificat...
Palm Oil Mill Waste Utilization (biogas, biomass briquette, biomass gasificat...Palm Oil Mill Waste Utilization (biogas, biomass briquette, biomass gasificat...
Palm Oil Mill Waste Utilization (biogas, biomass briquette, biomass gasificat...
 
Sustainable Approach Of Recycling Palm Oil Mill Effluent Using Integrated Bio...
Sustainable Approach Of Recycling Palm Oil Mill Effluent Using Integrated Bio...Sustainable Approach Of Recycling Palm Oil Mill Effluent Using Integrated Bio...
Sustainable Approach Of Recycling Palm Oil Mill Effluent Using Integrated Bio...
 
Palm Oil, Palm Kernel Oil Process - Fractions, Derivatives and Product Uses
Palm Oil, Palm Kernel Oil Process - Fractions, Derivatives and Product UsesPalm Oil, Palm Kernel Oil Process - Fractions, Derivatives and Product Uses
Palm Oil, Palm Kernel Oil Process - Fractions, Derivatives and Product Uses
 
Version Control With GitHub & RStudio
Version Control With GitHub & RStudioVersion Control With GitHub & RStudio
Version Control With GitHub & RStudio
 
R program
R programR program
R program
 
R Programming: First Steps
R Programming: First StepsR Programming: First Steps
R Programming: First Steps
 
R programming intro with examples
R programming intro with examplesR programming intro with examples
R programming intro with examples
 
Policy on Protection and Management of Peatland Ecosystem in Indonesia
Policy on Protection and Management of Peatland Ecosystem in IndonesiaPolicy on Protection and Management of Peatland Ecosystem in Indonesia
Policy on Protection and Management of Peatland Ecosystem in Indonesia
 
Peatlands, carbon and climate change
Peatlands, carbon and climate changePeatlands, carbon and climate change
Peatlands, carbon and climate change
 
Wiring diagrams and ladder logic
Wiring diagrams and ladder logicWiring diagrams and ladder logic
Wiring diagrams and ladder logic
 
R Programming: Introduction to Matrices
R Programming: Introduction to MatricesR Programming: Introduction to Matrices
R Programming: Introduction to Matrices
 
Digital Mapping of Peat soils in Indonesia
Digital Mapping of Peat soils in IndonesiaDigital Mapping of Peat soils in Indonesia
Digital Mapping of Peat soils in Indonesia
 
Introduction to palm oil processing
Introduction to palm oil processingIntroduction to palm oil processing
Introduction to palm oil processing
 
Getting Up to Speed with R: Certificate Program in R for Statistical Analysis...
Getting Up to Speed with R: Certificate Program in R for Statistical Analysis...Getting Up to Speed with R: Certificate Program in R for Statistical Analysis...
Getting Up to Speed with R: Certificate Program in R for Statistical Analysis...
 

Chapter 4

  • 1. Chapter 4 Arrays Ragia A. Ibrahim, Ph.D. Student 1 ‫القاهرة‬ ‫جامعة‬ ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫مركز‬‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬
  • 2. ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ 2 Arrays used to group of similar type data items. Accessed via an index. Array Declaration examples int myarray[100]; Data Type Name Size Introduction Brackets delimits array size char name[30]; float salaries[10];
  • 3. Array Elements-1 ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ 3
  • 4. Array Elements-2 ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ 4 44 16 23 68 Age[0] Age[1] Age[2] Age[3]
  • 5. ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ 5 Insert 4 students ages into an array #include<iostream.h> Int main() { float age[4]; for(int i=0;i<4;i++) { cout<<“Enter an age”; cin>>age[i]; } / / to print the array values for(int i=0;i<4;i++) cout<<“you Entered”<<age[i]; // why no {} in the second for? Return 0; } Example - 1 Enter an age:44 Enter an age:16 Enter an age:23 Enter an age:68 You entered 44 You entered 16 You entered 23 You entered 68 Array element accessed twice. The first time: insert a value into array cin>>age[j]; The second time: we read it cout<< “you entered”<<age[i];
  • 6. ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ 6 ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ 6 Sales: enter 6 sales values , calc avrg , sum. #include<iostream.h> Int main() { const int size=6; //size of array double sales[size]; //array of 6 variable cout<<“enter widget sales for 6 days n”; for (int j=0; j<size; j++) //puts figures from array { cin<<sales[j]; total+=sales[j]; // to find total } // ------- double average = total/size; //to find average cout<<“average= ”<<average<<endl; return 0; } Example - 2 variables declared with ‘const’ added become constants Enter widget sales for 6 days 352.64 867.70 781.32 867.35 746.21 189.45 Average = 634.11
  • 7. Syntax of array initialization ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ By: Ragia Ibrahim 7 Assignment operator Intialize values int coins [6]or[] = [1,5,10,25,50,100]; braces Array size (optional) commas
  • 8. Initializing Array days from start of year to date specified #include<iostream.h> Int main() { //shows days from start of year to date specified int month, day, total_days; int days_per_month[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; cout<<“n enter month(1 to 12):”; //get date cin>>month; cout<<“n enter day (1 to 31):”; cin>>day; total_days=days; //seprate days for (int j=0;j<month-1;j++) //add days each month total_days +=days_per_month[j]; cout<<“total days from start of year is:”<<total_days<<endl; return 0; } days_per_month[0] days_per_month[1] days_per_month[2] days_per_month[3] days_per_month[4] days_per_month[5] days_per_month[6] days_per_month[7] days_per_month[8] days_per_month[9] days_per_month[10] days_per_month[11]
  • 9. Multidimensional array • //salemon.cpp • //display sales chart using 2.d array • #include <iostream.h> • #include <iomanip.h> • Const int districts=4; • Const int months=3; How to represent Sales for each district per month ?
  • 10. ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ 10 Defining multidimensional arrays District 1 District 2 District 3 District 4 Month 1 2 3 Sales[0][0] Sales[0][1] Sales[0][2] Sales[1][0] Sales[1][1] Sales[1][2] Sales[2][0] Sales[2][1] Sales[2][2] Sales[3][0] Sales[3][1] Sales[3][2] double sales[districts][month];
  • 11. #include <iostream> const int district= 4; const int month= 3; Int main() { int d,m; d=0; m=0; double sales[d] [m] ; for(int d=0; d<districts; d++) for(int m=0; m<months; m++) { cout<<"Enter sales for district (" << d+1 <<')' ; cout<<" and Enter month (" << m+1 <<") : " ; cin>>sales[d][m]; } //put number in array }
  • 12. ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ 12 // program continue cout<<“nn”; cout<<“ monthn”; // can we display array values ?? when for(int d=0; d<4; d++) for(int m=0; m<3; m++) { for(int m=0;m<months;m++) //display array values cout<<“n District ”<< d+1 << “Month:” <<m+1; cout << sales[d][m]; } //end for Cout <<endl; return 0; } //end main cout<<setiosflags(ios::fixed) //not exponential <<setiosflags(ios::showpoint) //always use point <<setprecision(2) //digits to right <<setw(10) //field width cout <<sales[d][m]; //get number from array
  • 13. ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ 13 sales[districts][months]= { {1432.07,234.5,654.01}, {322.00,13838.32,17589.88}, {9328.34,934.00,449.30} , {12838.29,2332.63,32.93}}; //two-dimensional array definition with initialization Initialization for Multidimensional Array
  • 14. ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ 14 String ?? A M A N U E N S I S 0 Terminating 0 or null byte representing by ‘0’ characters constant Characters in string string String buffer str Unused part of buffer String stored in array of char’s variable Str={‘A’, ‘M’, ‘A’, ’N’, ’U’, ’E,’…}
  • 15. ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ 15 #include<iostream.h> Int main() { char str[80]; cout<<“Enter String :”; cin>>str; return 0; } String - variable
  • 16. ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ 16 //constant string #include<iostream.h> Int main() { char str[]=“farewell! Thou art too dear for my possessing.”; cout<<str<<endl; return 0; } String Constant
  • 17. ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ 17 #include<iostream.h> Int main() { char str1[]=“Welcome to ISSR.”; char str2[80]; Int j=0; while(str1[j]!=’0’; { str2[j]=str1[j]; j++; } Str2[j]=“0” //adding termination. cout<< str2; return 0; } String -Copying
  • 18. ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ 18 #include<iostream.h> Int main() { char weekDays[7][10] = { “Monday “, “ Tuesday “, “ Wednesday“, “Thursday“, “Friday“, “Saturday“, “Sunday“}; For ( int j =0; j< 7 ; j++) cout<< weekDays[j]<<endl; return 0; } Array of String
  • 19. ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ 19
  • 20. ‫االحصائيه‬ ‫والبحوث‬ ‫الدراسات‬ ‫معهد‬ ‫القاهرة‬ ‫جامعة‬ ‫والبرمجيات‬ ‫المعلومات‬ ‫قواعد‬ ‫بحوث‬ ‫مركز‬ 20 Q3: Write C++ prog. to reverse string. #include<iostream> using namespace std; int main() { char str[10]; int i, len; cout << "Enter a string: "; cin>>str; i=0; //intial value while (str[i]!='0') i++; len=i; // print in reversed order starting from string end to first.. for(i = len-1; i>=0; i--) cout<<str[i]; cin.get(); return 0; }