# Programming Exercise

Array Programming Exercise P-3

Conditional Structure Programming Exercise P-8

#### Q.1 Write a program that inputs ten values in an array and calculates the sum of these values.

```#include<iostream.h>
#include<conio.h>
main()
{
int ar, s = 0;
clrscr();
for(int i=0; i<=9; i++)
{
cout<<"Enter value in element "<<i<<" ? ";
cin>>ar[i];
s = s + ar[i];
}
cout<<"Sum of array is : "<<s<<endl;
getch();
}```

#### Q.2 Write a program that inputs real values into float type array. It reads the value of each element of float type array and assigns to int type array. It also displays the value of int type array.

```#include<iostream.h>
#include<conio.h>
main()
{
float a;
int b;
clrscr();
for(int i=0; i<=4; i++)
{
cout<<"Enter real value in element "<<i<<" ? ";
cin>>a[i];
b[i] = a[i];
}

for(i=0; i<=4; i++)
cout<<"b["<<i<<"] ="<<b[i]<<endl;
getch();
}```

#### Q.3 Write a program using two arrays and enter values in 1st array. The program should copy the values of the 1st array to 2nd array and display the 2nd array in the reverse order.

```#include<iostream.h>
#include<conio.h>
main()
{
int a, b, i;
clrscr();
for(i = 0; i<=4; i++)
{
cout<<"Enter value in element "<<i<<" of 1st array? ";
cin>>a[i];
b[i] = a[i];
}

for(i = 4; i>=0; i--)
cout<<"b["<<i<<"] ="<<b[i]<<endl;
getch();
}```

#### Q.4 Write a program to input an array of 10 elements from user and count the number of odd numbers and display it.

```#include<iostream.h>
#include<conio.h>
main()
{
int ar, odd = 0;
clrscr();
for(int i=0; i<=9; i++)
{
cout<<"Enter value in element "<<i<<" ? ";
cin>>ar[i];
if (ar[i]%2 == 1)
odd = odd + 1;
}
cout<<"Number of odd values in array are : "<<odd<<endl;
getch();
}```

#### Q.5 Write a program that inputs different values into an array and finds out the smallest value and largest value from it.

```#include<iostream.h>
#include<conio.h>
main()
{
int ar, smallest, largest, i;
clrscr();
for(i=0; i<=9; i++)
{
cout<<"Enter value in element "<<i<<" ? ";
cin>>ar[i];
}
smallest = largest = ar;
for(i=1; i<=9; i++)
{
if(smallest > ar[i])
smallest = ar[i];
if(largest < ar[i])
largest = ar[i];

}
cout<<"Largest value in array is : "<<largest<<endl;
cout<<"Smallest value in array is : "<<smallest<<endl;
getch();
}```

#### Q.6 Write a program that inputs 10 numbers into an array and finds out the second largest value from that array.

```#include<iostream.h>
#include<conio.h>
main()
{
int ar, largest, sec_largest, i;
clrscr();
for(i=0; i<=9; i++)
{
cout<<"Enter value in element "<<i<<" ? ";
cin>>ar[i];
}
sec_largest = largest = ar;
for(i = 1; i<=9; i++)
{
if(largest < ar[i])
largest = ar[i];
if(sec_largest < ar[i] && largest > ar[i])
sec_largest = ar[i];
}
cout<<"Second Largest value in array is : "<<sec_largest<<endl;
getch();
}```

#### Q.7 Write a program to enter the temperature of a week and determine the maximum temperature, minimum temperature and average temperature.

```#include<iostream.h>
#include<conio.h>
main()
{
float temp, max, mini, avg, i, s = 0;
clrscr();
for(i=0; i<=6; i++)
{
cout<<"Enter temperature of the day "<<i+1<<" ? ";
cin>>temp[i];
s = s + temp[i];
}
avg = s/7;
max = mini = temp;
for(i = 1; i<=6; i++)
{
if(temp[i] > max)
max = temp[i];
if(temp[i] < mini)
mini = temp[i];
}
cout<<"Maximum temperature is : "<<max<<endl;
cout<<"Minimum temperature is : "<<mini<<endl;
cout<<"Average temperature is : "<<avg<<endl;
getch();
}```

#### Q.8 Write a program that inputs ten values into an array. It finds out the average value of the array and displays the values of array which are greater than the average value of array.

```#include<iostream.h>
#include<conio.h>
main()
{
float arr, avg, i, s = 0;
clrscr();
for(i=0; i<=9; i++)
{
cout<<"Enter value no. "<<i+1<<" ? ";
cin>>arr[i];
s = s + arr[i];
}
avg = s/10;
cout<<"Values of array that are greater than averages value :"<<avg<<endl;
for(i = 0; i<=9; i++)
{
if(arr[i]>avg)
cout<<arr[i]<<endl;
}
getch();
}```

#### Q.9 Write a program that inputs values into int type array. It reads the value from array and draws a bar chart by printing asterisks.

```#include<iostream.h>
#include<conio.h>
main()
{
int arr, i, j;
clrscr();
for(i=0; i<=9; i++)
{
cout<<"Enter value no. "<<i+1<<" ? ";
cin>>arr[i];
}

for(i = 0; i<=9; i++)
{
for(j = 1; j<=arr[i]; j++)
cout<<'*';
cout<<arr[i]<<endl;
}
getch();
}```

#### Q.10 Write a program that initializes an array with the number of days of different months (i.e. months= {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}). It inputs the number of month and displays the number of days of the entered month. Suppose user inputs 2, program displays “number of days of this month are 28”.

```#include<iostream.h>
#include<conio.h>
main()
{
int months= {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, i, n;
clrscr();
cout<<"Enter number of month [1-12] ? ";
cin>>n;
if (n>=1 && n<=12)
cout<<"The number of days of the entered month are : "<<months[n-1];
else
cout<<"Invalid month number ";
getch();
}```