Sei sulla pagina 1di 22

DATA STRUCTURES AND ALGORITHMS LAB 1

Melania Nițu

GRADING

The lab represents 50% of the final grade:

30% - 3 Big Homeworks

20% - Labs (in-class activity + small homework from week to week)

Bonuses for exceptional hw

Conditions for passing:

10 presences at the lab (max 13 presences)

5 out of 10 for hw + labs (if not, the lab will have to be redo the next year and the students won’t be allowed in the exam)

HW + GRADING

The 3 main assignments and the labs will be published on Moodle (fils.curs.pub.ro, section CTI(E)- Data Structures and Algorithms).

You will use the same platform to upload the homeworks (within the deadline) and you will have to present them during the next laboratory. If there is any problem with the platform, you can send the homeworks by email.

Your code will be checked using a software against plagiarism. Two almost identical homeworks will be noted with 0 points.

You can consult the grades for the laboratory at:

https://docs.google.com/spreadsheets/d/1fBgZ2CUe05fv1fvO766Qu9Vg52oycfjtrS00RNrnP9Q/edit#gid=1
816440752

QUESTIONS

For any other question you can contact me via email: melania.nitu@yahoo.com

You can use the forum on Moodle where you can also find the courses and the laboratories. (http://fils.curs.pub.ro)

TOOLS

Code::Blocks (http://www.codeblocks.org/downloads ) C-Free 4.0 Standard / C-Free 5.0 Professional

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

OBJECTIVES

to run and compile C programs

to identify the structure of a C program

to use standard I/O operations

to define variables

to declare and implement functions

to make structures

IDENTIFY THE STRUCTURE OF A TYPICAL C PROGRAM!

The structure of a C program:

Pre-processing directives: stdio.h – header file from the C library, necessary for using the printf and scanf functions

One or more functions ( the main function is necessary for a C program to be executed ), with conditional structures, loop instructions etc.

Attention, C is case sensitive!

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.

STANDARD OUTPUT OPERATIONS

STANDARD OUTPUT OPERATIONS Other format specifiers:

Other format specifiers:

STANDARD OUTPUT OPERATIONS Other format specifiers:

SIGNATURE OF PRINTF FUNCTION

printf(control, par1, par2, …, parn);

Where control = a string which defines the texts and the formats specifiers

par1, par2, …, parn = expressions; their values are written taking into account the format specifiers from control (same order)

Exercise: Test the format specifiers of the function printf.

EXCERCISE

Run the below example and see how each format specifier works

#include <stdio.h> int main(void) { printf("%d\n", 7); printf("%3d\n", 7); printf("%03d\n", 7); printf("%3.2f\n", 5.1); printf("%.2f\n", 4.245); printf("%s\n", "blue"); return 0;

}

7); printf("%3.2f\n", 5.1); printf("%.2f\n", 4.245); printf("%s\n", "blue"); return 0; }

STANDARD INPUT OPERATIONS

STANDARD INPUT OPERATIONS Scanf has the same signature as printf and it is defined in stdio.h

Scanf has the same signature as printf and it is defined in stdio.h.

EXERCISE

EX1: Write a program to calculate the average between two float numbers. The result shall be displayed with 2 decimals. Use scanf and printf!

Hint: %.2f -> format specifier for float with 2 decimals

FUNCTIONS: DECLARATION AND IMPLEMENTATION

Signature:

type_of_the_returned_result function_name (list_of_formal_params) {

}

declaration_of_local_variables;

instructions;

Visibility domain: local vs. global variables

Parameter passing: by-value (the value of the formal parameter is modified only in the local function, the variable from main function is not affected); we will discuss the passing by-address during the lab with the pointers.

GOLDBACH 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

else, for, …) •Note the function definition and call: the implemented function calculates if a number

EXERCISES

EX2: Check whether a number is a palindrome or not.

Hint: a palindrome is a number that remains the same when its digits are reversed. (e.g. 333 is a palindrome, 123 is not a palindrome)

Ex3: Display the minimum from the three float numbers.

Ex4: Write a program that calculates the sum of the digits for all integers in an interval. The limits of the interval are read from the keyboard.

STRUCTURES

a user-defined data type that allows grouping of heterogeneous elements

a collection of one or more variables (fields), grouped under one name

the members of a structure are accessed with “.”

Format: struct [structure tag, optional]

{members} [structure alias]

DATE EXAMPLE

DATE EXAMPLE

EX: UTILISATION

void writeDDMMYYYY(data myDate)

{

printf(“%d %d %4d” , myDate.day, myDate.month, myDate.year);

}

EX5: 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 (tomorrow).

- Calculate the date before a given date (yesterday).

HOMEWORK

1. Write a program that reads a number from the console and writes back if the number is odd or even.

2. Write a program that prints the odd numbers up to 25.

3. 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:

0. They can be represented by a data structure defined as: Write functions for writing, reading

Write functions for writing, reading and addition of rare polynomials.

Ex:

defined as: Write functions for writing, reading and addition of rare polynomials. Ex: is a rare

is a rare polynomial;

defined as: Write functions for writing, reading and addition of rare polynomials. Ex: is a rare

is a monom.

HOMEWORK

4. Write a program to calculate the greatest common divisor

(cmmdc) for any 2 numbers inserted from the keyboard.

REFERENCES FOR THE COURSE

“C++ ProgrammingLanguage”, Bjarne Stroustroup

“Thinkingin C++”, by Bruce Eckel& Chuck Allison

“C++ Plus Data Structures”, by NellDale

“LimbajeleC si C++ pentruincepatori”(vol 1-C, vol 2-C++), by LiviuNegrescu(en roumain)

Tutorials point: http://www.tutorialspoint.com/cprogramming/

C Programming and C++ Programming: http://www.cprogramming.com/