SRC 2 M

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

buggy-0.

c
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.

/****************************************************************************
* buggy-0.c
*
* David J. Malan
* malan@harvard.edu
*
* Should print 10 asterisks but doesn't!
***************************************************************************/
#include <stdio.h>
int main(void)
{
for (int i = 0; i <= 10; i++)
printf("*");
}

buggy-1.c
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.

/****************************************************************************
* buggy-1.c
*
* David J. Malan
* malan@harvard.edu
*
* Should print 10 asterisks, one per line, but doesn't!
***************************************************************************/
#include <stdio.h>
int main(void)
{
for (int i = 0; i <= 10; i++)
printf("*");
printf("\n");
}

floats-0.c
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.

/**
* floats-0.c
*
* David J. Malan
* malan@harvard.edu
*
* Tries to print 1/10 as a floating-point value.
*
* Demonstrates truncation.
*/
#include <stdio.h>
int main(void)
{
float f = 1 / 10;
printf("%.1f\n", f);
}

floats-1.c
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.

/**
* floats-1.c
*
* David J. Malan
* malan@harvard.edu
*
* Prints 1/10 as a floating-point value to one decimal place.
*
* Demonstrates division of floating-point values.
*/
#include <stdio.h>
int main(void)
{
float f = 1.0 / 10.0;
printf("%.1f\n", f);
}

floats-2.c
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.

/**
* floats-2.c
*
* David J. Malan
* malan@harvard.edu
*
* Prints 1/10 as a floating-point value to 28 decimal places.
*
* Demonstrates imprecision of floating-point values.
*/
#include <stdio.h>
int main(void)
{
float f = 1.0 / 10.0;
printf("%.28f\n", f);
}

function-0.c
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.

/**
* function-0.c
*
* David J. Malan
* malan@harvard.edu
*
* Prints a user's name.
*
* Demonstrates a function (not from a library) with a side effect.
*/
#include <cs50.h>
#include <stdio.h>
// prototype
void PrintName(string name);
int main(void)
{
printf("Your name: ");
string s = GetString();
PrintName(s);
}
/**
* Says hello to someone by name.
*/
void PrintName(string name)
{
printf("hello, %s\n", name);
}

function-1.c
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.

/**
* function-1.c
*
* David J. Malan
* malan@harvard.edu
*
* Demands that user provide a positive integer.
*
* Demonstrates use of a function (not from a library) with a return value.
*/
#include <cs50.h>
#include <stdio.h>
// prototype
int GetPositiveInt();
int main(void)
{
int n = GetPositiveInt();
printf("Thanks for the %i!\n", n);
}
/**
* Gets a positive integer from a user.
*/
int GetPositiveInt(void)
{
int n;
do
{
printf("Please give me a positive int: ");
n = GetInt();
}
while (n < 1);
return n;
}

return.c
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.

/****************************************************************************
* return.c
*
* David J. Malan
* malan@harvard.edu
*
* Cubes a variable.
*
* Demonstrates use of parameter and return value.
***************************************************************************/
#include <stdio.h>
// function prototype
int cube(int a);
int main(void)
{
int x = 2;
printf("x is now %i\n", x);
printf("Cubing...\n");
x = cube(x);
printf("Cubed!\n");
printf("x is now %i\n", x);
}
/**
* Cubes argument.
*/
int cube(int n)
{
return n * n * n;
}

You might also like