From 0270a8c4b008d8249eb06f74705712f7469fe3a3 Mon Sep 17 00:00:00 2001 From: rbrtdambrosio Date: Thu, 12 Jan 2017 18:37:16 +0100 Subject: [PATCH 1/3] [c++] Challenge 1 (Unreviewed) --- challenge_1/cpp/rbrt/README.md | 3 ++ challenge_1/cpp/rbrt/src/[C++]Challenge1.cpp | 43 ++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 challenge_1/cpp/rbrt/README.md create mode 100644 challenge_1/cpp/rbrt/src/[C++]Challenge1.cpp diff --git a/challenge_1/cpp/rbrt/README.md b/challenge_1/cpp/rbrt/README.md new file mode 100644 index 000000000..f48293232 --- /dev/null +++ b/challenge_1/cpp/rbrt/README.md @@ -0,0 +1,3 @@ +The code works using inky the iostream library. +It works for input from the command line (multiple strings) +as well as run time input (single string). \ No newline at end of file diff --git a/challenge_1/cpp/rbrt/src/[C++]Challenge1.cpp b/challenge_1/cpp/rbrt/src/[C++]Challenge1.cpp new file mode 100644 index 000000000..3d304a5c1 --- /dev/null +++ b/challenge_1/cpp/rbrt/src/[C++]Challenge1.cpp @@ -0,0 +1,43 @@ +#include + +// I tried to solve this challenge using only the library + +using namespace std; + +int main(int argc, char* argv[]) +{ + // First we check inputs from the command line + // if there are no strings from the command line + // we wait for a string as input. + // The control (if) is don on the variable argv[1] + // argv[0] is reserved to the name of the program + + // In both cases the output is generated using a for loop. + // The starting point for reading the string, i.e. its end, + // is obtained with the function strlen() + + if (argv[1]!=NULL) + { + for (int i = argc - 1; i > 0; --i) + { + for (int j=strlen(argv[i]);j>=0;j--) + { + cout << argv[i][j]; + } + cout <> "; + cin>>x; + + for (int j=strlen(x);j>=0;j--) + { + cout << x[j]; + } + cout< Date: Mon, 16 Jan 2017 15:27:07 +0100 Subject: [PATCH 2/3] [cpp] Challenge 2 (Unreviewed) --- challenge_2/cpp/rbrt/README.md | 2 + challenge_2/cpp/rbrt/src/[C++]Challenge2.cpp | 67 ++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 challenge_2/cpp/rbrt/README.md create mode 100644 challenge_2/cpp/rbrt/src/[C++]Challenge2.cpp diff --git a/challenge_2/cpp/rbrt/README.md b/challenge_2/cpp/rbrt/README.md new file mode 100644 index 000000000..17739d01b --- /dev/null +++ b/challenge_2/cpp/rbrt/README.md @@ -0,0 +1,2 @@ +The code accept an array of any length and find the values that appear only once in the array. The complexity of the code is linear with the length of the array. +In order to obtain this result we generate an array where we stored how many time the values in the input array appear. In this way it is necessary to scan the list only once and update the values in the counters array. Once the reading of the input list is over we look for the element that appear only once, i.e. the one that has the counter == 1. \ No newline at end of file diff --git a/challenge_2/cpp/rbrt/src/[C++]Challenge2.cpp b/challenge_2/cpp/rbrt/src/[C++]Challenge2.cpp new file mode 100644 index 000000000..0f87606e9 --- /dev/null +++ b/challenge_2/cpp/rbrt/src/[C++]Challenge2.cpp @@ -0,0 +1,67 @@ +#include + + +using namespace std; + +int find_max(int x[],int n); //This function read an array and give as output the maximum element in the array + +int main() +{ + int n; + + //we accept as input a value representing the length of the array + //and we create an array of the given size + + cout<< "Please insert the array length >> "; + cin>>n; + int *x=new int[n+1]; + + cout<< "Please insert an array >> "; + + for(int i=0;i>x[i]; + } + + + //we generate an array from 1 to the maximum value present in the input array. + //Every time that a value occurs in the input array we increment it in ctrl + //To know which element occur once is sufficient to find the value in ctrl==1 + + int *ctrl=new int[find_max(x,n)+1]; + + for(int i=0;i Date: Mon, 16 Jan 2017 15:33:11 +0100 Subject: [PATCH 3/3] [cpp] Challenge 2 (Unreviewed) --- challenge_2/cpp/rbrt/src/[C++]Challenge2.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/challenge_2/cpp/rbrt/src/[C++]Challenge2.cpp b/challenge_2/cpp/rbrt/src/[C++]Challenge2.cpp index 0f87606e9..9eb216e12 100644 --- a/challenge_2/cpp/rbrt/src/[C++]Challenge2.cpp +++ b/challenge_2/cpp/rbrt/src/[C++]Challenge2.cpp @@ -12,6 +12,7 @@ int main() //we accept as input a value representing the length of the array //and we create an array of the given size + cout<< "Please insert the array length >> "; cin>>n; int *x=new int[n+1];