-
Notifications
You must be signed in to change notification settings - Fork 1
/
Arrays
217 lines (178 loc) · 5.33 KB
/
Arrays
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
Question 1: Write a c++ program to find the maximum and minimum numbers from the given array.
Code Solution:
#include <iostream>
using namespace std;
const int SIZE = 5;
int main() {
int arr[SIZE];
cout << "Enter " << SIZE << " numbers: ";
for (int i = 0; i < SIZE; i++) {
cin >> arr[i];
}
int maxNum = INT_MIN, minNum = INT_MAX;
for (int i = 0; i < SIZE; i++) {
if (arr[i] > maxNum) {
maxNum = arr[i];
}
if (arr[i] < minNum) {
minNum = arr[i];
}
}
cout << "Maximum number: " << maxNum << endl;
cout << "Minimum number: " << minNum << endl;
return 0;
}
----------------------------------------------------------------------------------------------------------------------
Question 2: Write a c++ program to search for a given value from the given array.
Code Solution:
#include <iostream>
using namespace std;
const int SIZE = 5;
int main() {
int arr[SIZE];
cout << "Enter " << SIZE << " numbers: ";
for (int i = 0; i < SIZE; i++) {
cin >> arr[i];
}
int searchValue;
cout << "Enter a value to search for: ";
cin >> searchValue;
bool found = false;
for (int i = 0; i < SIZE; i++) {
if (arr[i] == searchValue) {
found = true;
cout << "Value found at index " << i << endl;
}
}
if (!found) {
cout << "Value not found in array\n";
}
return 0;
}
---------------------------------------------------------------------------------------------------------------------
Question 3: Given an array, count numbers whose last element is 2, 3, or 9.
Code Solution:
#include <iostream>
using namespace std;
const int SIZE = 7;
int main() {
int arr[SIZE] = { 12, 23, 32, 41, 59, 68, 79 };
int count = 0;
for (int i = 0; i < SIZE; i++) {
int lastDigit = arr[i] % 10;
if (lastDigit == 2 || lastDigit == 3 || lastDigit == 9) {
count++;
}
}
cout << "Count of numbers whose last digit is 2, 3, or 9: " << count << endl;
return 0;
}
-----------------------------------------------------------------------------------------------------------------------
Question 4: Write a program to input two matrices and check whether they are equal.
Code Solution:
#include <iostream>
using namespace std;
const int ROWS = 5;
const int COLS = 5;
int main() {
int mat1[ROWS][COLS];
int mat2[ROWS][COLS];
cout << "Enter " << ROWS*COLS << " elements of matrix 1: " << endl;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
cin >> mat1[i][j];
}
}
cout << "Enter " << ROWS*COLS << " elements of matrix 2: " << endl;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
cin >> mat2[i][j];
}
}
bool equal = true;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (mat1[i][j] != mat2[i][j]) {
equal = false;
break;
}
}
if (!equal) {
break;
}
}
if (equal) {
cout << "Matrices are equal" << endl;
} else {
cout << "Matrices are not equal" << endl;
}
return 0;
}
-----------------------------------------------------------------------------------------------------------------------
Question 5: Write a program to find the position of an element in a 2d array.
Code Solution:
#include <iostream>
using namespace std;
const int ROWS = 5;
const int COLS = 5;
int main() {
int arr[ROWS][COLS];
cout << "Enter " << ROWS*COLS << " elements of matrix: " << endl;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
cin >> arr[i][j];
}
}
int searchValue;
cout << "Enter a value to search for: ";
cin >> searchValue;
bool found = false;
int row, col;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (arr[i][j] == searchValue) {
found = true;
row = i;
col = j;
break;
}
}
if (found) {
break;
}
}
if (found) {
cout << "Value found at position (" << row << ", " << col << ")" << endl;
} else {
cout << "Value not found in array" << endl;
}
return 0;
}
------------------------------------------------------------------------------------------------------------------------------
Question 6: Write a program to print sum of the first row and last row separately.
Code Solution:
#include <iostream>
using namespace std;
const int ROWS = 5;
const int COLS = 5;
int main() {
int arr[ROWS][COLS];
cout << "Enter " << ROWS*COLS << " elements of matrix: " << endl;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
cin >> arr[i][j];
}
}
int sumFirstRow = 0;
for (int j = 0; j < COLS; j++) {
sumFirstRow += arr[0][j];
}
int sumLastRow = 0;
for (int j = 0; j < COLS; j++) {
sumLastRow += arr[ROWS-1][j];
}
cout << "Sum of first row: " << sumFirstRow << endl;
cout << "Sum of last row: " << sumLastRow << endl;
return 0;
}
----------------------------------------------------------------------------------------------------------------------------