Professional Documents
Culture Documents
Example
int abc [5]; // array name abc of size 5
abc[0]=2;
abc[1]=4;
float b[3];
#include<iostream>
using namespace std;
int main ()
{
abc[0] 1
abc[1] 2
abc[2] 3
abc[3] 4
abc[4] 5
abc[0] 1
abc[1] 2
abc[2] 3
abc[3] 4
abc[4] 5
abc[i] Value
abc[0] 1
abc[1] 2
abc[2] 3
abc[3] 4
abc[4] 5
abc[5] i=3;
abc[0] 1 Sum=sum+abc[3];
Sum=5+4=9
abc[1] 2
i=2;
abc[2] 3 sum-=sum+abc[2];
Sum=9+3=12
abc[3] 4
i=1;
sum-=sum+abc[1];
abc[4] 5 Sum=12+2=14
i=0;
Sum=sum+abc[0];
Sum=14+1=15
Syed M Hamedoon (Assistant Professor)
Program 4.4
Write a program in C++ to find out and print
the maximum value in the array
#include<iostream>
using namespace std;
main ()
{
float abc[5],max;
int i;
max=abc[i]; abc[1] 4
} abc[2] 8
abc[3] 9
Program 4.4
(Understanding)
abc[4] 1
for(i=0;i<=4;i++)
{
cout<<“enter the value in element”<<i<<“=“;
cin>>abc[i];
}
Syed M Hamedoon (Assistant Professor)
p=0;
cout<<“Enter any integer value?”; abc[0] 6
cin>>n;
for(i=0;i<=4;i+) abc[1] 4
{
if(n==abc[i]) abc[2] 8
{
p=i+1; abc[3] 9
break;
} abc[4] 1
}
if (p==0)
cout<<“Number not found”;
else
cout<<“Number found at position =“<<p;
}
for(i=0;i<=4;i+)
abc[1] 4 {
if(n==abc[i])
abc[2] 8 {
p=i+1;
break;
abc[3] 9
}
abc[4] 1
temp[2] 99.2
temp[3] 63.9
temp[4] 59.3
char second[7]
char first[7]
second [0] M
second [1] A
second [2] R
second [3] R
second [4] I
second [5] A
second [6] M
str1[0] H
str1[1] E
str1[2] L
str1[3] L
str1[4] O
str1[0] H str2[0] H
str1[1] E str2[1] E
str1[2] L str2[2] L
str1[3] L str2[3] L
str1[4] O str2[4] O
for(u=3;u>=1;u--)
for(i=0;i<u;i++)
4 1 3 19 if (ARR[i]>ARR[i+1]
{
1 4 3 19 t=ARR[i];
ARR[i]=ARR[i+1];
ARR[i+1]=t;
1 3 4 19
}
1 3 4 19
for(u=3;u>=1;u--)
1 3 4 19
for(i=0;i<u;i++)
if (ARR[i]>ARR[i+1]
1 3 4 19 {
t=ARR[i];
ARR[i]=ARR[i+1];
ARR[i+1]=t;
}
4 19 1 13
1 19 4 13
Iteration 1
Find out the smallest value from the list starting from first element to
the last element of the list. The smallest value is 1 in location 3.
interchange the value of the first element with the third element i.e
swap values of ARR[0] and ARR[2]
1 4 19 13
• Iteration 2
Find out the smallest value from the list starting
from the second element to the last element of
the list. The smallest value is 4 at location 3.
interchange the value of the first element with
the third element i.e swap values of ARR[1] and
ARR[2]
Syed M Hamedoon (Assistant Professor)
1 4 19 13
1 4 13 19
• Iteration 3
Find out the smallest value from the list starting
from the third element to the last element of the
list. The smallest value is 13 in location 4.
interchange the value of the first element with
the third element i.e swap values of ARR[2] and
ARR[3]
4 19 1 13