Skip to content

Instantly share code, notes, and snippets.

@s3f
s3f / Chapter 32- Returning Data From Your Functions.c
Created January 12, 2017 16:43
Chapter 32- Returning Data From Your Functions created by s3f - https://repl.it/FHcM/9
/* Returning Values
The return statement terminates the execution of a function and returns control to the calling function usually main().
The following program demonstrates functions returning a value by passing 3 floating point numbers and calculating the average and returns the answer.
*/
#include <stdio.h>
float gradeAvg(float test1, float test2, float test3);
@s3f
s3f / Chapter 31 - Passing Variables to Your Functions.c
Created January 12, 2017 16:10
Chapter 31 - Passing Variables to Your Functions created by s3f - https://repl.it/FH1x/11
/* Passing Arguments
When you pass a variable from one function to another, you are passing an argument from the first function to the next. The recieving function recieevs the parameters from the function that sent the variables.
Methods of Passing Arguments
The variables you want to pass go inside the parenthesis of the function call () and also in the recieving function.
You pass arguments from function to function in 2 ways, by value and by address,
@s3f
s3f / Chapter 30- Functions.c
Created January 12, 2017 14:34
Chapter 30- Functions created by s3f - https://repl.it/FG0B/3
/* Functions
In a lot of textbooks, the programs you see are usually brief (20 - 30 lines) however, in the real world, programs are much much longer and therefore it helps a lot if you can break your code into sections. Doing so ensures that it'll be much more easier to write and maintain your code.
Breaking programs into smaller functions is called structured programming
Now the following example program is a simple demonstration of the difference between global and local variables.
*/
#include <stdio.h>
int g1 = 10; // This is a gloabal variable
@s3f
s3f / Chapter 27 - Structures.c
Created January 11, 2017 16:42
Chapter 27 - Structures created by s3f - https://repl.it/FEZW/10
/* Structures
In programming, if you want to store multiple values of a certain data type (integer, character, floating point etc), you use an array however, if you want to store data consisting of different kinds of data types, you can use a structure.
Here's how you would write it:
struct student {
char name [20];
int roll;
@s3f
s3f / Chapter 26- Maximizing Your Computer's Memory.c
Created January 9, 2017 19:52
Chapter 26- Maximizing Your Computer's Memory created by s3f - https://repl.it/FESr/8
// Heap Memory
/*
The heap is a collection of unused memory in the computer. You usually want to access the heap because your program will need more memory than you initially defined in the variables and arrays. The only way to access heap memory is through pointer variables.
The heap anables your program to use only as much memory as it needs. When the user needs more memory, your program can allocate the memory. When the user is fisnished using the certain amount of memory, you can deallocate the memory, making it available for other tasks that might need it.
How do you allocate the heap? There are two functions you need to know:
malloc() --> Allocates Heap memory
@s3f
s3f / Chapter 25 - Arrays and Pointers.c
Created January 6, 2017 19:25
Chapter 25 - Arrays and Pointers created by s3f - https://repl.it/FC1k/14
// Arrays and Pointers
/*
An array name is nothing more than a pointer to the first element in that array. It is important to mention that array names are not exactly pointer variables but rather pointer constants.
As you know, if you want to retrieve a value from an array, all you have to do is write it like this:
printf("The first array value is %d.\n", vals[0]);
but you can also write it like this:
@s3f
s3f / Chapter 24 - Intro to Pointers.c
Created January 3, 2017 19:02
Chapter 24 - Intro to Pointers created by s3f - https://repl.it/EyR4/10
// Pointers
/*
Inside your computer is a bunch of memory which holds your program as it executes as well as the variables you declare. Just as your house has a unique address, so does a memory location.
Here are the pointer operators: & --> Address of operator
* --> Dereferencing operator
Here's how you define an integer and floating point variable:
@s3f
s3f / Chapter 23 - Sorting Data.c
Created December 29, 2016 22:57
Chapter 23 - Sorting Data created by s3f - https://repl.it/EvqV/12
/* Bubble Sort
The order you put values in can be either ascending (low to high) or descending (high to low)
In a bubble sort, the sort's float up the list each time a pass is made through the data.
The program below generates 10 random numbers and then sorts them
#include <stdio.h>
#include <stdlib.h>
@s3f
s3f / Chapter 22- Searching Arrays.c
Created December 29, 2016 22:57
Chapter 22- Searching Arrays created by s3f - https://repl.it/EvbH/16
// Searching Arrays
/*
One important use of arrays is filling them with values that you don't have initially and that the user has to enter.
Now the following program below takes an id number from the user and then checks the ID against a list of customers in the database. If the customer exists, it uses that array element to check their current balance, and warns the user if the balance is more than 100.
#include <stdio.h>
@s3f
s3f / Chapter 21- Reviewing Arrays.c
Created December 29, 2016 22:57
Chapter 21- Reviewing Arrays created by s3f - https://repl.it/EvIX/7
// Reviewing Arrays
/*
Remember: An array is simply a list of items with a specific name. An array of characters can only have characters. An array of integers can only contain integers and so on
To define an array, you must add brackets after the name and specify the max number of elements you'll put inside the array.
ex: char name[6] = "Italy";