COMP 003 Module 1 Arrays and Strings
COMP 003 Module 1 Arrays and Strings
avg = sum / 10 ;
int main(){
const int SIZE=5; // defines the size N for 5 elements
double a[SIZE]; // declares the array’s elements as
// type double
int main(){
int a[] = { 11,33, 55,77 };
int size = sizeof(a) / sizeof(a[0]);
cout << "sum(a,size) = " << sum(a,size) << endl;
cout << endl << a[0] << endl;
system("PAUSE"); return 0;
}
ary[0][0]= 0x22ff40 = 11
ary[0][1]= 0x22ff44 = 12
11 12 13
21 22 23
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
ary[2][0]= 0x22ff48 = 31
ary[2][2]= 0x22ff50 = 33
Calculating Square of a Matrix
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){ const int row=3, col=3; int i,j;
int A[row][col];
cout << "Square of a " <<row <<"x"<<col<<"Matrices"<<endl;
for(i=0 ; i< row ; i++){
for(j=0 ; j<col; j++){
cout << "A[" << i+1 << "][" << j+1 << "]= ";
cin >> A[i][j];
}
}
for(i=0 ; i< row ; i++)
for(j=0 ; j<col; j++)
A[i][j] = A[i][j]*A[i][j];
for(i=0 ; i< row ; i++){
for(j=0 ; j<col; j++)
cout << A[i][j] << "\t";
cout << endl;
}
system("PAUSE"); return 0; }
Passing 2D Array to Function (1/2)
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
float matrix[4][3]; // 4 rows and 3 columns
get_data(matrix,4,3);
show_data(matrix,4,3);
system("PAUSE");
return 0;
}
Passing 3D Array to Function (1/2)
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
float matrix[4][3][2]; // 4 rows, 3 columns, 2 pages
get_data(matrix,4,3,2);
show_data(matrix,4,3,2);
system("PAUSE");
return 0;
}
Sorting Data Using Bubble Sort (1/2)
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int i; float data[10];
cout << "Enter 10 Numbers for Sorting \n";
for( i=0 ; i<10 ; i++ ){
cout << "Enter No." <<i+1<< ":" ;
cin >> data[i];
}
sort(data,10);
print(data,10);
system("PAUSE");
return 0;
}
Sorting Data Using Bubble Sort (2/2)
void sort( float a[], int n ){ // bubble sort:
for (int i=1; i<n; i++) // bubble up max{a[0..n-i]}:
for (int j=0; j<n-i; j++)
if (a[j] > a[j+1])
{
float temp = a[j];
a[j]=a[j+1];
a[j+1] = temp;
}
}
int main(){
char str[] = { 'M','.',' ','A','l','i',0,' ',
'I','I','U',0}; // char ch [] = "M. Ali";
int size = sizeof(str);
int main(){
cout << "\nEnter a multiline string:\n";
cin.get(str, MAX, '$'); //terminate with $
system("PAUSE");
return 0;
}
Copying a String the Hard Way
// copies a string using a for loop
#include <iostream>
#include <cstring> //for strlen()
#include <stdlib.h>
using namespace std;
int main(){ //initialized string
char str1[] = "Oh, Captain, my Captain! "
"our fearful trip is done";
system("PAUSE"); return 0;
}
Copying a String the Easy Way
// strcopy2.cpp
// copies a string using strcpy() function
#include <iostream>
#include <cstring> //for strcpy()
#include <stdlib.h>
using namespace std;
int main(){
char str1[] = "Tiger, tiger, burning bright\n"
"In the forests of the night";
const int MAX = 80; // size of str2 buffer
char str2[MAX]; // empty string
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);