Lab3 Solutions
Lab3 Solutions
Lab’s Scope:
• Two-dimensional Arrays
---------------------------------------------------------------------------------------------------------------------------
Problem 1:
Write a program declaring a 2D array with 3 rows and 5 columns. Initialize the array with integers. Your
program should then find the largest and smallest values in the whole array and print them out.
Solution:
#include <iostream>
using namespace std;
int main() {
const int ROWS = 3;
const int COLS = 5;
cout << "The largest number is : " << largest << endl;
cout << "The smallest number is : " << smallest << endl;
return 0;
}
Problem 2:
Write a program declaring a 2D array and initialize the array with integers. Your program should count
the number of odd numbers in the array and print it out.
2 Introduction to Programming
Solution:
#include <iostream>
using namespace std;
int main() {
const int ROWS = 3;
const int COLS = 5;
int arr[ROWS][COLS] = {
{50,10,89,36,69},{87,94,52,37,78},{48,65,75,19,23} };
int count = 0;
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
if ((arr[i][j] % 2) != 0)
count++;
}
}
cout << "The count of odd numbers is : " << count << endl;
return 0;
}
Problem 3:
Write a program declaring a 2D array and initialize the array with integers. Your program should
multiply all the elements in each column and store the results of multiplication in a one-dimensional
array. Hint: Think carefully about how you will manage your index.
Solution:
#include <iostream>
using namespace std;
int main() {
const int ROWS = 3;
const int COLS = 5;
int a[ROWS][COLS] = { {5,3,2,1,5},{2,3,4,3,6},{10,9,8,4,2} };
int b[COLS];
int product;
int count = 0;
for (int i = 0; i < COLS; i++)
{
product = 1;
for (int j = 0; j < ROWS; j++)
{
product *= a[j][i];
}
b[i] = product;
cout << b[i] << " ";
}
return 0;
}
3 Introduction to Programming
Problem 4:
Write a program given a 2D array initialized with integers. Your program should ask the user to input
a number and the program searches the array for this number. If the number is found your program
should output the row and column of the element. If the element is not found, your program should
print a message that the element is not found.
Solution:
#include <iostream>
using namespace std;
int main() {
const int ROWS = 3;
const int COLS = 5;
int a[ROWS][COLS] = { {50,10,89,36,69},{87,94,52,37,78},{48,65,75,19,23} };
int x;
if (found == false)
cout << "The element is not found" << endl;
return 0;
}
Problem 5:
Write a program that, given a 2D array where each row stores the grades of a student. Your program
should calculate the average grade for each student.
4 Introduction to Programming
Solution:
#include <iostream>
using namespace std;
int main() {
const int ROWS = 3;
const int COLS = 5;
int a[ROWS][COLS] = { {50,10,89,36,69},{87,94,52,37,78},{48,65,75,19,23} };
float average;
return 0;
}
Problem 6:
Given the below figure for multiplying two matrices. Write a program declaring and initializing two
matrices of suitable sizes. Your program should multiply the two matrices and print out the output
matrix.
Solution:
#include <iostream>
using namespace std;
int main() {
const int R1 = 3;
const int C1 = 1;
const int R2 = 1;
const int C2 = 3;
int a[R1][C1] = { 4,5,6 };
int b[R2][C2] = { 1,2,3 };
int c[R1][C2];
Problem 7:
A word puzzle game: Write a program that involves a 2D array of characters, the user will search for
a word of 3 letters. Your program should search the array horizontally for the word and print out the
row and column of the starting position of the word, or print “the word is not found” if the word is
not in the array.
Example:
A C A T N
F M D O G
K I T P Q
M S A F A
6 Introduction to Programming
Solution:
#include <iostream>
using namespace std;
int main() {
const int ROWS = 4;
const int COLS = 5;
string word;
char a[ROWS][COLS] = {
{'A','C','A','T','N'},{'F','M','D','O','G'},{'K','I','T','P','Q'},{'M','S','A','F','A'
} };
cout << "Enter the word you are searching for:" << endl;
cin >> word;
char letter1 = tolower(word.at(0));
char letter2 = tolower(word.at(1));
char letter3 = tolower(word.at(2));
bool found = false;
if (found == false)
{
Problem 8:cout << "the word is not found" << endl;
}
Simplereturn
Sudoku0;
game: Assume you have a 3x3 sudoku puzzle initialized as shown in the below figure,
}
your program should insert the missing number instead of the zero by traversing the matrix
horizontally to find the missing number. You should not repeat a number in the same row.
1 0 3
3 1 0
2 3 0
7 Introduction to Programming
Hint:
• if number 1 is missing, the sum of the other two numbers is 5.
• if number 2 is missing, the sum of the other two numbers is 4.
• if number 3 is missing, the sum of the other two numbers is 3.
Solution:
#include <iostream>
using namespace std;
int main() {
const int ROWS = 3;
const int COLS = 3;
int puzzle[ROWS][COLS] = { {1,0,3},{3,1,0},{2,3,0} };
int sum[ROWS] = {};
return 0;
}