This is one of the most important topics in programming, especially in the C language. This starts by looking at variable scope without functions, then bringing them into the mix later.
#include <stdio.h>
int main(void)
{
int my_number = 5;
printf("The value of my_number is: %d\n", my_number);
{
int my_number = 10;
}
printf("The value of my_number is: %d\n", my_number);
return 0;
}
This is important to get right. Functions are the base of every program, and knowing what your variables are doing in them is crucial.
_____ add_everything(_____ num1, int num2)
{
return num1 + num2;
}
int main(void)
{
int x, y;
printf("%_____\n", add_everything(10, 5));
x = 20;
y = 15;
printf("%d\n", add_everything(x, y));
return 0;
}
floaty_float
after the execution of this program, assuming the user enters
5
?void get_value(float f)
{
("%f", &f);
scanf= f + 0.5;
f }
int main(void)
{
float floaty_float;
(floaty_float);
get_value
("%f\n", floaty_float);
printf
return 0;
}
floaty_float
? Assume, again, that the user enters
5
.float get_value(void)
{
float f;
("%f", &f);
scanfreturn f + 15.5;
}
int main(void)
{
float floaty_float;
= get_value();
floaty_float
("%f\n", floaty_float);
printf
return 0;
}
main
print in lines 10, 12, and 14?void add_num(int x, int y)
{
x = x + y;
}
int main(void)
{
int x = 10;
printf("At first x was %d\n", x);
add_num(20, 10);
printf("Now x is %d\n", x);
add_num(x, 10);
printf("While x is now %d\n", x);
return 0;
}
main
print in lines 12, 14, and 16?int x;
void add_num(int y)
{
x = x + y;
}
int main(void)
{
x = 10;
printf("At first x was %d\n", x);
add_num(20);
printf("Now x is %d\n", x);
add_num(10);
printf("While x is now %d\n", x);
return 0;
}
// Return the slope of two points.
double slope(float x1, float y1, float x2, float y2)
{
// This is the function body. Your code goes here.
}
int main(void)
{
double point_x1 = 10;
double point_x2 = 40.55;
double point_y1 = 98.122321;
double point_y2 = 12;
("The first point is: (%lf.2, %lf.2)\n" point_x1, point_y1);
printf("The second point is: (%lf.2, %lf.2)\n", point_x2, point_y2);
printf("The slope of the points is %lf\n", slope(point_x1, point_y1, point_x2, point_y2));
printf
return 0;
}
sqrt()
function provided by the
math.h
library.)// Return the distance between two points.
double distance_between_points(float x1, float y1, float x2, float y2)
{
// Code goes here.
}
int main(void)
{
double p_x1, p_x2, p_y1, p_y2;
("Please enter the first point\n");
printf("> x: ");
printf("%lf", &p_x1);
scanf("> y: ");
printf("%lf", &p_y1);
scanf
("Please enter the second point\n");
printf("> x: ");
printf("%lf", &p_x2);
scanf("> y: ");
printf("%lf", &p_y2);
scanf
("The distance between these two points is %lf\n", distance_between_points(p_x1, p_y1, p_x2, p_y2));
printf
return 0;
}
mario()
function that prints an m
x n
block using
#
characters.// Example output:
// mario(3, 4) should print:
// ###
// ###
// ###
// ###
// mario(1, 1) should print:
// #
// mario(10, 5) should print:
// ##########
// ##########
// ##########
// ##########
// ##########
void mario(int m, int n)
{
// Your code here
}
int main(void)
{
int m, n;
("Enter a width: ");
printf("%d", &m);
scanf("Enter a height: ");
printf("%d", &n);
scanf
(m, n);
mario
return 0;
}
Fun fact: This problem is based on the one seen in CS50.