0% found this document useful (0 votes)
2 views

Lecture6 and 7 m

The document is a lecture outline for a C++ programming course at Ninevah University, focusing on nested loops and one-dimensional arrays. It includes syntax examples for nested for, while, and do-while loops, as well as various programming exercises. Additionally, it covers rules for declaring arrays and provides examples of array manipulation, such as finding maximum values and swapping elements.

Uploaded by

Stive Brack
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture6 and 7 m

The document is a lecture outline for a C++ programming course at Ninevah University, focusing on nested loops and one-dimensional arrays. It includes syntax examples for nested for, while, and do-while loops, as well as various programming exercises. Additionally, it covers rules for declaring arrays and provides examples of array manipulation, such as finding maximum values and swapping elements.

Uploaded by

Stive Brack
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

C++ Programming

Ninevah University
College of Electronics Engineering
Department of Electronic Engineering
Medical Instrumentation

2nd Year
2024 – 2025

Lecturer
Prof Dr. Qais Thanon

Lecture #6 and #7

All the lectures of this course will upload at the


Google classroom

11/9/2024
C + + N E S T E D L O O P S

A loop can be nested inside of another loop. C++ allows at least 256
levels of nesting
Syntax:
The syntax for a nested for loop statement in C++ is as follows:

for ( init; condition; update )


{
for (init; condition; update )
{
statement(s);
}
statement(s); // you can put more
}

2
11/9/2024
Example:
What do you think the output of the following program would be

#include <iostream.h>
void main (){
@ @ @
int R = 5, C = 3, i, j;
@ @ @
for(i=0; i < R; i++) { @ @ @
for(j=0; j < C; j++){ @ @ @
@ @ @
cout << “@”<<“\t”;
}
cout << “\n”;}
}
3
11/9/2024
Example:
What do you think the output of the following program would be

#include <iostream.h>
void main (){
int R = 5, C = 3, i, j, z=0; 0 1 2
3 4 5
for(i=0; i < R; i++) {
6 7 8
for(j=0; j < C; j++){ 9 10 11
cout << z++ <<“\t”; 12 13 14
}
cout << “\n”;}
}
4
11/9/2024
C + + N E S T E D L O O P S

Syntax:
The syntax for a nested while loop statement in C++ is as follows:

init1;
while (condition1)
{
init2;
while (condition2)
{
statement(s);
update2;
}
update1;
statement(s); // you can put more
}

5
11/9/2024
Example: Write a program to print half pyramid of numbers as
shown :

#include<iostream.h>
void main(){
1
int N, M, Z=1;
for (N=1;N<=5; N++){ 2 3
4 5 6
for (M=1; M <= N; M++) 7 8 9 10
{
11 12 13 14 15
cout << Z++ << “ “;
}
cout << “\n”;
}
}

6
11/9/2024
Example: Write a program to print half pyramid of numbers as
shown using while statement:

#include<iostream.h>
void main(){
100
int N=100, M;
while (N<=500){ 100 200
M = 100; 100 200 300
while (M <= N) 100 200 300 400
{
100 200 300 400 500
cout << M << “ “;
M+=100;
}
cout << “\n”;
N+=100;
}
}

7
11/9/2024
Example: Write a program to print the main diagonal of 5x5
array:

#include<iostream.h>
void main(){
int N, M, Z=1; 11
for (N=1;N<=5; N++){ 22
33
for (M=1; M <= 5; M++) {
44
if (N==M) cout << N<< M<< “\t“;
} 55
cout << “\n”;
}
}

8
11/9/2024
C + + N E S T E D L O O P S

Syntax:
The syntax for a nested do-while loop statement in C++ is as follows:

init1;
do{
init2;
do{
statement(s);
update2;
} while (condition2);
update1;
statement(s);

} while (condition1);

9
11/9/2024
Example: Write program to print half pyramid of numbers as shown :

#include<iostream.h>
void main(){
int N = 1, M, K;
do{
M = 1;
do{
cout << “ “;
M++;
} while (M <= 5-N);
K = 1;
do{
cout << N;
K++;
} while (K <= N);
N++;
} while (N <= 5);
} 10
11/9/2024
Write a program in C++ to print the Floyd's Triangle
0
#include <iostream.h> 1 0
0 1 0
void main (){
1 0 1 0
int R = 5, C = 5, i, j, z = 0; 0 1 0 1 0
for(i=0; i < R; i++) {
i j
for(j=0; j < C; j++){
C
cout << z <<“\t”; 00 01 02 03 04
10 11 12 13 14
z = !z;
R 20 21 22 23 24
}
30 31 32 33 34
cout << “\n”;} 40 41 42 43 44
}
11
11/9/2024
One diamention ARRAY

Rules for Declaring One Dimensional Array


 An array variable must be declared before being used in a program.
 The declaration must have a data type(int, float, char, double, etc.),
variable name, and subscript.
 The subscript represents the size of the array. ...
 An array index always starts from 0.

int c[ 5 ];

12
11/9/2024
Example: Write a C++ Program to define one dimension array with 5
integer elements and print them?
#include<iostream.h>
#include<conio.h>
void main(){
int a[5], i;
clrscr();
cout<<"Enter any 5 num in array: \n";

for(i=0;i<5;i++){ Loop for array elements


cin>>a[i]; reading
}

for(i=0;i<5;i++) { Loop for array elements


cout << a[i] << “\t”; printing
}
getch();
} 13
11/9/2024
Example: Write a C++ Program to find the average of even numbers in one
dimension N elements array?

(sum *1.0/E_N);

14
11/9/2024
Example: Write a C++ Program to find the max. even number in one
dimension array?
111
#include<iostream.h>
#include<conio.h>
void main(){
int i , a[5], max;
clrscr();
cout<<"Enter any 5 num in array: \n";
for(i=0;i<5;i++) cin>>a[i];
max = a[0];
for(i=1;i<5;i++) {
if(a[i]%2 ==0 && a[i]>max ) max = a[i];
}
cout<<"\n The max number is:“<< max;
getch();
}

15
11/9/2024
Example: Write a C++ Program to find the max. number and its
position in one dimension array?
#include<iostream.h>
#include<conio.h>
void main(){
int i , a[5], max, ind;
clrscr();
cout<<"Enter any 5 num in array: \n";
for(i=0;i<5;i++) cin>>a[i];
max = a[0]; ind=0;
for(i=1;i<5;i++) {
if(a[i]>max) { max = a[i]; ind=i;}
}

cout << max <<“\t”<< ind;


getch();
}

16
11/9/2024
Example: Write a C++ Program to find the max. number and min
number and swap there positions in one dimension array?
#include<iostream.h>
#include<conio.h>
void main(){
int i , a[5], max, min, ind1, ind2;
clrscr();
cout<<"Enter any 5 num in array: \n";
for(i=0;i<5;i++) cin>>a[i];
max = a[0]; ind1=0;
min = a[0]; ind2=0
for(i=1;i<5;i++) {
if(a[i]>max) { max = a[i]; ind1=i;}
if(a[i]<min) { min = a[i]; ind2=i;}
}
a[ind2] = max; a[ind1]=min;
for(i=0;i<5;i++) cin>>a[i];
getch();
} 17
11/9/2024

You might also like