0% found this document useful (0 votes)
4 views

CS5 SRC Code

The document contains code snippets demonstrating basic C programming concepts like input/output, conditionals, loops, functions, and nested loops. The snippets show incrementally more complex examples of each concept, from simple examples to those with abstraction and parameterization.

Uploaded by

Binay Adhikari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

CS5 SRC Code

The document contains code snippets demonstrating basic C programming concepts like input/output, conditionals, loops, functions, and nested loops. The snippets show incrementally more complex examples of each concept, from simple examples to those with abstraction and parameterization.

Uploaded by

Binay Adhikari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

hello0.

1 // A program that says hello to the world


2
3 #include <stdio.h>
4
5 int main(void)
6 {
7 printf("hello, world\n");
8 }
hello1.c

1 // get_string and printf with incorrect placeholder


2
3 #include <cs50.h>
4 #include <stdio.h>
5
6 int main(void)
7 {
8 string answer = get_string("What's your name? ");
9 printf("hello, answer\n");
10 }
hello2.c

1 // get_string and printf with %s


2
3 #include <cs50.h>
4 #include <stdio.h>
5
6 int main(void)
7 {
8 string answer = get_string("What's your name? ");
9 printf("hello, %s\n", answer);
10 }
compare0.c

1 // Conditional, Boolean expression, relational operator


2
3 #include <cs50.h>
4 #include <stdio.h>
5
6 int main(void)
7 {
8 // Prompt user for integers
9 int x = get_int("What's x? ");
10 int y = get_int("What's y? ");
11
12 // Compare integers
13 if (x < y)
14 {
15 printf("x is less than y\n");
16 }
17 }
compare1.c

1 // Conditionals that are mutually exclusive


2
3 #include <cs50.h>
4 #include <stdio.h>
5
6 int main(void)
7 {
8 // Prompt user for integers
9 int x = get_int("What's x? ");
10 int y = get_int("What's y? ");
11
12 // Compare integers
13 if (x < y)
14 {
15 printf("x is less than y\n");
16 }
17 else
18 {
19 printf("x is not less than y\n");
20 }
21 }
compare2.c

1 // Conditionals that aren't mutually exclusive


2
3 #include <cs50.h>
4 #include <stdio.h>
5
6 int main(void)
7 {
8 // Prompt user for integers
9 int x = get_int("What's x? ");
10 int y = get_int("What's y? ");
11
12 // Compare integers
13 if (x < y)
14 {
15 printf("x is less than y\n");
16 }
17 if (x > y)
18 {
19 printf("x is greater than y\n");
20 }
21 if (x == y)
22 {
23 printf("x is equal to y\n");
24 }
25 }
compare3.c

1 // Conditional that isn't necessary


2
3 #include <cs50.h>
4 #include <stdio.h>
5
6 int main(void)
7 {
8 // Prompt user for integers
9 int x = get_int("What's x? ");
10 int y = get_int("What's y? ");
11
12 // Compare integers
13 if (x < y)
14 {
15 printf("x is less than y\n");
16 }
17 else if (x > y)
18 {
19 printf("x is greater than y\n");
20 }
21 else if (x == y)
22 {
23 printf("x is equal to y\n");
24 }
25 }
agree0.c

1 // Comparing against lowercase char


2
3 #include <cs50.h>
4 #include <stdio.h>
5
6 int main(void)
7 {
8 // Prompt user to agree
9 char c = get_char("Do you agree? ");
10
11 // Check whether agreed
12 if (c == 'y')
13 {
14 printf("Agreed.\n");
15 }
16 else if (c == 'n')
17 {
18 printf("Not agreed.\n");
19 }
20 }
agree1.c

1 // Comparing against lowercase and uppercase char


2
3 #include <cs50.h>
4 #include <stdio.h>
5
6 int main(void)
7 {
8 // Prompt user to agree
9 char c = get_char("Do you agree? ");
10
11 // Check whether agreed
12 if (c == 'y')
13 {
14 printf("Agreed.\n");
15 }
16 else if (c == 'Y')
17 {
18 printf("Agreed.\n");
19 }
20 else if (c == 'n')
21 {
22 printf("Not agreed.\n");
23 }
24 else if (c == 'N')
25 {
26 printf("Not agreed.\n");
27 }
28 }
agree2.c

1 // Logical operators
2
3 #include <cs50.h>
4 #include <stdio.h>
5
6 int main(void)
7 {
8 // Prompt user to agree
9 char c = get_char("Do you agree? ");
10
11 // Check whether agreed
12 if (c == 'Y' || c == 'y')
13 {
14 printf("Agreed.\n");
15 }
16 else if (c == 'N' || c == 'n')
17 {
18 printf("Not agreed.\n");
19 }
20 }
meow0.c

1 // Opportunity for better design


2
3 #include <stdio.h>
4
5 int main(void)
6 {
7 printf("meow\n");
8 printf("meow\n");
9 printf("meow\n");
10 }
meow1.c

1 // Better design
2
3 #include <stdio.h>
4
5 int main(void)
6 {
7 int i = 3;
8 while (i > 3)
9 {
10 printf("meow\n");
11 i--;
12 }
13 }
meow2.c

1 // Better design
2
3 #include <stdio.h>
4
5 int main(void)
6 {
7 int i = 3;
8 while (i > 3)
9 {
10 printf("%i\n", i);
11 i--;
12 }
13 }
meow3.c

1 // Better design
2
3 #include <stdio.h>
4
5 int main(void)
6 {
7 int i = 0;
8 while (i < 3)
9 {
10 printf("meow\n");
11 i++;
12 }
13 }
meow4.c

1 // Better design
2
3 #include <stdio.h>
4
5 int main(void)
6 {
7 for (int i = 0; i < 3; i++)
8 {
9 printf("meow\n");
10 }
11 }
meow5.c

1 // Abstraction
2
3 #include <stdio.h>
4
5 void meow(void);
6
7 int main(void)
8 {
9 for (int i = 0; i < 3; i++)
10 {
11 meow();
12 }
13 }
14
15 // Meow once
16 void meow(void)
17 {
18 printf("meow\n");
19 }
meow6.c

1 // Abstraction with parameterization


2
3 #include <stdio.h>
4
5 void meow(int n);
6
7 int main(void)
8 {
9 meow(3);
10 }
11
12 // Meow some number of times
13 void meow(int n)
14 {
15 for (int i = 0; i < n; i++)
16 {
17 printf("meow\n");
18 }
19 }
calculator0.c

1 // Addition with int


2
3 #include <cs50.h>
4 #include <stdio.h>
5
6 int main(void)
7 {
8 // Prompt user for x
9 int x = get_int("x: ");
10
11 // Prompt user for y
12 int y = get_int("y: ");
13
14 // Perform addition
15 printf("%i\n", x + y);
16 }
calculator1.c

1 // Scope error
2
3 #include <cs50.h>
4 #include <stdio.h>
5
6 int add(void);
7
8 int main(void)
9 {
10 // Prompt user for x
11 int x = get_int("x: ");
12
13 // Prompt user for y
14 int y = get_int("y: ");
15
16 // Perform addition
17 int z = add();
18 printf("%i\n", z);
19 }
20
21 int add(void)
22 {
23 return x + y;
24 }
calculator2.c

1 // Helper function with arguments and return value


2
3 #include <cs50.h>
4 #include <stdio.h>
5
6 int add(int a, int b);
7
8 int main(void)
9 {
10 // Prompt user for x
11 int x = get_int("x: ");
12
13 // Prompt user for y
14 int y = get_int("y: ");
15
16 // Perform addition
17 int z = add(x, y);
18 printf("%i\n", z);
19 }
20
21 int add(int a, int b)
22 {
23 int c = a + b;
24 return c;
25 }
calculator3.c

1 // Eliminates unnecessary variables


2
3 #include <cs50.h>
4 #include <stdio.h>
5
6 int add(int a, int b);
7
8 int main(void)
9 {
10 // Prompt user for x
11 int x = get_int("x: ");
12
13 // Prompt user for y
14 int y = get_int("y: ");
15
16 // Perform addition
17 printf("%i\n", add(x, y));
18 }
19
20 int add(int a, int b)
21 {
22 return a + b;
23 }
calculator4.c

1 // Addition with long


2
3 #include <cs50.h>
4 #include <stdio.h>
5
6 int main(void)
7 {
8 // Prompt user for x
9 long x = get_long("x: ");
10
11 // Prompt user for y
12 long y = get_long("y: ");
13
14 // Perform addition
15 printf("%li\n", x + y);
16 }
mario0.c

1 // Prints a row of 4 question marks


2
3 #include <stdio.h>
4
5 int main(void)
6 {
7 printf("????\n");
8 }
mario1.c

1 // Prints a row of 4 question marks with a loop


2
3 #include <stdio.h>
4
5 int main(void)
6 {
7 for (int i = 0; i < 4; i++)
8 {
9 printf("?");
10 }
11 printf("\n");
12 }
mario2.c

1 // Prints a column of 3 bricks with a loop


2
3 #include <stdio.h>
4
5 int main(void)
6 {
7 for (int i = 0; i < 3; i++)
8 {
9 printf("#\n");
10 }
11 }
mario3.c

1 // Prints a 3-by-3 grid of bricks with nested loops


2
3 #include <stdio.h>
4
5 int main(void)
6 {
7 for (int i = 0; i < 3; i++)
8 {
9 for (int j = 0; j < 3; j++)
10 {
11 printf("#");
12 }
13 printf("\n");
14 }
15 }
mario4.c

1 // Prints a 3-by-3 grid of bricks with nested loops using a constant


2
3 #include <stdio.h>
4
5 int main(void)
6 {
7 const int n = 3;
8 for (int i = 0; i < n; i++)
9 {
10 for (int j = 0; j < n; j++)
11 {
12 printf("#");
13 }
14 printf("\n");
15 }
16 }
mario5.c

1 // Prints an n-by-n grid of bricks with nested loops


2
3 #include <cs50.h>
4 #include <stdio.h>
5
6 int main(void)
7 {
8 int n = get_int("Size: ");
9
10 for (int i = 0; i < n; i++)
11 {
12 for (int j = 0; j < n; j++)
13 {
14 printf("#");
15 }
16 printf("\n");
17 }
18 }
mario6.c

1 // Prints an n-by-n grid of bricks, re-prompting user for positive integer


2
3 #include <cs50.h>
4 #include <stdio.h>
5
6 int main(void)
7 {
8 int n = get_int("Size: ");
9 while (n < 1)
10 {
11 n = get_int("Size: ");
12 }
13
14 for (int i = 0; i < n; i++)
15 {
16 for (int j = 0; j < n; j++)
17 {
18 printf("#");
19 }
20 printf("\n");
21 }
22 }
mario7.c

1 // Prints an n-by-n grid of bricks, re-prompting user for positive integer


2
3 #include <cs50.h>
4 #include <stdio.h>
5
6 int main(void)
7 {
8 // Get size of grid
9 int n;
10 do
11 {
12 n = get_int("Size: ");
13 }
14 while (n < 1);
15
16 // Print grid of bricks
17 for (int i = 0; i < n; i++)
18 {
19 for (int j = 0; j < n; j++)
20 {
21 printf("#");
22 }
23 printf("\n");
24 }
25 }
calculator5.c

1 // Division with ints, demonstrating truncation


2
3 #include <cs50.h>
4 #include <stdio.h>
5
6 int main(void)
7 {
8 // Prompt user for x
9 int x = get_int("x: ");
10
11 // Prompt user for y
12 int y = get_int("y: ");
13
14 // Divide x by y
15 printf("%i\n", x / y);
16 }
calculator6.c

1 // Uses %f
2
3 #include <cs50.h>
4 #include <stdio.h>
5
6 int main(void)
7 {
8 // Prompt user for x
9 int x = get_int("x: ");
10
11 // Prompt user for y
12 int y = get_int("y: ");
13
14 // Divide x by y
15 printf("%f\n", x / y);
16 }
calculator7.c

1 // Uses float variable


2
3 #include <cs50.h>
4 #include <stdio.h>
5
6 int main(void)
7 {
8 // Prompt user for x
9 int x = get_int("x: ");
10
11 // Prompt user for y
12 int y = get_int("y: ");
13
14 // Divide x by y
15 float z = x / y;
16 printf("%f\n", z);
17 }
calculator8.c

1 // Type casting
2
3 #include <cs50.h>
4 #include <stdio.h>
5
6 int main(void)
7 {
8 // Prompt user for x
9 int x = get_int("x: ");
10
11 // Prompt user for y
12 int y = get_int("y: ");
13
14 // Divide x by y
15 float z = (float) x / (float) y;
16 printf("%f\n", z);
17 }
calculator9.c

1 // Floating-point imprecision
2
3 #include <cs50.h>
4 #include <stdio.h>
5
6 int main(void)
7 {
8 // Prompt user for x
9 int x = get_int("x: ");
10
11 // Prompt user for y
12 int y = get_int("y: ");
13
14 // Divide x by y
15 float z = (float) x / (float) y;
16 printf("%.20f\n", z);
17 }
calculator10.c

1 // Division with longs, demonstrating double


2
3 #include <cs50.h>
4 #include <stdio.h>
5
6 int main(void)
7 {
8 // Prompt user for x
9 long x = get_long("x: ");
10
11 // Prompt user for y
12 long y = get_long("y: ");
13
14 // Divide x by y
15 double z = (double) x / (double) y;
16 printf("%.20f\n", z);
17 }

You might also like