Sunday, August 30, 2020

say - command (program) that comes with some systems that speaks out what you type

for example:

say this is cs50

(this works on mac)


figlet - command (program) that comes with some systems which produces ASCII art

for example:

figlet this is cs50


ACSII art

ACSII art


floating point imprecision & integer overflow

both exist because computers only use a finite amount of bits to store numbers


sleep(1) - wait for i second

to use this handy function, you must :

#include <unistd.h>


RAM - where programs are stored while they are running, and where files are stored while they are open

[source: harvard, cs50]


positive.c

#include <cs50.h>
#include <stdio.h>

int get_positive_int(void);

int main(void)
{
int i = get_positive_int();
printf("The positive integer you entered is: %i\n", i);

}


get_positive_int(void)
{
int n;

do
{
n = get_int("Enter a positive integer:\n");
}while(n<1)

return n;

}








it is convention to have main() up top before other function definitions

hence the need for function prototypes ("one liners")

(C is dumb in that it does not scan further down to look for function definitions)


function prototypes : C is dumb

#include <cs50.h>
#include <stdio.h>

void cough(int n);

int main(void)
{
cough(3);
}


void cough(int n)
{
for (int i; i<n; i++)
{
printf("cough\n");
}
}


defining your own functions: abstraction

#include <cs50.h>
#include <stdio.h>

int main(void)
{

void cough(void)
{
printf("cough\n");
}

for (int i=0; i<3; i++)
{
cough();
}

}


agree.c

#include <cs50.h>
#include <stdio.h>

int main(void)
{

char c = get_char("do you agree?\n");

if (c== 'y' || c=='Y')
{
printf("agreed\n");
}
else if (c=='n' || c=='N')
{
printf("do not agree\n");
}


}

parity.c

#include <cs50.h>
#include <stdio.h>

int main(void)
{

int n = get_int("please enter an integer:\n");

if(n % 2 == 0)
{
printf("the number you entered is an even number\n");
}
else
{
printf("the number you entered is odd.\n");
}

}