CP Lab 8

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Computer Programming

Lab Manual

Department of Computer Science - BUIC

Rabail Zahid
Lecturer (CS)
Team Lead:
Dr. Momina Moetesum
Adapted from the previous content of:
Dr. Ghulam Ali Mirza
November, 2022
Exercises/Lab Journal 8:
Exercises/Lab Journal 8:
1. Declare an array of 4 by 5 size of integers and get user input to fill the array values. Then,
find the minimum values in the arrays.

SOLUTUION:
#include<iostream>
using namespace std;
int main() {
int arr[4][5];
cout << "Enter th array elements: ";
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 5; j++) {
cin >> arr[i][j];
}
}
int min = arr[0][0];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 5; j++) {
if (min > arr[i][j]) {
min = arr[i][j];
}
}
}
cout << "The smallest element is: " << min;
}

2. Write a program to input value in an array of type integer and size 6 by 7. Find out the
total number of even and odd values entered in the array.

SOLUTUION:
#include<iostream>
using namespace std;
int main() {
int arr[6][7],even=0,odd=0;
cout << "Enter th array elements: ";
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
cin >> arr[i][j];
}
}
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
if (arr[i][j]%2==0) {
even++;
}
else {
odd++;
}
}
}
cout << "\nEven elements: " << even;
cout << "\nOdd elements: " << odd;
}

3. Write a C++ program that declares an array of size 5 by 20 and assign values to it. Then
it asks the user “Enter the number to search in this array”. Then search the number
entered by user in the array and display the index on which it was found. (This task is
similar to exercise 1.)

SOLUTUION:

#include<iostream>
using namespace std;
int main() {
int arr[5][20], number;
cout << "Enter the array elements: ";
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 20; j++) {
cin >> arr[i][j];
}
}
cout << "Enter the number to be searched in the array: ";
cin >> number;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 20; j++) {
if (arr[i][j] == number) {
cout << "The number " << number << " is at
row " << i << " column " << j;
}

}
}
}

You might also like