Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Languages
5. Week
loops (do-while, for), Arrays, array
operations, C libraries
Hazrlayan
for
loops
for loop
for( initial statements;logical expression;updates)
{
statements;
}
for loop
for( initial statements;logical expression;updates)
{
statements;
}
for loop
for( initial statements;logical expression;updates)
{
statements;
}
for while
statement1 ;
while ( expression2 )
{
statements ;
statement3 ;
}
for ( statement1;expression2;statement3 )
{
statements ;
}
void main ( )
{
int k, n ;
for( k = 1, n = 12 ; k<9 && n>6 ; k++, n--)
{
printf ("k=%d, n=%d\n" , k , n ) ;
}
}
Exercises:
What is the value of x when the following statement is complete?
for (x = 0 ; x < 100 ; x++) ;
What is the value of ctr when the following statement is complete?
for (ctr = 2 ; ctr < 10 ; ctr += 3) ;
How many X s does the following print?
for (x = 0; x < 10; x++)
for (y = 5; y > 0; y--)
printf("X");
Loop termination
The keyword break can be used for terminating the loop. If
the loop is nested, it terminates the current block.
The keyword continue is used for skipping until the end of
the loop block, but the block does not terminate.
Depending on the algorithm, you may use combinations of
these keywords.
}
}
4
3
12
8
6
15
12
5
9
16
8
12
5
5
13
10
10
17
15
13
15
20
17
20
Exercises:
Can you write the previous program using only two nested
loops?
Exercises:
Write a program in which you can enter a string into a string
variable using for loop and getchar. Whenever an enter is
pressed, the new string must be available for printing with
printf(%s,..).
Find a simple program that converts lowercase letters to
uppercase ones inside a string. Do not use long lists of if-else
or switch-case. Do not use ready C functions either. You do
not need to know ASCII codes of letter, either. Be smart.
Arrays
Arrays
List of variables: [ ]
Arrays
Arrays
It is the declaration of a list of same type of variables
under a single name.
Arrays
Arrays
Another example is the days of a month. This can be
considered as a 1-D array, or a 2-D array whose
horizontal elements are days of weeks, and vertical
elements are the weeks.
Days of a year, therefore, can be considered as a 3-D
array. First dimension is days of week, second
dimension is weeks of month, third dimension is
months of year.
Arrays
Array declarations
A 1-D array is declared by a variable name and number of
elements inside a square bracket.
Example:
int gun [ 7 ];
gun corresponds to the array name, whose elements are of
int type. There are seven elements.
Similarly, a 2-D array is declared by two square brackets:
int ay[ 4 ][ 7 ];
There are 4 rows and 7 columns of ay.
Arrays
Array usage
Each element of an array is reached by its index
written in square parantheses.
In C, the first element is with index 0 (zero). For
example, the gun array has first 0th, and last 6th
element.
The numbers inside the square parantheses are
called index numbers.
Arrays
int gun[7];
Usage examples:
gun[5] = 1;
if( gun[5] == 4 ) break;
gun[5] = gun[6] - 1;
12
54
-10
Arrays
Example
#include <stdio.h>
void main()
{
int gun [ 7 ];
int i;
for( i = 0 ; i < 7 ; i++ )
{
gun[ i ] = 0;
}
}
Bil-200
Arrays
int c[12];
c[0]=-45;
c[1]=0;
c[2]=6;
....
c[11]=78;
c[0]
-45
c[1]
c[2]
c[3]
72
c[4]
1543
c[5]
-89
c[6]
c[7]
62
c[8]
-3
c[9]
c[10]
c[11]
1
6453
78
Bil-200
Arrays
x=2;
c[x] == c[5-3] == c[2] == 6;
typical mistakes:
c(4)
float f=1.0;
c[f];
Arrays
Initializations of arrays
We know how to initialize single variables while declaration. The
situation is similar for arrays. Just use a list o values inside a
block separated by ,s.
#include <stdio.h>
void main()
{
int gun[7] = { 0,2,4,6,8,10,11 };
..........
}
Arrays
Initializations of arrays
If the array size is too big, declarative initialization is difficult.
In that case, make initial assignments inside a program using
loops. If we still want to make initializations, we may initialize
the first few, and the rest will be zero. As an example:
int sayilar [ 10 ] = { 2 };
Arrays - Example
#include <stdio.h>
void main()
{
int gun[7] = { 2,3,4,5,6,7,8 };
int i;
for (i = 0 ; i < 7 ; i++ ) {
printf ("gn[%d] ubat %d, 2004\n", i, gun[i] ) ;
}
}
Arrays - Example
Arrays Exercise
Exercise
It will display
1.000054 on the
screen.
Why?
Floating point numbers are not exactly real
numbers.
Floating point numbers are represented by
inverse powers of 2 (binary representation).
Each 0.0001 will not be precisely 0.0001,
so when we add them 10000 times, the
result will be different.
For a better precision, use double. But it
will cost more memory.
Rounding errors
Array operations
Array search and sorting
Array operations
Array operations are very frequently used in various
programs.
In a sorted array, search can be done by simply
checking each and every element. However, there are
faster ways to do it.
A common fast method is called binary search.
Sequential search
array a list of values
a_size array size
target the searched value
num 1
found false
While (num <= a_size) VE (found = false)
{
if target equals array[num]
found true
else
num num + 1
}
Sequential search
#include <stdio.h>
#define UZUNLUK
10
void main()
{
int dizi[UZUNLUK], bulundu = 0;
int sayi=UZUNLUK, hedef, num = 1;
/* target and array[] is read from file */
while( (num <=sayi) && (!bulundu) ) {
if(hedef == dizi[num])
bulundu = 1;
else
num++;
}
}
Binary search
Binary search
left 1
right LENGTH
found 0 (false)
while not found and left <= right
{
mid center of left and right
If target equals array[mid] then found 1 (true)
else if target < array[mid] then right mid 1
else if target > array[mid] then left mid + 1
}
Binary search
Lets search for 33. At first, left 0, right 26, found 0:
Left
Right
12 15 18 25 33 46 47 49 50 53 54 57 59 61 63 66 68 71 74 78 81 83 88 90 94 97 99
10 11
12
13
14 15
16
17
18
19
20 21
22
23 24 25
26
Left
Mid
Right
12 15 18 25 33 46 47 49 50 53 54 57 59 61 63 66 68 71 74 78 81 83 88 90 94 97 99
0
10 11
12
13
14 15
16
17
18
19
20 21
22
23 24 25 26
Binary search
target (33) < array[mid] (61), right mid - 1:
Left
Right Mid
12 15 18 25 33 46 47 49 50 53 54 57 59 61 63 66 68 71 74 78 81 83 88 90 94 97 99
0
10 11
12
13
14 15
16
17
18
19
20 21
22
23 24 25 26
Mid
Right
12 15 18 25 33 46 47 49 50 53 54 57 59 61 63 66 68 71 74 78 81 83 88 90 94 97 99
0
10 11
12
13
14 15
16
17
18
19
20 21
22
23 24 25 26
Binary search
target (33) < array[mid] (47), right mid - 1:
left
right
mid
12 15 18 25 33 46 47 49 50 53 54 57 59 61 63 66 68 71 74 78 81 83 88 90 94 97 99
0
10 11
12
13
14 15
16
17
18
19
20 21
22
23 24 25
26
mid
right
12 15 18 25 33 46 47 49 50 53 54 57 59 61 63 66 68 71 74 78 81 83 88 90 94 97 99
0
10 11
12
13
14 15
16
17
18
19
20 21
22
23 24 25
26
Binary search
target (33) > array[mid] (18), left mid + 1:
mid left right
12 15 18 25 33 46 47 49 50 53 54 57 59 61 63 66 68 71 74 78 81 83 88 90 94 97 99
0
10 11
12
13
14 15
16
17
18
19
20 21
22
23 24 25
26
right
12 15 18 25 33 46 47 49 50 53 54 57 59 61 63 66 68 71 74 78 81 83 88 90 94 97 99
0
10 11
12
13
14 15
16
17
18
19
20 21
22
23 24 25
26
Binary search
target (33) == array[mid] (33), found true
Orta
Sa
Sol
12 15 18 25 33 46 47 49 50 53 54 57 59 61 63 66 68 71 74 78 81 83 88 90 94 97 99
0
10 11
12
13
14 15
16
17
18
19
20 21
22
23 24 25
26
Exercise
C Libraries
C Liabraries
"math.h", "stdlib.h", "string.h"
C Libraries
C Libraries
Function Libraries
<stdio.h> - printf(), fprintf(), scanf(),
fscanf(), fopen(), putchar(), getchar(), ....
<math.h> - pow(), sqrt(), fabs()...
<ctype.h> - toupper(), tolower(),
isalpha(), isdigit(), ....
<stdlib.h> - rand(), srand(), exit(), ....
C Libraries
C Libraries
C Libraries
C Libraries
Approximate value of pi
#include <stdio.h>
#include <math.h>
int main(void)
{
double pi, yak, hata;
pi = 2 * asin(1);
/* realistic pi value*/
yak = (double) 22 / 7;
/* approximate pi */
hata = fabs(yak pi) / pi; /* find error
*/
printf("pi = %.15f\n", pi); /* write result
printf("yaklak = %.15f\n", yak);
printf("hata = %.5f", hata * 100);
return 0;
*/
pi
yaklak
= 3.141592653589793
= 3.142857142857143
hata = 0.04025
time(NULL)
Gives number of seconds since 01/01/1970
At 15:00 on 01/24/2004, its value was.
Before using you should write:
#include <time.h>
Usage:
int t;
t = time(NULL);
Clock: tik-tak
#include <stdio.h>
#include <time.h>
void main(void)
{
int i, t;
for(i=0; i <= 10; ++i) {
/* just for 10 sac */
t = time(NULL) + 1;
/* t next sec
*/
while (time(NULL) < t);
/* wait
printf("tik\n");
/* write tik
*/
*/