Week 2: outcome to write programs using functions
● Functions
● standard library functions
● math library functions
● Random number generation
● Programmer defined functions
● Calling functions by value
● Scope rules (global and local variables)
Age example from week 1 using functions :
#include <stdio.h>
// Global variables
int birthYear, birthMonth, birthDay;
int currentYear, currentMonth, currentDay;
int daysInYear = 360; // Assuming a year has 360 days
// Function to read input for birth and current dates
void readInput() {
// Input birth date
printf("Enter your birth year (e.g., 1990): ");
scanf("%d", &birthYear);
printf("Enter your birth month (1-12): ");
scanf("%d", &birthMonth);
printf("Enter your birth day (1-31): ");
scanf("%d", &birthDay);
// Input current date (for calculating the age)
printf("Enter the current year (e.g., 2023): ");
scanf("%d", ¤tYear);
printf("Enter the current month (1-12): ");
scanf("%d", ¤tMonth);
printf("Enter the current day (1-31): ");
scanf("%d", ¤tDay);
}
// Function to compute age in days
int computeAgeInDays() {
int ageInDays = (currentYear - birthYear) * daysInYear;
ageInDays += (currentMonth - birthMonth) * 30; // Assuming 30 days
per month
ageInDays += (currentDay - birthDay);
return ageInDays;
}
int main() {
readInput(); // Call the function to read input
int ageInDays = computeAgeInDays(); // Compute the age in days
printf("Your age in days is: %d\n", ageInDays);
return 0;
}
Q1 :Here's a simple example in C that uses the math library functions ceil, floor, pow, sqrt, and
abs to perform various mathematical operations:
#include <stdio.h>
#include <math.h>
int main() {
double x = 4.5;
double y = -7.8;
// Calculate the ceiling of x and y
double ceil_x = ceil(x);
double ceil_y = ceil(y);
// Calculate the floor of x and y
double floor_x = floor(x);
double floor_y = floor(y);
// Calculate the square of x
double squared_x = pow(x, 2);
// Calculate the square root of y
double sqrt_y = sqrt(fabs(y)); // We use fabs to ensure a non-negative
value
// Calculate the absolute value of y
double abs_y = fabs(y);
printf("Original x: %lf\n", x);
printf("Original y: %lf\n", y);
printf("Ceiling of x: %lf\n", ceil_x);
printf("Ceiling of y: %lf\n", ceil_y);
printf("Floor of x: %lf\n", floor_x);
printf("Floor of y: %lf\n", floor_y);
printf("Square of x: %lf\n", squared_x);
printf("Square root of |y|: %lf\n", sqrt_y);
printf("Absolute value of y: %lf\n", abs_y);
return 0;
}
Q2: You can define your own functions that make use of the math library functions
to perform specific calculations. Here's an example of a user-defined function that
uses math library functions to calculate the distance between two points in 2D
space:
#include <stdio.h>
#include <math.h>
// Function to calculate the Euclidean distance between two points
(x1, y1) and (x2, y2)
double calculateDistance(double x1, double y1, double x2, double
y2) {
double deltaX = x2 - x1;
double deltaY = y2 - y1;
// Use the Pythagorean theorem to calculate the distance
double distance = sqrt(pow(deltaX, 2) + pow(deltaY, 2));
return distance;
}
int main() {
double x1 = 1.0, y1 = 2.0;
double x2 = 4.0, y2 = 6.0;
double distance = calculateDistance(x1, y1, x2, y2);
printf("The distance between (%lf, %lf) and (%lf, %lf) is %lf\n",
x1, y1, x2, y2, distance);
return 0;
}
Q3: your turn :Write a C function that calculates and returns the area of a circle
using the math library's pow and M_PI (which represents the value of π). The
formula for the area of a circle is A = π * r^2, where "r" is the radius of the circle
Here is a code to start :
#include <stdio.h>
#include <math.h>
// Function to calculate the area of a circle
double calculateCircleArea(double radius) {
// Use the formula A = π * r^2
// You can use the M_PI constant for π and pow function for
raising to the power
// Your code here
// Return the calculated area
// Your code here
}
int main() {
double radius = 5.0; // Change the radius as needed
double area = calculateCircleArea(radius);
printf("The area of the circle with radius %.2lf is %.2lf\n",
radius, area);
return 0;
}
Q4: C program with a function called generateRand that takes a start and end
value, generates a random number within that range, and then prints the result in
the main function:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int generateRand(int start, int end) {
return rand() % (end - start + 1) + start;
}
int main() {
// Seed the random number generator with the current time
srand(time(NULL));
int start = 10; // Replace with your desired start value
int end = 50; // Replace with your desired end value
int random_number = generateRand(start, end);
printf("Random number in the range %d to %d: %d\n", start,
end, random_number);
return 0;
}
Q5 . Calculate the Hypotenuse Length:
Write a C program that takes the lengths of two sides of a right triangle as input
and uses the math library's hypot function to calculate and display the length of the
hypotenuse.
#include <stdio.h>
#include <math.h>
int main() {
double side1, side2;
printf("Enter the lengths of the two sides: ");
scanf("%lf %lf", &side1, &side2);
double hypotenuse = hypot(side1, side2);
printf("Hypotenuse length: %lf\n", hypotenuse);
return 0;
}