2nd Year Computer Science Notes Chapter 5 Arrays and Strings, it series computer book 2nd year pdf free download board of Khyber Pakhtunkhwa (KPK).
KPK Grade 12 Computer Science Arrays and Strings
Q.5) Write down a C++ program to find the multiplication of two matrices A[3][2] and B[2][3] .
Answer:
int main()
{
int r1, c1, r2, c2, i, j, k;
int A[3][2], B[2][3], C[3][3];
cout << “Enter elements of matrix A : “;
for (i = 0; i < 3; i++)
for (j = 0; j < 2; j++)
cin >> A[i][j];
cout << “Enter elements of matrix B : “;
for (i = 0; i < 2; i++)
for (j = 0; j < 3; j++)
cin >> B[i][j];
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
C[i][j] = 0;
for (k = 0; k < 2; k++)
{
C[i][j] += A[i][k] * B[k][j];
}
}
}
cout << “Product of matrices\n”;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
cout << C[i][j] << ” “;
cout << “\n”;
}
return 0;
}
Output:
The output of the program is as follows:

Q.6) Write down a C++ program to find the subtraction and addition of two matrices A[3][3] and B[3][3].
Answer:
int main()
{
int i, j;
int A[3][3], B[3][3], Add[3][3], Sub[3][3];
cout << “Enter elements of matrix A : “;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
cin >> A[i][j];
cout << “Enter elements of matrix B : “;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
cin >> B[i][j];
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
Add[i][j] = 0;
Add[i][j] += A[i][j] + B[i][j];
}
}
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
Sub[i][j] = 0;
Sub[i][j] += A[i][j] – B[i][j];
}
}
cout << “Addition of matrices\n”;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
cout << Add[i][j] << ” “;
cout << “\n”;
}
cout << “Subtraction of matrices\n”;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
cout << Sub[i][j] << ” “;
cout << “\n”;
}
return 0;
}
Output:
The output of the program is as follows:

Q.7) Write a C++ program to find the transpose of a matrix A[3][3].
Answer:
int main()
{
int A[3][3], m, n, i, j;
cout << “Enter elements of matrix : “;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
cin >> A[i][j];
cout << “Entered Matrix : \n “;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
cout << A[i][j] << ” “;
cout << “\n “;
}
cout << “Transpose of Matrix : \n “;
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
cout << A[j][i] << ” “;
cout << “\n “;
}
return 0;
}
Output:
The output of the program is as follows:

Q.8) Write a C++ program to read the temperature of the whole week in an array and then find the hottest day of the week.
Answer:
int main()
{
float days = 0;
float temperatures[50];
float temptotal = 0;
float average = 0;
float min = 9999999999999;
float max = -9999999999999;
string dayName;
for (int i = 0; i < 7; i++)
{
cout << “Enter the temperature for day number ” << i+1 << ” : “;
cin >> temperatures[i];
temptotal += temperatures[i];
if (temperatures[i] > max)
{
max = temperatures[i];
}
if (temperatures[i] < min)
min = temperatures[i];
}
average = (temptotal / days);
cout << “The Hottest Day is having the Temperature : “<< max << endl;
return 0;
}
Output:
The output of the program is as follows:

Q.9) Write a C++ program to read ten alphabets of English from the keyboard into a character type array and then sort them in descending order.
Answer:
Output:
The output of the program is as follows:

Q.10) Write a C++ program to find sum of the values of a two dimensional array int Test [2][3] and display the result on the screen.
Answer:
int main()
{
int i, j, k;
int A[2][3], Add[2][3], C[3][3];
cout << “Enter elements of matrix A : “;
for (i = 0; i < 2; i++)
for (j = 0; j < 3; j++)
cin >> A[i][j];
for (i = 0; i < 2; i++)
{
for (j = 0; j < 3; j++)
{
Add[i][j] = 0;
Add[i][j] += A[i][j] + A[i][j];
}
}
cout << “Addition of matrices\n”;
for (i = 0; i < 2; i++)
{
for (j = 0; j < 3; j++)
cout << Add[i][j] << ” “;
cout << “\n”;
}
return 0;
}
Output:
The output of the program is as follows:

Reated Post:
- 2nd-year computer science notes Chapter 1 Operating Systems
- 2nd-year computer science notes Chapter 2 System Development Life Cycle
- Chapter 3 Object-Oriented Programming using C++
- 2nd year Computer science Notes Chapter 4 Control Structures