Upload files to "array"

This commit is contained in:
Vichingo455 2024-12-03 16:38:35 +00:00
parent 49dd35348a
commit b24ab3683f
3 changed files with 120 additions and 0 deletions

View File

@ -0,0 +1,36 @@
/*
AUTORE: Vichingo455
Function returning if all values stored in an array of values are peer numbers or not
*/
#include <iostream>
#include <array>
#include <vector>
#include <cstdlib>
using namespace std;
bool isPeerArray(int array[], size_t size) {
int numberOfPeers = 0;
for (int i = 0; i < size; i++) {
if (array[i] % 2 == 0) {
numberOfPeers++;
}
}
return numberOfPeers == size;
}
int main() {
size_t numberOfValues;
cout << "Insert the number of values to store on an array: ";
cin >> numberOfValues;
cout << endl;
int array[numberOfValues];
for (int i = 0; i < numberOfValues; i++) {
cout << "Insert the value: ";
cin >> array[i];
}
if (isPeerArray(array, numberOfValues)) {
cout << "All numbers are peers." << endl;
}
else {
cout << "Not all numbers are peers." << endl;
}
return 0;
}

View File

@ -0,0 +1,38 @@
/*
AUTORE: Vichingo455
The program deletes peer numbers from a vector without using memmove
*/
#include <iostream>
#include <array>
#include <vector>
#include <cstdlib>
using namespace std;
void printVector(const vector<int> & vett) {
for (int i = 0; i < vett.size(); i++)
cout << vett.at(i) << " ";
cout << endl;
}
void deletePeers(vector<int> & vett) {
for (int i = 0; i < vett.size(); i++) {
if (vett.at(i) % 2 == 0) {
vett.erase(vett.begin() + i);
}
}
}
int main() {
vector<int> vett;
int vectorSize;
cout << "Insert the size of the vector: ";
cin >> vectorSize;
cout << endl;
for (int i = 0,value; i < vectorSize; i++) {
cout << "Insert a value: ";
cin >> value;
vett.push_back(value);
cout << endl;
}
printVector(vett);
deletePeers(vett);
printVector(vett);
return 0;
}

View File

@ -0,0 +1,46 @@
/*
AUTORE: Vichingo455
The program deletes peer numbers from a vector using memmove
*/
#include <iostream>
#include <array>
#include <vector>
#include <cstdlib>
#include <cstring>
using namespace std;
void printVector(const vector<int> & vett) {
for (int i = 0; i < vett.size(); i++)
cout << vett.at(i) << " ";
cout << endl;
}
void deletePeers(vector<int>& vec) {
int* data = vec.data();
size_t size = vec.size();
size_t newSize = 0;
for (size_t i = 0; i < size; ++i) {
if (data[i] % 2 != 0) {
if (newSize != i) {
std::memmove(&data[newSize], &data[i], sizeof(int));
}
++newSize;
}
}
vec.resize(newSize);
}
int main() {
vector<int> vett;
int vectorSize;
cout << "Insert the size of the vector: ";
cin >> vectorSize;
cout << endl;
for (int i = 0,value; i < vectorSize; i++) {
cout << "Insert a value: ";
cin >> value;
vett.push_back(value);
cout << endl;
}
printVector(vett);
deletePeers(vett);
printVector(vett);
return 0;
}