Skip to content

Instantly share code, notes, and snippets.

@jnikolak
Last active October 17, 2017 09:33
Show Gist options
  • Save jnikolak/efe0eee136940afa2b07151765d1ca6a to your computer and use it in GitHub Desktop.
Save jnikolak/efe0eee136940afa2b07151765d1ca6a to your computer and use it in GitHub Desktop.
Countdown
/* This is an example taken from a book, one day I might need to reference this so I thought I would save it,
it uses many different techniques in c such as:
- adding modules
- create global variables using constsants and local variables inside functions
- setting up prototyes for functions
- defining different types such as long int, char
- loops inside functions
- different style of comments
- break out of loop
- if statements */
#include <stdio.h> // Include Standard Input Output Module
#include <stdio_ext.h> // Inserted module to be able to run __fpurge function
#define COUNT 50000000 // Create a constant(a variable that never changes) called COUNT
void dropTakeoff(void); // Prototype the dropTakeoff function
void delay(void); // Protoype the delay function
int launches; // Create a global integer variable called launches
/******************* Main Function **************************/
int main() // set first function as an integer
{
char x; // set x as a character type
launches=0;
for(;;) // A recurring loop
{
printf("Press any key to start the Space Ship Countdown: ");
x=getchar(); // prompt terminal and assign it to x
__fpurge(stdin); // This clears extra characters, otherwise will run twice
if(x=='~') // If the value of x is equal to the character ~
{
break; // We break out of the loop if the statement is true
printf("Thanks for playing\n");
}
dropTakeoff(); // Initiate the function DropTakeOff
printf("The current launches are set to %d\n",launches);
// Formatted print, that prints value of x as an integer.
}
return(0);
}
/***************** End of Main *****************************/
void dropTakeoff() // enter details of function dropTakeoff
{
int x; // set a local variable x as an integer
for(x=10;x>1;x--) // create a loop that starts with 10 and decrements
{
printf("counting down %d \n",x); // While the loops is not true, print and delay
delay(); // Initiate the delay function
}
launches++;
puts(" HOUSTON WE HAVE TAKEOFF!"); // Once function is true, print the following
}
void delay() // enter details of function delay
{
long int x; // set a local variable x with a long integer type
for(x=0;x<COUNT;x++)// create a loop, that sets x as 0, then increment until value is equal to COUNT
; // Needed as part of a loop to iterate
}
/* Another script that explains passing arguments from functions
#include <stdio.h>
void graph(int count);
int main()
{
int value;
value = 2;
while(value<=64) // While the value is less than 64, loop through the function graph.
// We will also pass on value to the argument.
{
graph(value);
printf("Value is %d\n",value);
value*=2;
}
return(0);
}
void graph(int count)
{
// This function called graph, increments by one, until it is less than count
// This (int count) is an argument. The argument is provided by value.
// So it will put those characters
int x;
for(x=0;x<count;x++)
printf("*");
printf("\n");
}
*/
/*************************************************************************
Another script which passes arguments the difference is that we return a value (which was calculated using user inputted info
from the scanf function which is in main.
#include <stdio.h>
int main()
{
float convert(float f);
int main()
{
float temp_f,temp_c; // create two floats
printf("Temperature in Fahrenheit: "); // Print something
scanf("%f",&temp_f); // store user input into temp_f (its a float so we use &
temp_c = convert(temp_f); /* we pass the (user entered variable into an argument against the convert function
and assign this operation against the variable temp_c */
printf("%.1fF is %.fC\n",temp_f,temp_c); // display the variables before and after the conversion
}
float convert(float f); // Initate the function
{
float t; // create another float var called
t = (f -32) / 1.8; /* This basically means that t is equal to (calculation)
the important part is that the f is the argument, that gets its value from the main
function, i.e temp_c = convert(temp_f) */
return(t) // We return all this from the variable t
}
}
*///////////////////////////////////////////////////////////////////////////////////////
/* Stopping a function with return
#include <stdio.h>
void limit(int stop);
int main()
{
int s; // set up s as a var.
printf(" Countdown from a value between (0-100): ");
scanf("%d",&s); // have the var be decided by user input
limit(s); // call the function limit and pass an argument (which is user input)
return(0);
}
void limit(int stop) // we setup the function called limit, with an integer called stop
{
int x;
for(x=0;x<=100;x++) // we loop until x is less than 100 but x will be the called from the scanf in main()
{
printf("%d ",x); // we print each iteration of x
if(x==stop) // if the int stop matches with the value of x, we return (like a break)
{
puts("You won!");
return;
}
}
puts("i won");
}
*/
/* Looping through an array
#include <stdio.h>
#include <stdlib.h>
int main()
{
int highscore[4]; //We setup an array that can hold 4 values
int x;
for(x=0;x<4;x++) //Loop through 4 runs
{
printf("Your #%d score: ",x+1); // in the loop, print the number of array that equals 1 x = (0+1 = 1)
scanf("%d",&highscore[x]); // in the loop, ask users to enter all entries
}
puts("Here are your high scores");
for(x=0;x<4;x++) // Loop through 4 runs
printf("#%d %d\n",x+1,highscore[x]); // print the number of the array and they highscore (of each array)
return(0);
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment