for example:
say this is cs50
(this works on mac)
Sunday, August 30, 2020
figlet - command (program) that comes with some systems which produces ASCII art
for example:
figlet this is cs50
figlet this is cs50
floating point imprecision & integer overflow
both exist because computers only use a finite amount of bits to store numbers
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;
}
#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)
(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");
}
}
#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();
}
}
#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");
}
}
#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");
}
}
#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");
}
}
Subscribe to:
Posts (Atom)