Ch07 Arrays and Strings
Ch07 Arrays and Strings
Arrays
Calculating Average
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
double avg, sum = 0 ;
int i ; int marks[10] ; /* array declaration */
for ( i = 0 ; i <= 9 ; i++ ){
cout << "\nEnter marks ";
cin >> marks[i]; /* store data in array */
}
for ( i = 0 ; i <= 9 ; i++ )
sum = sum + marks[i] ; /* read data from array*/
avg = sum / 10 ;
cout << "\n Average marks = " << avg <<endl;
system("PAUSE"); return 0;
}
Out of Bounds
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
float a[] = { 22.2,44.4, 66.6 };
float x=11.1;
cout << "I m going to Crash " << endl;
cin >> x;
a[3333] = 88.8; // ERROR: index is out of bounds!
system("PAUSE"); return 0;
}
n Dimensional Arrays
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){ const int row=2, col=3; int i,j;
int ary[row][col] = {
{11,12,13},
{21,22,23}
};
for(i=0 ; i< row ; i++){
for(j=0 ; j<col; j++){ cout << ary[i][j] << " ";}
cout << endl;
}
for(i=0 ; i< row ; i++){
for(j=0 ; j<col; j++){
cout << &ary[i][j] << "="<<ary[i][j]<<"\t";}
cout << endl;}
system("PAUSE"); return 0;
}
n Dimensional Arrays
ary[0][0]= 0x22ff40 = 11
11 12 13
21 22 23
ary[0][1]= 0x22ff44 = 12
ary[0][2]= 0x22ff48 = 13
ary[1][0]= 0x22ff4C = 21
ary[1][1]= 0x22ff50 = 22
ary[1][2]= 0x22ff54 = 23
2-Dimensional Arrays
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){ const int row=3, col=3; int i,j;
int ary[row][col] = {
{11,12,13},
{21,22,23},
{31,32,33}
};
for(i=0 ; i< row ; i++){
for(j=0 ; j<col; j++){ cout << ary[i][j] << " ";}
cout << endl;
}
for(i=0 ; i< row ; i++){
for(j=0 ; j<col; j++){
cout << &ary[i][j] << "="<<ary[i][j]<<"\t";}
cout << endl;}
system("PAUSE"); return 0; }
3-Dimensional Arrays
ary[0][0]= 0x22ff30 = 11
ary[0][1]= 0x22ff34 = 12
ary[0][2]= 0x22ff38 = 13
ary[1][0]= 0x22ff3C = 21
11 12 13
21 22 23
31 32 33
ary[1][1]= 0x22ff40 = 22
ary[1][2]= 0x22ff44 = 23
ary[2][0]= 0x22ff48 = 31
ary[2][1]= 0x22ff4C = 32
ary[2][2]= 0x22ff50 = 33
Using C-string
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
char str[] = { 'M','.',' ','A','l','i',0,' ',
'I','I','U',0}; // char ch [] = "M. Ali";
int size = sizeof(str);
cout << "\n The Character Array Size is :" <<size
<< " Bytes" << endl;
for ( int i=0 ; i<size ; i++ )
cout << "str[" << i << "]=" <<str[i] <<" =["
<< int(str[i]) << "]" << endl;
cout << endl << str << endl;
system("PAUSE");
return 0;
}
Array of Strings
// straray.cpp
// array of strings
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
const int DAYS = 7; //number of strings in array
const int MAX = 10; //maximum size of each string
//An array of strings
char star[DAYS][MAX] = { "Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday" , "Saturday" };
for( int j=0 ; j<DAYS ; j++) //display every string
cout << star[j] << endl;
system("PAUSE");
return 0;
}
Array of Strings
Assignment #4
1.
Write and test the following function that returns through its reference
parameters both the maximum and the minimum values stored in an
array: void getExtremes(float& min, float& max, float a[], int n);
2.
Write and test the following function that returns dot product of two
vectors: float innerProduct(float a[], int n, float b[]);
3.
4.
Assignment #4
1.
2.
3.
Write a program that takes a string from the user. The program then
calculates the
Total number of Characters
Total number of words
Total number of lines
Decimal
100
500
1000
Roman
c
d
m