#include <iostream>
#include <memory>
#include<time.h>
void processElements(std::unique_ptr<int[]> &, int);
int main(int argc,char *args[]){
int elements;
srand(time(0));
do{
std::cout<<"Enter the number of items you'd like to store: ";
std::cin>>elements;
if(!std::cin){
std::cin.clear();
std::cin.ignore(50,'\n');
}
else
continue;
}while(elements<1);
std::unique_ptr<int[]> values(new int[elements]);
for (int i = 0; i < elements; ++i) {
values[i] = (i == 0) ? 1 : rand()%10+1;
}
for (int i = 0; i < elements; ++i) { //before function call
std::cout << i << ": " << values[i] << '\n';
}
processElements(values,elements);
for (int i = 0; i < elements; ++i) { //after function call
std::cout << i << ": " << values[i] << '\n';
}
return 0;
}
void processElements(std::unique_ptr<int[]> &toProcess, int size){
srand(time(0));
for (int i = 0; i < size; ++i) {
toProcess[i] = (i == 0) ? 1 : rand()%10+1;
}
for (int i = 0; i < size; ++i) {
std::cout << i << ": " << toProcess[i] << '\n';
}
}
No comments:
Post a Comment