Programming Exercise Looping Structure P-2

Programming Exercise Looping Structure P-1

Conditional Structure Programming Exercise P-6

Program 8:
Write a program that inputs values into two linear arrays. It adds the two arrays and stores the sum in a third array. Further, it displays values of three arrays as follows:
// First + Second = Sum

#include<iostream.h>
#include<conio.h>
main()
{
int x[5], y[5], s[5], i;
clrscr();
cout<<"Enter five values in first array"<<endl;
for(i=0; i<=4; i++) 
cin>>x[i];
cout<<"Enter five values in second array\n";
for(i=0; i<=4; i++) 
cin>>y[i];
cout<<"First + Second = Sum"<<endl;
for(i=0; i<=4; i++) 
{
s[i] = x[i] + y[i];
cout<<x[i]<<"+"<<y[i]<<" = "<<s[i]<<endl;
}
getch(); 
}

Program 9:
Write a program that inputs values into one-dimensional array of ten elements. It reads the values of array elements one by one and draws a bar chart or histogram. For example, if first and second element of the array have values 19 and 8 respectively then the output should be as:
// Element Value Histogram
// 0. 19 *******************
// 1. 8 ********
//The entered value for each element should be between 1 to 30.

#include<iostream.h>
#include<conio.h>
main()
{
int arr[10],i=0;
clrscr();
while(i<=9)
{
cout<<"Value for element "<<i<<" ? ";
cin>>arr[i];
if(arr[i]>30 || arr[i]<=0) continue;
i++;
}
i = 0;
cout<<"\nElement\tValue\tHistogram\n";
while(i<=9)
{
cout<<i<<"\t"<<arr[i]<<"\t";
for(int c=1; c<=arr[i]; c++)
cout<<"*";
cout<<endl;
i++;
}
getch();
}

Program 10:
Write a program that initializes the values in one-dimensional array and computes the sum of even values assigned to the array. It also displays even values and their sum on the screen.

#include<iostream.h>
#include<conio.h>
main()
{
int arr[10]={14,16,34,43,77,34,21,12,33,30};
int i, sum;
i = sum = 0;
clrscr();
while(i<=9)
{
if(arr[i]%2==0)
{
cout<<arr[i]<<endl;
sum = sum + arr[i];
}
i++;
}
cout<<"\nSum of Even Values : "<<sum;
getch();
}

Program 11:
Write a program that initializes values 2, 6, 3, 5, 4 and 7 in one-dimensional array. It computes the factorial of value of each element of the array and displays the result on the screen.

#include<iostream.h>
#include<conio.h>
main()
{
long arr[] = {2,6,3,5,4,7},i = 0, f, n;
clrscr();
while(i<=5)
{
f = 1;
n = arr[i];
for(; n>=1; n--)
f = f * n;
cout<<"Factorial of "<<arr[i]<<": "<<f<<endl;
i++;
}
getch();
}

Program 12:
Write a program in C++ that inputs data into an array. It inputs a value from the user and searches this value in the array using sequential search. If the entered value is found in the array, it displays the position of the value in the array; otherwise, it displays the message “Value not found”.

#include<iostream.h>
#include<conio.h>
main()
{
int arr[5], n, i, pos;
clrscr();
i = 0;
while(i<=4)
{
cout<<"Enter value in element "<<i<<"? "; cin>>arr[i];
i++;
}
pos = 0;
cout<<"Enter any value ? ";
cin>>n;
i = 0;
while(i<=4)
{
if(n == arr[i])
{
pos = i + 1;
break;
}
i++;
}
if(pos == 0)
cout<<"Value not found "<<endl;
else
cout<<"Value found at position = "<<pos<<endl;
getch();
}

Program 13:
Write a program to enter data into one-dimensional array. Find out the maximum value and its location in the array and print the result on the screen.

#include<iostream.h>
#include<conio.h>
main()
{
float arr[10], max;
int loc, i = 0;
clrscr();
while(i<=9)
{
cout<<"Value for element "<<i<<" ? ";
cin>>arr[i];
i++;
}
max = arr[0];
i = 1;
while(i<=9)
{
if(max<arr[i])
{
max = arr[i];
loc = i;
}
i++;
}
cout<<"\nMaximum value is :"<<max<<endl;
cout<<"Location of value in array :"<<loc+1;
getch();
}

Leave a Reply