KEMBAR78
Data structures and algorithms lab1 | PPTX
DATA STRUCTURES AND ALGORITHMS
LAB 1

Bianca Tesila

FILS, Feb 2014
COURSE INFO


Lectures: Maria-Iuliana Dascalu
mariaiuliana.dascalu@gmail.com
 http://mariaiulianadascalu.com/ (where you can find
the courses)




Labs: Bianca Tesila
bianca.tesila@gmail.com
 http://biancatesila.wordpress.com/ (labs, grades,
home assignments)

GRADING
Exam: 40p
 Tests & presence at the course: 10p
 Labs & Assignments: 50p


Assignments: 30p ( 3 x 10p)
 Lab Activity: 20p ( presence, class assignments, small
home assignments)


Rules:
• Minimum 45p in order to pass
• Minimum 25p out of 60p in order to take the exam
• One should attend at least 10 labs(out of 12)
TOOLS & USEFUL LINKS


C-Free 5.0 Standard
(http://www.programarts.com/cfree_en/download.htm)



Any other IDE or compiler for C/C++ (ex. GCC under
Linux)



http://www.cs.usfca.edu/~galles/visualization/Algorith
ms.html (it helps you understand how various data
structures work)
OBJECTIVES
run and compile C programs
 identify the structure of a C program
 use standard I/O operations
 define variables
 declare and implement functions
 make structures

INTRODUCTION TO C PROGRAMMING


The basic structure of a C program:








Similarities to the structure of a Java program:







Inclusion of headers is similar to importing packages or classes

Differences from the structure of a Java program:






Inclusion of headers
Definition of types/classes
Declaration of global variables
Definition of functions
The main function

Functions and variables may also be defined outside of a class
The main function is not part of a class
Arrays can also be allocated statically in C/C++

A C program is written in a file with the “.c” extension: the source
code
After compilation, another file, with the “.o” extension
appears: the object code.
After execution, another file, with the “.exe” extension
appears: the executable
INTRODUCTION TO C PROGRAMMING
EXAMPLE OF A C PROGRAM:
#include <stdio.h> // inclusion of the stdio.h header
int a, b, c; // global variables of the type int: a, b, c
int main() { // beginning of the main function
a = 10;
scanf("%d", &b); // read the value of b from the standard input
c = a + b; // assign the sum of a and b to the variable c
printf("%dn", c); // print the value of c to the standard output
return 0; // finish the main function successfully
}

Pay attention: C is case-sensitive!
I/O OPERATIONS: STANDARD OUTPUT
OPERATIONS


We shall use the printf function:
printf(format, param_1, param_2, …, param_n);
format = a string containing characters and format specifiers
 param_1, param_2, …, param_n = expressions; their values are written
taking into account the corresponding format specifier


Format Specifier

Type

%i or %d

int

%ld

long

%f

float

%lf

double

%c

char

%s

string
I/O OPERATIONS: STANDARD OUTPUT
OPERATIONS
‼ Exercise: run the below example and see how each format specifier works
#include <stdio.h>
int main() {
printf("%dn", 7);
printf("%3dn", 7);
printf("%03dn", 7);
printf("%3.2fn", 5.1);
printf("%.2fn", 4.245);
printf("%sn", "blue");
return 0;
}

Note: we used a special character /n (newline character)
The n used in the printf statements is called an escape sequence. Commonly
used escape sequences are:








n (newline)
t (tab)
v (vertical tab)
f (new page)
b (backspace)
r (carriage return)
n (newline)
I/O OPERATIONS: STANDARD INPUT
OPERATIONS


We shall use the scanf function:
scanf(format, param_1, param_2, …, param_n);
format = a string containing characters and format specifiers
 param_1, param_2, …, param_n = expressions; their values are stored
taking into account the corresponding format specifier


Similar to printf
 Use «stdio.h» for I/O operations

I/O OPERATIONS: STANDARD INPUT
OPERATIONS
#include <stdio.h>
int main () {
char name [80];
int age;
printf ("Enter your family name: ");
scanf ("%s",name);
printf ("Enter your age: ");
scanf ("%d", &age);
printf ("Mr. %s , %d years old.n“, name, age);

return 0;
}
I/O OPERATIONS
‼Exercise: Write a program to calculate the average
of two float numbers. The result shall be displayed
with 2 decimals. Use scanf and printf!
Hint: %.2f -> format specifier for float with 2
decimals
I HOPE YOU’RE NOT SLEEPING YET 
FUNCTIONS: DECLARATION AND
IMPLEMENTATION


The general form of a function definition in C
programming language is as it follows:
return_type function_name( parameter list ) {
body of the function
}

Visibility domain: local vs. global variables
 Parameter passing: by-value

FUNCTIONS: EXAMPLE
• Note the use of
math.h library: for sqrt
function (the same
meaning as in Java)
•

Note the control flow
structures (if, if-else,
for, …)

• Note the function
definition and call: the
implemented function
calculates if a number
is prime or not
FUNCTIONS
‼Exercise: Check whether a number is a palindrome or
not.

Hint: a palindrome is a number that remains the same when
its digits are reversed.
333 is a palindrome
123 is not a palindrome
STRUCTURES
struct struct_name {
variables (fields of the struct type)
}



Like Java classes but without methods or public/private
specifications



Used to package related data together



User-defined collection of one or
more variables (fields), grouped under one name



The members of a structure are accessed with “.”
STRUCTURES: EXAMPLE
struct date {
unsigned int day;
unsigned int month;
unsigned long year;
char name_day[3];
char name_month[4];
};

typedef struct date {
unsigned int day;
unsigned int month;
unsigned long year;
char name_day[3];
char name_month[4];
} date;

typedef struct date date;
date today;

date today;

‼ date is now a type

‼ typedef allows you to declare
instances of a struct without
using keyword "struct"
STRUCTURES: EXAMPLE
void writeDDMMMYYYY(date myDate)
{
printf("%2d %s %4d ", myDate.day,
myDate.name_month, myDate.year);
}
STRUCTURES
‼ Exercise: Design a structure for representing dates
and write functions that:
- Check if a variable value of the structure is a valid
date
- Calculate the next date of a given date
- Calculate the date before a given date
HOMEWORK







Finish all the lab exercises.
Write a program that displays the first ten Fibonacci numbers.
Write a program to simulate the Bulls and Cows
game(http://en.wikipedia.org/wiki/Bulls_and_cows), by giving
two input numbers.
Write functions for writing, reading, addition and
multiplication of rare polynomials.
Rare polynomials with integer coefficients are polynomials
of large degrees and many coefficients equal to 0. They
can be represented by a data structure defined as:
typedef struct TMonom
{
int coefficient;
unsigned int exponent;
}TMonom;
TMonom[50] polynomial;

Data structures and algorithms lab1

  • 1.
    DATA STRUCTURES ANDALGORITHMS LAB 1 Bianca Tesila FILS, Feb 2014
  • 2.
    COURSE INFO  Lectures: Maria-IulianaDascalu mariaiuliana.dascalu@gmail.com  http://mariaiulianadascalu.com/ (where you can find the courses)   Labs: Bianca Tesila bianca.tesila@gmail.com  http://biancatesila.wordpress.com/ (labs, grades, home assignments) 
  • 3.
    GRADING Exam: 40p  Tests& presence at the course: 10p  Labs & Assignments: 50p  Assignments: 30p ( 3 x 10p)  Lab Activity: 20p ( presence, class assignments, small home assignments)  Rules: • Minimum 45p in order to pass • Minimum 25p out of 60p in order to take the exam • One should attend at least 10 labs(out of 12)
  • 4.
    TOOLS & USEFULLINKS  C-Free 5.0 Standard (http://www.programarts.com/cfree_en/download.htm)  Any other IDE or compiler for C/C++ (ex. GCC under Linux)  http://www.cs.usfca.edu/~galles/visualization/Algorith ms.html (it helps you understand how various data structures work)
  • 5.
    OBJECTIVES run and compileC programs  identify the structure of a C program  use standard I/O operations  define variables  declare and implement functions  make structures 
  • 6.
    INTRODUCTION TO CPROGRAMMING  The basic structure of a C program:       Similarities to the structure of a Java program:     Inclusion of headers is similar to importing packages or classes Differences from the structure of a Java program:     Inclusion of headers Definition of types/classes Declaration of global variables Definition of functions The main function Functions and variables may also be defined outside of a class The main function is not part of a class Arrays can also be allocated statically in C/C++ A C program is written in a file with the “.c” extension: the source code After compilation, another file, with the “.o” extension appears: the object code. After execution, another file, with the “.exe” extension appears: the executable
  • 7.
    INTRODUCTION TO CPROGRAMMING EXAMPLE OF A C PROGRAM: #include <stdio.h> // inclusion of the stdio.h header int a, b, c; // global variables of the type int: a, b, c int main() { // beginning of the main function a = 10; scanf("%d", &b); // read the value of b from the standard input c = a + b; // assign the sum of a and b to the variable c printf("%dn", c); // print the value of c to the standard output return 0; // finish the main function successfully } Pay attention: C is case-sensitive!
  • 8.
    I/O OPERATIONS: STANDARDOUTPUT OPERATIONS  We shall use the printf function: printf(format, param_1, param_2, …, param_n); format = a string containing characters and format specifiers  param_1, param_2, …, param_n = expressions; their values are written taking into account the corresponding format specifier  Format Specifier Type %i or %d int %ld long %f float %lf double %c char %s string
  • 9.
    I/O OPERATIONS: STANDARDOUTPUT OPERATIONS ‼ Exercise: run the below example and see how each format specifier works #include <stdio.h> int main() { printf("%dn", 7); printf("%3dn", 7); printf("%03dn", 7); printf("%3.2fn", 5.1); printf("%.2fn", 4.245); printf("%sn", "blue"); return 0; } Note: we used a special character /n (newline character) The n used in the printf statements is called an escape sequence. Commonly used escape sequences are:        n (newline) t (tab) v (vertical tab) f (new page) b (backspace) r (carriage return) n (newline)
  • 10.
    I/O OPERATIONS: STANDARDINPUT OPERATIONS  We shall use the scanf function: scanf(format, param_1, param_2, …, param_n); format = a string containing characters and format specifiers  param_1, param_2, …, param_n = expressions; their values are stored taking into account the corresponding format specifier  Similar to printf  Use «stdio.h» for I/O operations 
  • 11.
    I/O OPERATIONS: STANDARDINPUT OPERATIONS #include <stdio.h> int main () { char name [80]; int age; printf ("Enter your family name: "); scanf ("%s",name); printf ("Enter your age: "); scanf ("%d", &age); printf ("Mr. %s , %d years old.n“, name, age); return 0; }
  • 12.
    I/O OPERATIONS ‼Exercise: Writea program to calculate the average of two float numbers. The result shall be displayed with 2 decimals. Use scanf and printf! Hint: %.2f -> format specifier for float with 2 decimals
  • 13.
    I HOPE YOU’RENOT SLEEPING YET 
  • 14.
    FUNCTIONS: DECLARATION AND IMPLEMENTATION  Thegeneral form of a function definition in C programming language is as it follows: return_type function_name( parameter list ) { body of the function } Visibility domain: local vs. global variables  Parameter passing: by-value 
  • 15.
    FUNCTIONS: EXAMPLE • Notethe use of math.h library: for sqrt function (the same meaning as in Java) • Note the control flow structures (if, if-else, for, …) • Note the function definition and call: the implemented function calculates if a number is prime or not
  • 16.
    FUNCTIONS ‼Exercise: Check whethera number is a palindrome or not. Hint: a palindrome is a number that remains the same when its digits are reversed. 333 is a palindrome 123 is not a palindrome
  • 17.
    STRUCTURES struct struct_name { variables(fields of the struct type) }  Like Java classes but without methods or public/private specifications  Used to package related data together  User-defined collection of one or more variables (fields), grouped under one name  The members of a structure are accessed with “.”
  • 18.
    STRUCTURES: EXAMPLE struct date{ unsigned int day; unsigned int month; unsigned long year; char name_day[3]; char name_month[4]; }; typedef struct date { unsigned int day; unsigned int month; unsigned long year; char name_day[3]; char name_month[4]; } date; typedef struct date date; date today; date today; ‼ date is now a type ‼ typedef allows you to declare instances of a struct without using keyword "struct"
  • 19.
    STRUCTURES: EXAMPLE void writeDDMMMYYYY(datemyDate) { printf("%2d %s %4d ", myDate.day, myDate.name_month, myDate.year); }
  • 20.
    STRUCTURES ‼ Exercise: Designa structure for representing dates and write functions that: - Check if a variable value of the structure is a valid date - Calculate the next date of a given date - Calculate the date before a given date
  • 21.
    HOMEWORK     Finish all thelab exercises. Write a program that displays the first ten Fibonacci numbers. Write a program to simulate the Bulls and Cows game(http://en.wikipedia.org/wiki/Bulls_and_cows), by giving two input numbers. Write functions for writing, reading, addition and multiplication of rare polynomials. Rare polynomials with integer coefficients are polynomials of large degrees and many coefficients equal to 0. They can be represented by a data structure defined as: typedef struct TMonom { int coefficient; unsigned int exponent; }TMonom; TMonom[50] polynomial;