Skip to content

Hw0 #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 39 commits into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
3da0e7d
Create makefie
stevengogogo Feb 19, 2021
06ce9ae
try cutest
stevengogogo Feb 19, 2021
2417903
Add Unit Testing
stevengogogo Feb 19, 2021
839c8d5
Create Bigint. Need to revise malloc heap
stevengogogo Feb 20, 2021
71a7b0e
Constructing Add function
stevengogogo Feb 20, 2021
2a996cf
Add array doc
stevengogogo Feb 24, 2021
6bb6d2b
makefile merging targets completed
stevengogogo Feb 24, 2021
2eba5dd
adding vs code debugger
stevengogogo Feb 24, 2021
f0dca9e
gdb not found
stevengogogo Feb 24, 2021
71e2fe5
🎉Complete VS code debugger
stevengogogo Feb 25, 2021
22e8416
Merge pull request #2 from stevengogogo/MakeFileMergine
stevengogogo Feb 25, 2021
e3a336b
add gitignore
stevengogogo Feb 25, 2021
39073c1
add git ignore
stevengogogo Feb 25, 2021
1f4cf0f
create add function
stevengogogo Mar 2, 2021
edabafa
use static memory
stevengogogo Mar 2, 2021
0a2ff3f
changed to static type
stevengogogo Mar 2, 2021
0dfa0fb
create string compare
stevengogogo Mar 2, 2021
a593749
update utils
stevengogogo Mar 2, 2021
7947fc5
add submodule
stevengogogo Mar 3, 2021
b98e804
test submodule renew
stevengogogo Mar 3, 2021
b8e601d
Complete GCD
stevengogogo Mar 6, 2021
7f5d298
create pointer problem
stevengogogo Mar 7, 2021
721379c
Added Problem3a.png
stevengogogo Mar 7, 2021
e272740
Problem3a.png
stevengogogo Mar 7, 2021
2baa317
Problem3a.png
stevengogogo Mar 7, 2021
063ec13
upload img
stevengogogo Mar 7, 2021
938ddcb
remove c file. use onlinegdb for testing human compiler
stevengogogo Mar 7, 2021
e1dc6f0
Porblem3b_NegativeArray.png
stevengogogo Mar 7, 2021
fce3dce
Porblem3b_add array chart.png
stevengogogo Mar 7, 2021
38f35fa
upload Problem 3b
stevengogogo Mar 7, 2021
9709869
Problem3c.png
stevengogogo Mar 7, 2021
6e12a6f
Update Problem3c.png
stevengogogo Mar 7, 2021
c826d1d
Wrong binary tree
stevengogogo Mar 7, 2021
1177070
Fix wrong binary tree
stevengogogo Mar 7, 2021
811373d
formatting hand-written markdown
stevengogogo Mar 7, 2021
5a029a8
fixed tranverses data problem
stevengogogo Mar 7, 2021
d464d7f
write problem 3c
stevengogogo Mar 7, 2021
114e7a5
Correct Problem 3C
stevengogogo Mar 7, 2021
88918ae
create docs
stevengogogo Mar 8, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "HW0/GreatestCommonDivisor"]
path = HW0/GreatestCommonDivisor
url = https://github.com/stevengogogo/GreatestCommonDivisor
[submodule "HW0/NonogramSolver"]
path = HW0/NonogramSolver
url = https://github.com/stevengogogo/NonogramSolver
1 change: 1 addition & 0 deletions HW0/GreatestCommonDivisor
Submodule GreatestCommonDivisor added at e51b82
1 change: 1 addition & 0 deletions HW0/NonogramSolver
Submodule NonogramSolver added at 99ab2e
258 changes: 258 additions & 0 deletions HW0/Pointers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
# Prolem 3 - *(Human Compiler)(Hand-written)

## Problem 3a: Swaps two arrays using pointers

![Screen Shot 2021-03-07 at 8 34 25 AM](https://user-images.githubusercontent.com/29009898/110225352-efdec580-7f1f-11eb-9675-516acb605e42.png)


```c
int fake_a[] = {1,3};
int fake_b[] = {2,4};
int *real_a = fake_a;
int *real_b = fake_b;

for(int i=0;i<2;i++)
printf("%d", *(real_a + i));
for(int i=0;i<2;i++)
printf("%d", *(real_b + i));

int *tmp = real_a; //tmp is a pointer to pointer
real_b = fake_a; // fill the blanks
real_a = fake_b; // fil the blanks

for(int i=0;i<2;i++)
printf("%d", *(real_a + i));
for(int i=0;i<2;i++)
printf("%d", *(real_b + i));
```

Test at: https://onlinegdb.com/SJ4JFi-7_

![](img/Problem3a.png)

---

## Problem 3b: An array supporting negative indices

![Screen Shot 2021-03-07 at 9 17 08 AM](https://user-images.githubusercontent.com/29009898/110226009-f7a16880-7f25-11eb-81db-7c27eb264c33.png)

```c
#include <stdio.h>
#define MINN -50
#define MAXN 50

int main(){
int storage[MAXN - MINN + 1] = {0}; // 101
int *ary = storage - MINN ; //fill the blank

for(int i=MINN;i<=MAXN;i++)
ary[i] = i;
for(int i=MINN;i<=MAXN;i++)
printf("%d", ary[i]);
return 0;
}
```
Verified at: https://onlinegdb.com/rydhl3W7u

![](img/Problem3b.png)

---

## Problem 3C: Tranverses data nodes in a linked list.

![Screen Shot 2021-03-07 at 12 03 34 PM](https://user-images.githubusercontent.com/29009898/110228580-38f14280-7f3d-11eb-9891-65aeaeb849e0.png)

```c
#include <stdio.h>
#include <stdlib.h> //malloc / free
#include <memory.h> //memset

//use typedef to define "struct node" as "node"
typedef struct node{
int data;
struct node *nxt;
} node;

node* alloc(int data, node* nxt){
node *tmp = (node*)malloc(sizeof(node));
tmp->data=data;
tmp->nxt = nxt;
return tmp;
}

void destroy(node *head){
if(head != NULL){ //FIll the blank
destroy(head->nxt);
//clean sensitive data;
memset(head,0,sizeof(head));
free(head);
}
}

int main(){
// create nodes [0,1,2,4]
node* head = alloc(0, alloc(1,alloc(2,alloc(4,NULL))));
node* tmp = head;
//print the nodes subsequently
while(tmp!=NULL){
printf("%d -> ", *tmp-> data ); //FIll the blank
tmp = (*tmp)->nxt; //FIll the blank
}
printf("NULL");

//free the nodes subsequently to avoid memory leak
destroy(head);
return 0;
}
```

### Curate

Use `temp->data` not `(*tmp)->data`

#### Copy a pointer to another pointer

![Screen Shot 2021-03-07 at 12 31 17 PM](https://user-images.githubusercontent.com/29009898/110229016-09dcd000-7f41-11eb-9414-77bad043e4c8.png)
> From [Basics of C Programmin](https://computer.howstuffworks.com/c24.htm)

### Question

Wrong version: https://www.onlinegdb.com/
Corrected version: https://onlinegdb.com/BkReV0bXd

---

## Problem 3D: Binary Tree
![Screen Shot 2021-03-07 at 11 52 38 AM](https://user-images.githubusercontent.com/29009898/110228385-9f756100-7f3b-11eb-92e7-76ee4bf3413f.png)
![Screen Shot 2021-03-07 at 11 52 48 AM](https://user-images.githubusercontent.com/29009898/110228386-a603d880-7f3b-11eb-9108-fb2e0b11d480.png)

**問題**: `printf` 應該在 `if` 前面

```c
#include <stdio.h>
#include <stdlib.h> //malloc /free
#include <memory.h> //memset

//use typedef to substitute "struct node with "node""
typedef struct node {
int data;
struct node *left, *right;
} node;

//creates a node filledwith predefined values
node* alloc(int data, node *left, node *right){
node *tmp = (node*)malloc(sizeof(node));
tmp->data = data,
tmp->left = left;
tmp->right = right;
return tmp;
}

//traverses (遍歷) the nodes recursively
void traverse(node* root){
printf("%d", root->data);
if ((root->left != NULL) & (root->right != NULL)){
traverse(root->left);
traverse(root->right);
}
}

//frees the nodes recursively
void destroy(node *root){
if ((root->left != NULL) & (root->right!=NULL)){
destroy(root->left);
destroy(root->right);
//clean sensitive data
memset(root, 0, sizeof(root));
free(root);
}
}

int main(){
// creates a hierarchical data structure
node *root = \
alloc(0,
alloc(3,
alloc(7, NULL, NULL),
alloc(4, NULL, NULL)
),
alloc(2,
alloc(1, NULL, NULL),
alloc(9, NULL, NULL)
)
);

traverse(root);
destroy(root);
}
```
Verify at: https://onlinegdb.com/r1VxlA-7d
![](img/Problem3c.png)

---

## Notes

### Null Pointer and `Segmentation fault`

- Segmentation fault:
> Segmentation fault is a specific kind of error caused by accessing memory that “does not belong to you
- NULL Pointer
> People assign NULL to pointer to indicate that it points to nothing.

### Read Only String and its Dynamical type

- `char* str` Read only: able to share among functions
```c
char *str = "gfg";
```
Read only. `str[0]=a` leads to segmentation fault. Noted that read-only string can be passed among functions:
```c
char *getString(){
char *str = "GfG"; /* Stored in read only part of shared segment */

/* No problem: remains at address str after getString() returns*/
return str;
}

int main(){
printf("%s", getString());
getchar();
return 0;
}

```
- `char str[]` Mutable string: unable to share among functions.
```c
int main(){
char str[];
str = "gfg";
}
```
- Store in heap: allow to share and modify
```c
char *getString()
{
int size = 4;
char *str = (char *)malloc(sizeof(char)*size); /*Stored in heap segment*/
*(str+0) = 'G';
*(str+1) = 'f';
*(str+2) = 'G';
*(str+3) = '\0';

/* No problem: string remains at str after getString() returns */
return str;
}
int main()
{
printf("%s", getString());
getchar();
return 0;
}
```

---
## Reference
1. Segmentation fault and pointer. [[stackoverflow](https://stackoverflow.com/questions/17873561/pointer-initialisation-gives-segmentation-fault)]
2. String and storage. [[GreekforGeek](https://www.geeksforgeeks.org/storage-for-strings-in-c/)]
3. Pointing to the same address. [[eBook-Basics of C Programming](https://computer.howstuffworks.com/c24.htm)]
Binary file added HW0/Pointers/img/Problem3a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added HW0/Pointers/img/Problem3b.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added HW0/Pointers/img/Problem3c.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 0 additions & 6 deletions HW0/Problem_1/README.md

This file was deleted.

15 changes: 0 additions & 15 deletions HW0/Problem_1/bigint.c

This file was deleted.

10 changes: 0 additions & 10 deletions HW0/Problem_1/bigint.h

This file was deleted.

1 change: 0 additions & 1 deletion HW0/Problem_1/main.c

This file was deleted.

39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,40 @@
# Data Structures 2021 Spring

## Course Website


## How to use `gitsubmodule`


Show status
-----------

```bash
git submodule status --recursive
```

Update all submodule
--------------------

#### Add project

```bash
# Clone to the current folder:
git submodule add <repository>
git submodule init

```

#### Fetch
```bash
git submodule update --recursive
```

#### Pull
```bash
git pull --recurse-submodules
```

## Plotting with [diagrams](https://app.diagrams.net/)

[Diagram](https://app.diagrams.net/) features uploading to Github repository, making the drawing up-to-date easily!
Binary file added assets/Screen_Shot_2021-03-02_at_1.53.11_PM.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file added docs/.nojekyll
Empty file.
24 changes: 24 additions & 0 deletions docs/assets/css/custom.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
@import url("https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,400;0,700;1,400&display=swap");

:root {
--mono-hue: 222;
--mono-saturation: 14%;
--base-background-color: #282c35;
--theme-color: #66e0ff;
--link-color: var(--theme-color);

--base-font-family: Roboto, sans-serif;
--strong-font-weight: 700;

--search-flex-order: 0;
--search-margin: 1rem 0;
--docsify-example-panels-left-panel-width : 50%;
--docsify-example-panels-right-panel-width : 50%;
}

@media screen and (min-width: 1230px) {
:root {
--sidebar-width: 22rem;
--content-max-width : 66em;
}
}
Binary file added docs/favicon.ico
Binary file not shown.
Loading