Skip to content

Adding Elixir, src/READMEs, & updated README.md #342

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
478 changes: 417 additions & 61 deletions README.md

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions logos/elixir.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
118 changes: 118 additions & 0 deletions src/c/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
## C Language Basic Syntax Review
C language is one of the most popular general-purpose programming language developed by Dennis Ritchie at Bell laboratories for UNIX operating system. The initial release of C Language was in the year 1972. Most of the desktop operating systems are written in C Language.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we update 'most popular general-purpose programming language' to 'most popular general-purpose programming languages'?

Also, can we add a 'the' before 'UNIX operating system'?


Key features:
- Structured Programming
- Popular system programming language
- UNIX, MySQL and Oracle are completely written in C.
- Supports variety of platforms
- Efficient and also handle low-level activities.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we update this to, 'Efficient and can handle low-level activities.'?

- As fast as assembly language and hence used as system development language.


#### 1. Read inputs
````c
#include <stdio.h>
int main()
{
char name[50];
printf("Enter name:");
scanf("%s", &name);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, here, I think we would need to update this to '%49s' to limit the buffer as only '%s' could lead to a buffer overflow. As this was an introductory example, perhaps we should change the example?
Also, we don't need to use '&' with name in this scanf because name is an array name, which is basically a pointer.

printf("Hello %s" , name );
return 0;

}
````
#### Loops and Conditionals

##### 2. If-Else:

When ever you want to perform a set of operations based on a condition if-else is used.
```c
if(conditional-expression) {
// code
} else {
// code
}
```
You can also use if-else for nested Ifs and if-else-if ladder when multiple conditions are to be performed on a single variable.

##### 3. Switch:

Switch is an alternative to if-else-if ladder.

````c
switch(conditional-expression) {
case value1:
// code
break; // optional
case value2:
// code
break; // optional
...

default:
// code to be executed when all the above cases are not matched;
}
````

##### 4. For:

For loop is used to iterate a set of statements based on a condition.

````c
for(Initialization; Condition; Increment/decrement){
// code
}
````

##### 5. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

````c
while(condition) {
// code
}
````

##### 6. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

````c
do {
// code
} while (condition);

````
#### 7. Arrays
Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index. Index starts from 0 to size-1.

1. One dimentional Array: ` data-type array-name[size];`
2. Two dimensional array: `data-type array-name[size][size];`

#### 8. Functions
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.

Two types of functions are present in C

1. Library Functions: Library functions are the in-built functions which are declared in header files like `printf()`, `scanf()`, `puts()`, `gets()` etc.,
2. User defined functions: User defined functions are the ones which are written by the programmer based on the requirement.


##### 9. How to declare a Function
```c
return_type function_name(parameters);`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please delete the ` at the end of this line?

```
##### How to call a Function
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we follow the same pattern as above? We were at number 9 in 'How to declare a function'.

```c
function_name (parameters)
```

##### How to define a FunctionHow to define a Function
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we follow the same pattern as above? We were at number 9 in 'How to declare a function'.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, 'How to define a Function' is repeated here.

```c
return_type function_name(parameters) {
//code
}
```
123 changes: 123 additions & 0 deletions src/cpp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
## C++ Language Basic Syntax Review
C++ is one of the most popular programming languages. C++ can be found in todays operating systems, Graphical User Interfaces, and embedded systems. C++ is an object-oriented programming language which gives a clear structure to programs and allows code to be reused, lowering development costs. Note, C++ is an extension of C.

Key features:

- Supports different platforms like Windows, various Linux flavours, MacOS etc
- C++ supports OOPS concepts like Inheritance, Polymorphism, Encapsulation and Abstraction.
- Case-sensitive
- C++ is a compiler based language
- C++ supports structured programming language
- C++ provides alot of inbuilt functions and also supports dynamic memory allocation.
- Like C, C++ also allows you to play with memory using Pointers.


#### 1. Read inputs
````cpp
#include <iostream>
#include <string>
using namespace std;

int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
}
````
#### Loops and Conditionals

##### 2. If-Else:

When ever you want to perform a set of operations based on a condition if-else is used.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please update the word 'When ever' to 'Whenever' please?

```cpp
if(conditional-expression) {
// code
} else {
// code
}
```
You can also use if-else for nested Ifs and if-else-if ladder when multiple conditions are to be performed on a single variable.

##### 3. Switch:

Switch is an alternative to if-else-if ladder.

````cpp
switch(conditional-expression) {
case value1:
// code
break; // optional
case value2:
// code
break; // optional
...

default:
// code to be executed when all the above cases are not matched;
}
````

##### 4. For:

For loop is used to iterate a set of statements based on a condition.

````cpp
for(Initialization; Condition; Increment/decrement){
// code
}
````

##### 5. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

````cpp
while(condition) {
// code
}
````

##### 6. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

````cpp
do {
// code
} while (condition);

````
#### 7. Arrays
Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index. Index starts from 0 to size-1.

1. One dimentional Array: ` data-type array-name[size];`
2. Two dimensional array: `data-type array-name[size][size];`

#### 8. Functions
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.

Two types of functions are present in C++

1. Library Functions: Library functions are the in-built functions which are declared in header files like `floor(x)`, `tolower(x)`, `toupper()` etc.,
2. User defined functions: User defined functions are the ones which are written by the programmer based on the requirement.


##### How to declare a Function
```cpp
return_type function_name(parameters);`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as C page. Can we please delete the ` at the end of this line?

```
##### How to call a Function
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we follow the same pattern as you started in C? We stopped at number 8 in this readme 'Functions'.

```cpp
function_name (parameters)
```

##### How to define a FunctionHow to define a Function
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we follow the same pattern as you started in C? We stopped at number 8 in this readme 'Functions'.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate 'How to define a Function."'

```cpp
return_type function_name(parameters) {
//code
}
```
31 changes: 31 additions & 0 deletions src/elixir/CalculatePi.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Module to calculate an approximation of Pi
defmodule PiApproximation do

# Function to calculate pi given number of terms
def calculate_pi(terms) do

# Initialize pi accumulator
pi = 0.0

# Reduce over 1..terms range
# Accumulator is a tuple with pi, denominator, and operation
Enum.reduce(1..terms, {pi, 1.0, 1.0}, fn _, {acc_pi, acc_denom, acc_op} ->

# Calculate new pi value
new_pi = acc_pi + acc_op * (4.0 / acc_denom)

# Return tuple with updated values
{new_pi, acc_denom + 2.0, -acc_op}
end) |> elem(0)
end

end

# Number of terms to approximate
terms = 100_000

# Call approximation function
result = PiApproximation.calculate_pi(terms)

# Print result
IO.puts(result)
26 changes: 26 additions & 0 deletions src/elixir/FactorialIterative.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule Factorial do
# Define a module called Factorial.

def factorial(n) when n >= 0 do
# Calculate the factorial using Enum.reduce.
Enum.reduce(1..n, 1, &(&1 * &2))
end

defp format_factorial(i, result) do
# Format the output string.
"#{i}! = #{result}"
end

def main do
# Entry point of the program.
nums = [0, 1, 2, 3, 4, 5]

Enum.each(nums, fn i ->
# Iterate through the list of numbers.
result = factorial(i)
IO.puts(format_factorial(i, result))
end)
end
end

Factorial.main() # Execute the program.
23 changes: 23 additions & 0 deletions src/elixir/FactorialRecursive.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Define Math module for math utilities
defmodule Math do

# Recursive factorial function
def factorial(0), do: 1 # Base case

def factorial(n) when n > 0 do
# Recursive case
n * factorial(n-1)
end

end

# Array of numbers to get factorials for
nums = [0, 1, 2, 3, 4, 5]

# Loop through the nums array
for i <- nums do

# Calculate and print factorial
IO.puts "#{i}! = #{Math.factorial(i)}"

end
24 changes: 24 additions & 0 deletions src/elixir/FibonacciIterative.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Define a Fibonacci module
defmodule Fib do

# Function to calculate nth Fibonacci number
def fibonacci(n) when n >= 0 do

# Initialize starting values
last = 0
curr = 1

# Use Enum.reduce as a loop from 0 to n-1
Enum.reduce(0..(n-1), {last, curr}, fn _, {last, curr} ->

# Update last and curr, returning as a tuple
{curr, curr + last}
end)

# Return the last value after reducing
|> elem(0)
end
end

# Print 10th Fibonacci number
IO.puts(Fib.fibonacci(10))
14 changes: 14 additions & 0 deletions src/elixir/FibonacciRecursive.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Define a Fibonacci module
defmodule Fibonacci do
# Base case: If n is 0 or 1, return n.
def fibonacci(n) when n <= 1, do: n

# Recursive case: Calculate the Fibonacci number by summing the results
# of the previous two Fibonacci numbers.
def fibonacci(n) do
fibonacci(n - 1) + fibonacci(n - 2)
end
end

# Print the Fibonacci number for n = 10.
IO.puts("Fibonacci(n): #{Fibonacci.fibonacci(10)}")
Loading