SlideShare a Scribd company logo
1 of 21
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;

More Related Content

What's hot

Data structure new lab manual
Data structure  new lab manualData structure  new lab manual
Data structure new lab manual
SANTOSH RATH
 

What's hot (20)

Perl 6 command line scripting
Perl 6 command line scriptingPerl 6 command line scripting
Perl 6 command line scripting
 
C programming function
C  programming functionC  programming function
C programming function
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
Fucntions & Pointers in C
Fucntions & Pointers in CFucntions & Pointers in C
Fucntions & Pointers in C
 
C Structure and Union in C
C Structure and Union in CC Structure and Union in C
C Structure and Union in C
 
Arrays
ArraysArrays
Arrays
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
 
Tut1
Tut1Tut1
Tut1
 
Bcsl 033 data and file structures lab s3-3
Bcsl 033 data and file structures lab s3-3Bcsl 033 data and file structures lab s3-3
Bcsl 033 data and file structures lab s3-3
 
Data structure new lab manual
Data structure  new lab manualData structure  new lab manual
Data structure new lab manual
 
Compiler Design Lab File
Compiler Design Lab FileCompiler Design Lab File
Compiler Design Lab File
 
C Prog - Array
C Prog - ArrayC Prog - Array
C Prog - Array
 
String Manipulation Function and Header File Functions
String Manipulation Function and Header File FunctionsString Manipulation Function and Header File Functions
String Manipulation Function and Header File Functions
 
Beginning Scala Svcc 2009
Beginning Scala Svcc 2009Beginning Scala Svcc 2009
Beginning Scala Svcc 2009
 
Functions in c
Functions in cFunctions in c
Functions in c
 
string , pointer
string , pointerstring , pointer
string , pointer
 
Functions
FunctionsFunctions
Functions
 
Function (rule in programming)
Function (rule in programming)Function (rule in programming)
Function (rule in programming)
 

Viewers also liked (14)

Data structures and algorithms lab3
Data structures and algorithms lab3Data structures and algorithms lab3
Data structures and algorithms lab3
 
Data structures and algorithms lab11
Data structures and algorithms lab11Data structures and algorithms lab11
Data structures and algorithms lab11
 
Data structures and algorithms lab8
Data structures and algorithms lab8Data structures and algorithms lab8
Data structures and algorithms lab8
 
Data structures and algorithms lab2
Data structures and algorithms lab2Data structures and algorithms lab2
Data structures and algorithms lab2
 
Difference between c# generics and c++ templates
Difference between c# generics and c++ templatesDifference between c# generics and c++ templates
Difference between c# generics and c++ templates
 
Data structures and algorithms lab7
Data structures and algorithms lab7Data structures and algorithms lab7
Data structures and algorithms lab7
 
Introduction to computer architecture and organization
Introduction to computer architecture and organizationIntroduction to computer architecture and organization
Introduction to computer architecture and organization
 
Data structures and algorithms lab5
Data structures and algorithms lab5Data structures and algorithms lab5
Data structures and algorithms lab5
 
USMLE and Canadian Exams
USMLE and Canadian ExamsUSMLE and Canadian Exams
USMLE and Canadian Exams
 
Chapter 1 introduction to computers
Chapter 1   introduction to computersChapter 1   introduction to computers
Chapter 1 introduction to computers
 
0. Course Introduction
0. Course Introduction0. Course Introduction
0. Course Introduction
 
Ppt 1
Ppt 1Ppt 1
Ppt 1
 
DSA-2012-Lect00
DSA-2012-Lect00DSA-2012-Lect00
DSA-2012-Lect00
 
Structure & union
Structure & unionStructure & union
Structure & union
 

Similar to Data structures and algorithms lab1

C-Program Custom Library, Header File, and Implementation FilesI .pdf
C-Program Custom Library, Header File, and Implementation FilesI .pdfC-Program Custom Library, Header File, and Implementation FilesI .pdf
C-Program Custom Library, Header File, and Implementation FilesI .pdf
herminaherman
 
C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01
Zafor Iqbal
 

Similar to Data structures and algorithms lab1 (20)

Introduction to programming c and data-structures
Introduction to programming c and data-structures Introduction to programming c and data-structures
Introduction to programming c and data-structures
 
Introduction to programming c and data structures
Introduction to programming c and data structuresIntroduction to programming c and data structures
Introduction to programming c and data structures
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
 
Esoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programmingEsoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programming
 
Synapseindia dot net development
Synapseindia dot net developmentSynapseindia dot net development
Synapseindia dot net development
 
C language
C languageC language
C language
 
Oops lecture 1
Oops lecture 1Oops lecture 1
Oops lecture 1
 
OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
 
C- Programming Assignment 4 solution
C- Programming Assignment 4 solutionC- Programming Assignment 4 solution
C- Programming Assignment 4 solution
 
PPS Notes Unit 5.pdf
PPS Notes Unit 5.pdfPPS Notes Unit 5.pdf
PPS Notes Unit 5.pdf
 
Oop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer MelayiOop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer Melayi
 
VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4
 
C-Program Custom Library, Header File, and Implementation FilesI .pdf
C-Program Custom Library, Header File, and Implementation FilesI .pdfC-Program Custom Library, Header File, and Implementation FilesI .pdf
C-Program Custom Library, Header File, and Implementation FilesI .pdf
 
C++ tutorials
C++ tutorialsC++ tutorials
C++ tutorials
 
report
reportreport
report
 
Introduction to c part 2
Introduction to c   part  2Introduction to c   part  2
Introduction to c part 2
 
Csharp_mahesh
Csharp_maheshCsharp_mahesh
Csharp_mahesh
 
C++ language
C++ languageC++ language
C++ language
 
C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01
 

Data structures and algorithms lab1

  • 1. DATA STRUCTURES AND ALGORITHMS LAB 1 Bianca Tesila FILS, Feb 2014
  • 2. 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) 
  • 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 & 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)
  • 5. 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 
  • 6. 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
  • 7. 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!
  • 8. 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
  • 9. 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)
  • 10. 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 
  • 11. 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; }
  • 12. 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
  • 13. I HOPE YOU’RE NOT SLEEPING YET 
  • 14. 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 
  • 15. 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
  • 16. 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
  • 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(date myDate) { printf("%2d %s %4d ", myDate.day, myDate.name_month, myDate.year); }
  • 20. 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
  • 21. 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;