0% found this document useful (0 votes)
42 views

CP Lab 8

This document contains a computer programming lab manual from the Department of Computer Science at BUIC. It provides solutions to 3 exercises involving 2D arrays in C++. The first exercise declares a 4x5 integer array, gets user input to fill it, and finds the minimum value. The second tracks even and odd values in a 6x7 integer array. The third declares a 5x20 array, fills it, prompts the user for a number to search for, and returns its index if found.

Uploaded by

Abdur Rehman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

CP Lab 8

This document contains a computer programming lab manual from the Department of Computer Science at BUIC. It provides solutions to 3 exercises involving 2D arrays in C++. The first exercise declares a 4x5 integer array, gets user input to fill it, and finds the minimum value. The second tracks even and odd values in a 6x7 integer array. The third declares a 5x20 array, fills it, prompts the user for a number to search for, and returns its index if found.

Uploaded by

Abdur Rehman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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