# Order Array2015 .pdf

### File information

Original filename: Order Array2015.pdf
Title: Operating Systems CSC 522
Author: Sameh Elsharkawy

This PDF 1.5 document has been generated by Microsoft® PowerPoint® 2013, and has been sent on pdf-archive.com on 05/11/2015 at 21:47, from IP address 41.176.x.x. The current document download page has been viewed 638 times.
File size: 749 KB (32 pages).
Privacy: public file

Order Array2015.pdf (PDF, 749 KB)

### Document preview

Q1:
• int s(int[] arr) {

int s= 0;

for(int i=0; i&lt;arr.length; i++) {

s = s + arr[i];

}

return s;
•}

Q2:
• int m(int[] arr) {

int m = arr[0];

for (int i=1; i&lt;arr.length; i++) {

if (arr[i] &gt; m) {

m = arr[i];

}

return m;

Q3:
int f(int n, int[] a) {
for (int i=0; i&lt;n; i++) {
for (int j=0; j&lt;n; j++) {
for (int k=0; k&lt;n; k++) {
if (i!=j &amp;&amp; j!=k &amp;&amp; i!=k) {
if (a[i]==a[j] &amp;&amp; a[j]==a[k])
return 1;
}}}
return 0;
}
What does method f do?
What is the running time of f? Use the big-O notation.
If it takes 10 seconds to run f on array of size 100, how
long it takes to run it on the array of size 400.

Q4:
Rewrite the find method in array.cpp such that it:
returns the index of the element which is equal to searchKey,
returns -1 if there is no element in the array which is equal to
searchKey, and does not contain break statement.

Q5:
Write C++ program that that finds the
smallest odd number in a given array of
integers. What is the running time of the
algorithm?

Two-dimensional Arrays

4 x 11
columns
• Declaration:
int matrix[4][11];

rows

Element Access
• In order to access the j-th element
(column) of the i-th array (row)
column

matrix[i][j]
row

Initializing Two-dimensional Arrays
int arr[2][3] = { {1, 2, 3}, {4, 5, 6} };
1

2

3

4

5

6

• As with regular array size can be inferred from the
initialization
• Unlike regular array, this is only true for the number
of rows.
int arr[][3] = { {1, 2, 3}, {4, 5, 6} };

#define MATRIX_SIZE 3
int main(void)
{
int a[][MATRIX_SIZE]={{1,2,3}, {4,5,6}, {7,8,9}};
int b[][MATRIX_SIZE]={{1,1,1}, {2,2,2}, {3,3,3}};
int c[MATRIX_SIZE][MATRIX_SIZE];
int i = 0, j = 0;
for (i = 0; i &lt; MATRIX_SIZE; ++i)
for (j = 0; j &lt; MATRIX_SIZE; ++j)
c[i][j] = a[i][j] + b[i][j];
/* print c */
...
return 0;
}