You are on page 1of 8

CSL-113: Computer Programming

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

Department of Computer Sciences Semester BSCS1


CSL-113: Computer Programming Lab Journal
Exercise 2
2) Write a C++ program that checks whether the two arrays are equal or not. Declare two Integer Arrays with
7 elements, and fill up the array with keyboard input. Test if every element in Array 1 is equal to
corresponding Array 2.

#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");
}

Department of Computer Sciences Semester BSCS1


CSL-113: Computer Programming Lab Journal
Department of Computer Sciences Semester BSCS1
CSL-113: Computer Programming Lab Journal
Exercise 3
3) (a). Write a C++ program to find the sum and average of one-dimensional integer array.
#include <iostream>
#include <string>
using namespace std;

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

Department of Computer Sciences Semester BSCS1


CSL-113: Computer Programming Lab Journal
b). Write a C++ program to swap first and last element of an integer 1-d array. 

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

Department of Computer Sciences Semester BSCS1


CSL-113: Computer Programming Lab Journal
(c). Write a C++ program to reverse the element of an integer 1-D array. 

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

Department of Computer Sciences Semester BSCS1


CSL-113: Computer Programming Lab Journal
(d). Write a C++ program to find the largest and smallest element of an array. 

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

Department of Computer Sciences Semester BSCS1


CSL-113: Computer Programming Lab Journal
cout << "largest value in the array : " << max << endl;
cout << "Smallest value in the array : " << min << endl;
system("pause");
return 0;
}

Department of Computer Sciences Semester BSCS1


CSL-113: Computer Programming Lab Journal

You might also like