From 4d5100d9b2104b26349ccc4dadb53cfe20051687 Mon Sep 17 00:00:00 2001 From: aviralkapoor <51873075+aviralkapoor@users.noreply.github.com> Date: Sat, 6 Jul 2019 10:25:33 +0530 Subject: [PATCH 01/14] Add files via upload --- main.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 main.c diff --git a/main.c b/main.c new file mode 100644 index 0000000..2430c99 --- /dev/null +++ b/main.c @@ -0,0 +1,87 @@ +#include +#include +struct count +{ +char s1[2],s2[2],s3[2]; +}; +int main() +{ + struct count m[18]; + int a[3][3],b[3][3],i,j,c[18],k=0,l=0,p=0,q=0,r=0; + printf("Enter the elements of 1st array.\n"); + for(i=0;i<3;i++) + { + for(j=0;j<3;j++) + { + scanf("%d",&a[i][j]); + } + } + printf("Enter the elements of 2nd array.\n"); + for(i=0;i<3;i++) + { + for(j=0;j<3;j++) + { + scanf("%d",&b[i][j]); + } + } + for(i=0;i<3;i++) + { + for(j=0;j<3;j++) + { + c[k]=a[i][j]; + k++; + } + } + for(i=0;i<3;i++) + { + for(j=0;j<3;j++) + { + c[k]=b[i][j]; + k++; + } + } + for(i=0;i<18;i++) + { + if(i>2&&i<=5) + { + q=1; + r=0; + } + if(i>5&&i<=8) + { + q=2; + r=0; + } + if(i>8&&i<=11) + { + p=1; + q=0; + r=0; + } + if(i>11&&i<=14) + { + p=1; + q=1; + r=0; + } + if(i>14&&i<=17) + { + p=1; + q=2; + r=0; + } + itoa(p,m[l].s1,10); + itoa(q,m[l].s2,10); + itoa(r,m[l].s3,10); + r++; + l++; + } + l=0; + printf("The linear arranged array is->\n"); + for(i=0;i<18;i++) + { + printf("%d Str Code is-%s%s%s\n",c[i],m[l].s1,m[l].s2,m[l].s3); + l++; + } + return 0; +} From 8c9fd21983111e1ecd44d206387f2b1ad493f86c Mon Sep 17 00:00:00 2001 From: frankhart2017 Date: Sat, 6 Jul 2019 15:55:05 +0530 Subject: [PATCH 02/14] Refactor --- .gitignore | 1 + main.c => src/main.c | 0 2 files changed, 1 insertion(+) rename main.c => src/main.c (100%) diff --git a/.gitignore b/.gitignore index e0e8c99..d55858e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ __pycache__ .DS_Store +a.out diff --git a/main.c b/src/main.c similarity index 100% rename from main.c rename to src/main.c From abe3b3e9815341b55d8164ceac2a07fc96563135 Mon Sep 17 00:00:00 2001 From: aviralkapoor <51873075+aviralkapoor@users.noreply.github.com> Date: Tue, 9 Jul 2019 00:34:24 +0530 Subject: [PATCH 03/14] For NXN arrays --- src/main.c | 66 +++++++++++++++++++++++++----------------------------- 1 file changed, 31 insertions(+), 35 deletions(-) diff --git a/src/main.c b/src/main.c index 2430c99..8fb48e1 100644 --- a/src/main.c +++ b/src/main.c @@ -6,81 +6,77 @@ char s1[2],s2[2],s3[2]; }; int main() { - struct count m[18]; - int a[3][3],b[3][3],i,j,c[18],k=0,l=0,p=0,q=0,r=0; + int a[3][3],b[3][3],i,j,k=0,l=0,p=0,q=0,r=-1,g,h,x=0,y=1; + printf("Enter the no. rows.\n"); + scanf("%d",&g); + printf("Enter the no. columns.\n"); + scanf("%d",&h); + int c[2*g*h]; + struct count m[2*g*h]; printf("Enter the elements of 1st array.\n"); - for(i=0;i<3;i++) + for(i=0;i2&&i<=5) + if(i>=x*h&&i5&&i<=8) - { - q=2; - r=0; + r++; } - if(i>8&&i<=11) + else if(i==g*h) { - p=1; + p++; q=0; r=0; + x++; + y++; + } - if(i>11&&i<=14) + else { - p=1; - q=1; - r=0; - } - if(i>14&&i<=17) - { - p=1; - q=2; - r=0; + x++; + y++; + q++; + r=0; } itoa(p,m[l].s1,10); itoa(q,m[l].s2,10); itoa(r,m[l].s3,10); - r++; l++; - } + } l=0; printf("The linear arranged array is->\n"); - for(i=0;i<18;i++) + for(i=0;i<2*g*h;i++) { - printf("%d Str Code is-%s%s%s\n",c[i],m[l].s1,m[l].s2,m[l].s3); + printf("%d Str Code is %s%s%s\n",c[i],m[l].s1,m[l].s2,m[l].s3); l++; } return 0; From 12a939e99f10edf0be6153f6f75eaab3394c4a0d Mon Sep 17 00:00:00 2001 From: aviralkapoor <51873075+aviralkapoor@users.noreply.github.com> Date: Sat, 10 Aug 2019 11:41:24 +0530 Subject: [PATCH 04/14] Delete main.c --- src/main.c | 83 ------------------------------------------------------ 1 file changed, 83 deletions(-) delete mode 100644 src/main.c diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 8fb48e1..0000000 --- a/src/main.c +++ /dev/null @@ -1,83 +0,0 @@ -#include -#include -struct count -{ -char s1[2],s2[2],s3[2]; -}; -int main() -{ - int a[3][3],b[3][3],i,j,k=0,l=0,p=0,q=0,r=-1,g,h,x=0,y=1; - printf("Enter the no. rows.\n"); - scanf("%d",&g); - printf("Enter the no. columns.\n"); - scanf("%d",&h); - int c[2*g*h]; - struct count m[2*g*h]; - printf("Enter the elements of 1st array.\n"); - for(i=0;i=x*h&&i\n"); - for(i=0;i<2*g*h;i++) - { - printf("%d Str Code is %s%s%s\n",c[i],m[l].s1,m[l].s2,m[l].s3); - l++; - } - return 0; -} From af780825c236d35878ef9f7d4f7e6b38d681aaf0 Mon Sep 17 00:00:00 2001 From: aviralkapoor <51873075+aviralkapoor@users.noreply.github.com> Date: Sat, 10 Aug 2019 11:44:02 +0530 Subject: [PATCH 05/14] Create numPP.hpp --- numPP.hpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 numPP.hpp diff --git a/numPP.hpp b/numPP.hpp new file mode 100644 index 0000000..e1ed00c --- /dev/null +++ b/numPP.hpp @@ -0,0 +1,25 @@ +#ifndef NUMPP_H +#define NUMPP_H + +#include + +using namespace std; + +template +class numpp { + private: + vector dim; + int ndim; + int shape_match(vector); + public: + numpp(vector); + vector data; + void shape(); + void reshape(vector); + void push_vec(vector, vector); + void add(vector); + void sub(vector); + +}; + +#endif From 76025717f03982a6b67ff9408f77164fd7db94ec Mon Sep 17 00:00:00 2001 From: aviralkapoor <51873075+aviralkapoor@users.noreply.github.com> Date: Sat, 10 Aug 2019 11:44:45 +0530 Subject: [PATCH 06/14] Create numPP.cpp --- numPP.cpp | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 numPP.cpp diff --git a/numPP.cpp b/numPP.cpp new file mode 100644 index 0000000..1ce1c1f --- /dev/null +++ b/numPP.cpp @@ -0,0 +1,93 @@ +#include +#include +#include + +#include "numpp.hpp" + +using namespace std; + +template +int numpp::shape_match(vector dim) { + if(this->dim.size() == dim.size()) { + for(int i=0; idim.size(); i++) + if(this->dim[i] < dim[i]) + return 0; + return 1; + } + + return 0; +} + +template +numpp::numpp(vector dim) { + this->dim = dim; + this->ndim = 1; + for(int i=0; idim.size(); i++) + this->ndim *= this->dim[i]; + try { + T *a = new T[this->ndim]; + delete[] a; + } catch(bad_alloc&) { + cerr<<"\033[1;31m Error\033[0m: Memory out of bounds!"; + } +} + +template +void numpp::shape() { + cout<<"{ "; + for(int i=0; idim.size()-1; i++) + cout<dim[i]<<", "; + cout<dim[this->dim.size() - 1]<<" }"; + +} +template +void numpp::add(vector data){ + cout<dim.size()*dim.size();i++) + { + cout<data[i]+data[i]<<" "; + } +} + +template +void numpp::sub(vector data){ + cout<dim.size()*dim.size();i++) + { + cout<data[i]-data[i]<<" "; + } +} + +template +void numpp::reshape(vector new_dim) { + this->ndim = 1; + int new_ndim = 1; + for(int i=0; idim.size(); i++) + this->ndim *= this->dim[i]; + for(int i=0; indim == new_ndim) { + cout<<"New dimensions: "; + cout<<"{ "; + for(int i=0; i +void numpp::push_vec(vector dim, vector data) { + if(shape_match(dim)) { + this->ndim = 1; + for(int i=0; indim *= dim[i]; + if(data.size() == this->ndim) + this->data = data; + else + cerr<<"\033[1;31m Error\033[0m: Dimension doesn't match declared numpp dimension!\n"; + } + else + cerr<<"\033[1;31m Error\033[0m: Dimension doesn't match declared numpp dimension!\n"; +} From 4edeeede2d19349bb669448906fcfa588123ac6a Mon Sep 17 00:00:00 2001 From: aviralkapoor <51873075+aviralkapoor@users.noreply.github.com> Date: Sat, 10 Aug 2019 11:45:16 +0530 Subject: [PATCH 07/14] Create test.cpp --- test.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 test.cpp diff --git a/test.cpp b/test.cpp new file mode 100644 index 0000000..453f669 --- /dev/null +++ b/test.cpp @@ -0,0 +1,29 @@ +#include +#include +#include +#include "numpp.cpp" + +using namespace std; + +using namespace std::chrono; + +int main() { + + numpp np({2, 2}); + numpp np1({2,2}); + + auto start = high_resolution_clock::now(); + + np.push_vec({2, 2}, {1, 2, 3, 4}); + np1.push_vec({2, 2}, {5, 4, 7, 8}); + + auto stop = high_resolution_clock::now(); + auto duration = duration_cast(stop - start); + + cout< Date: Sat, 10 Aug 2019 14:01:31 +0530 Subject: [PATCH 08/14] Update and rename numPP.cpp to numpp.cpp --- numPP.cpp => numpp.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) rename numPP.cpp => numpp.cpp (89%) diff --git a/numPP.cpp b/numpp.cpp similarity index 89% rename from numPP.cpp rename to numpp.cpp index 1ce1c1f..122eddd 100644 --- a/numPP.cpp +++ b/numpp.cpp @@ -40,21 +40,23 @@ void numpp::shape() { cout<dim[this->dim.size() - 1]<<" }"; } + template -void numpp::add(vector data){ +void numpp::add(numpp np1,numpp np2){ cout<dim.size()*dim.size();i++) { - cout<data[i]+data[i]<<" "; + this->data[i]=np1.data[i]+np2.data[i]; + cout<data[i]<<" "; } } - template -void numpp::sub(vector data){ +void numpp::sub(numpp np1,numpp np2){ cout<dim.size()*dim.size();i++) { - cout<data[i]-data[i]<<" "; + this->data[i]=np1.data[i]-np2.data[i]; + cout<data[i]<<" "; } } From 0cf637cc80673a397f618f8dbf7d5c7ba94f090c Mon Sep 17 00:00:00 2001 From: aviralkapoor <51873075+aviralkapoor@users.noreply.github.com> Date: Sat, 10 Aug 2019 14:02:13 +0530 Subject: [PATCH 09/14] Update and rename numPP.hpp to numpp.hpp --- numPP.hpp => numpp.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) rename numPP.hpp => numpp.hpp (86%) diff --git a/numPP.hpp b/numpp.hpp similarity index 86% rename from numPP.hpp rename to numpp.hpp index e1ed00c..18ded5d 100644 --- a/numPP.hpp +++ b/numpp.hpp @@ -9,16 +9,16 @@ template class numpp { private: vector dim; + vector data; int ndim; int shape_match(vector); public: numpp(vector); - vector data; void shape(); void reshape(vector); void push_vec(vector, vector); - void add(vector); - void sub(vector); + void add(numpp,numpp); + void sub(numpp,numpp); }; From 4bbba8065b7e225112cce4edf431cee0b95dede3 Mon Sep 17 00:00:00 2001 From: aviralkapoor <51873075+aviralkapoor@users.noreply.github.com> Date: Sat, 10 Aug 2019 14:02:43 +0530 Subject: [PATCH 10/14] Update test.cpp --- test.cpp | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/test.cpp b/test.cpp index 453f669..47cb99b 100644 --- a/test.cpp +++ b/test.cpp @@ -9,21 +9,24 @@ using namespace std::chrono; int main() { - numpp np({2, 2}); + numpp np({2,2}); numpp np1({2,2}); + numpp np2({2,2}); + numpp np3({2,2}); auto start = high_resolution_clock::now(); - np.push_vec({2, 2}, {1, 2, 3, 4}); - np1.push_vec({2, 2}, {5, 4, 7, 8}); - + np.push_vec({2,2}, {1,2,3,4}); + np1.push_vec({2,2}, {5,4,7,8}); + np2.push_vec({2,2}, {4,2,6,7}); + np3.push_vec({2,2}, {0,0,0,0}); + auto stop = high_resolution_clock::now(); auto duration = duration_cast(stop - start); - cout< Date: Wed, 14 Aug 2019 18:35:33 +0530 Subject: [PATCH 11/14] Fixes --- src/numpp.cpp | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/numpp.hpp | 5 +++++ 2 files changed, 65 insertions(+) diff --git a/src/numpp.cpp b/src/numpp.cpp index 8dd44e0..ca32559 100644 --- a/src/numpp.cpp +++ b/src/numpp.cpp @@ -22,6 +22,7 @@ template numpp::numpp(vector dim) { this->dim = dim; this->ndim = 1; + is_zero = false; for(int i=0; idim.size(); i++) this->ndim *= this->dim[i]; try { @@ -59,6 +60,65 @@ void numpp::reshape(vector new_dim) { cerr<<"\033[1;31m Error\033[0m: Invalid dimensions!"; } +template +uint64_t numpp::max() { + long long int maximum=data[0]; + long i=1; + while(i!=ndim) + { + if(data[i]>maximum) + maximum=data[i++]; + } + return maximum; + +} + +template +uint64_t numpp::min() { + long long int minimum=data[0]; + long i=1; + while(i!=ndim) + { + if(data[i] +void numpp::zeros() { + is_zero = true; +} + +template +void numpp::print() { + long i=1; + cout< void numpp::push_vec(vector dim, vector data) { if(shape_match(dim)) { diff --git a/src/numpp.hpp b/src/numpp.hpp index e0976f8..e981074 100644 --- a/src/numpp.hpp +++ b/src/numpp.hpp @@ -10,6 +10,7 @@ class numpp { private: vector dim; vector data; + bool is_zero; uint64_t ndim; int shape_match(vector); public: @@ -17,6 +18,10 @@ class numpp { void shape(); void reshape(vector); void push_vec(vector, vector); + uint64_t max(); + uint64_t min(); + void zeros(); + void print(); }; #endif From 68c6f9a9a24ab4d56de0738b2bea3d3fdbd41592 Mon Sep 17 00:00:00 2001 From: frankhart2017 Date: Thu, 15 Aug 2019 17:14:32 +0530 Subject: [PATCH 12/14] Add add() and sub() --- src/numpp.cpp | 19 +++++++++++++++++++ src/numpp.hpp | 2 ++ 2 files changed, 21 insertions(+) diff --git a/src/numpp.cpp b/src/numpp.cpp index ca32559..afcc429 100644 --- a/src/numpp.cpp +++ b/src/numpp.cpp @@ -133,3 +133,22 @@ void numpp::push_vec(vector dim, vector data) { else cerr<<"\033[1;31m Error\033[0m: Dimension doesn't match declared numpp dimension!\n"; } + +template +void numpp::add(numpp np1,numpp np2){ + cout<dim.size()*dim.size();i++) + { + this->data[i]=np1.data[i]+np2.data[i]; + cout<data[i]<<" "; + } +} +template +void numpp::sub(numpp np1,numpp np2){ + cout<dim.size()*dim.size();i++) + { + this->data[i]=np1.data[i]-np2.data[i]; + cout<data[i]<<" "; + } +} diff --git a/src/numpp.hpp b/src/numpp.hpp index e981074..fd02604 100644 --- a/src/numpp.hpp +++ b/src/numpp.hpp @@ -22,6 +22,8 @@ class numpp { uint64_t min(); void zeros(); void print(); + void add(numpp,numpp); + void sub(numpp,numpp); }; #endif From bdd59f57a27af58637eba1ed109c2f26687bcfbb Mon Sep 17 00:00:00 2001 From: frankhart2017 Date: Thu, 15 Aug 2019 17:36:19 +0530 Subject: [PATCH 13/14] Func merge --- src/numpp.cpp | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/numpp.hpp | 2 ++ 2 files changed, 90 insertions(+) diff --git a/src/numpp.cpp b/src/numpp.cpp index afcc429..3b31f75 100644 --- a/src/numpp.cpp +++ b/src/numpp.cpp @@ -152,3 +152,91 @@ void numpp::sub(numpp np1,numpp np2){ cout<data[i]<<" "; } } + +template +void numpp::search(T a, char* ch) { + if(ch == "linear") { + int count=0; + for(int i=0; i data[j+1]) { + temp=data[j]; + data[j]=data[j+1]; + data[j+1]=temp; + } + } + } + int count=0; + int start, end, middle; + start=0; + end=ndim-1; + for(int i=0;idata[middle]) + start=middle+1; + else + end=middle-1; + } + if(count>0) + cout< +void numpp::sort(char* ch) { + if(ch=="Bubble") { + int i, j; + T temp; + for (i = 0; i < ndim-1; i++) { + for (j = 0; j < ndim-i-1; j++) { + if (data[j] > data[j+1]) { + temp=data[j]; + data[j]=data[j+1]; + data[j+1]=temp; + } + } + } + } else if(ch=="Insertion") { + int i, j; + T key; + for (i = 1; i < ndim; i++) { + key = data[i]; + j = i - 1; + while (j >= 0 && data[j] > key) { + data[j + 1] = data[j]; + j = j - 1; + } + data[j + 1] = key; + } + } else if(ch=="Selection") { + int i, j, min_idx; + T temp; + + for (i = 0; i < ndim-1; i++) { + min_idx = i; + for (j = i+1; j < ndim; j++) + if (data[j] < data[min_idx]) + min_idx = j; + temp=data[min_idx]; + data[min_idx]=data[i]; + data[i]=temp; + } + for(i=0;i Date: Thu, 15 Aug 2019 18:00:46 +0530 Subject: [PATCH 14/14] Random from dist --- src/numpp.cpp | 29 +++++++++++++++++++++++------ src/numpp.hpp | 6 ++++-- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/numpp.cpp b/src/numpp.cpp index 3b31f75..0848eb6 100644 --- a/src/numpp.cpp +++ b/src/numpp.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include "numpp.hpp" @@ -110,13 +111,11 @@ void numpp::print() { if(ndim==1) cout<<" }"; else - { while(i!=ndim) - { - cout<<", "< @@ -154,7 +153,7 @@ void numpp::sub(numpp np1,numpp np2){ } template -void numpp::search(T a, char* ch) { +void numpp::search(T a, string ch) { if(ch == "linear") { int count=0; for(int i=0; i::search(T a, char* ch) { } template -void numpp::sort(char* ch) { +void numpp::sort(string ch) { if(ch=="Bubble") { int i, j; T temp; @@ -240,3 +239,21 @@ void numpp::sort(char* ch) { cout< + void numpp::random_normal(double mean, double stddev) { + default_random_engine generator; + normal_distribution distribution(mean, stddev); + this->data.clear(); + for(int i=0; indim; i++) + this->data.push_back(distribution(generator)); + } + + template + void numpp::random_uniform(double mean, double stddev) { + default_random_engine generator; + uniform_real_distribution distribution(mean, stddev); + this->data.clear(); + for(int i=0; indim; i++) + this->data.push_back(distribution(generator)); + } diff --git a/src/numpp.hpp b/src/numpp.hpp index 6664bb2..254f401 100644 --- a/src/numpp.hpp +++ b/src/numpp.hpp @@ -24,8 +24,10 @@ class numpp { void print(); void add(numpp,numpp); void sub(numpp,numpp); - void search(T, char*); - void sort(char*); + void search(T, string); + void sort(string); + void random_normal(double, double); + void random_uniform(double, double); }; #endif