# Programming Exercise

Programming Exercise Looping Structure P-12

Conditional Structure Programming Exercise P-8

#### Program 1:Write a program that inputs values into the individual elements of an array during program execution and displays the values of the array. No loop statement should be used in the program.

```#include<iostream>using namespace std;
main()
{
int temp;
cout<<"Enter value in 1st element of temp. ? ";
cin>>temp;
cout<<"Enter value in 2nd element of temp. ? ";
cin>>temp;
cout<<"Enter value in 3rd element of temp. ? ";
cin>>temp;
cout<<"Enter value in 4th element of temp. ? ";
cin>>temp;
cout<<"Enter value in 5th element of temp. ? ";
cin>>temp;
cout<<"Values in array temp are:"<<endl;
cout<<temp<<endl;
cout<<temp<<endl;
cout<<temp<<endl;
cout<<temp<<endl;
cout<<temp<<endl;
}```

#### Program 2:Write a program that assigns values into the individual elements of an array and displays the values of the array.

```#include<iostream>using namespace std; main()
{
int temp, i;
temp = 2;
temp = 20;
temp = 15;
temp = 254;
temp = 5;

for(i = 0; i<=4; i++)
cout<<"Value in temp["<<i<<"] = "<<temp[i]<<endl;

}```

#### Program 3:Write a program that assigns value 50 to all the elements of an array “abc” and then displays these values on the screen.

```#include<iostream>using namespace std;
main()
{
int abc, i;

// Assign values to array “abc”
for(i = 0; i <= 14; i++)
abc[i] = 50;

// Display contents of array “abc”
for(i = 0; i<=14; i++)
cout << abc[i] << endl;

}```

#### Program 4:Write a program that gets 5 integer values into an array and then displays the values of the elements of array in reverse order.

```#include<iostream>using namespace std;
main()
{
int arr, i;

for(i=0; i<=4; i++)
{
cout<<"Enter value in element "<<i<<" ? ";
cin>>arr[i];
}
cout<<"Output in reverse order"<<endl;
for(i=4; i>=0; i--)
{
cout<<arr[i]<<endl;
}

}```

#### Program 5:Write a program that inputs temperatures for seven days of the week into a one-dimensional array “temp”. It computes the average temperature and displays the values of array and calculated average temperature on the screen.

```#include<iostream>using namespace std;
main()
{
float temp, sum = 0;
int i=0;

while(i<=6)
{
cout<<"Enter temp. of day "<<i+1<<" ? ";
cin>>temp[i];
sum = sum + temp[i];
i++;
}
cout<<"\nAverage temperature : "<<sum/7.0;

}```

#### Program 6:Write a program that inputs values into one-dimensional array and finds out the total number of odd and even values entered into one-dimensional array.

```#include<iostream>using namespace std;
main()
{
int arr, i, odd, even;
i = odd = even = 0;

while(i<=9)
{
cout<<"Enter value into element "<<i<<"?";
cin>>arr[i];
(arr[i]%2==0)? even++ : odd++;
i++;
}
cout<<"\nEven Values : "<<even;
cout<<"\nOdd Values : "<<odd;

}```

#### Program 7:Develop a program that inputs ‘n’ values in a list and prints the product of the value and the position at which it resides in the list, i.e. product L(1)*1, product L(2)*2, and so on. For example:// Position 1 2 3 4 5// Value 5 3 7 1 4// Output 5 6 21 4 20// Note: The list does not change.

```#include<iostream>using namespace std;
main()
{
int arr, i;
i = 0;

while(i<=4)
{
cout<<"Enter value into element "<<i<<"?";
cin>>arr[i];
i++;
}
cout<<"\n products of the values and the positions: "<<endl;
for(i = 0; i<=4; i++)
cout<<arr[i]<<"*"<<i+1<<" = "<< arr[i]*(i+1)<<endl;

}```

#### 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>using namespace std;
main()
{
int x, y, s, i;

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;
}

}```

#### 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>using namespace std;
main()
{
int arr,i=0;

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++;
}

}```