Topic: Arrays & Strings
Task 1
Write a C++ program that takes input from users and stores them in an array.
Sample Output:
Enter 5 numbers: 1 2 3 4 5
The numbers are: 1 2 3 4 5
Solution
#include<iostream>
using namespace std;
int main()
{
int arr[5];
cout << "enter a
number:";
for (int i = 0; i < 5; i++)
{
cin >> arr[i];
}
cout << "numbers
are:";
for (int i = 0; i < 5; i++)
{
cout <<"
"<< arr[i];
}
system("pause");
return 0;
}
Task 2
Write a C++ program that takes the number of elements from the user and finds the total number of
positive integers in an array.
Sample
Output:
Enter elements in an array: 0
-1 2
Positive numbers are= 1
Solution:
#include<iostream>
using namespace std;
int main()
{
int arr[5],count=0;
bool flag = false;
for (int i = 0; i < 5; i++)
{
cin >> arr[i];
if (arr[i]>0)
{
flag = true;
count++;
}
}
if (flag == true)
{
cout << "positive
numbers are:" << count << endl;
}
system("pause");
return 0;
}
Task 3
Write a C++ program that takes several elements from
user and find the maximum number in an integer array.
Sample
Output:
Enter numbers in an array: 10 5 12
Max number is: 12
Solution
#include<iostream>
using
namespace std;
int main()
{
int arr[5];
int max = arr[0];
for (int i = 0; i < 5; i++)
{
cin >> arr[i];
}
cout << "maximum
no.:";
for (int i = 0; i < 5; i++)
{
if (arr[i]>max)
{
max = arr[i];
}
}
cout << max << endl;
system("pause");
return 0;
}
Task 4
Write a C++ program that takes a number of elements from
user and find the second maximum number in an integer array.
Sample
Output:
Enter numbers in an array: 10 5 12
2nd max number is: 10
Solution
#include<iostream>
using namespace std;
int main()
{
int arr[5],largest,second;
int max = arr[0];
for (int i = 0; i < 5; i++)
{
cin >> arr[i];
}
if (arr[0]>arr[1])
{
second = arr[1];
largest = arr[0];
}
else
{
largest = arr[1];
second = arr[0];
}
for (int i = 2; i < 5; i++)
{
if (arr[i]>largest)
{
largest = arr[i];
second = largest;
}
else if (arr[i] >
second&&arr[i] != largest)
{
second = arr[i];
}
}
cout << "second largest in
array:" << second << endl;
system("pause");
return 0;
}
Task 5
Write a C++ program that takes the number of elements from a user in two different arrays and
merge the data of both arrays in the third array.
Sample Output:
Enter elements of 1st array: 4
6 2
Enter elements of 2nd array: 9
7 8
New Array (Merged): 4 6 2
9 7 8
Solution
#include<iostream>
using namespace std;
int main()
{
int arrOne[5], arrTwo[5],
arrMerge[10];
int sizeOne, sizeTwo, i, k;
cout << "Enter the Size for
First Array: ";
cin >> sizeOne;
cout << "Enter "
<< sizeOne << " Elements for First Array: ";
for (i = 0; i<sizeOne; i++)
{
cin >> arrOne[i];
arrMerge[i] = arrOne[i];
}
k = i;
cout << "\nEnter the Size
for Second Array: ";
cin >> sizeTwo;
cout << "Enter "
<< sizeTwo << " Elements for Second Array: ";
for (i = 0; i<sizeTwo; i++)
{
cin >> arrTwo[i];
arrMerge[k] = arrTwo[i];
k++;
}
cout << "\nThe New Array
(Merged Array):\n";
for (i = 0; i<k; i++)
cout << arrMerge[i]
<< " ";
cout << endl;
system("pause");
return 0;
}
Task 6
Write a C++ program that takes
input in an array from a user and adds all the positive numbers and negative
numbers separately. Also, show the total sum of both positive and negative
numbers.
Sample
Output:
Enter elements of an array: 2
-2 4 6
Positive numbers sum: 6
Negative numbers sum: - 8
Total sum: - 2
Solution:
#include<iostream>
using namespace std;
int main()
{
int arr[5],sum1=0,sum2=0,totsum;
cout << "enter
numbers:";
for (int i = 0; i < 5; i++)
{
cin >> arr[i];
}
cout << "positive numbers
sum:";
for (int i = 0; i < 5; i++)
{
if
(arr[i]>0)
{
sum1 = sum1 +
arr[i];
}
}
cout << sum1<<endl;
cout << "negative numbers
sum";
for (int i = 0; i < 5; i++)
{
if (arr[i]<0)
{
sum2 = sum2 +
arr[i];
}
}
cout << sum2<<endl;
totsum = sum1 + sum2;
cout << "total sum"
<< totsum << endl;
system("pause");
return 0;
}
0 Comments