one dimensional array · two dimensional array فٕفهن لٔلأا : حهخاري 2 loops ٗنا...

24
1 حظة م-: مطلوب العملز النجا وريةبة الخطوات الضر سيتم هنا كتالبرنامج كام وليس ا. One Dimensional Array ج ان ذحرااحذفح راخ انثعذ انظف انloop ح ف ذرشااحذ ح ق انعذادi ت0 اطشذد انعع 1 د كارا فاr=5 عذد أاطش انع5 فا(i=0..4) a[0] a[1] a[2] a[3] a[4] #define r 5 int a[r]; Set the value of elements to 5 for (i=0;i<r;i++) a[i]=5; Find the sum of elements int sum =0; for (i=0;i<r;i++) sum+=a[i]; cout <<"Sum of elements="<<sum; Find the location, number, sum and average of positive even numbers int sum =0,n=0; for (i=0;i<r;i++) if ((!(a[i]%2) && (a[i]>0)) { cout << "Positive even number="<<a[i]<<" at location= "<<i<<"\n"; sum+=a[i]; n++; } cout <<"\nNo. of Positive even number="<<n; cout <<"\nSum of Positive even number="<<sum; cout <<"\nAverage of Positive even number="<<(float )sum/n; Find the location, number, sum and average of number 3 int sum =0,n=0; for (i=0;i<r;i++) if (a[i]==3) { cout << "Number 3 is found at location="<<i<<"\n"; sum+=3; n++; } cout <<"\nNo. of Number 3 ="<<n; cout <<"\nSum of Number 3 ="<<sum; cout <<"\nAverage Number 3 ="<<(float )sum/n;

Upload: others

Post on 29-Mar-2020

21 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: One Dimensional Array · Two Dimensional Array فٕفهن لٔلأا : حهخاري 2 loops ٗنا جارحذ( ٍٚعثنا خا حفٕفًنا اُع r دَاك ااف 1 فٕفنا

1

سيتم هنا كتابة الخطوات الضرورية النجاز العمل المطلوب :-مالحظة

. وليس البرنامج كامال

One Dimensional Array

احذ ذرشاح ف loopانظففح راخ انثعذ اناحذ ذحراج ان

أ عذد r=5فارا كاد 1 –عذد انعاطش 0ت i انعذادقح

(i=0..4)فا 5انعاطش

a[0]

a[1]

a[2]

a[3]

a[4]

#define r 5

int a[r];

Set the value of elements to 5 for (i=0;i<r;i++)

a[i]=5;

Find the sum of elements

int sum =0;

for (i=0;i<r;i++)

sum+=a[i];

cout <<"Sum of elements="<<sum;

Find the location, number, sum and average of positive even numbers int sum =0,n=0;

for (i=0;i<r;i++)

if ((!(a[i]%2) && (a[i]>0))

{

cout << "Positive even number="<<a[i]<<" at location= "<<i<<"\n";

sum+=a[i];

n++;

}

cout <<"\nNo. of Positive even number="<<n;

cout <<"\nSum of Positive even number="<<sum;

cout <<"\nAverage of Positive even number="<<(float )sum/n;

Find the location, number, sum and average of number 3 int sum =0,n=0;

for (i=0;i<r;i++)

if (a[i]==3)

{

cout << "Number 3 is found at location="<<i<<"\n";

sum+=3;

n++;

}

cout <<"\nNo. of Number 3 ="<<n;

cout <<"\nSum of Number 3 ="<<sum;

cout <<"\nAverage Number 3 ="<<(float )sum/n;

Page 2: One Dimensional Array · Two Dimensional Array فٕفهن لٔلأا : حهخاري 2 loops ٗنا جارحذ( ٍٚعثنا خا حفٕفًنا اُع r دَاك ااف 1 فٕفنا

2

Find the smallest number int s=a[0];

for (i=0;i<r;i++)

if (a[i]<s) s=a[i];

cout <<"Smallest number ="<<s;

Find the largeest number int l=a[0];

for (i=0;i<r;i++)

if (a[i]>l) l=a[i];

cout <<"Largest number ="<<l;

Two Dimensional Array األل نهظفف : يرذاخهح loops 2ذحراج ان ) راخ انثعذعاطش انظففح

rفارا كاد 1-عذد انظفف 0ك انظف ف يحظس ت (( انثا نألعذج

أل انثذاح ) 3 0ت ك يحظسجة ا iانعذادفا 4= فف ذثم عذد انض

1-ان عذد األعذج 0أيا انعد نكم طف فرزاذ ي ، (i=0 .. 3)أ ( 0ي

2 0جة ا ك يحظس ت jفا انعذاد 3= ذثم عذد األعذج cفارا كاد

(j=0 .. 2)أ ( 0أل انثذاح ي )

i=1ف انثا فا ص، أيا عاطش ال j=0..2األعذج نا i=0ل فعاطش انظف األ

،كزا j=0..2األعذج نا

Set the value of elements to 5

for (i=0;i<r;i++)

for (j=0;j<c;j++)

a[i][j]=5;

Find the sum of elements int sum =0;

for (i=0;i<r;i++)

for (j=0;j<c;j++)

sum+=a[i][j];

cout <<"Sum of elements ="<<sum;

Find the location, number, sum and average of positive even numbers int sum =0,n=0;

for (i=0;i<r;i++)

for (j=0;j<c;j++)

if ((!(a[i][j]%2) && (a[i][j]>0))

{

cout << "Positive even number ="<<a[i][j]<<" at location:

row="<<i<<", column="<<j<<"\n";

sum+=a[i][j];

n++;

}

cout <<"\nNo. of Positive even number ="<<n;

cout <<"\nSum of Positive even number ="<<sum;

cout <<"\nAverage of Positive even number ="<<(float )sum/n;

Col0 Col1 Col2

Row0

Row1

Row2

Row3

Page 3: One Dimensional Array · Two Dimensional Array فٕفهن لٔلأا : حهخاري 2 loops ٗنا جارحذ( ٍٚعثنا خا حفٕفًنا اُع r دَاك ااف 1 فٕفنا

3

Find the location, number, sum and average of number 3 int sum =0,n=0;

for (i=0;i<r;i++)

for (j=0;j<c;j++)

if (a[i][j]==3)

{

cout << "Number 3 is found at location: row="<<i<<",

column="<<j<<"\n";

sum+=3;

n++;

}

cout <<"\nNo. of Number 3 ="<<n;

cout <<"\nSum of Number 3 ="<<sum;

cout <<"\nAverage Number 3 ="<<(float )sum/n;

Find the smallest number int s=a[0][0];

for (i=0;i<r;i++)

for (j=0;j<c;j++)

if (a[i][j]<s) s=a[i][j];

cout <<"Smallest number ="<<s;

Find the largest number int l=a[0][0];

for (i=0;i<r;i++)

for (j=0;j<c;j++)

if (a[i][j]>l) l=a[i][j];

cout <<"Largest number ="<<l;

Squared Array

عذد األعذج فثذل ا = يظففح راخ تعذ فا عذد انظفف

نر ذثم ا rنعذد األعذج سرخذو فقط cنعذد انظفف rسرخذو

عذد انظفف أ عذد األعذج انعم كه فس عذا االسرعاضح ع كم

c تـr

Specific Row ياقع ( احذ loopشث يظففح راخ تعذ احذ فحراج ان )انظف انحذد

1-عذد األعذج 0انعد يحظس ت 1-عاطش ثثد فا سقى انظف انعط

ذثم عذد األعذج rا (j=0 .. 3) فأ r=4ارا كاد أ

Set the value of elements in 2nd row to 5 for (j=0;j<r;j++)

a[1][j]=5;

Find the sum of elements in 2nd row int sum =0;

for (j=0;j<r;j++)

sum+=a[1][j];

cout <<"Sum of elements in 2nd

row="<<sum;

Find the location, number, sum and

average of positive even numbers in 2nd row

int sum =0,n=0;

for (j=0;j<r;j++)

if ((!(a[1][j]%2) && (a[1][j]>0))

{

cout << "Positive even number in 2nd row="<<a[1][j]<<" at

column="<<j<<"\n";

Col0 Col1 Col2 Col3

Row0

Row1

Row2

Row3

Page 4: One Dimensional Array · Two Dimensional Array فٕفهن لٔلأا : حهخاري 2 loops ٗنا جارحذ( ٍٚعثنا خا حفٕفًنا اُع r دَاك ااف 1 فٕفنا

4

sum+=a[1][j];

n++;

}

cout <<"\nNo. of Positive even number in 2nd row="<<n;

cout <<"\nSum of Positive even number in 2nd row="<<sum;

cout <<"\nAverage of Positive even number in 2nd row="<<(float )sum/n;

Find the location, number, sum and average of number 3 in 2nd row int sum =0,n=0;

for (j=0;j<r;j++)

if (a[1][j]==3)

{

cout << "Number 3 is found in 2nd row at column="<<j<<"\n";

sum+=3;

n++;

}

cout <<"\nNo. of Number 3 in 2nd row="<<n;

cout <<"\nSum of Number 3 in 2nd row="<<sum;

cout <<"\nAverage Number 3 in 2nd row="<<(float )sum/n;

Find the smallest number in 2nd row int s=a[1][0];

for (j=0;j<r;j++)

if (a[1][j]<s) s=a[1][j];

cout <<"Smallest number in 2nd row="<<s;

Find the largeest number in 2nd row int l=a[1][0];

for (j=0;j<r;j++)

if (a[1][j]>l) l=a[1][j];

cout <<"Largest number in 2nd row="<<l;

Specific Column ياقع ( احذ loopشث يظففح راخ تعذ احذ فحراج ان )انعد انحذد

1-عذد انظفف 0انظف يحظس ت 1-عاطش ثثد فا سقى انعد انعط

ذثم عذد انظفف rا (i=0 .. 3) فأ r=4 أ ارا كاد

Col0 Col1 Col2 Col3

Row0

Row1

Row2

Row3

Set the value of elements in 2nd column to 5

for (i=0;i<r;i++)

a[i][1]=5;

Find the sum of elements in 2nd column int sum =0;

for (i=0;i<r;i++)

sum+=a[i][1];

cout <<"Sum of elements in 2nd column="<<sum;

Page 5: One Dimensional Array · Two Dimensional Array فٕفهن لٔلأا : حهخاري 2 loops ٗنا جارحذ( ٍٚعثنا خا حفٕفًنا اُع r دَاك ااف 1 فٕفنا

5

Find the location, number, sum and average of positive even numbers in 2nd column

int sum =0,n=0;

for (i=0;i<r;i++)

if ((!(a[i][1]%2) && (a[i][1]>0))

{

cout << "Positive even number in 2nd column="<<a[i][1]<<" at row=

"<<i<<"\n";

sum+=a[i][1];

n++;

}

cout <<"\nNo. of Positive even number in 2nd column ="<<n;

cout <<"\nSum of Positive even number in 2nd column ="<<sum;

cout <<"\nAverage of Positive even number in 2nd column="<<(float

)sum/n;

Find the location, number, sum and average of number 3 in 2nd column int sum =0,n=0;

for (i=0;i<r;i++)

if (a[i][1]==3)

{

cout << "Number 3 is found in 2nd column at row="<<i<<"\n";

sum+=3;

n++;

}

cout <<"\nNo. of Number 3 in 2nd column ="<<n;

cout <<"\nSum of Number 3 in 2nd column ="<<sum;

cout <<"\nAverage Number 3 in 2nd column ="<<(float )sum/n;

Find the smallest number in 2nd column int s=a[0][1];

for (i=0;i<r;i++)

if (a[i][1]<s) s=a[i][1];

cout <<"Smallest number in 2nd column ="<<s;

Find the largeest number in 2nd column int l=a[0][1];

for (i=0;i<r;i++)

if (a[i][1]>l) l=a[i][1];

cout <<"Largest number in 2nd column ="<<l;

Main Diagonal شث يظففح راخ تعذ احذ فحراج )انقطش انشئس ي انظففح انشتعح

اقع عاطش رسا فا سقى انظف يع سقى انعد نكم ي( احذ loopان

أ 1-عذد انظفف أ عذد األعذج 0عاطش فك انعد انظف يحظس ت

(i=0 .. 3)) ارا كادr=4 حسr ذثم عذد انظفف عذد األعذج

Col0 Col1 Col2 Col3

Row0

Row1

Row2

Row3

Page 6: One Dimensional Array · Two Dimensional Array فٕفهن لٔلأا : حهخاري 2 loops ٗنا جارحذ( ٍٚعثنا خا حفٕفًنا اُع r دَاك ااف 1 فٕفنا

6

Set the value of elements in main diagonal to 5 for (i=0;i<r;i++)

a[i][i]=5;

Find the sum of elements in main diagonal int sum =0;

for (i=0;i<r;i++)

sum+=a[i][i];

cout <<"Sum of elements in main diagonal ="<<sum;

Find the location, number, sum and average of positive even numbers in main diagonal

int sum =0,n=0;

for (i=0;i<r;i++)

if ((!(a[i][i]%2) && (a[i][i]>0))

{

cout << "Positive even number in main diagonal ="<<a[i][i]<<" at

location: row="<<i<<", column="<<i<<"\n";

sum+=a[i][i];

n++;

}

cout <<"\nNo. of Positive even number in main diagonal ="<<n;

cout <<"\nSum of Positive even number in main diagonal ="<<sum;

cout <<"\nAverage of Positive even number in main diagonal="<<(float

)sum/n;

Find the location, number, sum and average of number 3 in main

diagonal

int sum =0,n=0;

for (i=0;i<r;i++)

if (a[i][i]==3)

{

cout << "Number 3 is found in main diagonal at location:

row="<<i<<", column="<<i<<"\n";

sum+=3;

n++;

}

cout <<"\nNo. of Number 3 in main diagonal ="<<n;

cout <<"\nSum of Number 3 in main diagonal ="<<sum;

cout <<"\nAverage Number 3 in main diagonal ="<<(float )sum/n;

Find the smallest number in main diagonal int s=a[0][0];

for (i=0;i<r;i++)

if (a[i][i]<s) s=a[i][i];

cout <<"Smallest number in main diagonal ="<<s;

Find the largest number in main diagonal int l=a[0][0];

for (i=0;i<r;i++)

if (a[i][i]>l) l=a[i][i];

cout <<"Largest number in main diagonal ="<<l;

Page 7: One Dimensional Array · Two Dimensional Array فٕفهن لٔلأا : حهخاري 2 loops ٗنا جارحذ( ٍٚعثنا خا حفٕفًنا اُع r دَاك ااف 1 فٕفنا

7

Secondary Diagonal شث يظففح راخ تعذ احذ فحراج )انقطش انثا ي انظففح انشتعح

عذد 0ياقع عاطش ك فا انظف ف يحظس ت ( احذ loopان

أ 0ان 1-أيا انعد فراقض ي عذد األعذج (i=0 .. 3)أ 1-انظفف

(j=3 .. 0) ارا كادr=4 اr عذج ذثم عذد انظفف أ عذد األ

Col0 Col1 Col2 Col3

Row0

Row1

Row2

Row3

Set the value of elements in secondary diagonal to 5 for (i=0,j=r-1;i<r;i++,j--)

a[i][j]=5;

Find the sum of elements in secondary diagonal int sum =0;

for (i=0,j=r-1;i<r;i++,j--)

sum+=a[i][j];

cout <<"Sum of elements in secondary diagonal ="<<sum;

Find the location, number, sum and average of positive even numbers in secondary diagonal

int sum =0,n=0;

for (i=0,j=r-1;i<r;i++,j--)

if ((!(a[i][j]%2) && (a[i][j]>0))

{

cout << "Positive even number in secondary diagonal ="<<a[i][j]<<"

at location: row="<<i<<", column="<<j<<"\n";

sum+=a[i][j];

n++;

}

cout <<"\nNo. of Positive even number in secondary diagonal ="<<n;

cout <<"\nSum of Positive even number in secondary diagonal ="<<sum;

cout <<"\nAverage of Positive even number in secondary

diagonal="<<(float )sum/n;

Find the location, number, sum and average of number 3 in secondary diagonal

int sum =0,n=0;

for (i=0,j=r-1;i<r;i++,j--)

if (a[i][j]==3)

{

cout << "Number 3 is found in secondary diagonal at location:

row="<<i<<", column="<<j<<"\n";

sum+=3;

n++;

}

cout <<"\nNo. of Number 3 in secondary diagonal ="<<n;

cout <<"\nSum of Number 3 in secondary diagonal ="<<sum;

cout <<"\nAverage Number 3 in secondary diagonal ="<<(float )sum/n;

Page 8: One Dimensional Array · Two Dimensional Array فٕفهن لٔلأا : حهخاري 2 loops ٗنا جارحذ( ٍٚعثنا خا حفٕفًنا اُع r دَاك ااف 1 فٕفنا

8

Find the smallest number in secondary diagonal int s=a[0][r-1];

for (i=0,j=r-1;i<r;i++,j--)

if (a[i][j]<s) s=a[i][j];

cout <<"Smallest number in secondary diagonal ="<<s;

Find the largest number in secondary diagonal int l=a[0][r-1];

for (i=0,j=r-1;i<r;i++,j--)

if (a[i][j]>l) l=a[i][j];

cout <<"Largest number in secondary diagonal ="<<l;

Upper Triangle of The Main Diagonal ذشث يظففح راخ )عاطش انثهس انعه نهقطش انشئس ي انظففح انشتعح

فارا ((األل نهظفف انثا نألعذج: يرذاخهح loops 2تعذ فرحراج ان

:فقل (i=0 .. 2)أ 2-عذد انظفف 0ك انظف ف يحظس ت ف r=4كاد

for (i=0;i<lr;i++)ز حlr=r-1=3 د نكم طف فرزاذ ي سقى انظف أيا انع

ذثم عذد rا for (j=i+1;j<r;j++): فقل (j=i+1 .. 3) 1-ان عذد األعذج

. انظفف أ عذد األعذج

i=1، أيا عاطش انظف انثا فا j=1..3األعذج نا i=0فعاطش انظف األل

j=3..3ا األعذج ن i=2، عاطش انظف انثانس j=2..3األعذج نا

Col0 Col1 Col2 Col3

Row0 M

Row1 a

Row2 i

Row3 n

Set the value of elements in upper triangle of the main diagonal to 5

for (i=0;i<lr;i++)

for (j=i+1;j<r;j++)

a[i][j]=5;

Find the sum of elements in upper triangle of the main diagonal int sum =0;

for (i=0;i<lr;i++)

for (j=i+1;j<r;j++)

sum+=a[i][j];

cout <<"Sum of elements in upper triangle of the main diagonal

="<<sum;

Find the location, number, sum and average of positive even numbers in upper triangle of the main diagonal

int sum =0,n=0;

for (i=0;i<lr;i++)

for (j=i+1;j<r;j++)

if ((!(a[i][j]%2) && (a[i][j]>0))

{

cout << "Positive even number in upper triangle of the main

diagonal ="<<a[i][j]<<" at location: row="<<i<<", column="<<j<<"\n";

sum+=a[i][j];

n++;

}

Page 9: One Dimensional Array · Two Dimensional Array فٕفهن لٔلأا : حهخاري 2 loops ٗنا جارحذ( ٍٚعثنا خا حفٕفًنا اُع r دَاك ااف 1 فٕفنا

9

cout <<"\nNo. of Positive even number in upper triangle of the main

diagonal ="<<n;

cout <<"\nSum of Positive even number in upper triangle of the main

diagonal ="<<sum;

cout <<"\nAverage of Positive even number in upper triangle of the main

diagonal ="<<(float )sum/n;

Find the location, number, sum and average of number 3 in upper

triangle of the main diagonal int sum =0,n=0;

for (i=0;i<lr;i++)

for (j=i+1;j<r;j++)

if (a[i][j]==3)

{

cout << "Number 3 is found in upper triangle of the main diagonal

at location: row="<<i<<", column="<<j<<"\n";

sum+=3;

n++;

}

cout <<"\nNo. of Number 3 in upper triangle of the main diagonal ="<<n;

cout <<"\nSum of Number 3 in upper triangle of the main diagonal

="<<sum;

cout <<"\nAverage Number 3 in upper triangle of the main diagonal

="<<(float )sum/n;

Find the smallest number in upper triangle of the main diagonal int s=a[0][r-1];

for (i=0;i<lr;i++)

for (j=i+1;j<r;j++)

if (a[i][j]<s) s=a[i][j];

cout <<"Smallest number in upper triangle of the main diagonal ="<<s;

Find the largest number in upper triangle of the main diagonal int l=a[0][r-1];

for (i=0;i<lr;i++)

for (j=i+1;j<r;j++)

if (a[i][j]>l) l=a[i][j];

cout <<"Largest number in upper triangle of the main diagonal ="<<l;

Lower Triangle of The Main Diagonal ذشث يظففح راخ )نهقطش انشئس ي انظففح انشتعح سفهعاطش انثهس ال

فارا (( األل نهظفف انثا نألعذج: يرذاخهح loops 2تعذ فرحراج ان

(i=3 .. 1)أ 1ت 1-ك انظف ف يحظس ت عذد انظفف ف r=4كاد

ي سقى اقضنعد نكم طف فدأيا ا lr=r-1=3حس for(i=lr;i>0;i--): فقل

ذثم rا for (j=i-1;j>=0;j--): فقل(j=(i-1) .. 0)ان 0ان 1-انظف

عذد انظفف أ عذد األعذج

i=2، أيا عاطش انظف انثانس j=2..0األعذج نا i=3فعاطش انظف انشاتع

j=0..0نا األعذج i=1تا عاطش انظف انثا فا j=1..0األعذج نا

Col0 Col1 Col2 Col3

Row0 M

Row1 a

Row2 i

Row3 n

Page 10: One Dimensional Array · Two Dimensional Array فٕفهن لٔلأا : حهخاري 2 loops ٗنا جارحذ( ٍٚعثنا خا حفٕفًنا اُع r دَاك ااف 1 فٕفنا

10

Set the value of elements in lower triangle of the main diagonal to 5.

for (i=lr;i>0;i--)

for (j=i-1;j>=0;j--)

a[i][j]=5;

Find the sum of elements in lower triangle of the main diagonal int sum =0;

for (i=lr;i>0;i--)

for (j=i-1;j>=0;j--)

sum+=a[i][j];

cout <<"Sum of elements in lower triangle of the main diagonal

="<<sum;

Find the location, number, sum and average of positive even numbers in lower triangle of the main diagonal

int sum =0,n=0;

for (i=lr;i>0;i--)

for (j=i-1;j>=0;j--)

if ((!(a[i][j]%2) && (a[i][j]>0))

{

cout << "Positive even number in lower triangle of the main

diagonal ="<<a[i][j]<<" at location: row="<<i<<", column="<<j<<"\n";

sum+=a[i][j];

n++;

}

cout <<"\nNo. of Positive even number in lower triangle of the main

diagonal ="<<n;

cout <<"\nSum of Positive even number in lower triangle of the main

diagonal ="<<sum;

cout <<"\nAverage of Positive even number in lower triangle of the main

diagonal ="<<(float )sum/n;

Find the location, number, sum and average of number 3 in lower

triangle of the main diagonal int sum =0,n=0;

for (i=lr;i>0;i--)

for (j=i-1;j>=0;j--)

if (a[i][j]==3)

{

cout << "Number 3 is found in lower triangle of the main diagonal

at location: row="<<i<<", column="<<j<<"\n";

sum+=3;

n++;

}

cout <<"\nNo. of Number 3 in lower triangle of the main diagonal ="<<n;

cout <<"\nSum of Number 3 in lower triangle of the main diagonal

="<<sum;

cout <<"\nAverage Number 3 in lower triangle of the main diagonal

="<<(float )sum/n;

Find the smallest number in lower triangle of the main diagonal int s=a[r-1][0];

for (i=lr;i>0;i--)

for (j=i-1;j>=0;j--)

if (a[i][j]<s) s=a[i][j];

cout <<"Smallest number in lower triangle of the main diagonal ="<<s;

Page 11: One Dimensional Array · Two Dimensional Array فٕفهن لٔلأا : حهخاري 2 loops ٗنا جارحذ( ٍٚعثنا خا حفٕفًنا اُع r دَاك ااف 1 فٕفنا

11

Find the largest number in lower triangle of the main diagonal int l=a[r-1][0];

for (i=lr;i>0;i--)

for (j=i-1;j>=0;j--)

if (a[i][j]>l) l=a[i][j];

cout <<"Largest number in lower triangle of the main diagonal ="<<l;

Upper Triangle of The Secondary Diagonal ذشث يظففح راخ )عاطش انثهس انعه نهقطش انشئس ي انظففح انشتعح

فارا كاد (( األل نهظفف انثا نألعذج: يرذاخهح loop 2تعذ فرحراج ان

r=4 أ 2-عذد انظفف 0انظف ف يحظس ت فك(i=0 .. 2) فقل:

for(i=0;i<lr;i++) حسlr=r-1=3 عذد األعذج ي اقضنكم طف فد أيا انعد

for (j=lr-1-i;j>=0;j--): فقل (j=(lr-1-i) .. 0) أ 0سقى انظف ان -2 –

ذثم عذد انظفف أ عذد األعذج rا

i=1، أيا عاطش انظف انثا فا j=2..0األعذج نا i=0فعاطش انظف األل

j=0..0األعذج نا i=2 ، عاطش انظف انثانس j=1..0األعذج نا

Col0 Col1 Col2 Col3

Row0 S

Row1 c

Row2 n

Row3 d

Set the value of elements in upper triangle of the secondary

diagonal to 5

for (i=0;i<lr;i++)

for (j=lr-1-i;j>=0;j--)

a[i][j]=5;

Find the sum of elements in upper triangle of the secondary diagonal int sum =0;

for (i=0;i<lr;i++)

for (j=lr-1-i;j>=0;j--)

sum+=a[i][j];

cout <<"Sum of elements in upper triangle of the secondary diagonal

="<<sum;

Find the location, number, sum and average of positive even numbers in upper triangle of the secondary diagonal

int sum =0,n=0;

for (i=0;i<lr;i++)

for (j=lr-1-i;j>=0;j--)

if ((!(a[i][j]%2) && (a[i][j]>0))

{

cout << "Positive even number in upper triangle of the secondary

diagonal ="<<a[i][j]<<" at location: row="<<i<<", column="<<j<<"\n";

sum+=a[i][j];

n++;

}

cout <<"\nNo. of Positive even number in upper triangle of the secondary

diagonal ="<<n;

cout <<"\nSum of Positive even number in upper triangle of the secondary

diagonal ="<<sum;

cout <<"\nAverage of Positive even number in upper triangle of the

secondary diagonal ="<<(float )sum/n;

Page 12: One Dimensional Array · Two Dimensional Array فٕفهن لٔلأا : حهخاري 2 loops ٗنا جارحذ( ٍٚعثنا خا حفٕفًنا اُع r دَاك ااف 1 فٕفنا

12

Find the location, number, sum and average of number 3 in upper

triangle of the secondary diagonal int sum =0,n=0;

for (i=0;i<lr;i++)

for (j=lr-1-i;j>=0;j--)

if (a[i][j]==3)

{

cout << "Number 3 is found in upper triangle of the secondary

diagonal at location: row="<<i<<", column="<<j<<"\n";

sum+=3;

n++;

}

cout <<"\nNo. of Number 3 in upper triangle of the secondary diagonal

="<<n;

cout <<"\nSum of Number 3 in upper triangle of the secondary diagonal

="<<sum;

cout <<"\nAverage Number 3 in upper triangle of the secondary diagonal

="<<(float )sum/n;

Find the smallest number in upper triangle of the secondary diagonal int s=a[0][0];

for (i=0;i<lr;i++)

for (j=lr-1-i;j>=0;j--)

if (a[i][j]<s) s=a[i][j];

cout <<"Smallest number in upper triangle of the secondary diagonal

="<<s;

Find the largest number in upper triangle of the secondary diagonal int l=a[0][0];

for (i=0;i<lr;i++)

for (j=lr-1-i;j>=0;j--)

if (a[i][j]>l) l=a[i][j];

cout <<"Largest number in upper triangle of the secondary diagonal

="<<l;

Lower Triangle of The Secondary Diagonal ذشث يظففح راخ )عاطش انثهس انعه نهقطش انشئس ي انظففح انشتعح

فارا كاد (( األل نهظفف انثا نألعذج: يرذاخهح loop 2تعذ فرحراج ان

r=4 أ 1ت 1-عذد انظفف انظف ف يحظس ت فك(i= lr . .0) حس

lr=r-1=3 فقل :for (i=lr;i>0;i--) عذد انعد نكم طف فرزاذ ي أيا

: فقل(j=(r-i) .. 3)أ 1-سقى انظف ان عذد األعذج -األعذج

for (j=r-i;j<r;j++) اr ذثم عذد انظفف أ عذد األعذج

i=2، أيا عاطش انظف انثانس j=1..3األعذج نا i=3فعاطش انظف انشاتع

j=3..3األعذج نا i=1اطش انظف انثا فا تا ع j=2..3األعذج نا

Col0 Col1 Col2 Col3

Row0 S

Row1 c

Row2 n

Row3 d

Set the value of elements in lower triangle of the secondary

diagonal to 5

for (i=lr;i>0;i--)

for (j=r-i;j<r;j++)

a[i][j]=5;

Page 13: One Dimensional Array · Two Dimensional Array فٕفهن لٔلأا : حهخاري 2 loops ٗنا جارحذ( ٍٚعثنا خا حفٕفًنا اُع r دَاك ااف 1 فٕفنا

13

Find the sum of elements in lower triangle of the secondary diagonal int sum =0;

for (i=lr;i>0;i--)

for (j=r-i;j<r;j++)

sum+=a[i][j];

cout <<"Sum of elements in lower triangle of the secondary diagonal

="<<sum;

Find the location, number, sum and average of positive even numbers in lower triangle of the secondary diagonal

int sum =0,n=0;

for (i=lr;i>0;i--)

for (j=r-i;j<r;j++)

if ((!(a[i][j]%2) && (a[i][j]>0))

{

cout << "Positive even number in lower triangle of the secondary

diagonal ="<<a[i][j]<<" at location: row="<<i<<", column="<<j<<"\n";

sum+=a[i][j];

n++;

}

cout <<"\nNo. of Positive even number in lower triangle of the secondary

diagonal ="<<n;

cout <<"\nSum of Positive even number in lower triangle of the secondary

diagonal ="<<sum;

cout <<"\nAverage of Positive even number in lower triangle of the

secondary diagonal ="<<(float )sum/n;

Find the location, number, sum and average of number 3 in lower

triangle of the secondary diagonal int sum =0,n=0;

for (i=lr;i>0;i--)

for (j=r-i;j<r;j++)

if (a[i][j]==3)

{

cout << "Number 3 is found in lower triangle of the secondary

diagonal at location: row="<<i<<", column="<<j<<"\n";

sum+=3;

n++;

}

cout <<"\nNo. of Number 3 in lower triangle of the secondary diagonal

="<<n;

cout <<"\nSum of Number 3 in lower triangle of the secondary diagonal

="<<sum;

cout <<"\nAverage Number 3 in lower triangle of the secondary diagonal

="<<(float )sum/n;

Find the smallest number in lower triangle of the secondary diagonal int s=a[r-1][r-1];

for (i=lr;i>0;i--)

for (j=r-i;j<r;j++)

if (a[i][j]<s) s=a[i][j];

cout <<"Smallest number in lower triangle of the secondary diagonal

="<<s;

Find the largest number in lower triangle of the secondary diagonal int l=a[r-1][r-1];

for (i=lr;i>0;i--)

for (j=r-i;j<r;j++)

if (a[i][j]>l) l=a[i][j];

cout <<"Largest number in lower triangle of the secondary diagonal

="<<l;

Page 14: One Dimensional Array · Two Dimensional Array فٕفهن لٔلأا : حهخاري 2 loops ٗنا جارحذ( ٍٚعثنا خا حفٕفًنا اُع r دَاك ااف 1 فٕفنا

14

Exchange حر ن كا انطهة يعكسا فثااليك اسرخذاو فس انخطاخ

Exchange 2nd row with 4th row

int z;

for (j=0;j<r;j++)

{

z=a[1][j];

a[1][j]=a[3][j];

a[3][j]=z;

}

Exchange 2nd column with 4th column

for (i=0;i<r;i++)

{

z=a[i][1];

a[i][1]=a[i][3];

a[i][3]=z;

}

Exchange main diagonal with 2nd row

for (i=0;i<r;i++)

{

z=a[i][i];

a[i][i]=a[1][i];

a[1][i]=z;

}

Exchange main diagonal with 2nd column

for (i=0;i<r;i++)

{

z=a[i][i];

a[i][i]=a[i][1];

a[i][1]=z;

}

Exchange secondary diagonal with 2nd row

for (i=0,j=r-1;i<r;i++,j--)

{

z=a[i][j];

a[i][j]=a[1][i];

a[1][i]=z;

}

Exchange secondary diagonal with 2nd column

for (i=0,j=r-1;i<r;i++,j--)

{

z=a[i][j];

a[i][j]=a[i][1];

a[i][1]=z;

}

Exchange main diagonal with secondary diagonal

for (i=0,j=r-1;i<r;i++,j--)

{

z=a[i][i];

a[i][i]=a[i][j];

a[i][j]=z;

}

Page 15: One Dimensional Array · Two Dimensional Array فٕفهن لٔلأا : حهخاري 2 loops ٗنا جارحذ( ٍٚعثنا خا حفٕفًنا اُع r دَاك ااف 1 فٕفنا

15

ذثم األعذج نهثهس jذثم انظفف نهثهس األل iنرثذم انثهثاخ عرثش

. ذثم األعذج نهثهس انثا jjفرثم انظفف نهثهس انثا iiأيا. األل

أيا فحض ششط . قارثع فس حذد انظفف األعذج نهثهثاخ حسة يا ذى ششح ساب

. فك ا عرذ عه ششط أ يثهس ي تا loopناح نكم

Exchange upper triangle of main diagonal with lower triangle of main

diagonal

int z,ii,jj,lr=r-1;

for (i=0,ii=lr;i<lr;i++,ii--)

for (j=i+1,jj=ii-1;j<r;j++,jj--)

{

z=a[i][j];

a[i][j]=a[ii][jj];

a[ii][jj]=z;

}

Exchange upper triangle of main diagonal with upper triangle of

secondary diagonal

int z,ii,jj,lr=r-1;

for (i=0,ii=0;i<lr;i++,ii++)

iألا ذسا iiتااليكا االسرغاء ع

for (j=i+1,jj=lr-1-ii;j<r;j++,jj--)

{

z=a[i][j];

a[i][j]=a[ii][jj];

a[ii][jj]=z;

}

Exchange upper triangle of main diagonal with lower triangle of

secondary diagonal

int z,ii,jj,lr=r-1;

for (i=0,ii=lr;i<lr;i++,ii--)

for (j=i+1,jj=r-ii;j<r;j++,jj++)

{

z=a[i][j];

a[i][j]=a[ii][jj];

a[ii][jj]=z;

}

Exchange lower triangle of main diagonal with upper triangle of

secondary diagonal

int z,ii,jj,lr=r-1;

for (i=lr,ii=0;i>0;i--,ii++)

for (j=i-1,jj=lr-1-ii;j>=0;j--,jj--)

{

z=a[i][j];

a[i][j]=a[ii][jj];

a[ii][jj]=z;

}

Exchange lower triangle of main diagonal with lower triangle of

secondary diagonal

int z,ii,jj,lr=r-1;

for (i=lr,ii=lr;i>0;i--,ii--)

iألا ذسا iiتااليكا االسرغاء ع

for (j=i-1,jj=r-ii;j>=0;j--,jj++)

Page 16: One Dimensional Array · Two Dimensional Array فٕفهن لٔلأا : حهخاري 2 loops ٗنا جارحذ( ٍٚعثنا خا حفٕفًنا اُع r دَاك ااف 1 فٕفنا

16

{

z=a[i][j];

a[i][j]=a[ii][jj];

a[ii][jj]=z;

}

Exchange upper triangle of secondary diagonal with lower triangle of

secondary diagonal

int z,ii,jj,lr=r-1;

for (i=0,ii=lr;i<lr;i++,ii--)

for (j=lr-1-i,jj=r-ii;j>=0;j--,jj++)

{

z=a[i][j];

a[i][j]=a[ii][jj];

a[ii][jj]=z;

}

Q) Write a program to draw the following figure, fill color is green and lines color is white

(100,100)

(500,300)

#include <graphics.h>

#include <stdlib.h>

#include <stdio.h>

#include <conio.h>

int main(void)

{

/* request auto detection */

int gdriver = DETECT, gmode, errorcode;

/* initialize graphics mode */

initgraph(&gdriver, &gmode, "");

/* read result of initialization */

errorcode = graphresult();

if (errorcode != grOk) /* an error occurred */

{

printf("Graphics error: %s\n", grapherrormsg(errorcode));

printf("Press any key to halt:"); getch();

exit(1); /* return with error code */

}

/* draw green bar with white border */

/* setfillstyle(1, 2);

bar(100, 100, 500, 300);

setcolor(15);

rectangle(100, 100, 500, 300);*/

//or

setcolor(15);

rectangle(100, 100, 500, 300);

setfillstyle(1, 2);

Page 17: One Dimensional Array · Two Dimensional Array فٕفهن لٔلأا : حهخاري 2 loops ٗنا جارحذ( ٍٚعثنا خا حفٕفًنا اُع r دَاك ااف 1 فٕفنا

17

floodfill(200,200,15);

line(300,100,300,300);

circle(300,200,30);

rectangle(100,150,160,250);

rectangle(100,170,130,230);

rectangle(500,150,440,250);

rectangle(500,170,470,230);

arc(160,200,270,90,20);

arc(440,200,90,270,20);

arc(100,100,270,360,10);

arc(500,100,180,270,10);

arc(100,300,0,90,10);

arc(500,300,90,180,10);

getch();

closegraph();

return 0;

}

Q) Draw Tanis stadd, fill color is green and lines color is white

(100,100)

(400,300)

#include <graphics.h>

#include <stdlib.h>

#include <stdio.h>

#include <conio.h>

int main(void)

{

/* request auto detection */

int gdriver = DETECT, gmode, errorcode;

/* initialize graphics mode */

initgraph(&gdriver, &gmode, "");

/* read result of initialization */

errorcode = graphresult();

if (errorcode != grOk) /* an error occurred */

{

printf("Graphics error: %s\n", grapherrormsg(errorcode));

printf("Press any key to halt:");

getch();

exit(1); /* return with error code */

}

/* draw green bar with white border */

/* setfillstyle(1, 2);

bar(100, 100, 400, 300);

setcolor(15);

rectangle(100, 100, 400, 300);*/

//or

Page 18: One Dimensional Array · Two Dimensional Array فٕفهن لٔلأا : حهخاري 2 loops ٗنا جارحذ( ٍٚعثنا خا حفٕفًنا اُع r دَاك ااف 1 فٕفنا

18

setcolor(15);

rectangle(100, 100, 400, 300);

setfillstyle(1, 2);

floodfill(200,200,15);

rectangle(100, 150, 400, 250);

rectangle(175, 150, 325, 250);

setfillstyle(1, 15);

circle(500,200,10);

floodfill(500,200,15);

bar(452,180,458,250);

setfillstyle(8, 14);

ellipse(455,160,0,360,10,20);

floodfill(455,160,15);

/* clean up */

getch();

closegraph();

return 0;

}

Q) Write a program to draw the following figure; don't forget the colors of the flag: red,

white, and black. The statement color which is written as lines is green

(200,100)

(500,250) #include <graphics.h>

#include <stdlib.h>

#include <stdio.h>

#include <conio.h>

int main(void)

{

/* request auto detection */

int gdriver = DETECT, gmode, errorcode;

/* initialize graphics mode */

initgraph(&gdriver, &gmode, "");

/* read result of initialization */

errorcode = graphresult();

if (errorcode != grOk) /* an error occurred */

{

printf("Graphics error: %s\n", grapherrormsg(errorcode));

printf("Press any key to halt:");

Page 19: One Dimensional Array · Two Dimensional Array فٕفهن لٔلأا : حهخاري 2 loops ٗنا جارحذ( ٍٚعثنا خا حفٕفًنا اُع r دَاك ااف 1 فٕفنا

19

getch();

exit(1); /* return with error code */

}

setfillstyle(1, 1);

bar(0, 0, 639, 479);

setfillstyle(1, 4);

bar(200, 100, 500, 150);

setfillstyle(1, 15);

bar(200, 150, 500, 200);

setfillstyle(1, 0);

bar(200, 200, 500, 250);

setlinestyle(0,1,3);

setcolor(2);

line(450,170,450,190);

line(440,170,440,185);

line(440,185,410,185);

line(410,185,410,170);

line(410,185,390,185);

line(390,185,390,180);

line(390,180,370,180);

line(370,180,370,185);

line(330,170,330,185);

line(320,170,300,170);

line(300,170,300,185);

line(300,185,240,185);

line(270,185,270,180);

arc(230,185,270,0,10);

line(250,190,255,190);

/* clean up */

getch();

closegraph();

return 0;

}

Q) Write a program to draw the following figure, the traffic column color is white and

don't forget the lambs color: red, yellow, and green.

(300,100)

(360, 410)

Page 20: One Dimensional Array · Two Dimensional Array فٕفهن لٔلأا : حهخاري 2 loops ٗنا جارحذ( ٍٚعثنا خا حفٕفًنا اُع r دَاك ااف 1 فٕفنا

20

#include <graphics.h>

#include <stdlib.h>

#include <stdio.h>

#include <conio.h>

int main(void)

{

/* request auto detection */

int gdriver = DETECT, gmode, errorcode;

/* initialize graphics mode */

initgraph(&gdriver, &gmode, "");

/* read result of initialization */

errorcode = graphresult();

if (errorcode != grOk) /* an error occurred */

{

printf("Graphics error: %s\n", grapherrormsg(errorcode));

printf("Press any key to halt:");

getch();

exit(1); /* return with error code */

}

setfillstyle(1, 15);

bar(300, 100, 400, 260);

bar(340, 260, 360, 410);

setcolor(4);

circle(350, 130,20);

setfillstyle(1, 4);

floodfill(350,130,4);

setcolor(14);

circle(350, 180,20);

setfillstyle(1, 14);

floodfill(350,180,14);

setcolor(2);

circle(350, 230,20);

setfillstyle(1, 2);

floodfill(350,230,2);

/* clean up */

getch();

closegraph();

return 0;

}

Q) Write a program to draw the following figure, fill color is green and lines color is black

(100,100)

(500,300)

Page 21: One Dimensional Array · Two Dimensional Array فٕفهن لٔلأا : حهخاري 2 loops ٗنا جارحذ( ٍٚعثنا خا حفٕفًنا اُع r دَاك ااف 1 فٕفنا

21

#include <graphics.h>

#include <stdlib.h>

#include <stdio.h>

#include <conio.h>

int main(void)

{

/* request auto detection */

int gdriver = DETECT, gmode, errorcode;

/* initialize graphics mode */

initgraph(&gdriver, &gmode, "");

/* read result of initialization */

errorcode = graphresult();

if (errorcode != grOk) /* an error occurred */

{

printf("Graphics error: %s\n", grapherrormsg(errorcode));

printf("Press any key to halt:");

getch();

exit(1); /* return with error code */

}

setfillstyle(1, 15);

bar(0, 0, 639, 479);

setfillstyle(1, 2);

bar(100, 100, 500, 300);

setcolor(0);

rectangle(100, 100, 500, 300);

line(300,100,300,300);

circle(300,200,30);

arc(100,200,270,90,80);

arc(500,200,90,270,80);

circle(120,200,15);

circle(480,200,15);

line(105,180,115,220);

line(495,180,485,220);

line(160,180,160,220);

line(440,180,440,220);

/* clean up */

getch();

closegraph();

return 0;

}

Q) Write a program to draw the red car

(300,220)

(200,270)

(500,350)

Black color, and the insider color is white

Page 22: One Dimensional Array · Two Dimensional Array فٕفهن لٔلأا : حهخاري 2 loops ٗنا جارحذ( ٍٚعثنا خا حفٕفًنا اُع r دَاك ااف 1 فٕفنا

22

#include <graphics.h>

#include <stdlib.h>

#include <stdio.h>

#include <conio.h>

int main(void)

{

/* request auto detection */

int gdriver = DETECT, gmode, errorcode;

/* initialize graphics mode */

initgraph(&gdriver, &gmode, "");

/* read result of initialization */

errorcode = graphresult();

if (errorcode != grOk) /* an error occurred */

{

printf("Graphics error: %s\n", grapherrormsg(errorcode));

printf("Press any key to halt:");

getch();

exit(1); /* return with error code */

}

setfillstyle(1, 15);

bar(0, 0, 639, 479);

setfillstyle(1, 4);

bar(200, 270, 500, 350);

setcolor(0);

circle(300, 350,20);

circle(400, 350,20);

setfillstyle(1, 0);

floodfill(300,350,0);

floodfill(400,350,0);

setcolor(15);

circle(300, 350,10);

circle(400, 350,10);

setfillstyle(1, 15);

floodfill(300,350,15);

floodfill(400,350,15);

setcolor(0);

line(300,270,300,220);

line(300,220,400,220);

line(400,220,400,270);

line(350,220,350,350);

/* clean up */

getch();

closegraph();

return 0;

}

Page 23: One Dimensional Array · Two Dimensional Array فٕفهن لٔلأا : حهخاري 2 loops ٗنا جارحذ( ٍٚعثنا خا حفٕفًنا اُع r دَاك ااف 1 فٕفنا

23

Q) Write a program to draw the yellow face

(300,300) and R=100

Red mouth #include <graphics.h>

#include <stdlib.h>

#include <stdio.h>

#include <conio.h>

int main(void)

{

/* request auto detection */

int gdriver = DETECT, gmode, errorcode;

/* initialize graphics mode */

initgraph(&gdriver, &gmode, "");

/* read result of initialization */

errorcode = graphresult();

if (errorcode != grOk) /* an error occurred */

{

printf("Graphics error: %s\n", grapherrormsg(errorcode));

printf("Press any key to halt:");

getch();

exit(1); /* return with error code */

}

setcolor(15);

circle(300, 300,100);

setfillstyle(1, 14);

floodfill(300,300,15);

setcolor(0);

circle(250, 250,10);

setfillstyle(1, 0);

floodfill(250,250,0);

circle(350, 250,10);

floodfill(350,250,0);

line(300,290,290,310);

line(290,310,310,310);

line(310,310,300,290);

setcolor(4);

arc(300,350,0,180,20);

line(320,350,280,350);

setfillstyle(1,4);

floodfill(300,340,4);

/* clean up */

getch();

closegraph();

return 0;

}

Page 24: One Dimensional Array · Two Dimensional Array فٕفهن لٔلأا : حهخاري 2 loops ٗنا جارحذ( ٍٚعثنا خا حفٕفًنا اُع r دَاك ااف 1 فٕفنا

24

Q) Write a program to draw the following figure

(200,200) yellow color

Green color (500,400)

#include <graphics.h>

#include <stdlib.h>

#include <stdio.h>

#include <conio.h>

int main(void)

{

/* request auto detection */

int gdriver = DETECT, gmode, errorcode;

/* initialize graphics mode */

initgraph(&gdriver, &gmode, "");

/* read result of initialization */

errorcode = graphresult();

if (errorcode != grOk) /* an error occurred */

{

printf("Graphics error: %s\n", grapherrormsg(errorcode));

printf("Press any key to halt:");

getch();

exit(1); /* return with error code */

}

setfillstyle(1, 15);

bar(0,0,639,479);

setcolor(2);

arc(350,200,0,180,50);

setfillstyle(1, 2);

bar(200,200,500,400);

floodfill(350,180,2);

setcolor(0);

rectangle(200,200,500,400);

rectangle(300,300,400,400);

setfillstyle(1, 14);

floodfill(350,350,0);

line(350,300,350,400);

setfillstyle(1, 14);

bar(230,100,250,199);

setcolor(14);

circle(240, 90,10);

setfillstyle(1, 14);

floodfill(240,90,14);

line(350,150,350,100);

arc(350,90,180,0,10);

/* clean up */

getch();

closegraph();

return 0;

}