Compare commits
2 Commits
ae15ffd4f0
...
94d33b3d69
Author | SHA1 | Date | |
---|---|---|---|
94d33b3d69 | |||
b292838433 |
225
Spring-2023/CS-2124/Assignment-2/.clang-format
Normal file
@ -0,0 +1,225 @@
|
|||||||
|
---
|
||||||
|
Language: Cpp
|
||||||
|
# BasedOnStyle: LLVM
|
||||||
|
AccessModifierOffset: -2
|
||||||
|
AlignAfterOpenBracket: Align
|
||||||
|
AlignArrayOfStructures: None
|
||||||
|
AlignConsecutiveAssignments:
|
||||||
|
Enabled: false
|
||||||
|
AcrossEmptyLines: false
|
||||||
|
AcrossComments: false
|
||||||
|
AlignCompound: false
|
||||||
|
PadOperators: true
|
||||||
|
AlignConsecutiveBitFields:
|
||||||
|
Enabled: false
|
||||||
|
AcrossEmptyLines: false
|
||||||
|
AcrossComments: false
|
||||||
|
AlignCompound: false
|
||||||
|
PadOperators: false
|
||||||
|
AlignConsecutiveDeclarations:
|
||||||
|
Enabled: false
|
||||||
|
AcrossEmptyLines: false
|
||||||
|
AcrossComments: false
|
||||||
|
AlignCompound: false
|
||||||
|
PadOperators: false
|
||||||
|
AlignConsecutiveMacros:
|
||||||
|
Enabled: false
|
||||||
|
AcrossEmptyLines: false
|
||||||
|
AcrossComments: false
|
||||||
|
AlignCompound: false
|
||||||
|
PadOperators: false
|
||||||
|
AlignEscapedNewlines: Right
|
||||||
|
AlignOperands: Align
|
||||||
|
AlignTrailingComments:
|
||||||
|
Kind: Always
|
||||||
|
OverEmptyLines: 0
|
||||||
|
AllowAllArgumentsOnNextLine: true
|
||||||
|
AllowAllParametersOfDeclarationOnNextLine: true
|
||||||
|
AllowShortBlocksOnASingleLine: Never
|
||||||
|
AllowShortCaseLabelsOnASingleLine: false
|
||||||
|
AllowShortEnumsOnASingleLine: true
|
||||||
|
AllowShortFunctionsOnASingleLine: All
|
||||||
|
AllowShortIfStatementsOnASingleLine: Never
|
||||||
|
AllowShortLambdasOnASingleLine: All
|
||||||
|
AllowShortLoopsOnASingleLine: false
|
||||||
|
AlwaysBreakAfterDefinitionReturnType: None
|
||||||
|
AlwaysBreakAfterReturnType: None
|
||||||
|
AlwaysBreakBeforeMultilineStrings: false
|
||||||
|
AlwaysBreakTemplateDeclarations: MultiLine
|
||||||
|
AttributeMacros:
|
||||||
|
- __capability
|
||||||
|
BinPackArguments: true
|
||||||
|
BinPackParameters: true
|
||||||
|
BitFieldColonSpacing: Both
|
||||||
|
BraceWrapping:
|
||||||
|
AfterCaseLabel: false
|
||||||
|
AfterClass: false
|
||||||
|
AfterControlStatement: Never
|
||||||
|
AfterEnum: false
|
||||||
|
AfterExternBlock: false
|
||||||
|
AfterFunction: false
|
||||||
|
AfterNamespace: false
|
||||||
|
AfterObjCDeclaration: false
|
||||||
|
AfterStruct: false
|
||||||
|
AfterUnion: false
|
||||||
|
BeforeCatch: false
|
||||||
|
BeforeElse: false
|
||||||
|
BeforeLambdaBody: false
|
||||||
|
BeforeWhile: false
|
||||||
|
IndentBraces: false
|
||||||
|
SplitEmptyFunction: true
|
||||||
|
SplitEmptyRecord: true
|
||||||
|
SplitEmptyNamespace: true
|
||||||
|
BreakAfterAttributes: Never
|
||||||
|
BreakAfterJavaFieldAnnotations: false
|
||||||
|
BreakArrays: true
|
||||||
|
BreakBeforeBinaryOperators: None
|
||||||
|
BreakBeforeConceptDeclarations: Always
|
||||||
|
BreakBeforeBraces: Attach
|
||||||
|
BreakBeforeInlineASMColon: OnlyMultiline
|
||||||
|
BreakBeforeTernaryOperators: true
|
||||||
|
BreakConstructorInitializers: BeforeColon
|
||||||
|
BreakInheritanceList: BeforeColon
|
||||||
|
BreakStringLiterals: true
|
||||||
|
ColumnLimit: 80
|
||||||
|
CommentPragmas: '^ IWYU pragma:'
|
||||||
|
CompactNamespaces: false
|
||||||
|
ConstructorInitializerIndentWidth: 4
|
||||||
|
ContinuationIndentWidth: 4
|
||||||
|
Cpp11BracedListStyle: true
|
||||||
|
DerivePointerAlignment: false
|
||||||
|
DisableFormat: false
|
||||||
|
EmptyLineAfterAccessModifier: Never
|
||||||
|
EmptyLineBeforeAccessModifier: LogicalBlock
|
||||||
|
ExperimentalAutoDetectBinPacking: false
|
||||||
|
FixNamespaceComments: true
|
||||||
|
ForEachMacros:
|
||||||
|
- foreach
|
||||||
|
- Q_FOREACH
|
||||||
|
- BOOST_FOREACH
|
||||||
|
IfMacros:
|
||||||
|
- KJ_IF_MAYBE
|
||||||
|
IncludeBlocks: Preserve
|
||||||
|
IncludeCategories:
|
||||||
|
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
|
||||||
|
Priority: 2
|
||||||
|
SortPriority: 0
|
||||||
|
CaseSensitive: false
|
||||||
|
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
|
||||||
|
Priority: 3
|
||||||
|
SortPriority: 0
|
||||||
|
CaseSensitive: false
|
||||||
|
- Regex: '.*'
|
||||||
|
Priority: 1
|
||||||
|
SortPriority: 0
|
||||||
|
CaseSensitive: false
|
||||||
|
IncludeIsMainRegex: '(Test)?$'
|
||||||
|
IncludeIsMainSourceRegex: ''
|
||||||
|
IndentAccessModifiers: false
|
||||||
|
IndentCaseBlocks: false
|
||||||
|
IndentCaseLabels: false
|
||||||
|
IndentExternBlock: AfterExternBlock
|
||||||
|
IndentGotoLabels: true
|
||||||
|
IndentPPDirectives: None
|
||||||
|
IndentRequiresClause: true
|
||||||
|
IndentWidth: 4
|
||||||
|
IndentWrappedFunctionNames: false
|
||||||
|
InsertBraces: false
|
||||||
|
InsertNewlineAtEOF: false
|
||||||
|
InsertTrailingCommas: None
|
||||||
|
IntegerLiteralSeparator:
|
||||||
|
Binary: 0
|
||||||
|
BinaryMinDigits: 0
|
||||||
|
Decimal: 0
|
||||||
|
DecimalMinDigits: 0
|
||||||
|
Hex: 0
|
||||||
|
HexMinDigits: 0
|
||||||
|
JavaScriptQuotes: Leave
|
||||||
|
JavaScriptWrapImports: true
|
||||||
|
KeepEmptyLinesAtTheStartOfBlocks: true
|
||||||
|
LambdaBodyIndentation: Signature
|
||||||
|
LineEnding: DeriveLF
|
||||||
|
MacroBlockBegin: ''
|
||||||
|
MacroBlockEnd: ''
|
||||||
|
MaxEmptyLinesToKeep: 1
|
||||||
|
NamespaceIndentation: None
|
||||||
|
ObjCBinPackProtocolList: Auto
|
||||||
|
ObjCBlockIndentWidth: 2
|
||||||
|
ObjCBreakBeforeNestedBlockParam: true
|
||||||
|
ObjCSpaceAfterProperty: false
|
||||||
|
ObjCSpaceBeforeProtocolList: true
|
||||||
|
PackConstructorInitializers: BinPack
|
||||||
|
PenaltyBreakAssignment: 2
|
||||||
|
PenaltyBreakBeforeFirstCallParameter: 19
|
||||||
|
PenaltyBreakComment: 300
|
||||||
|
PenaltyBreakFirstLessLess: 120
|
||||||
|
PenaltyBreakOpenParenthesis: 0
|
||||||
|
PenaltyBreakString: 1000
|
||||||
|
PenaltyBreakTemplateDeclaration: 10
|
||||||
|
PenaltyExcessCharacter: 1000000
|
||||||
|
PenaltyIndentedWhitespace: 0
|
||||||
|
PenaltyReturnTypeOnItsOwnLine: 60
|
||||||
|
PointerAlignment: Right
|
||||||
|
PPIndentWidth: -1
|
||||||
|
QualifierAlignment: Leave
|
||||||
|
ReferenceAlignment: Pointer
|
||||||
|
ReflowComments: true
|
||||||
|
RemoveBracesLLVM: false
|
||||||
|
RemoveSemicolon: false
|
||||||
|
RequiresClausePosition: OwnLine
|
||||||
|
RequiresExpressionIndentation: OuterScope
|
||||||
|
SeparateDefinitionBlocks: Leave
|
||||||
|
ShortNamespaceLines: 1
|
||||||
|
SortIncludes: CaseSensitive
|
||||||
|
SortJavaStaticImport: Before
|
||||||
|
SortUsingDeclarations: LexicographicNumeric
|
||||||
|
SpaceAfterCStyleCast: false
|
||||||
|
SpaceAfterLogicalNot: false
|
||||||
|
SpaceAfterTemplateKeyword: true
|
||||||
|
SpaceAroundPointerQualifiers: Default
|
||||||
|
SpaceBeforeAssignmentOperators: true
|
||||||
|
SpaceBeforeCaseColon: false
|
||||||
|
SpaceBeforeCpp11BracedList: false
|
||||||
|
SpaceBeforeCtorInitializerColon: true
|
||||||
|
SpaceBeforeInheritanceColon: true
|
||||||
|
SpaceBeforeParens: ControlStatements
|
||||||
|
SpaceBeforeParensOptions:
|
||||||
|
AfterControlStatements: true
|
||||||
|
AfterForeachMacros: true
|
||||||
|
AfterFunctionDefinitionName: false
|
||||||
|
AfterFunctionDeclarationName: false
|
||||||
|
AfterIfMacros: true
|
||||||
|
AfterOverloadedOperator: false
|
||||||
|
AfterRequiresInClause: false
|
||||||
|
AfterRequiresInExpression: false
|
||||||
|
BeforeNonEmptyParentheses: false
|
||||||
|
SpaceBeforeRangeBasedForLoopColon: true
|
||||||
|
SpaceBeforeSquareBrackets: false
|
||||||
|
SpaceInEmptyBlock: false
|
||||||
|
SpaceInEmptyParentheses: false
|
||||||
|
SpacesBeforeTrailingComments: 1
|
||||||
|
SpacesInAngles: Never
|
||||||
|
SpacesInConditionalStatement: false
|
||||||
|
SpacesInContainerLiterals: true
|
||||||
|
SpacesInCStyleCastParentheses: false
|
||||||
|
SpacesInLineCommentPrefix:
|
||||||
|
Minimum: 1
|
||||||
|
Maximum: -1
|
||||||
|
SpacesInParentheses: false
|
||||||
|
SpacesInSquareBrackets: false
|
||||||
|
Standard: Latest
|
||||||
|
StatementAttributeLikeMacros:
|
||||||
|
- Q_EMIT
|
||||||
|
StatementMacros:
|
||||||
|
- Q_UNUSED
|
||||||
|
- QT_REQUIRE_VERSION
|
||||||
|
TabWidth: 8
|
||||||
|
UseTab: Never
|
||||||
|
WhitespaceSensitiveMacros:
|
||||||
|
- BOOST_PP_STRINGIZE
|
||||||
|
- CF_SWIFT_NAME
|
||||||
|
- NS_SWIFT_NAME
|
||||||
|
- PP_STRINGIZE
|
||||||
|
- STRINGIZE
|
||||||
|
...
|
||||||
|
|
8
Spring-2023/CS-2124/Assignment-2/.gitignore
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
bin
|
||||||
|
CMakeFiles
|
||||||
|
Makefile
|
||||||
|
CMakeCache.txt
|
||||||
|
cmake_install.cmake
|
||||||
|
README.pdf
|
||||||
|
*.tex
|
||||||
|
.tex-out
|
BIN
Spring-2023/CS-2124/Assignment-2/Assignment.pdf
Normal file
12
Spring-2023/CS-2124/Assignment-2/CMakeLists.txt
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.25)
|
||||||
|
|
||||||
|
set(SOURCES src/lib/vec.h src/lib/lib.h src/lib/lib.c)
|
||||||
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY bin)
|
||||||
|
|
||||||
|
|
||||||
|
project(AssignmentOne LANGUAGES C)
|
||||||
|
|
||||||
|
add_executable(PartOne src/part_one.c ${SOURCES})
|
||||||
|
add_executable(PartTwo src/part_two.c ${SOURCES})
|
||||||
|
add_executable(PartThree src/part_three.c ${SOURCES})
|
||||||
|
|
47
Spring-2023/CS-2124/Assignment-2/README.org
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
* Assignment 2
|
||||||
|
|
||||||
|
- ABC123: =zfp106=
|
||||||
|
- Name: =Price Hiller=
|
||||||
|
- Course: =CS2124=
|
||||||
|
- Section: =0C3=
|
||||||
|
- Semester: =Spring 2024=
|
||||||
|
|
||||||
|
** Source Code
|
||||||
|
The full source code for this project can be found at [[https://git.orion-technologies.io/Price/college/src/branch/Development/Spring-2023/CS-2124/Assignment-2]]
|
||||||
|
|
||||||
|
** Running the Programs
|
||||||
|
1. Install [[https://cmake.org/download/][cmake]] version 3.25 or greater.
|
||||||
|
2. Ensure you have a recent version of ~make~ at the time of writing. This project successfully
|
||||||
|
compiles with ~GNU make~ version ~4.4.1~.
|
||||||
|
3. Go the directory with ~CMakeLists.txt~ and run ~cmake .~ to generate a Makefile.
|
||||||
|
4. Run ~make all~ to compile all the programs.
|
||||||
|
5. Go into the newly created ~bin~ directory where all the compiled programs will be output to
|
||||||
|
6. Programs will be named ~PartOne~, ~PartTwo~, and ~PartThree~
|
||||||
|
|
||||||
|
** Program Outputs
|
||||||
|
|
||||||
|
*** Part One
|
||||||
|
C program to Check for balanced Parentheses in an Expression using Stack. The program will
|
||||||
|
check if the expression has balanced Parentheses.
|
||||||
|
|
||||||
|
1. [[./assets/PartOne/img1.png]]
|
||||||
|
2. [[./assets/PartOne/img2.png]]
|
||||||
|
3. [[./assets/PartOne/img3.png]]
|
||||||
|
4. [[./assets/PartOne/img4.png]]
|
||||||
|
5. [[./assets/PartOne/img5.png]]
|
||||||
|
|
||||||
|
*** Part Two
|
||||||
|
Write a program which will take input (Infix expression) from user and converts the expression
|
||||||
|
to Postfix expression (using stacks).
|
||||||
|
|
||||||
|
1. [[./assets/PartTwo/img1.png]]
|
||||||
|
2. [[./assets/PartTwo/img2.png]]
|
||||||
|
3. [[./assets/PartTwo/img3.png]]
|
||||||
|
4. [[./assets/PartTwo/img4.png]]
|
||||||
|
|
||||||
|
|
||||||
|
*** Part Three
|
||||||
|
Implement code that will tell you the time binary search took when implemented through both a
|
||||||
|
recursive method and iterative method
|
||||||
|
|
||||||
|
1. [[./assets/PartThree/img1.png]]
|
BIN
Spring-2023/CS-2124/Assignment-2/assets/PartOne/img1.png
Normal file
After Width: | Height: | Size: 22 KiB |
BIN
Spring-2023/CS-2124/Assignment-2/assets/PartOne/img2.png
Normal file
After Width: | Height: | Size: 25 KiB |
BIN
Spring-2023/CS-2124/Assignment-2/assets/PartOne/img3.png
Normal file
After Width: | Height: | Size: 24 KiB |
BIN
Spring-2023/CS-2124/Assignment-2/assets/PartOne/img4.png
Normal file
After Width: | Height: | Size: 24 KiB |
BIN
Spring-2023/CS-2124/Assignment-2/assets/PartOne/img5.png
Normal file
After Width: | Height: | Size: 20 KiB |
BIN
Spring-2023/CS-2124/Assignment-2/assets/PartThree/img1.png
Normal file
After Width: | Height: | Size: 211 KiB |
BIN
Spring-2023/CS-2124/Assignment-2/assets/PartTwo/img1.png
Normal file
After Width: | Height: | Size: 38 KiB |
BIN
Spring-2023/CS-2124/Assignment-2/assets/PartTwo/img2.png
Normal file
After Width: | Height: | Size: 42 KiB |
BIN
Spring-2023/CS-2124/Assignment-2/assets/PartTwo/img3.png
Normal file
After Width: | Height: | Size: 42 KiB |
BIN
Spring-2023/CS-2124/Assignment-2/assets/PartTwo/img4.png
Normal file
After Width: | Height: | Size: 37 KiB |
57
Spring-2023/CS-2124/Assignment-2/src/lib/lib.c
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
// Credit to
|
||||||
|
// https://stackoverflow.com/questions/40159892/using-asprintf-on-windows/49873938#49873938
|
||||||
|
// with modifications by me
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdarg.h> /* needed for va_* */
|
||||||
|
#include <stdio.h> /* needed for vsnprintf */
|
||||||
|
#include <stdlib.h> /* needed for malloc, free */
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#include <direct.h>
|
||||||
|
#else
|
||||||
|
#include <linux/limits.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int vscprintf(const char *format, va_list ap) {
|
||||||
|
va_list ap_copy;
|
||||||
|
va_copy(ap_copy, ap);
|
||||||
|
int retval = vsnprintf(NULL, 0, format, ap_copy);
|
||||||
|
va_end(ap_copy);
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* asprintf, vasprintf:
|
||||||
|
* MSVC does not implement these, thus we implement them here
|
||||||
|
* GNU-C-compatible compilers implement these with the same names, thus we
|
||||||
|
* don't have to do anything
|
||||||
|
*/
|
||||||
|
int vasprintf(char **strp, const char *format, va_list ap) {
|
||||||
|
int len = vscprintf(format, ap);
|
||||||
|
if (len == -1)
|
||||||
|
return -1;
|
||||||
|
char *str = (char *)malloc((size_t)len + 1);
|
||||||
|
if (!str)
|
||||||
|
return -1;
|
||||||
|
int retval = vsnprintf(str, len + 1, format, ap);
|
||||||
|
if (retval == -1) {
|
||||||
|
free(str);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
*strp = str;
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* fmtstr(const char *format, ...) {
|
||||||
|
char *str = NULL;
|
||||||
|
va_list ap;
|
||||||
|
va_start(ap, format);
|
||||||
|
vasprintf(&str, format, ap);
|
||||||
|
va_end(ap);
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_header() {
|
||||||
|
printf("Price Hiller | zfp106 | <Spring 2024>\n");
|
||||||
|
}
|
35
Spring-2023/CS-2124/Assignment-2/src/lib/lib.h
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
// Credit to
|
||||||
|
// https://stackoverflow.com/questions/40159892/using-asprintf-on-windows/49873938#49873938
|
||||||
|
// with modifications by me
|
||||||
|
|
||||||
|
#ifndef FMTSTR_H
|
||||||
|
#define FMTSTR_H
|
||||||
|
|
||||||
|
#if defined(__GNUC__) && !defined(_GNU_SOURCE)
|
||||||
|
#define _GNU_SOURCE /* needed for (v)asprintf, affects '#include <stdio.h>' */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <stdarg.h> /* needed for va_* */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* vscprintf:
|
||||||
|
* MSVC implements this as _vscprintf, thus we just 'symlink' it here
|
||||||
|
* GNU-C-compatible compilers do not implement this, thus we implement it here
|
||||||
|
*/
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#define vscprintf _vscprintf
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
int vasprintf(char **strp, const char *format, va_list ap);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __GNUC__
|
||||||
|
int vscprintf(const char *format, va_list ap);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
char* fmtstr(const char *format, ...);
|
||||||
|
void print_header();
|
||||||
|
|
||||||
|
#endif
|
79
Spring-2023/CS-2124/Assignment-2/src/lib/vec.h
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
#pragma once
|
||||||
|
#define VECTOR_LIB
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
// This code is pretty bad in all honesty, but I *really* didn't want to play
|
||||||
|
// the void pointer game.
|
||||||
|
#define DECL_VEC(type_name, type) \
|
||||||
|
typedef struct { \
|
||||||
|
size_t elem_count; \
|
||||||
|
size_t capacity; \
|
||||||
|
type *data; \
|
||||||
|
} type_name; \
|
||||||
|
\
|
||||||
|
type_name type_name##_new(size_t initial_capacity) { \
|
||||||
|
initial_capacity = (initial_capacity < 1) ? 1 : initial_capacity; \
|
||||||
|
type_name v = {.capacity = initial_capacity, \
|
||||||
|
.elem_count = 0, \
|
||||||
|
.data = malloc(sizeof(type *) * initial_capacity)}; \
|
||||||
|
return v; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
bool type_name##_alloc(type_name *v, size_t extra_capacity) { \
|
||||||
|
if (extra_capacity == 0) { \
|
||||||
|
return false; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
int desired_size = v->capacity + extra_capacity; \
|
||||||
|
type *newData = realloc(v->data, sizeof(type *) * desired_size); \
|
||||||
|
if (newData == NULL) \
|
||||||
|
return false; \
|
||||||
|
v->data = newData; \
|
||||||
|
v->capacity = desired_size; \
|
||||||
|
return true; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
bool type_name##_ensure_capacity(type_name *v, size_t capacity) { \
|
||||||
|
if (v->capacity < capacity) \
|
||||||
|
return type_name##_alloc(v, capacity - v->capacity); \
|
||||||
|
return true; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
void type_name##_push(type_name *v, type item) { \
|
||||||
|
type_name##_ensure_capacity(v, v->elem_count + 1); \
|
||||||
|
v->data[v->elem_count++] = item; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
type *type_name##_get(type_name *v, size_t index) { \
|
||||||
|
if (index >= 0 && index < v->elem_count) \
|
||||||
|
return &v->data[index]; \
|
||||||
|
return NULL; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
void type_name##_del(type_name *v, size_t index) { \
|
||||||
|
if (index >= 0 && index < v->elem_count) { \
|
||||||
|
for (int i = index; (i < v->elem_count - 1); ++i) { \
|
||||||
|
v->data[i] = v->data[i + 1]; \
|
||||||
|
} \
|
||||||
|
v->elem_count--; \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
type type_name##_pop(type_name *v) { \
|
||||||
|
type popped_value = *type_name##_get(v, v->elem_count - 1); \
|
||||||
|
type_name##_del(v, v->elem_count - 1); \
|
||||||
|
return popped_value; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
type type_name##_first(type_name *v) { \
|
||||||
|
type found_value = *type_name##_get(v, 0); \
|
||||||
|
type_name##_del(v, 0); \
|
||||||
|
return found_value; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
type *type_name##_peek(type_name *v) { \
|
||||||
|
return type_name##_get(v, v->elem_count - 1); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
void type_name##_free(type_name *v) { free(v->data); }
|
73
Spring-2023/CS-2124/Assignment-2/src/part_one.c
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
#include "lib/lib.h"
|
||||||
|
#include "lib/vec.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
DECL_VEC(CharVec, char);
|
||||||
|
|
||||||
|
CharVec readStdinToNewline() {
|
||||||
|
CharVec cvec = CharVec_new(2);
|
||||||
|
int ch;
|
||||||
|
while (EOF != (ch = fgetc(stdin)) && ch != '\n') {
|
||||||
|
CharVec_push(&cvec, ch);
|
||||||
|
}
|
||||||
|
return cvec;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool pairedBraces(char open_brace, char close_brace) {
|
||||||
|
if (open_brace == '\0')
|
||||||
|
return true;
|
||||||
|
|
||||||
|
int asciiDiff = 2;
|
||||||
|
if (open_brace == '(' || open_brace == ')') {
|
||||||
|
asciiDiff = 1;
|
||||||
|
}
|
||||||
|
return ((int)open_brace - ((int)close_brace - asciiDiff)) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool checkCharVecBalance(CharVec *cvec) {
|
||||||
|
int balance = 0;
|
||||||
|
CharVec braceStack = CharVec_new(cvec->elem_count);
|
||||||
|
char lastOpenBrace = '\0';
|
||||||
|
while (cvec->elem_count != 0) {
|
||||||
|
char val = CharVec_first(cvec);
|
||||||
|
switch (val) {
|
||||||
|
case '(':
|
||||||
|
case '[':
|
||||||
|
case '{':
|
||||||
|
CharVec_push(&braceStack, val);
|
||||||
|
balance++;
|
||||||
|
break;
|
||||||
|
case ')':
|
||||||
|
case ']':
|
||||||
|
case '}':
|
||||||
|
if (!pairedBraces(CharVec_pop(&braceStack), val))
|
||||||
|
return 0;
|
||||||
|
balance--;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (balance == 0) {
|
||||||
|
return -1;
|
||||||
|
} else {
|
||||||
|
return cvec->elem_count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
print_header();
|
||||||
|
printf("Enter Expression: ");
|
||||||
|
CharVec cvec = readStdinToNewline();
|
||||||
|
char *input = malloc(cvec.elem_count + 1);
|
||||||
|
strcpy(input, cvec.data);
|
||||||
|
|
||||||
|
if (checkCharVecBalance(&cvec)) {
|
||||||
|
printf("It's balanced (^_^)\n");
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
} else {
|
||||||
|
printf("It's Not Balanced (0_0)\n");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
}
|
104
Spring-2023/CS-2124/Assignment-2/src/part_three.c
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
#include "lib/lib.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
void swap(int *a, int *b) {
|
||||||
|
int t = *a;
|
||||||
|
*a = *b;
|
||||||
|
*b = t;
|
||||||
|
}
|
||||||
|
|
||||||
|
int partition(int arr[], int low, int high) {
|
||||||
|
int pivot = arr[high];
|
||||||
|
int i = (low - 1);
|
||||||
|
|
||||||
|
for (int j = low; j <= high - 1; j++) {
|
||||||
|
if (arr[j] < pivot) {
|
||||||
|
i++;
|
||||||
|
swap(&arr[i], &arr[j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
swap(&arr[i + 1], &arr[high]);
|
||||||
|
return (i + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void q_sort(int arr[], int low, int high) {
|
||||||
|
if (low < high) {
|
||||||
|
int p = partition(arr, low, high);
|
||||||
|
q_sort(arr, low, p - 1);
|
||||||
|
q_sort(arr, p + 1, high);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int bsearch_iterative(int arr[], int key, int len) {
|
||||||
|
int low = 0;
|
||||||
|
int high = len;
|
||||||
|
while (low < high) {
|
||||||
|
int mid = (low + high) / 2;
|
||||||
|
if (key == arr[mid]) {
|
||||||
|
return mid;
|
||||||
|
} else if (key > arr[mid]) {
|
||||||
|
low = mid + 1;
|
||||||
|
} else {
|
||||||
|
high = mid - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int bsearch_recursive(int arr[], int key, int low, int high) {
|
||||||
|
if (low > high) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mid = (low + high) / 2;
|
||||||
|
if (key == arr[mid]) {
|
||||||
|
return mid;
|
||||||
|
} else if (key > arr[mid]) {
|
||||||
|
return bsearch_recursive(arr, key, mid + 1, high);
|
||||||
|
} else {
|
||||||
|
return bsearch_recursive(arr, key, low, mid - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
print_header();
|
||||||
|
printf("Enter 5 elements:\n");
|
||||||
|
int arr_len = 5;
|
||||||
|
int arr[arr_len];
|
||||||
|
for (int i = 0; i < arr_len; i++) {
|
||||||
|
scanf("%d", &arr[i]);
|
||||||
|
}
|
||||||
|
printf("\nEnter element to search for:\n");
|
||||||
|
int search_element;
|
||||||
|
scanf("%d", &search_element);
|
||||||
|
q_sort(arr, 0, arr_len);
|
||||||
|
|
||||||
|
clock_t tic, toc;
|
||||||
|
printf("\n\n--------\n=> Binary Search (Iterative Approach)");
|
||||||
|
tic = clock();
|
||||||
|
int b_iter_index = bsearch_iterative(arr, search_element, arr_len);
|
||||||
|
toc = clock();
|
||||||
|
if (b_iter_index == -1) {
|
||||||
|
printf("Unable to find the element '%d'!\n", search_element);
|
||||||
|
} else {
|
||||||
|
printf("Element found at index %d\n", b_iter_index);
|
||||||
|
}
|
||||||
|
printf("Total time taken by CPU (End Time - Start Time)/clock per sec: %f\n--------\n",
|
||||||
|
(double)(toc - tic) / CLOCKS_PER_SEC);
|
||||||
|
|
||||||
|
printf("\n--------\n=> Binary Search (Recursive Approach)");
|
||||||
|
tic = clock();
|
||||||
|
int b_rec_index = bsearch_recursive(arr, search_element, 0, arr_len);
|
||||||
|
toc = clock();
|
||||||
|
if (b_iter_index == -1) {
|
||||||
|
printf("Unable to find the element '%d'!\n", search_element);
|
||||||
|
} else {
|
||||||
|
printf("Element found at index %d\n", b_rec_index);
|
||||||
|
}
|
||||||
|
printf("Total time taken by CPU (End Time - Start Time)/clock per sec: %f\n--------\n",
|
||||||
|
(double)(toc - tic) / CLOCKS_PER_SEC);
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
66
Spring-2023/CS-2124/Assignment-2/src/part_two.c
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
#include "lib/lib.h"
|
||||||
|
#include "lib/vec.h"
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
DECL_VEC(CharVec, char);
|
||||||
|
|
||||||
|
int priority(char x) {
|
||||||
|
switch (x) {
|
||||||
|
case '(':
|
||||||
|
return 0;
|
||||||
|
break;
|
||||||
|
case '+':
|
||||||
|
case '-':
|
||||||
|
return 1;
|
||||||
|
case '*':
|
||||||
|
case '/':
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
CharVec readStdinToNewline() {
|
||||||
|
CharVec cvec = CharVec_new(2);
|
||||||
|
int ch;
|
||||||
|
while (EOF != (ch = fgetc(stdin)) && ch != '\n') {
|
||||||
|
CharVec_push(&cvec, ch);
|
||||||
|
}
|
||||||
|
return cvec;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
print_header();
|
||||||
|
printf("Enter the Infix Expression:\n");
|
||||||
|
CharVec stack = CharVec_new(2);
|
||||||
|
CharVec input = readStdinToNewline();
|
||||||
|
printf("The Postfix expression:\n ");
|
||||||
|
while (input.elem_count != 0) {
|
||||||
|
char ch = CharVec_first(&input);
|
||||||
|
if (ch == ' ') {
|
||||||
|
continue;
|
||||||
|
} else if (isalnum(ch)) {
|
||||||
|
printf("%c ", ch);
|
||||||
|
} else if (ch == '(') {
|
||||||
|
CharVec_push(&stack, ch);
|
||||||
|
} else if (ch == ')') {
|
||||||
|
char x;
|
||||||
|
while ((x = CharVec_pop(&stack)) != '(') {
|
||||||
|
printf("%c ", x);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
while (stack.elem_count != 0 && priority(*CharVec_peek(&stack)) >= priority(ch))
|
||||||
|
printf("%c ", CharVec_pop(&stack));
|
||||||
|
CharVec_push(&stack, ch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while (stack.elem_count != 0) {
|
||||||
|
printf("%c ", CharVec_pop(&stack));
|
||||||
|
}
|
||||||
|
printf("\nEnd of Program\n");
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
BIN
Spring-2023/CS-2233/Assignment-3/Assignment.pdf
Normal file
161
Spring-2023/CS-2233/Assignment-3/Solution.typ
Normal file
@ -0,0 +1,161 @@
|
|||||||
|
#let m(math) = align(center)[$#math$]
|
||||||
|
#let pgbreakmsg = align(center, text(blue, weight: "black", size: 1.5em)[See Next Page\ ↓])
|
||||||
|
|
||||||
|
#let solve(work, solution) = align(
|
||||||
|
center,
|
||||||
|
)[
|
||||||
|
#let solution = align(center, block(
|
||||||
|
inset: 5pt,
|
||||||
|
stroke: blue + .3pt,
|
||||||
|
fill: rgb(0, 149, 255, 15%),
|
||||||
|
radius: 4pt,
|
||||||
|
)[#align(left)[#solution]])
|
||||||
|
|
||||||
|
#if work == [] [
|
||||||
|
#solution
|
||||||
|
] else [
|
||||||
|
#block(inset: 6pt, radius: 4pt, stroke: luma(50%) + .5pt, fill: luma(90%))[
|
||||||
|
#align(left, text(font: "Liberation Sans", size: .85em, work))
|
||||||
|
#solution
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
|
||||||
|
#let problem-header(number, points) = [== Problem #number. #text(weight: "regular")[[#points
|
||||||
|
points]]]
|
||||||
|
|
||||||
|
#let problem(number, points, body) = [
|
||||||
|
== Problem #number. #text(weight: "regular")[[#points points]]
|
||||||
|
#body
|
||||||
|
]
|
||||||
|
|
||||||
|
#set page(margin: (x: .4in, y: .4in))
|
||||||
|
#set table(align: center)
|
||||||
|
|
||||||
|
_*Price Hiller*_
|
||||||
|
#v(-.8em)
|
||||||
|
_*zfp106*_
|
||||||
|
#v(-.8em)
|
||||||
|
Homework Assignment 3
|
||||||
|
#v(-.8em)
|
||||||
|
CS 2233
|
||||||
|
#v(-.8em)
|
||||||
|
Section 001
|
||||||
|
|
||||||
|
#align(
|
||||||
|
center,
|
||||||
|
block(
|
||||||
|
inset: 6pt,
|
||||||
|
radius: 4pt,
|
||||||
|
stroke: luma(50%) + .5pt,
|
||||||
|
fill: luma(90%),
|
||||||
|
)[If you are interested in viewing the source code of this document, you can do so
|
||||||
|
by clicking
|
||||||
|
#text(
|
||||||
|
blue,
|
||||||
|
link(
|
||||||
|
"https://git.orion-technologies.io/Price/college/src/branch/Development/Spring-2023/CS-2233/Assignment-3/Solution.typ",
|
||||||
|
"here",
|
||||||
|
),
|
||||||
|
).],
|
||||||
|
)
|
||||||
|
= Problems
|
||||||
|
|
||||||
|
#problem(
|
||||||
|
1,
|
||||||
|
10,
|
||||||
|
)[
|
||||||
|
- Complete all participation activities in zyBook sections $2.1$, $2.2$, and $2.4$-$2.6$.
|
||||||
|
#solve[][Done]
|
||||||
|
]
|
||||||
|
|
||||||
|
#problem(
|
||||||
|
2,
|
||||||
|
10,
|
||||||
|
)[
|
||||||
|
Prove that if $a$, $b$, and $c$ are odd integers, then $a + b + c$ is an odd
|
||||||
|
integer.
|
||||||
|
|
||||||
|
#solve[
|
||||||
|
An odd integer is expressed as $2k + 1$ where $k$ is some integer.
|
||||||
|
][
|
||||||
|
1. Suppose that $a$, $b$, and $c$ are odd integers. I shall prove that $a + b + c$ is an odd
|
||||||
|
integer.
|
||||||
|
2. Since $a$, $b$, and $c$ are integers, $a + b + c$ is also an integer
|
||||||
|
3. Since $a$ is odd, there is an integer $q$ such that $a = 2q + 1$
|
||||||
|
4. Since $b$ is odd, there is an integer $w$ such that $b = 2w + 1$
|
||||||
|
5. Since $c$ is odd, there is an integer $e$ such that $c = 2e + 1$
|
||||||
|
6. $a + b + c = (2q + 1) + (2w + 1) + (2e + 1) = 2(q + w + e + 1) + 1$
|
||||||
|
7. Let $m = q + w + e + 1$
|
||||||
|
8. Since $q$, $w$, $e$, and $1$ are integers, $m$ must be an integer
|
||||||
|
9. Since $a + b + c = 2m + 1$, therefore $a + b + c$ is
|
||||||
|
an odd integer
|
||||||
|
]
|
||||||
|
]
|
||||||
|
#problem(
|
||||||
|
3,
|
||||||
|
30,
|
||||||
|
)[
|
||||||
|
Recall that a rational number can be put in the form $p/q$ where $p$ and $q$ are
|
||||||
|
integers and $q ≠
|
||||||
|
0$. Prove the following for any rational number, $x$:
|
||||||
|
|
||||||
|
a.) If $x$ is rational, then $x - 5$ is rational
|
||||||
|
#solve[][
|
||||||
|
1. Let $x$ be a rational number. I will show that $x - 5$ is also a rational
|
||||||
|
number.
|
||||||
|
2. Since $x$ is rational, there exists integers $p$ and $q$ such that $x = p/q$ and $q ≠ 0$.
|
||||||
|
3. Thus $x - 5 = p/q - 5$
|
||||||
|
4. Since $5$ is an integer, $5$ is also a rational number written as $5/1$
|
||||||
|
5. Therefore $p/q - 5 = p/q - 5/1$
|
||||||
|
6. Working $p/q - 5/1$ we get $(p - 5q)/(q × 1)$ which is $(p - 5q)/q$
|
||||||
|
7. Since $x - 5$ is equal to the ratio of two integers where the deonominator $≠ 0$, then $x - 5$ is a rational number
|
||||||
|
]
|
||||||
|
b.) If $x - 5$ is rational, then $x/3$ is rational
|
||||||
|
|
||||||
|
#solve[][
|
||||||
|
1. Let $x - 5$ be rational. I will show that $x/3$ is also rational
|
||||||
|
2. $x - 5 = p/q$ for some integers $p$, $q$, where $q ≠ 0$
|
||||||
|
3. Add $5$ to both sides giving $x = p/q + 5$
|
||||||
|
4. Divide both sides by $3$ giving $x/3 = (p/q + 5)/3$
|
||||||
|
5. $(p/q + 5)/3$ is also rational
|
||||||
|
6. Therefore $x/3$ is rational
|
||||||
|
]
|
||||||
|
\
|
||||||
|
c.) If $x/3$ is rational, then $x$ is rational
|
||||||
|
#solve[][
|
||||||
|
1. Let $x/3$ be rational. I will show that $x$ is rational
|
||||||
|
2. $x/3 = p/q$ for some integers $p$ and $q$ where $q ≠ 0$
|
||||||
|
3. Multiply both sides of the equation by $q$ to get $x = 3p/q$
|
||||||
|
4. Since $p$, $q$, and $3$ are integers, $x$ is rational
|
||||||
|
]
|
||||||
|
]
|
||||||
|
|
||||||
|
#problem(
|
||||||
|
4,
|
||||||
|
20,
|
||||||
|
)[
|
||||||
|
Consider the following statement: For all integers $m$ and $n$, if $m - n$ is
|
||||||
|
odd, then $m$ is odd or $n$ is odd.
|
||||||
|
|
||||||
|
a.) Prove the statement using a proof by contrapositive
|
||||||
|
#solve[][
|
||||||
|
1. Let $m$ and $n$ be integers. I will show that if $m$ is even and $n$ is even
|
||||||
|
then $m - n$ is even
|
||||||
|
2. Since $m$ is even, there exists an integer $k$ such that $m = 2k$
|
||||||
|
3. Since $n$ is even, there exists an integer $j$ such that $n = 2j$
|
||||||
|
4. Thus, $m - n = 2k - 2j = 2(k - j )$
|
||||||
|
5. Since $k - j$ is an integer, $2(k - j)$ is even
|
||||||
|
6. Therefore, if $m$ is even and $n$ is even, $m - n$ is even
|
||||||
|
]
|
||||||
|
b.) Prove the statement by using a proof by contradiction
|
||||||
|
#solve[][
|
||||||
|
1. Let $m$ and $n$ be integers. Assume then that $m - n$ is odd, but both $m$ and $n$ are
|
||||||
|
even
|
||||||
|
2. If $m$ is even, then $m = 2k$ for some integer $k$
|
||||||
|
3. If $n$ is even, then $n = 2j$ for some integer $j$
|
||||||
|
4. Then $m - n = 2k - 2j = 2(k - j)$
|
||||||
|
5. But $2(k - j)$ is even, which contradicts that $m - n$ is odd
|
||||||
|
6. Thus, if $m - n$ is odd, then $m$ must be odd or $n$ must be odd
|
||||||
|
]
|
||||||
|
]
|
@ -1,4 +1,10 @@
|
|||||||
* DONE Assignment 1
|
* DONE Assignment 1 :college:cs2233:
|
||||||
DEADLINE: <2024-01-26 Fri> SCHEDULED: <2024-01-25 Thu>
|
DEADLINE: <2024-01-26 Fri> SCHEDULED: <2024-01-25 Thu>
|
||||||
|
|
||||||
Complete Zybooks section ~1~ and the first homework assignment
|
Complete Zybooks section ~1~ and the first homework assignment
|
||||||
|
|
||||||
|
* TODO Assignment 3 :college:cs2233:
|
||||||
|
DEADLINE: <2024-02-11 Sun> SCHEDULED: <2024-02-11 Sun>
|
||||||
|
|
||||||
|
Complete Zybooks section ~2~ and the third homework assignment
|
||||||
|
|
||||||
|