Experiment 06
Aim: Programming using 1D Array & 2D array
Task 1: Write a program to multiply each element of an array by 5 and display the resultant array.
#include <iostream>
using namespace std;
int main()
{
int c, i, arr[10];
cout << "Enter the number of elements in Array: ";
cin >> c;
cout << endl << "Enter the elements of Array:" << endl;
for (i = 0; i < c; i++)
{
cin >> arr[i];
}
cout << endl;
for (i = 0; i < c; i++)
{
cout << (arr[i] * 5) << " ";
}
return 0;
}
Task 2: Write a program to find and display odd & even numbers from an array (1D) separately.
#include <iostream>
using namespace std;
int main()
{
int c, i, arr[10];
cout << "Enter the number of elements in Array: ";
cin >> c;
cout << endl << "Enter the elements of Array:" << endl;
for (i = 0; i < c; i++)
cin >> arr[i];
cout << "Odd numbers: ";
for (i = 0; i < c; i++)
if (arr[i] % 2 != 0)
cout << arr[i] << " ";
cout << endl << "Even numbers: ";
for (i = 0; i < c; i++)
if (arr[i] % 2 == 0)
cout << arr[i] << " ";
return 0;
}
Task 3: Write a program to copy one 1D array into another 1D array and display copied array.
#include <iostream>
using namespace std;
int main()
{
int c, i, arr1[10], arr2[10];
cout << "Enter the number of elements in Array: ";
cin >> c;
cout << endl << "Enter the elements of Array:" << endl;
for (i = 0; i < c; i++)
cin >> arr1[i];
for (i = 0; i < c; i++)
arr2[i] = arr1[i];
cout << "Copied Array:" << endl;
for (i = 0; i < c; i++)
cout << arr2[i] << " ";
return 0;
}
Task 4: Implement a program to reverse elements of 1D array and display it.
#include <iostream>
using namespace std;
int main()
{
int c, i, j;
cout << "Enter the number of elements in Array: ";
cin >> c;
int arr1[c];
int arr2[c];
cout << endl << "Enter the elements of Array:" << endl;
for (i = 0; i < c; i++)
cin >> arr1[i];
for (i = c - 1, j = 0; i >= 0; i--, j++)
arr2[i] = arr1[j];
cout << "Reversed Array:" << endl;
for (i = 0; i < c; i++)
cout << arr2[i] << " ";
return 0;
}
Task 5: Develop a program to perform sum of elements of matrix (2D array) of order MXN.
#include <iostream>
using namespace std;
int main()
{
int c, i, j, s;
cout << "Enter the size of Matrix: ";
cin >> c;
int arr[c][c];
cout << endl << "Enter the elements of Matrix:" << endl;
for (i = 0; i < c; i++)
for (j = 0; j < c; j++)
cin >> arr[i][j];
for (i = 0, s = 0; i < c; i++)
for (j = 0; j < c; j++)
s = s + arr[i][j];
cout << "Sum is: " << s;
return 0;
}
Task 6: Develop a program to find sum of elements of lower triangular matrix of order MxN.
#include <iostream>
using namespace std;
int main()
{
int c, i, j, s;
cout << "Enter the size of Matrix: ";
cin >> c;
int arr[c][c];
cout << endl << "Enter the elements of Matrix:" << endl;
for (i = 0; i < c; i++)
for (j = 0; j < c; j++)
cin >> arr[i][j];
for (i = 0, s = 0; i < c; i++)
for (j = 0; j < c; j++)
if ((i + j) >= c)
s = s + arr[i][j];
cout << "Sum is: " << s;
return 0;
}
Task 7: Write a program to perform addition of two matrix (2D array) and display the resultant matrix.
#include <iostream>
using namespace std;
int main()
{
int c, i, j;
cout << "Enter the size of Matrix: ";
cin >> c;
int m1[c][c];
int m2[c][c];
int ms[c][c];
cout << endl << "Enter the elements of Matrix 1:" << endl;
for (i = 0; i < c; i++)
for (j = 0; j < c; j++)
cin >> m1[i][j];
cout << endl << "Enter the elements of Matrix 2:" << endl;
for (i = 0; i < c; i++)
for (j = 0; j < c; j++)
cin >> m2[i][j];
for (i = 0; i < c; i++)
for (j = 0; j < c; j++)
ms[i][j] = m1[i][j] + m2[i][j];
cout << "Sum of the two Matrices:" << endl;
for (i = 0; i < c; i++)
{
for (j = 0; j < c; j++)
{
if (ms[i][j] < 10)
cout << " ";
if (ms[i][j] >= 10 && ms[i][j] < 100)
cout << " ";
if (ms[i][j] >= 100 && ms[i][j] < 1000)
cout << " ";
cout << ms[i][j] << " ";
}
cout << endl;
}
return 0;
}
Task 8: Implement a program to find the largest element 3X3 matrix.
#include <iostream>
using namespace std;
int main()
{
int N[3][3], a;
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
cin >> N[i][j];
}
}
a = N[0][0];
for (int i = 1; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
if (N[i][j] > a)
a = N[i][j];
}
}
cout << "The biggest no is " << a << endl;
return 0;
}