Ai Notes

Master 2D Arrays in C++ with Programming Fundamentals Lab Report (3)

Hey, Let’s Dive into 2D Arrays in C++!

Hello, coding enthusiasts! Ready to level up your C++ skills? Today, we’re unpacking the Programming Fundamentals Lab Report (2) C++, which focuses on the awesome world of 2D arrays. Whether you’re a student working through a lab assignment or just someone curious about programming, this guide is here to make things simple, fun, and friendly. Let’s get started with some matrix magic!


What’s This Lab Report All About?

The Programming Fundamentals Lab Report (2) C++ is all about mastering 2D arrays—a super useful data structure in programming. Here’s what you’ll learn:

  • How to initialize and work with 2D arrays.
  • Taking user input to fill arrays.
  • Performing operations like matrix addition and multiplication.
  • Using nested loops to handle rows and columns.

We’ll go through four tasks that build your skills step-by-step. Grab your coffee, and let’s code!


Tools You’ll Need

For this lab, you’ll use:

  • Embarcadero Dev-C++: A free IDE perfect for C++ coding. Download it here if you need it.
  • MS Word: For writing up your lab report (optional).

Once you’ve got Dev-C++ set up, you’re good to go!


Lab Task 1: Initializing a 5×5 2D Array

What’s the Goal?

Write a C++ program to initialize a 5×5 2D array.

The Code

#include <iostream> using namespace std;

int main() { int arr[5][5]; cout << “5×5 array initialized!” << endl; return 0; }

What You’ll See

5×5 array initialized!

Why It Works

  • int arr[5][5]; declares a 2D array with 5 rows and 5 columns.
  • We didn’t add values yet—just set up the structure.
  • cout lets us confirm it’s ready.

This is your first step into 2D arrays—simple, right?


Lab Task 2: User Input for a 2D Array

What’s the Goal?

Create a program that lets the user specify the size of a 2D array and input its elements.

The Code

#include <iostream> using namespace std;

int main() { int r, c; cout << “Enter number of rows: “; cin >> r; cout << “Enter number of columns: “; cin >> c; int arr[100][100]; cout << “Enter elements of the array:” << endl; for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { cin >> arr[i][j]; } } cout << “Your 2D array:” << endl; for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { cout << arr[i][j] << ” “; } cout << endl; } return 0; }

What You’ll See (Example)

Enter number of rows: 2 Enter number of columns: 2 Enter elements of the array: 1 2 3 4 Your 2D array: 1 2 3 4

Why It Works

  • We ask the user for rows (r) and columns (c) using cin.
  • Nested for loops (one for rows, one for columns) take input and display the array.
  • The array is stored and printed like a matrix—super handy!

Lab Task 3: Adding Two Matrices

What’s the Goal?

Write a program to take two matrices from the user and add them.

The Code

#include <iostream> using namespace std;

int main() { int r, c; cout << “Enter number of rows & columns.” << endl; cout << “Rows: “; cin >> r; cout << “Columns: “; cin >> c; int arr1[100][100], arr2[100][100], add[100][100]; cout << “Enter elements of array one.” << endl; for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { cin >> arr1[i][j]; } } cout << “Enter elements of array two.” << endl; for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { cin >> arr2[i][j]; } } for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { add[i][j] = arr1[i][j] + arr2[i][j]; } } cout << “Addition of two matrices:” << endl; for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { cout << add[i][j] << ” “; } cout << endl; } return 0; }

What You’ll See (Example)

Enter number of rows & columns. Rows: 2 Columns: 2 Enter elements of array one. 1 2 3 4 Enter elements of array two. 5 6 7 8 Addition of two matrices: 6 8 10 12

Why It Works

  • Two arrays (arr1 and arr2) store the matrices.
  • A third array (add) holds the sum of corresponding elements.
  • Nested loops handle input and addition—easy peasy!

Lab Task 4: Multiplying Two Matrices

What’s the Goal?

Write a program to multiply two matrices based on user input.

The Code

#include <iostream> using namespace std;

int main() { int r1, c1, r2, c2; cout << “Enter rows and columns for first matrix:” << endl; cin >> r1 >> c1; cout << “Enter rows and columns for second matrix:” << endl; cin >> r2 >> c2; if (c1 != r2) { cout << “Matrix multiplication not possible!” << endl; return 0; } int arr1[100][100], arr2[100][100], mult[100][100]; cout << “Enter elements of first matrix:” << endl; for (int i = 0; i < r1; i++) { for (int j = 0; j < c1; j++) { cin >> arr1[i][j]; } } cout << “Enter elements of second matrix:” << endl; for (int i = 0; i < r2; i++) { for (int j = 0; j < c2; j++) { cin >> arr2[i][j]; } } for (int i = 0; i < r1; i++) { for (int j = 0; j < c2; j++) { mult[i][j] = 0; for (int k = 0; k < c1; k++) { mult[i][j] += arr1[i][k] * arr2[k][j]; } } } cout << “Multiplication of two matrices:” << endl; for (int i = 0; i < r1; i++) { for (int j = 0; j < c2; j++) { cout << mult[i][j] << ” “; } cout << endl; } return 0; }

What You’ll See (Example)

Enter rows and columns for first matrix: 2 2 Enter rows and columns for second matrix: 2 2 Enter elements of first matrix: 1 2 3 4 Enter elements of second matrix: 5 6 7 8 Multiplication of two matrices: 19 22 43 50

Why It Works

  • Checks if multiplication is possible (columns of first = rows of second).
  • Uses three nested loops: two for rows/columns, one for the dot product.
  • Stores and displays the result in a new array (mult).

Why This Lab Rocks

The Programming Fundamentals Lab Report (2) C++ teaches you how 2D arrays are a game-changer for storing and manipulating data. From basic initialization to matrix math, you’re building skills that programmers use every day!


Take It Further

Loved this lab? Keep the momentum going! Check out C++ array tutorials on Tutorialspoint or dive into matrix operations with GeeksforGeeks. The more you practice, the better you’ll get!


Wrapping Up

That’s a wrap on the Programming Fundamentals Lab Report (2) C++! From setting up a 5×5 array to multiplying matrices, you’ve tackled some cool challenges. What’s your favorite part? Let me know in the comments, or hit me up if you need help with your next lab. Keep coding, and see you in the next adventure!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top