You are on page 1of 10

/* Write a program that inputs five integers from the user and stores

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;
}
-------------------------------------------------------------------------
-------------------------------------------------------------------------
--------------------------------------------------

Input and output values of an Array


Using loop

#include <iostream>
using namespace std;

int main() {
int arr[5];

// Input values into the array using a loop


cout << "Enter 5 integers: ";
for(int i = 0; i < 5; i++) {
cin >> arr[i];
}

// Output values of the array using a loop


cout << "You entered: ";
for(int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}

return 0;
}
-------------------------------------------------------------------------
-------------------------------------------------------------------------
-----------------------------------------------
sum and avg

#include <iostream>
using namespace std;

int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;

int arr[n]; // your array


cout << "Enter " << n << " elements: ";
for(int i = 0; i < n; i++) {
cin >> arr[i];
}

int sum = 0;
for(int i = 0; i < n; i++) {
sum += arr[i];
}

double average = (double)sum / n;


cout << "Sum of array elements is: " << sum << endl;
cout << "Average of array elements is: " << average << endl;

return 0;
}
-------------------------------------------------------------------------
-------------------------------------------------------------------------
------------------------------------------------

largest

#include <iostream>
using namespace std;

int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;

int arr[n]; // your array


cout << "Enter " << n << " elements: ";
for(int i = 0; i < n; i++) {
cin >> arr[i];
}

int max = arr[0];


for(int i = 1; i < n; i++) {
if(arr[i] > max) {
max = arr[i];
}
}

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];
}

// Display the values of all arrays


cout << "Numbers: ";
for(int i = 0; i < size; i++) {
cout << numbers[i] << " ";
}
cout << endl;

cout << "Squares: ";


for(int i = 0; i < size; i++) {
cout << squares[i] << " ";
}
cout << endl;

cout << "Cubes: ";


for(int i = 0; i < size; i++) {
cout << cubes[i] << " ";
}
cout << endl;

cout << "Sums: ";


int total = 0;
for(int i = 0; i < size; i++) {
cout << sums[i] << " ";
total += sums[i];
}
cout << endl;

// Display the total of all values in sums array


cout << "Total of all values in sums array: " << total << endl;

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;

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


if(arr[i] == target) {
cout << "Element found at index: " << i << endl;
return 0;
}
}

cout << "Element not found in the array." << endl;


return 0;
}
-------------------------------------------------------------------------
-------------------------------------------------------------------------
-------------------------------------------------------
copied string

#include <iostream>
#include <string>
using namespace std;

int main() {
string originalString, copiedString;

cout << "Enter a string: ";


getline(cin, originalString);

copiedString = originalString;

cout << "Copied string: " << copiedString << endl;

return 0;
}
-------------------------------------------------------------------------
-------------------------------------------------------------------------
------------------------------------------
vowel count

#include <iostream>
#include <string>
using namespace std;

int main() {
string str;
int vowelCount = 0;

cout << "Enter a string: ";


getline(cin, str);

for (int i = 0; i < str.length(); i++) {


if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] ==
'o' || str[i] == 'u' ||
str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] ==
'O' || str[i] == 'U') {
vowelCount++;
}
}

cout << "Number of vowels: " << vowelCount << endl;

return 0;
}
-------------------------------------------------------------------------
-------------------------------------------------------------------------
---------------------------------------------------
display

#include <iostream>
using namespace std;

int main() {
int arr[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

cout << "Elements of the 2D array are: " << endl;


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}

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}
};

int max = arr[0][0];


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (arr[i][j] > max) {
max = arr[i][j];
}
}
}

cout << "The largest element in the 2D array is: " << max << endl;

return 0;
}
-------------------------------------------------------------------------
-------------------------------------------------------------------------
---------------------------------------------------

sum of all element


#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}
};

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];

// Adding the matrices


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix3[i][j] = matrix1[i][j] + matrix2[i][j];
}
}

// Printing the result


cout << "The resulting matrix is: " << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << matrix3[i][j] << " ";
}
cout << endl;
}

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}
};

int odd[9], even[9];


int oddCount = 0, evenCount = 0;

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


for (int j = 0; j < cols; j++) {
if (arr[i][j] % 2 == 0) {
even[evenCount++] = arr[i][j];
} else {
odd[oddCount++] = arr[i][j];
}
}
}

cout << "Odd numbers in the array: ";


for (int i = 0; i < oddCount; i++) {
cout << odd[i] << " ";
}
cout << endl;

cout << "Even numbers in the array: ";


for (int i = 0; i < evenCount; i++) {
cout << even[i] << " ";
}
cout << endl;

-------------------------------------------------------------------------
-------------------------------------------------------------------------
----------------------------------------------
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}
};

int max1 = 0, max2 = 0;


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (arr[i][j] > max1) {
max2 = max1;
max1 = arr[i][j];
} else if (arr[i][j] > max2 && arr[i][j] < max1) {
max2 = arr[i][j];
}
}
}

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;
}
}
}

// Print Sorted Array


cout << "Sorted array: \n";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;

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;

int start = 0, end = n - 1;


bool found = false;
while (start <= end) {
int mid = start + (end - start) / 2;
if (arr[mid] == x) {
found = true;
break;
}
if (arr[mid] < x) {
start = mid + 1;
} else {
end = mid - 1;
}
}

if (found) {
cout << x << " is present in the array.\n";
} else {
cout << x << " is not present in the array.\n";
}

return 0;
}
-------------------------------------------------------------------------
--------------------------------------------------------

You might also like