0% found this document useful (0 votes)
39 views

C++ Task Vector

This document contains C++ code implementing various operations on vectors: 1. It calculates the sum of elements in a vector. 2. It prints the first element of a vector. 3. It calculates and compares the sums of two vectors. 4. It counts the number of odd elements in a vector. 5. It prints the elements of a vector in reverse order. 6. It demonstrates adding, deleting, inserting and clearing elements from a vector through a menu-driven program. The document also contains code to reverse a vector and check for non-alphabetic characters in a string.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

C++ Task Vector

This document contains C++ code implementing various operations on vectors: 1. It calculates the sum of elements in a vector. 2. It prints the first element of a vector. 3. It calculates and compares the sums of two vectors. 4. It counts the number of odd elements in a vector. 5. It prints the elements of a vector in reverse order. 6. It demonstrates adding, deleting, inserting and clearing elements from a vector through a menu-driven program. The document also contains code to reverse a vector and check for non-alphabetic characters in a string.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <iostream>

#include <algorithm>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
int main() {
// Task 1
/*int sum= 0;
vector<int>v = {1, 2, 3, 4, 5, 6, 7, 8, 9};
for (int i = 0; i < v.size(); i++ )
{
sum = sum + v.at(i);
}
cout << sum;

*/
// Task 2

/* vector<int>v = {1, 2, 3, 4, 5, 6, 7, 8, 9};


cout << v.at(0);*/

// Task 3
/* int sum1= 0, sum2 = 0;
vector<int>v = {1, 29, 3, 49, 5, 63, 75, 82, 9};
vector<int>v2 = {10, 23, 26, 19, 54, 0, 5, 2};
for (int i = 0; i < v.size(); i++ )
{
sum1 = sum1 + v.at(i);
}
cout << sum1<< endl;
for (int i = 0; i < v2.size(); i++ )
{
sum2 = sum2 + v2.at(i);
}
cout << sum2<<endl;
if (sum1 > sum2)
{
cout << sum1-sum2;
}
else if (sum2 > sum1)
{
cout << sum2-sum1;
}
else
{
cout << "They are equal";
}*/
// Task 4
/*int z=0;
vector<int>v = {1, 2, 3, 4, 5, 6, 7, 8, 9};
for (int i = 0; i < v.size(); i++ )
{
if (v.at(i)%2==1)
{
z = z+1;
}
}
cout << "Number of odd elements: " << z; */
// Task 5
vector<int>v = {1, 2, 3, 4, 5, 6, 7, 8, 9};

for (int i = v.end(); i != v.begin(); i-- )


{t
cout << i;
}
// Task 6
vector<int>v = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int n, number, number2;
cout<< "Enter the case: ";
cout<< "1. Add an element ";
cout<< "2. Delete an element ";
cout<< "3. Insert an element";
cout<< "4. Delete all the elements ";
cin >> n;
switch(n){
case 1:
cout << "Enter the number";
cin >> number;
v.push_back(number);
for (int i = 0; i < v.size(); i++ )
{
cout << i << " ";
}
break;
case 2:
v.pop_back();
for (int i = 0; i < v.size(); i++ )
{
cout << i << " ";
}
break;
case 3:
cout<< "Enter the number for insert: "
cin >> number2;
auto z = find(v.begin(), v.end(), 7);
insert(z,number2);
for (int i = 0; i < v.size(); i++ )
{
cout << i << " ";
}
break;
case 4:
auto z = find(v.begin(), v.end(), 7);
erase(z,v.end());
for (int i = 0; i < v.size(); i++ )
{
cout << i << " ";
}
break;
default:
break;
}
-----------------
REVERSE A VECTOR
-----------------
#include <iostream>
#include <algorithm>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int>v = {1, 2, 3, 4, 5, 6, 7, 8, 9};
for (auto i = v.crbegin(); i != v.crend(); i++ )
{
cout << *i << " ";
}
return 0;
}

#include <iostream>
#include <algorithm>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
int main() {
// Task 1
/*char s[10];
cin >> s;
int l = strlen(s);
for (int i = l-1; i >= 0; i--)
{
cout << s[i];
}
return 0; */
// Task 2
/*char s[10];
cin >> s;
char a[10];
cin >> a;
cout << s << " " << a; */
char s[100];
cin >> s;
int l = strlen(s);
for (int i = 0 ; i <= l - 1; i++)
{
if (s[i] < 65 || s[i] >= 90 && s[i] <= 97 || s[i] > 122)
{
cout << s[i];
}
}
}

You might also like