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];
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
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 ?
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,’…}
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;
}