Tuesday, September 15, 2020

In C - arrays do not remember their own length

 People who program in Java or Python might not understand this at first.


you do not get that feature for free in C.


cs50, lec2


Saturday, September 12, 2020

scores.c --- more advanced version

#include <cs50.h>

 #include <stdio.h>


float average(int length, int array[]);


int main(void)

{

int n=get_int("number of scores:");

int scores[n];

for(int i=0; i<n:i++)

{

scores[i] = get_int("Score %i:", i+1);

}

printf("Average: %.1f\n", average(n, scores))

}


float average(int length, int array[])

{

int sum=0;

for(int i=0;i<length;i++)

{

sum += array[i];

}

return (float) sum / (float) length

}


cs50, lec2


in C, divide float by float and you will get a float

 if you have two integers that you want to divide, but do not want everything after the decimal point to be thrown away, you can type cast so that the computer treats the integers as floats, thereby ending up with more precise results

const int N;

int sum;

average = (float) sum / (float) N;



cs50, lec2



in C, if you divide an integer by an integer, you will get an integer (everything after decimal point is truncated)

 cs50, lec2



global variables are often frowned upon, except for consts

 placed outside of main()

const

and variable name capitalized

....... that is the convention


This is so you wont have to go visually fishing for a certain number value throughout your code int the future.



const variable - do not have hard coded numbers in more than one place (hard to maintain, often source of bugs)

 #include <stdio.h>


const int N = 3;    //make scope global; capital denotes constant


int main(void)

{

int scores[3];

scores[0]=72;

scores[1]=73;

scores[2]=33;


printf("average: i%\n", (scores[0]+scores[1]+scores[2])/N );

}



cs50, lec2



Thursday, September 10, 2020

scores.c -- Arrays version

#include <stdio.h>

#include <cs50.h>

 int main(void)

{

int scores[3];

scores[0]=72;

scores[1]=73;

scores[2]=33;


printf("average: %i\n", (scores[0] + scores[1] + scores[2])/3 );

}



cs50, lec2



scores.c

#include <stdio.h>

#include <cs50.h>


int main(void)

{

int score1= 72;

int score2= 73;

int score3=33;


printf("average: %i\n", (score1+score2+score3)/3);

}




 cs50, lec2

Wednesday, September 9, 2020

type casting : converting one type to another

 refer to hi.c in the previous post


type casting: convert characters to the ASCII number value that represents those characters


#include <stdio.h>


int main(void)

{

char c1='H';

char c2='i';

char c3='!';


printf("i% i% i%\n", (int) c1, (int) c2, (int) c3);

}



in reality, clang is actually smart enough that even if you don't explicitly cast the chars to ints, it will know that the casting needs to occur and implicitly cast the chars to ints automatically


#include <stdio.h>


int main(void)

{

char c1='H';

char c2='i';

char c3='!';


printf("i% i% i%\n", (int) c1, (int) c2, (int) c3);

}



source: cs50, lec2


hi.c

 #include <stdio.h>


int main(void)

{

char c1='H';

char c2='i';

char c3='!';


printf("%c %c %c\n", c1, c2, c3);

}



source: cs50, lec2



single quotes for char ; double quotes for string ......... C requires this

 char c1 = 'a';

string s1 = "apple";



Sunday, September 6, 2020

bool, char, int, float, long, double, .... = data types in C

bool      1 byte
char      1 byte
int         4 bytes
float      4 bytes                    (in most systems these days, these are the amount of space these data
long      8 bytes                      types are most likely to take up)
double  8 bytes
string    ? bytes
....
....


cs50, lec2


design decisions - not only get problems solved, but solved well

when at facebook , google, microsoft ......

when huge amounts of data and users

design decisions could result in significant cost, cpu cycles, or memory .......



cs50, lec2



reasonable people may disagree on: (1) style (2) design

what style is better, more readable, more maintainable


what design is better




cs50, lec2

when cannot solve problem - go for jog, or rubber duck, or nap

many people have the experience of

when cannot solve problem in programming when using debugger etc.

go for jog

change mental mindset

many have the experience of solving problem when taking nap

or talk through your code with a rubber duck



cs50, lec2




help50, printf, debug50, check50, style50

help50 ........... correctness
printf ............. correctness
debug50......... correctness
check50 ......... correctness
style50 ........... style




cs50, lec2



style50 ........ important ........ code maintainability

nothing worse than being unable to understand what you are looking at when you come back after a while to maintain your code


cs50, lec2


check50 is representative of the real world process of testing one's code repeatedly

cs50, lec2, around 29:00 mins 處 ....... how to use the check50 test


check50 checks whether your program does what is required,

but it is actually a "test"




Using a debugger is a very important life long skill

not just for C but for other languages as well

cs50, david malan


CTRL + C ...... this jumps out of programs like the cs50 ide debugger

CTRL + C ........ jumps out out of the window, kills the debugger, and goes back to the code and terminal window view


"C" stands for cancel



Saturday, September 5, 2020

debugger - allows to step through code step by step

there is a debugger included in the cs50 ide but not in the cs50 sandbox


cs50 IDE (ide.cs50.io) - Eclipse, Visual Studio, Netbeans, and more ....

these are all "integrated development environments"

cs50 IDE: ide.cs50.io


cs50, lec2, 2019


canonical -meaning

conforming to a general rule or acceptable procedure: orthodox


source: google search (merriam-webster.com)


debugging tool: printf() - use to poke around and see what is going on inside your computer


printf() is a useful tool for debugging




for example:

#include <stdio.h>

int main(void)
{

for(int i =0; i<=10;i++)
{
printf("i is now %i\n", i);  //debug line
printf("#\n");
}

}