You are on page 1of 5

1.

Write a program to store and calculate the sum of 5 numbers entered by the user using
arrays.

#include <iostream.h>
int main()
{
int numbers[5], sum = 0;
cout << "Enter 5 numbers: ";

for (int i = 0; i < 5; ++i)


{
cin >> numbers[i];
sum += numbers[i];
}

cout << "Sum = " << sum << endl;

return 0;
}
2. Write C++ Program that display all elements of an initialized two dimensional array.
#include <iostream.h>

int main() {

int test[3][2] = {

{2, -5},

{4, 0},

{9, 1}

};

for(int i = 0; i < 3; ++i) {

for(int j = 0; j < 2; ++j)

cout<< "test["<< i << "][" << j << "] = " <<


test[i][j]<<endl;

return 0;

}
3. Write a program that Display largest number of an array b(elements entered by
user )

#include <iostream.h>

int main()
{
int i, n;
float arr[100];

cout << "Enter total number of elements(1 to 100): ";


cin >> n;
cout << endl;

for(i = 0; i < n; ++i)


{
cout << "Enter Number " << i + 1 << " : ";
cin >> arr[i];
}

for(i = 1;i < n; ++i)


{
if(arr[0] < arr[i])
arr[0] = arr[i];
}
cout << "Largest element = " << arr[0];

return 0;
}
4. Write a program that add Two Matrices using Multi-dimensional Arrays.

#include <iostream.h>

int main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i,
j;

cout << "Enter number of rows (between 1 and 100): ";


cin >> r;

cout << "Enter number of columns (between 1 and 100):


";
cin >> c;

cout << endl << "Enter elements of 1st matrix: " <<
endl;

// Storing elements of first matrix entered by user.


for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << "Enter element a" << i + 1 << j + 1 << "
: ";
cin >> a[i][j];
}

// Storing elements of second matrix entered by user.


cout << endl << "Enter elements of 2nd matrix: " <<
endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << "Enter element b" << i + 1 << j + 1 << "
: ";
cin >> b[i][j];
}

// Adding Two matrices


for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
sum[i][j] = a[i][j] + b[i][j];

// Displaying the resultant sum matrix.


cout << endl << "Sum of two matrix is: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << sum[i][j] << " ";
if(j == c - 1)
cout << endl;
}

return 0;
}

You might also like