Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
260 views

COMP1511 - Programming Fundamentals - 18s1

This document contains code examples from lectures on functions in the COMP1511 - Programming Fundamentals course. It includes examples of simple functions to calculate a quadratic equation, cube a number, print messages, and calculate factorials and Fibonacci numbers recursively. The examples illustrate defining, calling, and returning values from functions in C programming.

Uploaded by

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

COMP1511 - Programming Fundamentals - 18s1

This document contains code examples from lectures on functions in the COMP1511 - Programming Fundamentals course. It includes examples of simple functions to calculate a quadratic equation, cube a number, print messages, and calculate factorials and Fibonacci numbers recursively. The examples illustrate defining, calling, and returning values from functions in C programming.

Uploaded by

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

26/02/2019 COMP1511 - Programming Fundamentals - 18s1

COMP1511 18s1 (webcms) Code Examples from Lectures on functions COMP1511 18s1 (flask)

quadratic.c
 
Lecture example illustrating simple function  

#include <stdio.h>

// given x calculate value of polynomial with coefficients a, b, c

double evaluate_quadratic(double a, double b, double c, double x) {


return (a * x * x) + (b * x) + c;
}

int main(int argc, char *argv[]) {


double x;
printf("Enter x: ");
scanf("%lf", &x);

double value = evaluate_quadratic(2.0, -3.0, 4.0, x);

printf("2x^2 - 3x + 4 = %lf\n", value);

return 0;
}

quadratic_root.c
 
Lecture example illustrating simple function  

#include <stdio.h>
#include <math.h>

// given x calculate value of polynomial with coefficients a, b, c

double quadratic_root(double a, double b, double c) {


return (-b + sqrt(b * b - 4 * a * c)) / (2 * a);
}

double evaluate_quadratic(double a, double b, double c, double x) {


return (a * x * x) + (b * x) + c;
}

int main(int argc, char *argv[]) {


double a, b, c;
printf("Enter a: ");
scanf("%lf", &a);
printf("Enter b: ");
scanf("%lf", &b);
printf("Enter c: ");
scanf("%lf", &c);

double root = quadratic_root(a, b, c);

printf("Calculated root is %lf\n", root);

double value = evaluate_quadratic(a, b, c, root);

printf("The value of the quadratic for %lf is %lf\n", root, value);

return 0;
}

cube.c
 
Simple lecture example of using functions  

https://cgi.cse.unsw.edu.au/~cs1511/18s1/flask.cgi/code/functions/ 1/3
26/02/2019 COMP1511 - Programming Fundamentals - 18s1

#include <stdio.h>

// calculate x to the power of 3


double cube(double x) {
double result;
result = x * x * x;
return result;
}

int main(void) {
double a, b;
printf("42 cubed is %lf\n", cube(42.03));
a = 2;
b = cube(a);
printf("2 cubed is %lf\n", b);
return 0;
}

print_messages.c
 
Simple lecture example of using functions  

#include <stdio.h>

void print_hundred_messages(void);
void print_ten_messages(void);
void print_message(void);

int main(int argc, char *argv[]) {


print_hundred_messages();
return 0;
}

void print_hundred_messages(void) {
print_ten_messages();
print_ten_messages();
print_ten_messages();
print_ten_messages();
print_ten_messages();
print_ten_messages();
print_ten_messages();
print_ten_messages();
}

void print_ten_messages(void) {
print_message();
print_message();
print_message();
print_message();
print_message();
print_message();
print_message();
print_message();
}

void print_message(void) {
printf("C is good.\n");
printf("C is great.\n");
printf("We all love C.\n");
}

call_by_value.c
 
Simple example illustrating call by value  

https://cgi.cse.unsw.edu.au/~cs1511/18s1/flask.cgi/code/functions/ 2/3
26/02/2019 COMP1511 - Programming Fundamentals - 18s1

#include <stdio.h>

int f(int x) {
int y;
// these assignments will have no effect on variables
// outside the function
x = x + 1;
y = 3;
return x * y;
}

int main(int argc, char *argv[]) {


int x, y , z;
x = 1;
y = 2;
z = f(y);
// note the variables x & y are local to main
// and are not changed by assignment to variables
// of the same name in f
printf("x=%d y=%d z=%d\n", x, y, z);
return 0;
}

fact_and_fib.c
 
Simple recursive calculation of factorials & Fibonacci numbers http://en.wikipedia.org/wiki/Factorial
http://en.wikipedia.org/wiki/Fibonacci_number  
 
Fibonacci calculation is very inefficient  
 

#include <stdio.h>
#include <stdlib.h>

int fibonacci(int n) {
if (n < 2) {
return 1;
}
return fibonacci(n - 1) + fibonacci(n-2);
}

int factorial(int n) {
if (n < 2) {
return 1;
}
return n * factorial(n - 1);
}

int main(void) {
int x;

printf("Enter number: ");


scanf("%d", &x);

printf("factorial %d = %d\n", x, factorial(x));


printf("fibonacci %d = %d\n", x, fibonacci(x));

return 0;
}

COMP1511 18s1: Programming Fundamentals is brought to you by  
the School of Computer Science and Engineering at the University of New South Wales, Sydney. 
For all enquiries, please email the class account at cs1511@cse.unsw.edu.au 
CRICOS Provider 00098G

https://cgi.cse.unsw.edu.au/~cs1511/18s1/flask.cgi/code/functions/ 3/3

You might also like