Professional Documents
Culture Documents
Lab
Semester BS (CS) – 01
Yahya Faisal
02-134211-006
Lab 9
Exercise 1
1) Write a C++ program that ask a user to enter 10 integer values. Store those values in one dimensional
array. Create another one dimensional array of same size, and store the values of first array in reverse
order. Print the result on screen.
#include <iostream>
using namespace std;
void main()
{
int array1[10];
int array2[10];
for (int i = 0; i <= 9; i++){
cout << "Enter value " << i + 1 << " of the array : ";
cin >> array1[i];
}
cout << "=======================================================\n";
cout << "\t Matrix A - Original\n";
cout << "=======================================================\n";
for (int i = 0; i <= 9; i++){
cout << array1[i] << " ";
}
cout << "\n=======================================================\n";
cout << "\t Matrix A - Reverse\n";
cout << "=======================================================\n";
for (int i = 9; i >= 0; i--){
array2[i] = array1[i];
cout << array2[i] << " ";
}
cout << "\n";
system("pause");
}
#include <iostream>
using namespace std;
void main()
{
int array1[7];
int array2[7];
for (int i = 0; i <= 6; i++){
cout << "Enter the values of First array: ";
cin >> array1[i];
}
for (int j = 0; j <= 6; j++){
cout << "Enter the values of Second array: ";
cin >> array2[j];
}
for (int i = 0; i <= 6; i++){
cout << " Element " << i + 1 << " of 1st Array is : " << array1[i] << "\n";
}
for (int i = 0; i <= 6; i++){
cout << " Element " << i + 1 << " of 2nd Array is : " << array2[i] << "\n";
}
for (int i = 0; i <= 6; i++){
if (array1[i] == array2[i]){
cout << " Element " << i + 1 << " of both array is equal\n";
}
else{
cout << " Element " << i + 1 << " of both array is Not equal\n";
}
}
system("pause");
}
int main()
{
int arr[5], sum = 0, avg;
cout << "Enter Number of elements you want to enter : ";
for (int i = 0; i < 5; i++) {
cin >> arr[i];
sum = sum + arr[i];
}
for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
avg = sum / 5;
cout << "\nSum = " << sum << "\nAverage = " << avg << "\n";
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int arr[5], first, last;
cout << "Enter 5 values for array : \n";
for (int i = 0; i < 5; i++)
{
cin >> arr[i];
}
cout << "\n Original Array \n";
for (int i = 0; i < 5; i++)
{
cout << arr[i] << " ";
}
first = arr[0];
last = arr[4];
arr[0] = last;
arr[4] = first;
cout << "\n Array values after swap \n";
for (int i = 0; i < 5; i++)
{
cout << arr[i] << " ";
}
cout << "\n";
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int arr[5];
cout << "Enter number of values : ";
for (int i = 0; i < 5; i++)
{
cin >> arr[i];
}
cout << "==============================================" << endl;
cout << "\tReverse Array\n";
cout << "==============================================" << endl;
for (int j = 4; j >= 0; j--)
{
cout << arr[j] << " ";
}
cout << endl;
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int array[5], n, i, max, min;
cout << "Enter the values of the array : ";
for (i = 0; i < 5; i++) {
cin >> array[i];
}
max = array[0];
for (i = 0; i < 5; i++) {
if (max < array[i]) {
max = array[i];
}
}
min = array[0];
for (i = 0; i < 5; i++) {
if (min > array[i]) {
min = array[i];
}
}