Sei sulla pagina 1di 6

[DOCUMENT TITLE]

[Document subtitle]

*Introduction to programming*

Q.1 Create a matrix “A” (two-dimensional array) which has 5 rows and 5 columns. Then take input from
the user for all elements of the matrix “A”. After that, the program should determine if the matrix has
value “1” in ALL diagonal elements Or Not.

Program
#include<iostream>
using namespace std;
int main(){
int A[5][5];
int count = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
cin >> A[i][j];
}
}
for (int i = 0; i < 5; i++) {
if (A[i][i] == 1) {
count++;
}
if (count == 5) {
cout << "1 in All diagonal ";
}
}
system("pause");
}
Output

Q. 2: Create and assign random integer values in a 2D array of size 4(columns) and
4(rows). After that, count

(A) all positive values,

(B) all negative values,

(C) all even numbers, and

(D) all odd numbers

In the end, print the four counts such as positive values, negative values, even numbers, and odd
numbers.

Program
#include<iostream>
using namespace std;
int main(){
int array[4][4] = { {1,2,3,4},{4,5,6,7},{1,3,5,7},{3,5,8,9} };
int p=0, n=0, e=0, o=0;//p for positive, n for negative, e for even and o for odd
cout << "Enter the values in an 2-D Array" << endl<<endl;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
cout << array[i][j] << " ";
}
cout << endl;
}
cout << endl;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (array[i][j] > 0) {
p++;
}
if (array[i][j] < 0) {
n++;
}
if (array[i][j] % 2 == 0) {
e++;
}
if (array[i][j] % 2 != 0) {
o++;
}
}
}
cout << "Postive=" << p << endl << "Negative=" << n << endl << "Even=" << e <<
endl << "Odd=" << o << endl;
system("pause");
}
Output

Q-3: Write a program that creates a matrix (a two-dimensional integer array) having 5 rows and 5
columns. Assign random values between 1 to 20 for all the matrix values. The program then calculates
product (multiplication of) of all diagonal elements. In the end, the program should print the calculated
product value.

Program
#include<iostream>
using namespace std;
int main(){
int array[5][5] = { {1,2,3,4,5},{4,5,6,4,7},{1,3,7,5,7},{3,5,2,8,9},{1,2,3,4,5} };
int product = 1;
cout << "Enter the values in an 2-D Array" << endl<<endl;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
cout << array[i][j] << " ";
}
cout << endl;
}
cout << endl;
for (int i = 0; i < 5; i++) {
product *= array[i][i];
}
cout << "The product of an diagonal values of an array will be:" << product <<
endl;
system("pause");
}
Output

Q-4: Write a C++ program, which creates a 2D array of type int having 30 rows and 50 columns. Assign
random values in the 2D array in a range of 1 to 500. (A) After this, the program should display only
those values of the 2D array, which are divisible by 4 or 7. With each displayed value please also print its
location in the 2D array (for example row and columns index). (B) Next, the program should replace all
those values which are not divisible by 2 with a new value of -5.

Program

Potrebbero piacerti anche