diff --git a/.DS_Store b/.DS_Store index c88ae6e..c235873 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/README.md b/README.md index 53b49e8..3beef7b 100644 --- a/README.md +++ b/README.md @@ -4,4 +4,5 @@ 02. Basic Sorting Algorithms 03. Character Arrays/Strings 04. 2D Arrays -05. Pointers & Dynamic Memory \ No newline at end of file +05. Pointers & Dynamic Memory +06. Vector Data Structure \ No newline at end of file diff --git a/Vector Data Structure/01. Vector/.cproject b/Vector Data Structure/01. Vector/.cproject new file mode 100644 index 0000000..1ad1151 --- /dev/null +++ b/Vector Data Structure/01. Vector/.cproject @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Vector Data Structure/01. Vector/.project b/Vector Data Structure/01. Vector/.project new file mode 100644 index 0000000..8b9559e --- /dev/null +++ b/Vector Data Structure/01. Vector/.project @@ -0,0 +1,26 @@ + + + 01. Vector + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.core.ccnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + diff --git a/Vector Data Structure/01. Vector/.settings/language.settings.xml b/Vector Data Structure/01. Vector/.settings/language.settings.xml new file mode 100644 index 0000000..d55b887 --- /dev/null +++ b/Vector Data Structure/01. Vector/.settings/language.settings.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Vector Data Structure/01. Vector/Debug/01. Vector b/Vector Data Structure/01. Vector/Debug/01. Vector new file mode 100755 index 0000000..c07d87b Binary files /dev/null and b/Vector Data Structure/01. Vector/Debug/01. Vector differ diff --git a/Vector Data Structure/01. Vector/Debug/main.d b/Vector Data Structure/01. Vector/Debug/main.d new file mode 100644 index 0000000..a8355ba --- /dev/null +++ b/Vector Data Structure/01. Vector/Debug/main.d @@ -0,0 +1 @@ +main.o: ../main.cpp diff --git a/Vector Data Structure/01. Vector/Debug/main.o b/Vector Data Structure/01. Vector/Debug/main.o new file mode 100644 index 0000000..2d2a07c Binary files /dev/null and b/Vector Data Structure/01. Vector/Debug/main.o differ diff --git a/Vector Data Structure/01. Vector/Debug/makefile b/Vector Data Structure/01. Vector/Debug/makefile new file mode 100644 index 0000000..0038458 --- /dev/null +++ b/Vector Data Structure/01. Vector/Debug/makefile @@ -0,0 +1,71 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +-include ../makefile.init + +RM := rm -rf + +# All of the sources participating in the build are defined here +-include sources.mk +-include subdir.mk +-include objects.mk + +ifneq ($(MAKECMDGOALS),clean) +ifneq ($(strip $(C++_DEPS)),) +-include $(C++_DEPS) +endif +ifneq ($(strip $(CC_DEPS)),) +-include $(CC_DEPS) +endif +ifneq ($(strip $(CPP_DEPS)),) +-include $(CPP_DEPS) +endif +ifneq ($(strip $(CXX_DEPS)),) +-include $(CXX_DEPS) +endif +ifneq ($(strip $(C_DEPS)),) +-include $(C_DEPS) +endif +ifneq ($(strip $(C_UPPER_DEPS)),) +-include $(C_UPPER_DEPS) +endif +endif + +-include ../makefile.defs + +OPTIONAL_TOOL_DEPS := \ +$(wildcard ../makefile.defs) \ +$(wildcard ../makefile.init) \ +$(wildcard ../makefile.targets) \ + + +BUILD_ARTIFACT_NAME := 01. Vector +BUILD_ARTIFACT_EXTENSION := +BUILD_ARTIFACT_PREFIX := +BUILD_ARTIFACT := $(BUILD_ARTIFACT_PREFIX)$(BUILD_ARTIFACT_NAME)$(if $(BUILD_ARTIFACT_EXTENSION),.$(BUILD_ARTIFACT_EXTENSION),) + +# Add inputs and outputs from these tool invocations to the build variables + +# All Target +all: main-build + +# Main-build Target +main-build: 01.\ Vector + +# Tool invocations +01.\ Vector: $(OBJS) $(USER_OBJS) makefile objects.mk $(OPTIONAL_TOOL_DEPS) + @echo 'Building target: $@' + @echo 'Invoking: MacOS X C++ Linker' + g++ -o "01. Vector" $(OBJS) $(USER_OBJS) $(LIBS) + @echo 'Finished building target: $@' + @echo ' ' + +# Other Targets +clean: + -$(RM) 01.\\ Vector + -@echo ' ' + +.PHONY: all clean dependents main-build + +-include ../makefile.targets diff --git a/Vector Data Structure/01. Vector/Debug/objects.mk b/Vector Data Structure/01. Vector/Debug/objects.mk new file mode 100644 index 0000000..742c2da --- /dev/null +++ b/Vector Data Structure/01. Vector/Debug/objects.mk @@ -0,0 +1,8 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +USER_OBJS := + +LIBS := + diff --git a/Vector Data Structure/01. Vector/Debug/sources.mk b/Vector Data Structure/01. Vector/Debug/sources.mk new file mode 100644 index 0000000..c15c16c --- /dev/null +++ b/Vector Data Structure/01. Vector/Debug/sources.mk @@ -0,0 +1,27 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +ASM_SRCS := +C++_SRCS := +CC_SRCS := +CPP_SRCS := +CXX_SRCS := +C_SRCS := +C_UPPER_SRCS := +OBJ_SRCS := +O_SRCS := +S_UPPER_SRCS := +C++_DEPS := +CC_DEPS := +CPP_DEPS := +CXX_DEPS := +C_DEPS := +C_UPPER_DEPS := +EXECUTABLES := +OBJS := + +# Every subdirectory with source files must be described here +SUBDIRS := \ +. \ + diff --git a/Vector Data Structure/01. Vector/Debug/subdir.mk b/Vector Data Structure/01. Vector/Debug/subdir.mk new file mode 100644 index 0000000..9d81f4e --- /dev/null +++ b/Vector Data Structure/01. Vector/Debug/subdir.mk @@ -0,0 +1,31 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +# Add inputs and outputs from these tool invocations to the build variables +CPP_SRCS += \ +../main.cpp + +CPP_DEPS += \ +./main.d + +OBJS += \ +./main.o + + +# Each subdirectory must supply rules for building sources it contributes +%.o: ../%.cpp subdir.mk + @echo 'Building file: $<' + @echo 'Invoking: GCC C++ Compiler' + g++ -O0 -g3 -Wall -c -fmessage-length=0 --std=c++17 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$@" -o "$@" "$<" + @echo 'Finished building: $<' + @echo ' ' + + +clean: clean--2e- + +clean--2e-: + -$(RM) ./main.d ./main.o + +.PHONY: clean--2e- + diff --git a/Vector Data Structure/01. Vector/main.cpp b/Vector Data Structure/01. Vector/main.cpp new file mode 100644 index 0000000..e93333e --- /dev/null +++ b/Vector Data Structure/01. Vector/main.cpp @@ -0,0 +1,52 @@ +#include +#include +using namespace std; + +int main() { + vector arr; + + cout << arr.capacity() << endl; + arr.push_back(0); + cout << arr.capacity() << endl; + arr.push_back(1); + cout << arr.capacity() << endl; + arr.push_back(2); + cout << arr.capacity() << endl; + arr.push_back(3); + cout << arr.capacity() << endl; + arr.push_back(4); + cout << arr.capacity() << endl; + + cout << arr.size() << " elements exist in the vector" << endl; + + for (int i = 0; i < arr.size(); i++) { + cout << arr[i] << " "; + } + cout << endl; + + // pop element + arr.pop_back(); + + for (int i = 0; i < arr.size(); i++) { + cout << arr[i] << " "; + } + cout << endl; + + cout << "Is vector empty? " << arr.empty() << endl; + + // insert element at a given index + arr.insert(arr.begin() + 2, 10); + + for (int i = 0; i < arr.size(); i++) { + cout << arr[i] << " "; + } + cout << endl; + + // initialize a vector + vector x = {1, 2, 3, 4, 5}; + + // fill constructor initialization + vector y(10, 0); // 10 elements initialized to zero + + return 0; +} diff --git a/Vector Data Structure/02. 2D Vector using STL/.cproject b/Vector Data Structure/02. 2D Vector using STL/.cproject new file mode 100644 index 0000000..32dbe25 --- /dev/null +++ b/Vector Data Structure/02. 2D Vector using STL/.cproject @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Vector Data Structure/02. 2D Vector using STL/.project b/Vector Data Structure/02. 2D Vector using STL/.project new file mode 100644 index 0000000..80f7b1c --- /dev/null +++ b/Vector Data Structure/02. 2D Vector using STL/.project @@ -0,0 +1,26 @@ + + + 02. 2D Vector using STL + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.core.ccnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + diff --git a/Vector Data Structure/02. 2D Vector using STL/.settings/language.settings.xml b/Vector Data Structure/02. 2D Vector using STL/.settings/language.settings.xml new file mode 100644 index 0000000..630a41a --- /dev/null +++ b/Vector Data Structure/02. 2D Vector using STL/.settings/language.settings.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Vector Data Structure/02. 2D Vector using STL/Debug/02. 2D Vector using STL b/Vector Data Structure/02. 2D Vector using STL/Debug/02. 2D Vector using STL new file mode 100755 index 0000000..be29637 Binary files /dev/null and b/Vector Data Structure/02. 2D Vector using STL/Debug/02. 2D Vector using STL differ diff --git a/Vector Data Structure/02. 2D Vector using STL/Debug/main.d b/Vector Data Structure/02. 2D Vector using STL/Debug/main.d new file mode 100644 index 0000000..a8355ba --- /dev/null +++ b/Vector Data Structure/02. 2D Vector using STL/Debug/main.d @@ -0,0 +1 @@ +main.o: ../main.cpp diff --git a/Vector Data Structure/02. 2D Vector using STL/Debug/main.o b/Vector Data Structure/02. 2D Vector using STL/Debug/main.o new file mode 100644 index 0000000..7b29125 Binary files /dev/null and b/Vector Data Structure/02. 2D Vector using STL/Debug/main.o differ diff --git a/Vector Data Structure/02. 2D Vector using STL/Debug/makefile b/Vector Data Structure/02. 2D Vector using STL/Debug/makefile new file mode 100644 index 0000000..d9b737d --- /dev/null +++ b/Vector Data Structure/02. 2D Vector using STL/Debug/makefile @@ -0,0 +1,71 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +-include ../makefile.init + +RM := rm -rf + +# All of the sources participating in the build are defined here +-include sources.mk +-include subdir.mk +-include objects.mk + +ifneq ($(MAKECMDGOALS),clean) +ifneq ($(strip $(C++_DEPS)),) +-include $(C++_DEPS) +endif +ifneq ($(strip $(CC_DEPS)),) +-include $(CC_DEPS) +endif +ifneq ($(strip $(CPP_DEPS)),) +-include $(CPP_DEPS) +endif +ifneq ($(strip $(CXX_DEPS)),) +-include $(CXX_DEPS) +endif +ifneq ($(strip $(C_DEPS)),) +-include $(C_DEPS) +endif +ifneq ($(strip $(C_UPPER_DEPS)),) +-include $(C_UPPER_DEPS) +endif +endif + +-include ../makefile.defs + +OPTIONAL_TOOL_DEPS := \ +$(wildcard ../makefile.defs) \ +$(wildcard ../makefile.init) \ +$(wildcard ../makefile.targets) \ + + +BUILD_ARTIFACT_NAME := 02. 2D Vector using STL +BUILD_ARTIFACT_EXTENSION := +BUILD_ARTIFACT_PREFIX := +BUILD_ARTIFACT := $(BUILD_ARTIFACT_PREFIX)$(BUILD_ARTIFACT_NAME)$(if $(BUILD_ARTIFACT_EXTENSION),.$(BUILD_ARTIFACT_EXTENSION),) + +# Add inputs and outputs from these tool invocations to the build variables + +# All Target +all: main-build + +# Main-build Target +main-build: 02.\ 2D\ Vector\ using\ STL + +# Tool invocations +02.\ 2D\ Vector\ using\ STL: $(OBJS) $(USER_OBJS) makefile objects.mk $(OPTIONAL_TOOL_DEPS) + @echo 'Building target: $@' + @echo 'Invoking: MacOS X C++ Linker' + g++ -o "02. 2D Vector using STL" $(OBJS) $(USER_OBJS) $(LIBS) + @echo 'Finished building target: $@' + @echo ' ' + +# Other Targets +clean: + -$(RM) 02.\\ 2D\\ Vector\\ using\\ STL + -@echo ' ' + +.PHONY: all clean dependents main-build + +-include ../makefile.targets diff --git a/Vector Data Structure/02. 2D Vector using STL/Debug/objects.mk b/Vector Data Structure/02. 2D Vector using STL/Debug/objects.mk new file mode 100644 index 0000000..742c2da --- /dev/null +++ b/Vector Data Structure/02. 2D Vector using STL/Debug/objects.mk @@ -0,0 +1,8 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +USER_OBJS := + +LIBS := + diff --git a/Vector Data Structure/02. 2D Vector using STL/Debug/sources.mk b/Vector Data Structure/02. 2D Vector using STL/Debug/sources.mk new file mode 100644 index 0000000..c15c16c --- /dev/null +++ b/Vector Data Structure/02. 2D Vector using STL/Debug/sources.mk @@ -0,0 +1,27 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +ASM_SRCS := +C++_SRCS := +CC_SRCS := +CPP_SRCS := +CXX_SRCS := +C_SRCS := +C_UPPER_SRCS := +OBJ_SRCS := +O_SRCS := +S_UPPER_SRCS := +C++_DEPS := +CC_DEPS := +CPP_DEPS := +CXX_DEPS := +C_DEPS := +C_UPPER_DEPS := +EXECUTABLES := +OBJS := + +# Every subdirectory with source files must be described here +SUBDIRS := \ +. \ + diff --git a/Vector Data Structure/02. 2D Vector using STL/Debug/subdir.mk b/Vector Data Structure/02. 2D Vector using STL/Debug/subdir.mk new file mode 100644 index 0000000..9d81f4e --- /dev/null +++ b/Vector Data Structure/02. 2D Vector using STL/Debug/subdir.mk @@ -0,0 +1,31 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +# Add inputs and outputs from these tool invocations to the build variables +CPP_SRCS += \ +../main.cpp + +CPP_DEPS += \ +./main.d + +OBJS += \ +./main.o + + +# Each subdirectory must supply rules for building sources it contributes +%.o: ../%.cpp subdir.mk + @echo 'Building file: $<' + @echo 'Invoking: GCC C++ Compiler' + g++ -O0 -g3 -Wall -c -fmessage-length=0 --std=c++17 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$@" -o "$@" "$<" + @echo 'Finished building: $<' + @echo ' ' + + +clean: clean--2e- + +clean--2e-: + -$(RM) ./main.d ./main.o + +.PHONY: clean--2e- + diff --git a/Vector Data Structure/02. 2D Vector using STL/main.cpp b/Vector Data Structure/02. 2D Vector using STL/main.cpp new file mode 100644 index 0000000..5b73d61 --- /dev/null +++ b/Vector Data Structure/02. 2D Vector using STL/main.cpp @@ -0,0 +1,24 @@ +#include +#include +using namespace std; + +int main() { + // each row can have different number of elements + vector> arr = { + {1, 2, 3}, + {4, 5, 6}, + {7, 8, 9, 10}, + {11, 12} + }; + + arr[0][0] += 10; + + for (int i = 0; i < arr.size(); i++) { + for (int x : arr[i]) { + cout << x << " "; + } + cout << endl; + } + + return 0; +} diff --git a/Vector Data Structure/03. Vector Class/.cproject b/Vector Data Structure/03. Vector Class/.cproject new file mode 100644 index 0000000..03634a3 --- /dev/null +++ b/Vector Data Structure/03. Vector Class/.cproject @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Vector Data Structure/03. Vector Class/.project b/Vector Data Structure/03. Vector Class/.project new file mode 100644 index 0000000..f2b0f8d --- /dev/null +++ b/Vector Data Structure/03. Vector Class/.project @@ -0,0 +1,26 @@ + + + 03. Vector Class + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.core.ccnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + diff --git a/Vector Data Structure/03. Vector Class/.settings/language.settings.xml b/Vector Data Structure/03. Vector Class/.settings/language.settings.xml new file mode 100644 index 0000000..96038e5 --- /dev/null +++ b/Vector Data Structure/03. Vector Class/.settings/language.settings.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Vector Data Structure/03. Vector Class/Debug/03. Vector Class b/Vector Data Structure/03. Vector Class/Debug/03. Vector Class new file mode 100755 index 0000000..670ee6e Binary files /dev/null and b/Vector Data Structure/03. Vector Class/Debug/03. Vector Class differ diff --git a/Vector Data Structure/03. Vector Class/Debug/main.d b/Vector Data Structure/03. Vector Class/Debug/main.d new file mode 100644 index 0000000..a2df3cd --- /dev/null +++ b/Vector Data Structure/03. Vector Class/Debug/main.d @@ -0,0 +1,3 @@ +main.o: ../main.cpp ../vector.h + +../vector.h: diff --git a/Vector Data Structure/03. Vector Class/Debug/main.o b/Vector Data Structure/03. Vector Class/Debug/main.o new file mode 100644 index 0000000..5df7ee1 Binary files /dev/null and b/Vector Data Structure/03. Vector Class/Debug/main.o differ diff --git a/Vector Data Structure/03. Vector Class/Debug/makefile b/Vector Data Structure/03. Vector Class/Debug/makefile new file mode 100644 index 0000000..c6af44c --- /dev/null +++ b/Vector Data Structure/03. Vector Class/Debug/makefile @@ -0,0 +1,71 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +-include ../makefile.init + +RM := rm -rf + +# All of the sources participating in the build are defined here +-include sources.mk +-include subdir.mk +-include objects.mk + +ifneq ($(MAKECMDGOALS),clean) +ifneq ($(strip $(C++_DEPS)),) +-include $(C++_DEPS) +endif +ifneq ($(strip $(CC_DEPS)),) +-include $(CC_DEPS) +endif +ifneq ($(strip $(CPP_DEPS)),) +-include $(CPP_DEPS) +endif +ifneq ($(strip $(CXX_DEPS)),) +-include $(CXX_DEPS) +endif +ifneq ($(strip $(C_DEPS)),) +-include $(C_DEPS) +endif +ifneq ($(strip $(C_UPPER_DEPS)),) +-include $(C_UPPER_DEPS) +endif +endif + +-include ../makefile.defs + +OPTIONAL_TOOL_DEPS := \ +$(wildcard ../makefile.defs) \ +$(wildcard ../makefile.init) \ +$(wildcard ../makefile.targets) \ + + +BUILD_ARTIFACT_NAME := 03. Vector Class +BUILD_ARTIFACT_EXTENSION := +BUILD_ARTIFACT_PREFIX := +BUILD_ARTIFACT := $(BUILD_ARTIFACT_PREFIX)$(BUILD_ARTIFACT_NAME)$(if $(BUILD_ARTIFACT_EXTENSION),.$(BUILD_ARTIFACT_EXTENSION),) + +# Add inputs and outputs from these tool invocations to the build variables + +# All Target +all: main-build + +# Main-build Target +main-build: 03.\ Vector\ Class + +# Tool invocations +03.\ Vector\ Class: $(OBJS) $(USER_OBJS) makefile objects.mk $(OPTIONAL_TOOL_DEPS) + @echo 'Building target: $@' + @echo 'Invoking: MacOS X C++ Linker' + g++ -o "03. Vector Class" $(OBJS) $(USER_OBJS) $(LIBS) + @echo 'Finished building target: $@' + @echo ' ' + +# Other Targets +clean: + -$(RM) 03.\\ Vector\\ Class + -@echo ' ' + +.PHONY: all clean dependents main-build + +-include ../makefile.targets diff --git a/Vector Data Structure/03. Vector Class/Debug/objects.mk b/Vector Data Structure/03. Vector Class/Debug/objects.mk new file mode 100644 index 0000000..742c2da --- /dev/null +++ b/Vector Data Structure/03. Vector Class/Debug/objects.mk @@ -0,0 +1,8 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +USER_OBJS := + +LIBS := + diff --git a/Vector Data Structure/03. Vector Class/Debug/sources.mk b/Vector Data Structure/03. Vector Class/Debug/sources.mk new file mode 100644 index 0000000..c15c16c --- /dev/null +++ b/Vector Data Structure/03. Vector Class/Debug/sources.mk @@ -0,0 +1,27 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +ASM_SRCS := +C++_SRCS := +CC_SRCS := +CPP_SRCS := +CXX_SRCS := +C_SRCS := +C_UPPER_SRCS := +OBJ_SRCS := +O_SRCS := +S_UPPER_SRCS := +C++_DEPS := +CC_DEPS := +CPP_DEPS := +CXX_DEPS := +C_DEPS := +C_UPPER_DEPS := +EXECUTABLES := +OBJS := + +# Every subdirectory with source files must be described here +SUBDIRS := \ +. \ + diff --git a/Vector Data Structure/03. Vector Class/Debug/subdir.mk b/Vector Data Structure/03. Vector Class/Debug/subdir.mk new file mode 100644 index 0000000..9d81f4e --- /dev/null +++ b/Vector Data Structure/03. Vector Class/Debug/subdir.mk @@ -0,0 +1,31 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +# Add inputs and outputs from these tool invocations to the build variables +CPP_SRCS += \ +../main.cpp + +CPP_DEPS += \ +./main.d + +OBJS += \ +./main.o + + +# Each subdirectory must supply rules for building sources it contributes +%.o: ../%.cpp subdir.mk + @echo 'Building file: $<' + @echo 'Invoking: GCC C++ Compiler' + g++ -O0 -g3 -Wall -c -fmessage-length=0 --std=c++17 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$@" -o "$@" "$<" + @echo 'Finished building: $<' + @echo ' ' + + +clean: clean--2e- + +clean--2e-: + -$(RM) ./main.d ./main.o + +.PHONY: clean--2e- + diff --git a/Vector Data Structure/03. Vector Class/main.cpp b/Vector Data Structure/03. Vector Class/main.cpp new file mode 100644 index 0000000..a4558e4 --- /dev/null +++ b/Vector Data Structure/03. Vector Class/main.cpp @@ -0,0 +1,88 @@ +#include +#include "vector.h" +using namespace std; + +int main() { + Vector v(2); + int i; + + cout << "Initial size: " << v.size() << endl; + cout << "Initial capacity: " << v.capacity() << endl; + cout << "Empty? " << v.empty() << endl; + + cout << "Push element: " << endl; + cin >> i; + v.push_back(i); + cout << "Vector size: " << v.size() << endl; + cout << "Vector capacity: " << v.capacity() << endl; + for (int i = 0; i < v.size(); i++) { + cout << v.at(i) << " "; + } + cout << endl; + + cout << "Push element: " << endl; + cin >> i; + v.push_back(i); + cout << "Vector size: " << v.size() << endl; + cout << "Vector capacity: " << v.capacity() << endl; + for (int i = 0; i < v.size(); i++) { + cout << v.at(i) << " "; + } + cout << endl; + + cout << "Push element: " << endl; + cin >> i; + v.push_back(i); + cout << "Vector size: " << v.size() << endl; + cout << "Vector capacity: " << v.capacity() << endl; + for (int i = 0; i < v.size(); i++) { + cout << v[i] << " "; + } + cout << endl; + + cout << "Front: " << v.front() << endl; + cout << "Back: " << v.back() << endl; + cout << "Empty? " << v.empty() << endl; + + cout << endl; + + // char vector + Vector vc(2); + + cout << "Initial size: " << vc.size() << endl; + cout << "Initial capacity: " << vc.capacity() << endl; + cout << "Empty? " << vc.empty() << endl; + + cout << "Push element: " << endl; + vc.push_back('a'); + cout << "Vector size: " << vc.size() << endl; + cout << "Vector capacity: " << vc.capacity() << endl; + for (int i = 0; i < vc.size(); i++) { + cout << vc.at(i) << " "; + } + cout << endl; + + cout << "Push element: " << endl; + vc.push_back('b'); + cout << "Vector size: " << vc.size() << endl; + cout << "Vector capacity: " << vc.capacity() << endl; + for (int i = 0; i < vc.size(); i++) { + cout << vc.at(i) << " "; + } + cout << endl; + + cout << "Push element: " << endl; + vc.push_back('c'); + cout << "Vector size: " << vc.size() << endl; + cout << "Vector capacity: " << vc.capacity() << endl; + for (int i = 0; i < vc.size(); i++) { + cout << vc[i] << " "; + } + cout << endl; + + cout << "Front: " << vc.front() << endl; + cout << "Back: " << vc.back() << endl; + cout << "Empty? " << vc.empty() << endl; + + return 0; +} diff --git a/Vector Data Structure/03. Vector Class/vector.h b/Vector Data Structure/03. Vector Class/vector.h new file mode 100644 index 0000000..64bdd11 --- /dev/null +++ b/Vector Data Structure/03. Vector Class/vector.h @@ -0,0 +1,68 @@ +template +class Vector { + // data members + T * arr; + int s; + int c; + +public: + // constructor + Vector(int max_s = 0) { + s = 0; + c = max_s; + arr = new T[c]; + } + + // methods + void push_back(const T d) { + if (s == c) { + // create a new array with double the size + c = c ? c * 2 : 1; + T *new_arr = new T[c]; + for (int i = 0; i < s; i++) { + new_arr[i] = arr[i]; + } + + // delete old array + delete [] arr; + + // old array = new array + arr = new_arr; + } + arr[s++] = d; + } + + void pop_back() { + if (s > 0) + s--; + } + + bool empty() const { + return s == 0; + } + + T front() const { + return arr[0]; + } + + T back() const { + return arr[s-1]; + } + + T at(int i) const { + return arr[i]; + } + + int size() const { // const function that doesn't change any data members of the class + return s; + } + + int capacity() const { + return c; + } + + // operator overloading + T operator[](const int i) const { // i doesn't change in function + return arr[i]; + } +}; diff --git a/Vector Data Structure/04. Inbuilt Searching/.cproject b/Vector Data Structure/04. Inbuilt Searching/.cproject new file mode 100644 index 0000000..a1358e4 --- /dev/null +++ b/Vector Data Structure/04. Inbuilt Searching/.cproject @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Vector Data Structure/04. Inbuilt Searching/.project b/Vector Data Structure/04. Inbuilt Searching/.project new file mode 100644 index 0000000..04b0cf8 --- /dev/null +++ b/Vector Data Structure/04. Inbuilt Searching/.project @@ -0,0 +1,26 @@ + + + 04. Inbuilt Searching + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.core.ccnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + diff --git a/Vector Data Structure/04. Inbuilt Searching/.settings/language.settings.xml b/Vector Data Structure/04. Inbuilt Searching/.settings/language.settings.xml new file mode 100644 index 0000000..e7cb9db --- /dev/null +++ b/Vector Data Structure/04. Inbuilt Searching/.settings/language.settings.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Vector Data Structure/04. Inbuilt Searching/Debug/04. Inbuilt Searching b/Vector Data Structure/04. Inbuilt Searching/Debug/04. Inbuilt Searching new file mode 100755 index 0000000..df91e85 Binary files /dev/null and b/Vector Data Structure/04. Inbuilt Searching/Debug/04. Inbuilt Searching differ diff --git a/Vector Data Structure/04. Inbuilt Searching/Debug/main.d b/Vector Data Structure/04. Inbuilt Searching/Debug/main.d new file mode 100644 index 0000000..a8355ba --- /dev/null +++ b/Vector Data Structure/04. Inbuilt Searching/Debug/main.d @@ -0,0 +1 @@ +main.o: ../main.cpp diff --git a/Vector Data Structure/04. Inbuilt Searching/Debug/main.o b/Vector Data Structure/04. Inbuilt Searching/Debug/main.o new file mode 100644 index 0000000..c41ceab Binary files /dev/null and b/Vector Data Structure/04. Inbuilt Searching/Debug/main.o differ diff --git a/Vector Data Structure/04. Inbuilt Searching/Debug/makefile b/Vector Data Structure/04. Inbuilt Searching/Debug/makefile new file mode 100644 index 0000000..43aa41c --- /dev/null +++ b/Vector Data Structure/04. Inbuilt Searching/Debug/makefile @@ -0,0 +1,71 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +-include ../makefile.init + +RM := rm -rf + +# All of the sources participating in the build are defined here +-include sources.mk +-include subdir.mk +-include objects.mk + +ifneq ($(MAKECMDGOALS),clean) +ifneq ($(strip $(C++_DEPS)),) +-include $(C++_DEPS) +endif +ifneq ($(strip $(CC_DEPS)),) +-include $(CC_DEPS) +endif +ifneq ($(strip $(CPP_DEPS)),) +-include $(CPP_DEPS) +endif +ifneq ($(strip $(CXX_DEPS)),) +-include $(CXX_DEPS) +endif +ifneq ($(strip $(C_DEPS)),) +-include $(C_DEPS) +endif +ifneq ($(strip $(C_UPPER_DEPS)),) +-include $(C_UPPER_DEPS) +endif +endif + +-include ../makefile.defs + +OPTIONAL_TOOL_DEPS := \ +$(wildcard ../makefile.defs) \ +$(wildcard ../makefile.init) \ +$(wildcard ../makefile.targets) \ + + +BUILD_ARTIFACT_NAME := 04. Inbuilt Searching +BUILD_ARTIFACT_EXTENSION := +BUILD_ARTIFACT_PREFIX := +BUILD_ARTIFACT := $(BUILD_ARTIFACT_PREFIX)$(BUILD_ARTIFACT_NAME)$(if $(BUILD_ARTIFACT_EXTENSION),.$(BUILD_ARTIFACT_EXTENSION),) + +# Add inputs and outputs from these tool invocations to the build variables + +# All Target +all: main-build + +# Main-build Target +main-build: 04.\ Inbuilt\ Searching + +# Tool invocations +04.\ Inbuilt\ Searching: $(OBJS) $(USER_OBJS) makefile objects.mk $(OPTIONAL_TOOL_DEPS) + @echo 'Building target: $@' + @echo 'Invoking: MacOS X C++ Linker' + g++ -o "04. Inbuilt Searching" $(OBJS) $(USER_OBJS) $(LIBS) + @echo 'Finished building target: $@' + @echo ' ' + +# Other Targets +clean: + -$(RM) 04.\\ Inbuilt\\ Searching + -@echo ' ' + +.PHONY: all clean dependents main-build + +-include ../makefile.targets diff --git a/Vector Data Structure/04. Inbuilt Searching/Debug/objects.mk b/Vector Data Structure/04. Inbuilt Searching/Debug/objects.mk new file mode 100644 index 0000000..742c2da --- /dev/null +++ b/Vector Data Structure/04. Inbuilt Searching/Debug/objects.mk @@ -0,0 +1,8 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +USER_OBJS := + +LIBS := + diff --git a/Vector Data Structure/04. Inbuilt Searching/Debug/sources.mk b/Vector Data Structure/04. Inbuilt Searching/Debug/sources.mk new file mode 100644 index 0000000..c15c16c --- /dev/null +++ b/Vector Data Structure/04. Inbuilt Searching/Debug/sources.mk @@ -0,0 +1,27 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +ASM_SRCS := +C++_SRCS := +CC_SRCS := +CPP_SRCS := +CXX_SRCS := +C_SRCS := +C_UPPER_SRCS := +OBJ_SRCS := +O_SRCS := +S_UPPER_SRCS := +C++_DEPS := +CC_DEPS := +CPP_DEPS := +CXX_DEPS := +C_DEPS := +C_UPPER_DEPS := +EXECUTABLES := +OBJS := + +# Every subdirectory with source files must be described here +SUBDIRS := \ +. \ + diff --git a/Vector Data Structure/04. Inbuilt Searching/Debug/subdir.mk b/Vector Data Structure/04. Inbuilt Searching/Debug/subdir.mk new file mode 100644 index 0000000..9d81f4e --- /dev/null +++ b/Vector Data Structure/04. Inbuilt Searching/Debug/subdir.mk @@ -0,0 +1,31 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +# Add inputs and outputs from these tool invocations to the build variables +CPP_SRCS += \ +../main.cpp + +CPP_DEPS += \ +./main.d + +OBJS += \ +./main.o + + +# Each subdirectory must supply rules for building sources it contributes +%.o: ../%.cpp subdir.mk + @echo 'Building file: $<' + @echo 'Invoking: GCC C++ Compiler' + g++ -O0 -g3 -Wall -c -fmessage-length=0 --std=c++17 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$@" -o "$@" "$<" + @echo 'Finished building: $<' + @echo ' ' + + +clean: clean--2e- + +clean--2e-: + -$(RM) ./main.d ./main.o + +.PHONY: clean--2e- + diff --git a/Vector Data Structure/04. Inbuilt Searching/main.cpp b/Vector Data Structure/04. Inbuilt Searching/main.cpp new file mode 100644 index 0000000..3befa94 --- /dev/null +++ b/Vector Data Structure/04. Inbuilt Searching/main.cpp @@ -0,0 +1,67 @@ +#include +#include +#include +using namespace std; + +bool predicate(int haystack_element, int arr_element) { + return (haystack_element == arr_element-1); +} + +int main() { + vector arr = {10, 11, 2, 3, 4, 6, 7, 8}; + int key = 11; + + // linear search + vector::iterator it = find(arr.begin(), arr.end(), key); // address of the element + + if (it == arr.end()) + cout << "Element NOT found!" << endl; + else + cout << "Element at index: " << it - arr.begin() << endl; // address - base_address + + // search for sequence of elements + vector haystack; + + for (int i =1; i < 10; i++) haystack.push_back(i * 10); // haystack: 10 20 30 40 50 60 70 80 90 + + int n = 4; + int needle[] = {40, 50, 60, 70}; + it = search(haystack.begin(), haystack.end(), needle, needle+n); + + if (it != haystack.end()) + cout << "Needle found at position " << (it - haystack.begin()) << endl; + else + cout << "Needle NOT found!\n"; + + needle[0] = 41; + needle[1] = 51; + needle[2] = 61; + needle[3] = 71; + it = search(haystack.begin(), haystack.end(), needle, needle+n, predicate); + + if (it != haystack.end()) + cout << "Needle found at position " << (it - haystack.begin()) << endl; + else + cout << "Needle NOT found!\n"; + + vector hs = {1, 2, 3, 4, 6, 7, 8, 9}; + // binary_search() - returns bool - O(logn) + cout << binary_search(hs.begin(), hs.end(), 4) << endl; + cout << binary_search(hs.begin(), hs.end(), 5) << endl; + + // lower_bound() + auto lower = lower_bound(hs.begin(), hs.end(), 4); + cout << *lower << " at index " << (lower - hs.begin()) << endl; + // Search for first element x such that 5 ≤ x + lower = lower_bound(hs.begin(), hs.end(), 5); + cout << *lower << " at index " << (lower - hs.begin()) << endl; + + // upper_bound() + auto upper = upper_bound(hs.begin(), hs.end(), 4); + cout << *upper << " at index " << (upper - hs.begin()) << endl; + // Search first element that is greater than 5 + upper = upper_bound(hs.begin(), hs.end(), 5); + cout << *upper << " at index " << (upper - hs.begin()) << endl; + + return 0; +} diff --git a/Vector Data Structure/05. Sorting a Complex Vector!/.cproject b/Vector Data Structure/05. Sorting a Complex Vector!/.cproject new file mode 100644 index 0000000..6048be4 --- /dev/null +++ b/Vector Data Structure/05. Sorting a Complex Vector!/.cproject @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Vector Data Structure/05. Sorting a Complex Vector!/.project b/Vector Data Structure/05. Sorting a Complex Vector!/.project new file mode 100644 index 0000000..228d42d --- /dev/null +++ b/Vector Data Structure/05. Sorting a Complex Vector!/.project @@ -0,0 +1,26 @@ + + + 05. Sorting a Complex Vector! + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.core.ccnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + diff --git a/Vector Data Structure/05. Sorting a Complex Vector!/.settings/language.settings.xml b/Vector Data Structure/05. Sorting a Complex Vector!/.settings/language.settings.xml new file mode 100644 index 0000000..9765dfb --- /dev/null +++ b/Vector Data Structure/05. Sorting a Complex Vector!/.settings/language.settings.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Vector Data Structure/05. Sorting a Complex Vector!/Debug/05. Sorting a Complex Vector! b/Vector Data Structure/05. Sorting a Complex Vector!/Debug/05. Sorting a Complex Vector! new file mode 100755 index 0000000..4a6ff3e Binary files /dev/null and b/Vector Data Structure/05. Sorting a Complex Vector!/Debug/05. Sorting a Complex Vector! differ diff --git a/Vector Data Structure/05. Sorting a Complex Vector!/Debug/main.d b/Vector Data Structure/05. Sorting a Complex Vector!/Debug/main.d new file mode 100644 index 0000000..a8355ba --- /dev/null +++ b/Vector Data Structure/05. Sorting a Complex Vector!/Debug/main.d @@ -0,0 +1 @@ +main.o: ../main.cpp diff --git a/Vector Data Structure/05. Sorting a Complex Vector!/Debug/main.o b/Vector Data Structure/05. Sorting a Complex Vector!/Debug/main.o new file mode 100644 index 0000000..b44db6d Binary files /dev/null and b/Vector Data Structure/05. Sorting a Complex Vector!/Debug/main.o differ diff --git a/Vector Data Structure/05. Sorting a Complex Vector!/Debug/makefile b/Vector Data Structure/05. Sorting a Complex Vector!/Debug/makefile new file mode 100644 index 0000000..8422878 --- /dev/null +++ b/Vector Data Structure/05. Sorting a Complex Vector!/Debug/makefile @@ -0,0 +1,71 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +-include ../makefile.init + +RM := rm -rf + +# All of the sources participating in the build are defined here +-include sources.mk +-include subdir.mk +-include objects.mk + +ifneq ($(MAKECMDGOALS),clean) +ifneq ($(strip $(C++_DEPS)),) +-include $(C++_DEPS) +endif +ifneq ($(strip $(CC_DEPS)),) +-include $(CC_DEPS) +endif +ifneq ($(strip $(CPP_DEPS)),) +-include $(CPP_DEPS) +endif +ifneq ($(strip $(CXX_DEPS)),) +-include $(CXX_DEPS) +endif +ifneq ($(strip $(C_DEPS)),) +-include $(C_DEPS) +endif +ifneq ($(strip $(C_UPPER_DEPS)),) +-include $(C_UPPER_DEPS) +endif +endif + +-include ../makefile.defs + +OPTIONAL_TOOL_DEPS := \ +$(wildcard ../makefile.defs) \ +$(wildcard ../makefile.init) \ +$(wildcard ../makefile.targets) \ + + +BUILD_ARTIFACT_NAME := 05. Sorting a Complex Vector! +BUILD_ARTIFACT_EXTENSION := +BUILD_ARTIFACT_PREFIX := +BUILD_ARTIFACT := $(BUILD_ARTIFACT_PREFIX)$(BUILD_ARTIFACT_NAME)$(if $(BUILD_ARTIFACT_EXTENSION),.$(BUILD_ARTIFACT_EXTENSION),) + +# Add inputs and outputs from these tool invocations to the build variables + +# All Target +all: main-build + +# Main-build Target +main-build: 05.\ Sorting\ a\ Complex\ Vector! + +# Tool invocations +05.\ Sorting\ a\ Complex\ Vector!: $(OBJS) $(USER_OBJS) makefile objects.mk $(OPTIONAL_TOOL_DEPS) + @echo 'Building target: $@' + @echo 'Invoking: MacOS X C++ Linker' + g++ -o "05. Sorting a Complex Vector!" $(OBJS) $(USER_OBJS) $(LIBS) + @echo 'Finished building target: $@' + @echo ' ' + +# Other Targets +clean: + -$(RM) 05.\\ Sorting\\ a\\ Complex\\ Vector! + -@echo ' ' + +.PHONY: all clean dependents main-build + +-include ../makefile.targets diff --git a/Vector Data Structure/05. Sorting a Complex Vector!/Debug/objects.mk b/Vector Data Structure/05. Sorting a Complex Vector!/Debug/objects.mk new file mode 100644 index 0000000..742c2da --- /dev/null +++ b/Vector Data Structure/05. Sorting a Complex Vector!/Debug/objects.mk @@ -0,0 +1,8 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +USER_OBJS := + +LIBS := + diff --git a/Vector Data Structure/05. Sorting a Complex Vector!/Debug/sources.mk b/Vector Data Structure/05. Sorting a Complex Vector!/Debug/sources.mk new file mode 100644 index 0000000..c15c16c --- /dev/null +++ b/Vector Data Structure/05. Sorting a Complex Vector!/Debug/sources.mk @@ -0,0 +1,27 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +ASM_SRCS := +C++_SRCS := +CC_SRCS := +CPP_SRCS := +CXX_SRCS := +C_SRCS := +C_UPPER_SRCS := +OBJ_SRCS := +O_SRCS := +S_UPPER_SRCS := +C++_DEPS := +CC_DEPS := +CPP_DEPS := +CXX_DEPS := +C_DEPS := +C_UPPER_DEPS := +EXECUTABLES := +OBJS := + +# Every subdirectory with source files must be described here +SUBDIRS := \ +. \ + diff --git a/Vector Data Structure/05. Sorting a Complex Vector!/Debug/subdir.mk b/Vector Data Structure/05. Sorting a Complex Vector!/Debug/subdir.mk new file mode 100644 index 0000000..9d81f4e --- /dev/null +++ b/Vector Data Structure/05. Sorting a Complex Vector!/Debug/subdir.mk @@ -0,0 +1,31 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +# Add inputs and outputs from these tool invocations to the build variables +CPP_SRCS += \ +../main.cpp + +CPP_DEPS += \ +./main.d + +OBJS += \ +./main.o + + +# Each subdirectory must supply rules for building sources it contributes +%.o: ../%.cpp subdir.mk + @echo 'Building file: $<' + @echo 'Invoking: GCC C++ Compiler' + g++ -O0 -g3 -Wall -c -fmessage-length=0 --std=c++17 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$@" -o "$@" "$<" + @echo 'Finished building: $<' + @echo ' ' + + +clean: clean--2e- + +clean--2e-: + -$(RM) ./main.d ./main.o + +.PHONY: clean--2e- + diff --git a/Vector Data Structure/05. Sorting a Complex Vector!/main.cpp b/Vector Data Structure/05. Sorting a Complex Vector!/main.cpp new file mode 100644 index 0000000..c493d95 --- /dev/null +++ b/Vector Data Structure/05. Sorting a Complex Vector!/main.cpp @@ -0,0 +1,33 @@ +#include +#include +#include +using namespace std; + +int sum(vector v) { + int total_sum = 0; + + for (int i = 0; i < v.size(); i++) total_sum += v[i]; + + return total_sum; +} + +bool comparator(pair> s1, pair> s2) { + return sum(s1.second) > sum(s2.second); +} + +int main() { + vector>> student_marks = { + {"Mohamed", {10, 20, 11}}, + {"Ahmed", {10, 21, 3}}, + {"Mahmoud", {4, 5, 6}}, + {"Youssef", {10, 13, 20}} + }; + + sort(student_marks.begin(), student_marks.end(), comparator); + + for (int i = 0; i < student_marks.size(); i++) { + cout << student_marks[i].first << ": " << sum(student_marks[i].second) << endl; + } + + return 0; +} diff --git a/Vector Data Structure/06. Sorting Cabs!/.cproject b/Vector Data Structure/06. Sorting Cabs!/.cproject new file mode 100644 index 0000000..a40d41a --- /dev/null +++ b/Vector Data Structure/06. Sorting Cabs!/.cproject @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Vector Data Structure/06. Sorting Cabs!/.project b/Vector Data Structure/06. Sorting Cabs!/.project new file mode 100644 index 0000000..2a2f16e --- /dev/null +++ b/Vector Data Structure/06. Sorting Cabs!/.project @@ -0,0 +1,26 @@ + + + 06. Sorting Cabs! + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.core.ccnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + diff --git a/Vector Data Structure/06. Sorting Cabs!/.settings/language.settings.xml b/Vector Data Structure/06. Sorting Cabs!/.settings/language.settings.xml new file mode 100644 index 0000000..0a79dd4 --- /dev/null +++ b/Vector Data Structure/06. Sorting Cabs!/.settings/language.settings.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Vector Data Structure/06. Sorting Cabs!/Debug/06. Sorting Cabs! b/Vector Data Structure/06. Sorting Cabs!/Debug/06. Sorting Cabs! new file mode 100755 index 0000000..438e63a Binary files /dev/null and b/Vector Data Structure/06. Sorting Cabs!/Debug/06. Sorting Cabs! differ diff --git a/Vector Data Structure/06. Sorting Cabs!/Debug/main.d b/Vector Data Structure/06. Sorting Cabs!/Debug/main.d new file mode 100644 index 0000000..a8355ba --- /dev/null +++ b/Vector Data Structure/06. Sorting Cabs!/Debug/main.d @@ -0,0 +1 @@ +main.o: ../main.cpp diff --git a/Vector Data Structure/06. Sorting Cabs!/Debug/main.o b/Vector Data Structure/06. Sorting Cabs!/Debug/main.o new file mode 100644 index 0000000..f9a894e Binary files /dev/null and b/Vector Data Structure/06. Sorting Cabs!/Debug/main.o differ diff --git a/Vector Data Structure/06. Sorting Cabs!/Debug/makefile b/Vector Data Structure/06. Sorting Cabs!/Debug/makefile new file mode 100644 index 0000000..27e1b3e --- /dev/null +++ b/Vector Data Structure/06. Sorting Cabs!/Debug/makefile @@ -0,0 +1,71 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +-include ../makefile.init + +RM := rm -rf + +# All of the sources participating in the build are defined here +-include sources.mk +-include subdir.mk +-include objects.mk + +ifneq ($(MAKECMDGOALS),clean) +ifneq ($(strip $(C++_DEPS)),) +-include $(C++_DEPS) +endif +ifneq ($(strip $(CC_DEPS)),) +-include $(CC_DEPS) +endif +ifneq ($(strip $(CPP_DEPS)),) +-include $(CPP_DEPS) +endif +ifneq ($(strip $(CXX_DEPS)),) +-include $(CXX_DEPS) +endif +ifneq ($(strip $(C_DEPS)),) +-include $(C_DEPS) +endif +ifneq ($(strip $(C_UPPER_DEPS)),) +-include $(C_UPPER_DEPS) +endif +endif + +-include ../makefile.defs + +OPTIONAL_TOOL_DEPS := \ +$(wildcard ../makefile.defs) \ +$(wildcard ../makefile.init) \ +$(wildcard ../makefile.targets) \ + + +BUILD_ARTIFACT_NAME := 06. Sorting Cabs! +BUILD_ARTIFACT_EXTENSION := +BUILD_ARTIFACT_PREFIX := +BUILD_ARTIFACT := $(BUILD_ARTIFACT_PREFIX)$(BUILD_ARTIFACT_NAME)$(if $(BUILD_ARTIFACT_EXTENSION),.$(BUILD_ARTIFACT_EXTENSION),) + +# Add inputs and outputs from these tool invocations to the build variables + +# All Target +all: main-build + +# Main-build Target +main-build: 06.\ Sorting\ Cabs! + +# Tool invocations +06.\ Sorting\ Cabs!: $(OBJS) $(USER_OBJS) makefile objects.mk $(OPTIONAL_TOOL_DEPS) + @echo 'Building target: $@' + @echo 'Invoking: MacOS X C++ Linker' + g++ -o "06. Sorting Cabs!" $(OBJS) $(USER_OBJS) $(LIBS) + @echo 'Finished building target: $@' + @echo ' ' + +# Other Targets +clean: + -$(RM) 06.\\ Sorting\\ Cabs! + -@echo ' ' + +.PHONY: all clean dependents main-build + +-include ../makefile.targets diff --git a/Vector Data Structure/06. Sorting Cabs!/Debug/objects.mk b/Vector Data Structure/06. Sorting Cabs!/Debug/objects.mk new file mode 100644 index 0000000..742c2da --- /dev/null +++ b/Vector Data Structure/06. Sorting Cabs!/Debug/objects.mk @@ -0,0 +1,8 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +USER_OBJS := + +LIBS := + diff --git a/Vector Data Structure/06. Sorting Cabs!/Debug/sources.mk b/Vector Data Structure/06. Sorting Cabs!/Debug/sources.mk new file mode 100644 index 0000000..c15c16c --- /dev/null +++ b/Vector Data Structure/06. Sorting Cabs!/Debug/sources.mk @@ -0,0 +1,27 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +ASM_SRCS := +C++_SRCS := +CC_SRCS := +CPP_SRCS := +CXX_SRCS := +C_SRCS := +C_UPPER_SRCS := +OBJ_SRCS := +O_SRCS := +S_UPPER_SRCS := +C++_DEPS := +CC_DEPS := +CPP_DEPS := +CXX_DEPS := +C_DEPS := +C_UPPER_DEPS := +EXECUTABLES := +OBJS := + +# Every subdirectory with source files must be described here +SUBDIRS := \ +. \ + diff --git a/Vector Data Structure/06. Sorting Cabs!/Debug/subdir.mk b/Vector Data Structure/06. Sorting Cabs!/Debug/subdir.mk new file mode 100644 index 0000000..9d81f4e --- /dev/null +++ b/Vector Data Structure/06. Sorting Cabs!/Debug/subdir.mk @@ -0,0 +1,31 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +# Add inputs and outputs from these tool invocations to the build variables +CPP_SRCS += \ +../main.cpp + +CPP_DEPS += \ +./main.d + +OBJS += \ +./main.o + + +# Each subdirectory must supply rules for building sources it contributes +%.o: ../%.cpp subdir.mk + @echo 'Building file: $<' + @echo 'Invoking: GCC C++ Compiler' + g++ -O0 -g3 -Wall -c -fmessage-length=0 --std=c++17 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$@" -o "$@" "$<" + @echo 'Finished building: $<' + @echo ' ' + + +clean: clean--2e- + +clean--2e-: + -$(RM) ./main.d ./main.o + +.PHONY: clean--2e- + diff --git a/Vector Data Structure/06. Sorting Cabs!/main.cpp b/Vector Data Structure/06. Sorting Cabs!/main.cpp new file mode 100644 index 0000000..63bab0d --- /dev/null +++ b/Vector Data Structure/06. Sorting Cabs!/main.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +using namespace std; + +/* + * Sorting Cabs! + * Given a vector of pairs representing coordinates x and y + * of different cabs on a X-Y plane. Sort them according to + * their distance from the origin in the non-decreasing order + * i.e. the cab with shortest distance from the origin will be + * at first. + * Input Format + * In the function an integer vector of pairs is passed. + * Output Format + * Return an integer vector of pairs in sorted order. + * + * Sample Input + * { (2, 3), (1, 2), (3, 4), (2, 4), (1, 4) } + * + * Sample Output + * { (1, 2), (2, 3), (1, 4), (2, 4), (3, 4) } + */ + +bool compare(pair p1, pair p2) { + return sqrt(pow(p1.first, 2) + pow(p1.second, 2)) < sqrt(pow(p2.first, 2) + pow(p2.second, 2)); +} + +vector> sortCabs(vector> v) { + sort(v.begin(), v.end(), compare); + + return v; +} + +int main() { + vector> v = { + {2, 3}, {1, 2}, {3, 4}, {2, 4}, {1, 4} + }; + vector> v_sorted; + + v_sorted = sortCabs(v); + + for (pair p : v_sorted) { + cout << "(" << p.first << ", " << p.second << ")" << endl; + } + + return 0; +} diff --git a/Vector Data Structure/07. Sort Fruits!/.cproject b/Vector Data Structure/07. Sort Fruits!/.cproject new file mode 100644 index 0000000..6738413 --- /dev/null +++ b/Vector Data Structure/07. Sort Fruits!/.cproject @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Vector Data Structure/07. Sort Fruits!/.project b/Vector Data Structure/07. Sort Fruits!/.project new file mode 100644 index 0000000..73d2b46 --- /dev/null +++ b/Vector Data Structure/07. Sort Fruits!/.project @@ -0,0 +1,26 @@ + + + 07. Sort Fruits! + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.core.ccnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + diff --git a/Vector Data Structure/07. Sort Fruits!/.settings/language.settings.xml b/Vector Data Structure/07. Sort Fruits!/.settings/language.settings.xml new file mode 100644 index 0000000..34ef844 --- /dev/null +++ b/Vector Data Structure/07. Sort Fruits!/.settings/language.settings.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Vector Data Structure/07. Sort Fruits!/Debug/07. Sort Fruits! b/Vector Data Structure/07. Sort Fruits!/Debug/07. Sort Fruits! new file mode 100755 index 0000000..eac4887 Binary files /dev/null and b/Vector Data Structure/07. Sort Fruits!/Debug/07. Sort Fruits! differ diff --git a/Vector Data Structure/07. Sort Fruits!/Debug/main.d b/Vector Data Structure/07. Sort Fruits!/Debug/main.d new file mode 100644 index 0000000..a8355ba --- /dev/null +++ b/Vector Data Structure/07. Sort Fruits!/Debug/main.d @@ -0,0 +1 @@ +main.o: ../main.cpp diff --git a/Vector Data Structure/07. Sort Fruits!/Debug/main.o b/Vector Data Structure/07. Sort Fruits!/Debug/main.o new file mode 100644 index 0000000..a55fa10 Binary files /dev/null and b/Vector Data Structure/07. Sort Fruits!/Debug/main.o differ diff --git a/Vector Data Structure/07. Sort Fruits!/Debug/makefile b/Vector Data Structure/07. Sort Fruits!/Debug/makefile new file mode 100644 index 0000000..1da3c8c --- /dev/null +++ b/Vector Data Structure/07. Sort Fruits!/Debug/makefile @@ -0,0 +1,71 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +-include ../makefile.init + +RM := rm -rf + +# All of the sources participating in the build are defined here +-include sources.mk +-include subdir.mk +-include objects.mk + +ifneq ($(MAKECMDGOALS),clean) +ifneq ($(strip $(C++_DEPS)),) +-include $(C++_DEPS) +endif +ifneq ($(strip $(CC_DEPS)),) +-include $(CC_DEPS) +endif +ifneq ($(strip $(CPP_DEPS)),) +-include $(CPP_DEPS) +endif +ifneq ($(strip $(CXX_DEPS)),) +-include $(CXX_DEPS) +endif +ifneq ($(strip $(C_DEPS)),) +-include $(C_DEPS) +endif +ifneq ($(strip $(C_UPPER_DEPS)),) +-include $(C_UPPER_DEPS) +endif +endif + +-include ../makefile.defs + +OPTIONAL_TOOL_DEPS := \ +$(wildcard ../makefile.defs) \ +$(wildcard ../makefile.init) \ +$(wildcard ../makefile.targets) \ + + +BUILD_ARTIFACT_NAME := 07. Sort Fruits! +BUILD_ARTIFACT_EXTENSION := +BUILD_ARTIFACT_PREFIX := +BUILD_ARTIFACT := $(BUILD_ARTIFACT_PREFIX)$(BUILD_ARTIFACT_NAME)$(if $(BUILD_ARTIFACT_EXTENSION),.$(BUILD_ARTIFACT_EXTENSION),) + +# Add inputs and outputs from these tool invocations to the build variables + +# All Target +all: main-build + +# Main-build Target +main-build: 07.\ Sort\ Fruits! + +# Tool invocations +07.\ Sort\ Fruits!: $(OBJS) $(USER_OBJS) makefile objects.mk $(OPTIONAL_TOOL_DEPS) + @echo 'Building target: $@' + @echo 'Invoking: MacOS X C++ Linker' + g++ -o "07. Sort Fruits!" $(OBJS) $(USER_OBJS) $(LIBS) + @echo 'Finished building target: $@' + @echo ' ' + +# Other Targets +clean: + -$(RM) 07.\\ Sort\\ Fruits! + -@echo ' ' + +.PHONY: all clean dependents main-build + +-include ../makefile.targets diff --git a/Vector Data Structure/07. Sort Fruits!/Debug/objects.mk b/Vector Data Structure/07. Sort Fruits!/Debug/objects.mk new file mode 100644 index 0000000..742c2da --- /dev/null +++ b/Vector Data Structure/07. Sort Fruits!/Debug/objects.mk @@ -0,0 +1,8 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +USER_OBJS := + +LIBS := + diff --git a/Vector Data Structure/07. Sort Fruits!/Debug/sources.mk b/Vector Data Structure/07. Sort Fruits!/Debug/sources.mk new file mode 100644 index 0000000..c15c16c --- /dev/null +++ b/Vector Data Structure/07. Sort Fruits!/Debug/sources.mk @@ -0,0 +1,27 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +ASM_SRCS := +C++_SRCS := +CC_SRCS := +CPP_SRCS := +CXX_SRCS := +C_SRCS := +C_UPPER_SRCS := +OBJ_SRCS := +O_SRCS := +S_UPPER_SRCS := +C++_DEPS := +CC_DEPS := +CPP_DEPS := +CXX_DEPS := +C_DEPS := +C_UPPER_DEPS := +EXECUTABLES := +OBJS := + +# Every subdirectory with source files must be described here +SUBDIRS := \ +. \ + diff --git a/Vector Data Structure/07. Sort Fruits!/Debug/subdir.mk b/Vector Data Structure/07. Sort Fruits!/Debug/subdir.mk new file mode 100644 index 0000000..9d81f4e --- /dev/null +++ b/Vector Data Structure/07. Sort Fruits!/Debug/subdir.mk @@ -0,0 +1,31 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +# Add inputs and outputs from these tool invocations to the build variables +CPP_SRCS += \ +../main.cpp + +CPP_DEPS += \ +./main.d + +OBJS += \ +./main.o + + +# Each subdirectory must supply rules for building sources it contributes +%.o: ../%.cpp subdir.mk + @echo 'Building file: $<' + @echo 'Invoking: GCC C++ Compiler' + g++ -O0 -g3 -Wall -c -fmessage-length=0 --std=c++17 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$@" -o "$@" "$<" + @echo 'Finished building: $<' + @echo ' ' + + +clean: clean--2e- + +clean--2e-: + -$(RM) ./main.d ./main.o + +.PHONY: clean--2e- + diff --git a/Vector Data Structure/07. Sort Fruits!/main.cpp b/Vector Data Structure/07. Sort Fruits!/main.cpp new file mode 100644 index 0000000..78f1fe5 --- /dev/null +++ b/Vector Data Structure/07. Sort Fruits!/main.cpp @@ -0,0 +1,68 @@ +#include +#include +using namespace std; + +/* + * Sort Fruits! + * Given a vector of pairs of a string and an integer + * representing names and prices of different fruits. A string + * is also passed as a parameter with name equals either "price", + * meaning you have to sort the fruits on the basis of their + * price or "name", which means you have to sort the fruit on + * the basis of their name. + * Input Format + * In the function an integer vector of pairs is passed. + * Output Format + * Return an integer vector of pairs in sorted order. + * + * Sample Input + * { {"Mongo", 100}, {"Guava", 70}, {"Grapes", 40}, + * {"Apple", 60}, {"Banana", 30} }, S = "price" + * Sample Output + * { {"Banana", 30}, {"Grapes", 40}, {"Apple", 60}, + * {"Guava", 70}, {"Mongo", 100} } + */ + +bool compPrice(pair f1, pair f2) { + return f1.second < f2.second; +} + +bool compName(pair f1, pair f2) { + return f1.first < f2.first; +} + +vector> sortFruits(vector> v, string S){ + if (S == "price") + sort(v.begin(), v.end(), compPrice); + else if (S == "name") + sort(v.begin(), v.end(), compName); + + return v; +} + +int main() { + vector> v = { + {"Mongo", 100}, + {"Guava", 70}, + {"Grapes", 40}, + {"Apple", 60}, + {"Banana", 30} + }; + + string S = "price"; + vector> v_sorted_price = sortFruits(v, S);; + + for (pair p : v_sorted_price) + cout << "(" << p.first << ", " << p.second << ")" << " "; + cout << endl; + + S = "name"; + vector> v_sorted_name = sortFruits(v, S); + + for (pair p : v_sorted_name) + cout << "(" << p.first << ", " << p.second << ")" << " "; + cout << endl; + + + return 0; +} diff --git a/Vector Data Structure/08. Rotate Image/.cproject b/Vector Data Structure/08. Rotate Image/.cproject new file mode 100644 index 0000000..e48942e --- /dev/null +++ b/Vector Data Structure/08. Rotate Image/.cproject @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Vector Data Structure/08. Rotate Image/.project b/Vector Data Structure/08. Rotate Image/.project new file mode 100644 index 0000000..6cf8812 --- /dev/null +++ b/Vector Data Structure/08. Rotate Image/.project @@ -0,0 +1,26 @@ + + + 08. Rotate Image + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.core.ccnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + diff --git a/Vector Data Structure/08. Rotate Image/.settings/language.settings.xml b/Vector Data Structure/08. Rotate Image/.settings/language.settings.xml new file mode 100644 index 0000000..a9d1c6a --- /dev/null +++ b/Vector Data Structure/08. Rotate Image/.settings/language.settings.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Vector Data Structure/08. Rotate Image/Debug/08. Rotate Image b/Vector Data Structure/08. Rotate Image/Debug/08. Rotate Image new file mode 100755 index 0000000..23bab23 Binary files /dev/null and b/Vector Data Structure/08. Rotate Image/Debug/08. Rotate Image differ diff --git a/Vector Data Structure/08. Rotate Image/Debug/main.d b/Vector Data Structure/08. Rotate Image/Debug/main.d new file mode 100644 index 0000000..a8355ba --- /dev/null +++ b/Vector Data Structure/08. Rotate Image/Debug/main.d @@ -0,0 +1 @@ +main.o: ../main.cpp diff --git a/Vector Data Structure/08. Rotate Image/Debug/main.o b/Vector Data Structure/08. Rotate Image/Debug/main.o new file mode 100644 index 0000000..bbf5a80 Binary files /dev/null and b/Vector Data Structure/08. Rotate Image/Debug/main.o differ diff --git a/Vector Data Structure/08. Rotate Image/Debug/makefile b/Vector Data Structure/08. Rotate Image/Debug/makefile new file mode 100644 index 0000000..97017bc --- /dev/null +++ b/Vector Data Structure/08. Rotate Image/Debug/makefile @@ -0,0 +1,71 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +-include ../makefile.init + +RM := rm -rf + +# All of the sources participating in the build are defined here +-include sources.mk +-include subdir.mk +-include objects.mk + +ifneq ($(MAKECMDGOALS),clean) +ifneq ($(strip $(C++_DEPS)),) +-include $(C++_DEPS) +endif +ifneq ($(strip $(CC_DEPS)),) +-include $(CC_DEPS) +endif +ifneq ($(strip $(CPP_DEPS)),) +-include $(CPP_DEPS) +endif +ifneq ($(strip $(CXX_DEPS)),) +-include $(CXX_DEPS) +endif +ifneq ($(strip $(C_DEPS)),) +-include $(C_DEPS) +endif +ifneq ($(strip $(C_UPPER_DEPS)),) +-include $(C_UPPER_DEPS) +endif +endif + +-include ../makefile.defs + +OPTIONAL_TOOL_DEPS := \ +$(wildcard ../makefile.defs) \ +$(wildcard ../makefile.init) \ +$(wildcard ../makefile.targets) \ + + +BUILD_ARTIFACT_NAME := 08. Rotate Image +BUILD_ARTIFACT_EXTENSION := +BUILD_ARTIFACT_PREFIX := +BUILD_ARTIFACT := $(BUILD_ARTIFACT_PREFIX)$(BUILD_ARTIFACT_NAME)$(if $(BUILD_ARTIFACT_EXTENSION),.$(BUILD_ARTIFACT_EXTENSION),) + +# Add inputs and outputs from these tool invocations to the build variables + +# All Target +all: main-build + +# Main-build Target +main-build: 08.\ Rotate\ Image + +# Tool invocations +08.\ Rotate\ Image: $(OBJS) $(USER_OBJS) makefile objects.mk $(OPTIONAL_TOOL_DEPS) + @echo 'Building target: $@' + @echo 'Invoking: MacOS X C++ Linker' + g++ -o "08. Rotate Image" $(OBJS) $(USER_OBJS) $(LIBS) + @echo 'Finished building target: $@' + @echo ' ' + +# Other Targets +clean: + -$(RM) 08.\\ Rotate\\ Image + -@echo ' ' + +.PHONY: all clean dependents main-build + +-include ../makefile.targets diff --git a/Vector Data Structure/08. Rotate Image/Debug/objects.mk b/Vector Data Structure/08. Rotate Image/Debug/objects.mk new file mode 100644 index 0000000..742c2da --- /dev/null +++ b/Vector Data Structure/08. Rotate Image/Debug/objects.mk @@ -0,0 +1,8 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +USER_OBJS := + +LIBS := + diff --git a/Vector Data Structure/08. Rotate Image/Debug/sources.mk b/Vector Data Structure/08. Rotate Image/Debug/sources.mk new file mode 100644 index 0000000..c15c16c --- /dev/null +++ b/Vector Data Structure/08. Rotate Image/Debug/sources.mk @@ -0,0 +1,27 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +ASM_SRCS := +C++_SRCS := +CC_SRCS := +CPP_SRCS := +CXX_SRCS := +C_SRCS := +C_UPPER_SRCS := +OBJ_SRCS := +O_SRCS := +S_UPPER_SRCS := +C++_DEPS := +CC_DEPS := +CPP_DEPS := +CXX_DEPS := +C_DEPS := +C_UPPER_DEPS := +EXECUTABLES := +OBJS := + +# Every subdirectory with source files must be described here +SUBDIRS := \ +. \ + diff --git a/Vector Data Structure/08. Rotate Image/Debug/subdir.mk b/Vector Data Structure/08. Rotate Image/Debug/subdir.mk new file mode 100644 index 0000000..9d81f4e --- /dev/null +++ b/Vector Data Structure/08. Rotate Image/Debug/subdir.mk @@ -0,0 +1,31 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +# Add inputs and outputs from these tool invocations to the build variables +CPP_SRCS += \ +../main.cpp + +CPP_DEPS += \ +./main.d + +OBJS += \ +./main.o + + +# Each subdirectory must supply rules for building sources it contributes +%.o: ../%.cpp subdir.mk + @echo 'Building file: $<' + @echo 'Invoking: GCC C++ Compiler' + g++ -O0 -g3 -Wall -c -fmessage-length=0 --std=c++17 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$@" -o "$@" "$<" + @echo 'Finished building: $<' + @echo ' ' + + +clean: clean--2e- + +clean--2e-: + -$(RM) ./main.d ./main.o + +.PHONY: clean--2e- + diff --git a/Vector Data Structure/08. Rotate Image/main.cpp b/Vector Data Structure/08. Rotate Image/main.cpp new file mode 100644 index 0000000..28fed90 --- /dev/null +++ b/Vector Data Structure/08. Rotate Image/main.cpp @@ -0,0 +1,72 @@ +#include +#include +using namespace std; + +/* + * Rotate Image + * You are given an n x n 2D matrix representing an image, + * rotate the image by 90 degrees (clockwise). + * You have to rotate the image in-place, which means you have + * to modify the input 2D matrix directly. DO NOT allocate + * another 2D matrix to do the rotation. + * + * Example: + * 1 2 3 ======> 7 4 1 + * 4 5 6 ======> 8 5 2 + * 7 8 9 ======> 9 6 3 + * + * Sample Input + * matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] + * + * Sample Output + * [[7, 4, 1], [8, 5, 2], [9, 6, 3]] + * + * Constraints: + * matrix.length == n + * matrix[i].length == n + * 1 <= n <= 20 + * -1000 <= matrix[i][j] <= 1000 + * + * Idea: + * Indices: (0, 0) & (0, 2), Swapping: (1, 3) + * Indices: (0, 0) & (2, 2), Swapping: (3, 9) + * Indices: (0, 0) & (2, 0), Swapping: (9, 7) + * Indices: (0, 1) & (1, 2), Swapping: (2, 6) + * Indices: (0, 1) & (2, 1), Swapping: (6, 8) + * Indices: (0, 1) & (1, 0), Swapping: (8, 4) + */ + +void rotate(vector>& matrix) { + int n = matrix.size(); + int start = 0; + int end = n -1; + + while (start < end) { + for(int i = 0; i < (end-start); ++i) { + swap(matrix[start][start+i], matrix[start+i][end]); + swap(matrix[start][start+i], matrix[end][end-i]); + swap(matrix[start][start+i], matrix[end-i][start]); + } + ++start; + --end; + } +} + +int main() { + vector> matrix = { + {1, 2, 3}, + {4, 5, 6}, + {7, 8, 9} + }; + + rotate(matrix); + + for (int i = 0; i < matrix.size(); i++) { + for (int j = 0; j < matrix.size(); j++) { + cout << matrix[i][j] << " "; + } + cout << endl; + } + + return 0; +} diff --git a/Vector Data Structure/09. Make Zeroes/.cproject b/Vector Data Structure/09. Make Zeroes/.cproject new file mode 100644 index 0000000..176b701 --- /dev/null +++ b/Vector Data Structure/09. Make Zeroes/.cproject @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Vector Data Structure/09. Make Zeroes/.project b/Vector Data Structure/09. Make Zeroes/.project new file mode 100644 index 0000000..05aa796 --- /dev/null +++ b/Vector Data Structure/09. Make Zeroes/.project @@ -0,0 +1,26 @@ + + + 09. Make Zeroes + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.core.ccnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + diff --git a/Vector Data Structure/09. Make Zeroes/.settings/language.settings.xml b/Vector Data Structure/09. Make Zeroes/.settings/language.settings.xml new file mode 100644 index 0000000..b936310 --- /dev/null +++ b/Vector Data Structure/09. Make Zeroes/.settings/language.settings.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Vector Data Structure/09. Make Zeroes/Debug/09. Make Zeroes b/Vector Data Structure/09. Make Zeroes/Debug/09. Make Zeroes new file mode 100755 index 0000000..300d654 Binary files /dev/null and b/Vector Data Structure/09. Make Zeroes/Debug/09. Make Zeroes differ diff --git a/Vector Data Structure/09. Make Zeroes/Debug/main.d b/Vector Data Structure/09. Make Zeroes/Debug/main.d new file mode 100644 index 0000000..a8355ba --- /dev/null +++ b/Vector Data Structure/09. Make Zeroes/Debug/main.d @@ -0,0 +1 @@ +main.o: ../main.cpp diff --git a/Vector Data Structure/09. Make Zeroes/Debug/main.o b/Vector Data Structure/09. Make Zeroes/Debug/main.o new file mode 100644 index 0000000..4f181d1 Binary files /dev/null and b/Vector Data Structure/09. Make Zeroes/Debug/main.o differ diff --git a/Vector Data Structure/09. Make Zeroes/Debug/makefile b/Vector Data Structure/09. Make Zeroes/Debug/makefile new file mode 100644 index 0000000..d5e63d8 --- /dev/null +++ b/Vector Data Structure/09. Make Zeroes/Debug/makefile @@ -0,0 +1,71 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +-include ../makefile.init + +RM := rm -rf + +# All of the sources participating in the build are defined here +-include sources.mk +-include subdir.mk +-include objects.mk + +ifneq ($(MAKECMDGOALS),clean) +ifneq ($(strip $(C++_DEPS)),) +-include $(C++_DEPS) +endif +ifneq ($(strip $(CC_DEPS)),) +-include $(CC_DEPS) +endif +ifneq ($(strip $(CPP_DEPS)),) +-include $(CPP_DEPS) +endif +ifneq ($(strip $(CXX_DEPS)),) +-include $(CXX_DEPS) +endif +ifneq ($(strip $(C_DEPS)),) +-include $(C_DEPS) +endif +ifneq ($(strip $(C_UPPER_DEPS)),) +-include $(C_UPPER_DEPS) +endif +endif + +-include ../makefile.defs + +OPTIONAL_TOOL_DEPS := \ +$(wildcard ../makefile.defs) \ +$(wildcard ../makefile.init) \ +$(wildcard ../makefile.targets) \ + + +BUILD_ARTIFACT_NAME := 09. Make Zeroes +BUILD_ARTIFACT_EXTENSION := +BUILD_ARTIFACT_PREFIX := +BUILD_ARTIFACT := $(BUILD_ARTIFACT_PREFIX)$(BUILD_ARTIFACT_NAME)$(if $(BUILD_ARTIFACT_EXTENSION),.$(BUILD_ARTIFACT_EXTENSION),) + +# Add inputs and outputs from these tool invocations to the build variables + +# All Target +all: main-build + +# Main-build Target +main-build: 09.\ Make\ Zeroes + +# Tool invocations +09.\ Make\ Zeroes: $(OBJS) $(USER_OBJS) makefile objects.mk $(OPTIONAL_TOOL_DEPS) + @echo 'Building target: $@' + @echo 'Invoking: MacOS X C++ Linker' + g++ -o "09. Make Zeroes" $(OBJS) $(USER_OBJS) $(LIBS) + @echo 'Finished building target: $@' + @echo ' ' + +# Other Targets +clean: + -$(RM) 09.\\ Make\\ Zeroes + -@echo ' ' + +.PHONY: all clean dependents main-build + +-include ../makefile.targets diff --git a/Vector Data Structure/09. Make Zeroes/Debug/objects.mk b/Vector Data Structure/09. Make Zeroes/Debug/objects.mk new file mode 100644 index 0000000..742c2da --- /dev/null +++ b/Vector Data Structure/09. Make Zeroes/Debug/objects.mk @@ -0,0 +1,8 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +USER_OBJS := + +LIBS := + diff --git a/Vector Data Structure/09. Make Zeroes/Debug/sources.mk b/Vector Data Structure/09. Make Zeroes/Debug/sources.mk new file mode 100644 index 0000000..c15c16c --- /dev/null +++ b/Vector Data Structure/09. Make Zeroes/Debug/sources.mk @@ -0,0 +1,27 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +ASM_SRCS := +C++_SRCS := +CC_SRCS := +CPP_SRCS := +CXX_SRCS := +C_SRCS := +C_UPPER_SRCS := +OBJ_SRCS := +O_SRCS := +S_UPPER_SRCS := +C++_DEPS := +CC_DEPS := +CPP_DEPS := +CXX_DEPS := +C_DEPS := +C_UPPER_DEPS := +EXECUTABLES := +OBJS := + +# Every subdirectory with source files must be described here +SUBDIRS := \ +. \ + diff --git a/Vector Data Structure/09. Make Zeroes/Debug/subdir.mk b/Vector Data Structure/09. Make Zeroes/Debug/subdir.mk new file mode 100644 index 0000000..9d81f4e --- /dev/null +++ b/Vector Data Structure/09. Make Zeroes/Debug/subdir.mk @@ -0,0 +1,31 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +# Add inputs and outputs from these tool invocations to the build variables +CPP_SRCS += \ +../main.cpp + +CPP_DEPS += \ +./main.d + +OBJS += \ +./main.o + + +# Each subdirectory must supply rules for building sources it contributes +%.o: ../%.cpp subdir.mk + @echo 'Building file: $<' + @echo 'Invoking: GCC C++ Compiler' + g++ -O0 -g3 -Wall -c -fmessage-length=0 --std=c++17 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$@" -o "$@" "$<" + @echo 'Finished building: $<' + @echo ' ' + + +clean: clean--2e- + +clean--2e-: + -$(RM) ./main.d ./main.o + +.PHONY: clean--2e- + diff --git a/Vector Data Structure/09. Make Zeroes/main.cpp b/Vector Data Structure/09. Make Zeroes/main.cpp new file mode 100644 index 0000000..3b05590 --- /dev/null +++ b/Vector Data Structure/09. Make Zeroes/main.cpp @@ -0,0 +1,76 @@ +#include +#include +using namespace std; + +/* + * Make Zeroes + * Given a two-dimensional array, if any element within is zero, + * make its whole row and column zero. For example, consider + * the matrix below. + * + * 5 4 3 9 + * 2 0 7 6 + * 1 3 4 0 + * 9 8 3 4 + * + * 5 0 3 0 + * 0 0 0 0 + * 0 0 0 0 + * 9 0 3 0 + * + * Input Format + * In the function a 2d vector is passed. + * Output Format + * Return the updated 2d vector. + * + * Sample Input + * { {5, 4, 3, 9}, {2, 0, 7, 6}, {1, 3, 4, 0}, {9, 8, 3, 4} } + * + * Sample Output + * { {5, 0, 3, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {9, 0, 3, 0} } + */ + +vector> makeZeroes(vector> arr){ + // traverse the matrix and detect the ith and jth that contain zeros + vector> pairs; + for (int i = 0; i < arr.size(); i++) { + for (int j = 0; j < arr.size(); j++) { + if (!arr[i][j]) { + pairs.push_back({i, j}); + } + } + } + + // make pairs zeroes + for (int k = 0; k < pairs.size(); k++) { + for (int i = 0; i < arr.size(); i++) { + arr[pairs[k].first][i] = 0; + } + for (int j = 0; j < arr.size(); j++) { + arr[j][pairs[k].second] = 0; + } + } + + return arr; +} + +int main() { + vector> arr = { + {5, 4, 3, 9}, + {2, 0, 7, 6}, + {1, 3, 4, 0}, + {9, 8, 3, 4} + }; + vector> zeroes_arr; + + zeroes_arr = makeZeroes(arr); + + for (int i = 0; i < zeroes_arr.size(); i++) { + for (int j = 0; j < zeroes_arr[0].size(); j++) { + cout << zeroes_arr[i][j] << " "; + } + cout << endl; + } + + return 0; +}