Professional Documents
Culture Documents
them in an
array. It then display all values in the array without using loops*/
#include<iostream>
using namespace std;
int main()
{
int arr[5], i;
cout << "Enter Five integers:"<<endl;
cin>>arr[0];
cin>>arr[1];
cin>>arr[2];
cin>>arr[3];
cin>>arr[4];
cout << "The values are:"<<endl;
cout<<arr[0];
cout<<arr[1];
cout<<arr[2];
cout<<arr[3];
cout<<arr[4];
return 0;
}
-------------------------------------------------------------------------
-------------------------------------------------------------------------
--------------------------------------------------
#include <iostream>
using namespace std;
int main() {
int arr[5];
return 0;
}
-------------------------------------------------------------------------
-------------------------------------------------------------------------
-----------------------------------------------
sum and avg
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int sum = 0;
for(int i = 0; i < n; i++) {
sum += arr[i];
}
return 0;
}
-------------------------------------------------------------------------
-------------------------------------------------------------------------
------------------------------------------------
largest
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
cout << "The largest element in the array is: " << max << endl;
return 0;
}
-------------------------------------------------------------------------
-------------------------------------------------------------------------
-------------------------------------------------
• Write a program that uses four arrays numbers
,squares, cubes and sums each consisting of five
elements. The numbers array stores the values of its
indexes, the squares array stores the squares of its
indexes, the cubes array stores the cubes of its
indexes and sums array stores the sum of
corresponding indexes of three arrays. The program
should display the values of all arrays and the total of
all values in sums array.
#include <iostream>
using namespace std;
int main() {
const int size = 5;
int numbers[size], squares[size], cubes[size], sums[size];
// Initialize arrays
for(int i = 0; i < size; i++) {
numbers[i] = i;
squares[i] = i * i;
cubes[i] = i * i * i;
sums[i] = numbers[i] + squares[i] + cubes[i];
}
return 0;
}
-------------------------------------------------------------------------
-------------------------------------------------------------------------
------------------------------------------------
searching in array:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of elements in the array: ";
cin >> n;
int arr[n];
cout << "Enter the elements of the array: ";
for(int i = 0; i < n; i++) {
cin >> arr[i];
}
int target;
cout << "Enter the element to be searched: ";
cin >> target;
#include <iostream>
#include <string>
using namespace std;
int main() {
string originalString, copiedString;
copiedString = originalString;
return 0;
}
-------------------------------------------------------------------------
-------------------------------------------------------------------------
------------------------------------------
vowel count
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
int vowelCount = 0;
return 0;
}
-------------------------------------------------------------------------
-------------------------------------------------------------------------
---------------------------------------------------
display
#include <iostream>
using namespace std;
int main() {
int arr[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
return 0;
}
-------------------------------------------------------------------------
-------------------------------------------------------------------------
------------------------------------------------
maximum in 2d array
#include <iostream>
using namespace std;
int main() {
int rows = 3, cols = 3;
int arr[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
cout << "The largest element in the 2D array is: " << max << endl;
return 0;
}
-------------------------------------------------------------------------
-------------------------------------------------------------------------
---------------------------------------------------
int main() {
int rows = 3, cols = 3;
int arr[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int sum = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sum += arr[i][j];
}
}
cout << "The sum of all elements in the 2D array is: " << sum <<
endl;
return 0;
}
-------------------------------------------------------------------------
-------------------------------------------------------------------------
----------------------------------------
sum of array to find 3rd array.
#include <iostream>
using namespace std;
int main() {
int rows = 3, cols = 3;
int matrix1[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int matrix2[3][3] = {
{10, 11, 12},
{13, 14, 15},
{16, 17, 18}
};
int matrix3[3][3];
return 0;
}
-------------------------------------------------------------------------
-------------------------------------------------------------------------
-------------------------------------------------------
even and odd count
#include <iostream>
using namespace std;
int main() {
int rows = 3, cols = 3;
int arr[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
-------------------------------------------------------------------------
-------------------------------------------------------------------------
----------------------------------------------
2nd largest
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int rows = 3, cols = 3;
int arr[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
if (max2 == 0) {
cout << "There is no second largest element\n";
} else {
cout << "The second largest element in the 2D array is: " << max2
<< endl;
}
return 0;
}
-------------------------------------------------------------------------
-------------------------------------------------------------------------
-------------------------------------------------
bubble sort
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of elements in the array: ";
cin >> n;
int arr[n];
cout << "Enter elements of the array: " << endl;
for (int i = 0; i < n; i++) {
cout << "Enter element " << i+1 << ": ";
cin >> arr[i];
}
// Bubble Sort
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
// Swap arr[j] and arr[j+1]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
return 0;
}
------------------------------------------------------binary
search-------------------------------------------------------------------
-------------------------------------------------------------------------
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of elements in the array: ";
cin >> n;
int arr[n];
cout << "Enter elements of the array: " << endl;
for (int i = 0; i < n; i++) {
cout << "Enter element " << i+1 << ": ";
cin >> arr[i];
}
// Binary Search
int x;
cout << "Enter the element to search: ";
cin >> x;
if (found) {
cout << x << " is present in the array.\n";
} else {
cout << x << " is not present in the array.\n";
}
return 0;
}
-------------------------------------------------------------------------
--------------------------------------------------------