EE205 Data Structures for Engineers Lecture 1 Introduction - PowerPoint PPT Presentation

1 / 47
About This Presentation
Title:

EE205 Data Structures for Engineers Lecture 1 Introduction

Description:

EE205 Data Structures for Engineers Lecture 1 Introduction – PowerPoint PPT presentation

Number of Views:60
Avg rating:3.0/5.0
Slides: 48
Provided by: core3
Category:

less

Transcript and Presenter's Notes

Title: EE205 Data Structures for Engineers Lecture 1 Introduction


1
EE205 Data Structures for EngineersLecture 1
Introduction
Feb 3, 2009 Kyu Ho Park (kpark_at_ee.kaist.ac.kr), h
ttp//core.kaist.ac.kr
2
Syllabus
  • Professor Kyu Ho Park
  • 350-3425, kpark_at_ee.kaist.ac.kr
  • TA YoungWoo Park, KiWoong Park, HyunCheol Seok,
    SungKyu Park
  • ywpark, woongbak,hcseok,skpark_at_core.kaist.ac.kr
  • References
  • 1.Data Structures in C,Kalicharan,2008
  • 2.Data Structures and Algorithm Analysis in C,
    2nd Ed.Weiss, 1997
  • 3.Data Structures with Java, J.Hubbard and A.
    Huray2004,
  • Web http//core.kaist.ac.kr

3
Contents
  • C Programming Overview
  • Linked Structures
  • Stacks
  • Queues
  • Hash Tables
  • Recursion
  • Trees
  • Heap and Priority Queues
  • Graphs
  • Special Topics
  • The lectures notes of the above issues are
    based on the reference1 and 3.

4
Evaluation
  • Homeworks 10
  • Projects 35
  • Quizzes etc. 5
  • Midterm Exam 20
  • Final Exam 30

5
Review C -Programming
  • Procedure to solve a problem in a computer
  • Define the problem
  • Analyze the problem
  • Develop an algorithm for solving the problem
  • Write the computer program which implements the
    algorithm
  • Test and debug the program
  • Document the program
  • Maintain the program

6
Define the problem
  • I want to calculate the area of a circle

r
7
Analyze the problem
  • Confirm that we have clear understanding of it.
  • Determine
  • The inputs to the program
  • The outputs of the program
  • Problem Calculate area pr2
  • Input the radius of the circle
  • Output the area a of the circle
  • I

8
Algorithm to solve the problem
  • Algorithm A well defined set of instructions to
    solve a problem
  • AD 825 Abu Jafar Mohammed Musa al Khowarizmi
    wrote a book Kitab aljabr wal-mugabala

9
Algorithm
  • Computer instructions
  • Input instructions
  • Processing instructions
  • Output instructions
  • Data and variables
  • When we give data to a program, that data is
    stored in memory.
  • A program must be stored in the computers memory
    also.

10
Data and variables
  • r and area is called variable names, or simply
    variables.

Radius r
Area area
11
Algorithm
  • Ask for the radius of the circle
  • Save the value in the box r
  • Calculate the area of the circle
  • Save the area in the box area
  • Print the value in the box area
  • Stop

12
Write the program of the algorithm
Enter the value of your circle 10
Area of the circle314
13
Program
  • include ltstdio.hgt
  • main()
  • int r
  • float area, pi3.14
  • printf( Enter the value of the radius )
  • scanf(d, r)
  • area pirr
  • printf(\nArea of a circle is f\n, area)

14
Test and debug/Documentation
  • Test and debug
  • Compile the program
  • Run the program
  • Documentation
  • Description of the problem
  • The Algorithm for solving the problem
  • The list of program
  • Input test data and the output of the program

15
Maintaining
  • Error correction
  • Modification of the program
  • Migration to a new system

16
scanf()
  • Problem calculate 20 5
  • Input
  • Enter first number
  • Enter second number
  • Output
  • 20 5

17
Program
  • include ltstdio.hgt
  • main()
  • int a,b,sum
  • printf(Enter first input)
  • scanf(d, a)
  • printf(\nEnter second input)
  • scanf(d, b)
  • sum a b
  • printf(\nd dd,a,b,sum)

18
Reading strings gets() and strcp()
  • char item20
  • gets(item)// It reads characters and stores
    // them in item
  • or
  • strcpy(item, Data Structure)

19
gets() and strcpy()
  • include ltstdio.hgt
  • include ltstring.hgt
  • main()
  • char item30, charStr30
  • printf(Enter a string)
  • gets(item)
  • strcpy(charStr, EE205 Data Strcutures)
  • printf(Strings item s and charStrs, item,
    charStr)

20
Selection logic
  • ! gt gt lt
  • If(score gt50) printf(Pass\n)
  • Else printf(Fail\n)
  • True or False?
  • (hgt1)(hlt99)
  • hgt1hlt99
  • ngt1 n lt12
  • !(ngt1 nlt12)

21
if
  • A automobile repair shop charges W50,000 per hour
    for labor plus the cost of any replaced parts .
    But the minimum charge for the repair (including
    the cost of parts) is W100,000.
  • or

Labor hour 3.5 Parts cost W20,000 Charge
for the repair
Labor hour 1.0 Parts cost W30,000 Charge for
the repair
22
Algorithm
  • Read the hours worked
  • read the parts cost
  • calculate (Labor charge Parts cost)
  • If the total cost is less than W100,000,
  • total charge 100,000
  • 5. print the total charge

23
if
  • include ltstdio.hgt
  • main()
  • double hours, parts, repairCharge
  • printf(Labor Hour?)
  • scanf(lf, hours)
  • printf(Parts Cost?)
  • scanf(lf,parts)
  • repairCharge hours50000 parts
  • if( repairCharge lt 100000) repairCharge 100000
  • printf(\nCharge for the repair W3.2f\n,
    repairCharge)

24
If . else
  • A student has got 5 examinations, each marked out
    of 100. The student will get S if his average
    mark is greater than or equal to 60, and F if
    his average mark is less than 60.
  • or

Enter 5 marks Average is xxx , S
Enter 5 marks Average is xxx , F
25
Algorithm
  • Read 5 marks
  • Calculate the average
  • if the average is larger than 60,
  • print S
  • 4. else
  • print F.

26
Program
  • include ltstdio.hgt
  • main()
  • int m1, m2,m3, m4, m5
  • double average
  • printf(Enter 5 marks)
  • scanf(d d d d d,m1,m2,m3,m4,m5)
  • average (m1m2m3m4m5)/5.
  • printf(\nAverage is lf, average)
  • if( average gt60.) printf(S\n)
  • else
  • printf(F\n)

27
Data input from a filefscanf()
  • FILE infopen(input.txt, r)
  • int num
  • fscanf(in, d,num)
  • fclose(in)

28
Program for File In
  • // fileInOut.cpp Defines the entry point for
    the console application.
  • //
  • include "stdafx.h"
  • include ltstdio.hgt
  • int main(int argc, char argv)
  • FILE infopen("input.txt", "r")
  • int num, sum0,n0
  • fscanf(in, "d", num)
  • while(num!0)
  • nn1
  • sumsumnum
  • fscanf(in,"d", num)

15.if(n0) printf("\nNo numbers
entered\n") 16.else 17. printf("\nd
numbers were entered\n") 18. printf("The
sum is d\n", sum) 19. printf("The
average is 3.2f\n",(double)sum/n) 20. 21.
fclose(in) 22. return 0 23.
29
fprintf()
  • To send output to the file, fprintf is used.
  • FILE outfopen(output.txt, w)
  • fprintf(out, The sum is d\n,sum)
  • fclose(out)

30
Program
  • include "stdafx.h"
  • include ltstdio.hgt
  • int main(int argc, char argv)
  • FILE infopen("input.txt","r")
  • FILE outfopen("output.txt","w")
  • int num, sum0, n0
  • fscanf(in, "d", num)
  • while(num !0)
  • n n 1
  • sumsum num
  • fscanf(in,"d", num)

if( n0) fprintf(out, "No numbers enterd\n")
else fprintf(out, "d numbers were
entered\n",n) fprintf(out, "The sum
is d\n", sum) fprintf(out, "The
average is 3.2f\n", (double)sum/n)
fclose(in) fclose(out)
return 0
31
for
  • for i1 to 100 do
  • sum sum 1
  • end
  • for(i1 ilt100 i)
  • sum sum 1
  • for( ltexpr1gtltexpr2gtltexpr3gt)
  • ltstatementgt

32
for and while
  • for(ltexp1gt ltexp2gt ltexp3gt)
  • ltstatementgt
  • ltexp1gt
  • while(ltexp2gt)
  • ltstatementgt
  • ltexp3gt

33
Handling characters
  • Character sets
  • ASCII to represent characters
  • Digits 0 9 codes 48 57
  • Uppercase letters A Z codes 65 90
  • Lowercase letters a z codes 97 to 122
  • Control characters
  • \n new line
  • \f new page
  • \t tab

34
Character value
  • printf(Char value c, Integer valued\n,a,
    a)
  • Output Char value , Integer value
  • Character variable
  • char ch
  • int n
  • cha
  • n a 5
  • printf(Input character is c\n, ch)
  • printf(Value n d\n, n)

35
Read
  • scanf(c, ch)
  • or
  • getchar()
  • int c getchar()
  • char ch getchar()//If getchar() is called when
    there is no more data, it returns -1 , which is
    the value of EOF defined in stdio.h.

36
getchar()
  • include ltstdio.hgt
  • main()
  • printf(Enter data and press Enter\n)
  • char chgetchar()
  • printf(The first char is c\n, ch)
  • printf(Next code is c\n, getchar()

37
getchar()
  • include ltstdio.hgt
  • main()
  • printf( Input data and press Enter\n)
  • for( int i1 ilt5, i)
  • char ch getchar()
  • printf(d th character is c\n, i, ch)

38
Counting characters in a line
  • include ltstdio.hgt
  • main()
  • char ch
  • int countChar 0
  • printf(Type and press Enter\n)
  • while((chgetchar())!\n) countChar
  • printf(No. of characters is d\n,countChar)

39
Read/write characters from/to a filegetc() and
putc()
  • 1. FILE in fopen(input.txt, r)
  • 2. ch getc(in)//or fscanf(in, c, ch)
  • FILE out fopen(output.txt, w)
  • putc(ch, out) //or fprintf(out,c,ch)
  • ? getchar() , putchar( )

40
functions
  • We have already used functions such as
    main(),printf(),scanf(), putc(), getchr(), etc.
  • The use of functions avoid the need for repeated
    program codes.

41
Calculating factorials
  • f(n) nf(n-1)
  • f(n-1)(n-1)f(n-2)
  • f(2)2f(1)
  • f(1)1f(0)
  • f(0)1

Enter the value of n The value of n! is xxxx.
Algorithm 1. Get the value of n 2. Calculate
f(n) f(n)n(n-1)...1 3. Printout f(n)
42
n!
  • include ltstdio.hgt
  • long int f(int n)
  • main()
  • int n
  • printf(Enter the value n\n)
  • scanf(d, n)
  • printf(The value of d! is ld\n, n, f(n))

43
f(n)
  • long int f(int n)
  • Int i
  • long int p1
  • If(ngt1)
  • for(i2iltn i) p pi
  • return p

44
f(n) //recursion
Header of the function
  • long int f(int n)
  • if(n lt0) return 0
  • else if(n 0) return 1
  • else return nf(n-1)

Parameter list
Body of the function
45
function
  • How to give data to a function?
  • int test(int n, char a, double b)

46
nCr
  • To calculate nCr
  • Read n and r
  • nCr n!/((n-r)! r!) //use the function f()
  • Printout the result

Enter the value n and r of nCr The value of nCr
is XXXX
47
Program for nCr
  • include ltstdio.hgt
  • long int f(int)
  • long int combination(int, int)
  • main()
  • int n,r
  • printf(Enter n and r of nCr\n)
  • scanf(d d, n, r)
  • printf(The value of dCd is ld, n, r,
    combination(n,r))
  • long int combination(int n, int r)
  • long int f(int)
  • return f(n)/(f(n-r)f(r) )
Write a Comment
User Comments (0)
About PowerShow.com