-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeek 1
110 lines (84 loc) · 2.02 KB
/
Week 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
Source code - what human programmers write
Compiler - translates source code into machine code
Code Descriptions
correctness - does the code run as intended?
Design - how well is the code designed?
Style - how aesthetically pleasing is the code?
code.cs50.io - to access visual studio
---------------------------------------
// create script called hello.c
code hello.c
//make script executable
make hello
//compile code
./hello
---------------Example Script ---------------
#include <stdio.h>
int main(void)
{
printf("hello, world\n");
}
----------------------------------------
// load header library
#include <stdio.h>
int main(void)
{
}
printf("hello, world\n");
//printf(const char *format,...) sends formatted output to stdout
//Requires quotation marks for string
// /n terminates the line
//semicolon is an end statement to terminate the current statement
---------------------------------------
cs50 has its own library ==> cs50.h
Manual ==> manual.cs50.io
---------------------------------------
//Create a function that asks your names and outputs your name as a response
#include <stdio.h>
#include <cs50.h>
int main(void)
{
string name = get_string("What's your name? ");
printf("hello, %s\n", name);
}
-------Comparing integer variables----------
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int x = get_int("What's x? ");
int y = get_int("What's y? ");
if (x < y)
{
printf("x is less than y\n");
}
else if (x > y)
{
printf("x is greater than y\n");
}
else
{
printf("x is EQUAL to y\n");
}
}
------- Loops ---------
#include <stdio.h>
int main(void)
{
int i = 0;
while (i < 3)
{
printf("meow\n");
i++;
}
}
------- Further simplified ---------
#include <stdio.h>
int main(void)
{
for (int i = 0; i < 3; i++)
{
printf("meow\n");
}
}
--------> for loop. Load integer 'i' to zero. check if it's less than three. increment i and repeat. First part before the semicolon only cycles once.