1970 Colorado School of Mines Golden
1970 Colorado School of Mines Golden
1970 Colorado
School of Mines Golden
142
('&$% !
B(C .=6
143
('&$% !
Arrays & Strings
6.1
6.2
6.3
6.4
6.5
6.6
6.7
8G:G
6$ IJKHB
= N;=B('&8$%
! strings
('&$% PQR
= N;=B(7U
=(PQ
('&$% !
144
6.1
) ( .
= 5.1
= 6.3
=7.4
:
a0
a1
a2
a9 =8.2
10 . float
.
.
x
:
B(C .=6
145
146
('&$% !
6.2
.
cost 7
:
; ]float cost [7
:
]cost [0
]cost [1
]cost [2
]cost [3
]cost [4
]cost [5
]cost [6
= 0
= .......... 1 .
147
:
B(C .=6
[email protected]
) (
.
.
:
income0 , income1 , income2 , ..., income 6
.
; ]float income [7
7 . income
diff
). (6.2.1
148
('&$% !
)(main
{
;float income[7], sum , average
;int i
)for(i=0; i<=6 ; i++
{
"]printf("\n Enter income for day[%d
;) ,i+1
;)]scanf("%f",&income[i
;]sum += income[i
}
;average=sum/7
;)printf("\n avrage=%6.3f",average
printf("\n day
income
;)"diff
)for(i=0; i<=6 ; i++
printf("\n %d
%6.2f %6.2f",
;)i+1,income[i],income[i]-average
}
) (6.2.1
B(C .=6
149
x [0] = 10
x [1] = 42
x [2] = 33
x [3] = 15
x [4] = 26
) (
:
.
i=0 ) i = n-2 n-1
(
] x [i] < x [i+1 .
.
:
/*----------------PROGRAM EX631.C -----------*/
#define N 5
)(main
{
;int x[N], i ,k, temp , sorted
)for(i=0; i<N ; i++
{
150
('&$% !
B(C .=6
151
1
. :
; if ( sorted ) break
:
; if ( sorted = = 1) break
.
iterations ) ( .
) N ( .
:
} { )for( k=0 ; k<N ; k++
sorted
N
sorted
.
152
('&$% !
] x [i ] x [i+1
temp ]x [i
] x[i+1 ] x [i :
]x[I
]x[I+1
temp
) (6.3.1 ] x [i ]x [i+1
6.4 Strings
.
) ascii ( .
:
" " Kamal Ahmed
" " 32 Cairo Street
) (
153
B(C .=6
[email protected]
.
8 .
) ' ( '\0
:
""UNIVERSITY
11 ) 10
( :
; ]char w [11
w char 11
10 ''\0
:
11 14( :
Your name is ................
.
name family
. 12 ) 11 (
15 ) 14 ( .
154
('&$% !
)(main
{
;]char name[12
;]float family[15
;)" ?printf("\n What is your first name
;)scanf("%s",name
;) "?printf("\n What is your family name
;)scanf("%s",family
;)printf("\n Your name is %s %s ", name, family
}
) (6.4.1
"!
scanf %s )
s string( .
&
scanf
.
scanf gets
) ( get string :
; ) gets ( name
:
; ) scanf ( " %s " , name
B(C .=6
155
:
: string2 string1
20 string3
string2 . string1
) (6.4.2 :
- 1 gets
-2
' string [i] = = ' \0
string
-3 string3
' '\0 :
; ' string3 [k] = ' \0
-4 puts :
156
puts ( string3 ) ;
-5
concatenation
. strcat
( 6.4.2)
:
strcat ( string1 , string2 ) ;
. string1 string2
main()
{
char string1[20] , string2[20] , string3[40] ;
int i , k,j ;
printf("\n enter string1-->");
gets( string1 );
printf("\n enter string2-->");
gets( string2 );
for(i=0; ; i++)
{
if( string1[i] == '\0' ) break;
string3[i]=string1[i];
}
for(k=i, j=0 ; ; k++,j++)
{
if( string2[j] == '\0' ) break;
string3[k] = string2[j];
}
B(C .=6
157
; 'string3[k] = '\0
;) puts ( string3
}
) (6.4.2
string1 string3 ) strcpy
(string copy :
; )strcpy ( string 3 , string 1
: 5
.
0 . 9
:
.1 ) (.
.2
} { 0 , 1 , 2 , ....... , 9
.3 )(
) 5 ( .
.
158
('&$% !
) (6.4.3
:
) getch (get character
.
getche e .getch
) putchar (put character
.
>#include <stdio.h
)(main
{
;]char x[5
;int i
;)">printf("\n Please enter a number--
)for(i=0;i<=4 ;i++
);;(for
{
;)(x[i]=getch
)'if( x[i]>= '0' && x[i]<= '9
;)]{ putchar(x[i
;break
}
B(C .=6
159
else
;)"printf("\a
}
;'x[5]='\0
;)printf("\n The number entered is %s ", x
;)(getch
}
) (6.4.3
] x [5 :
; ' x [5] = ' \0
x . printf
. puts
getch
. keyboard
> # include < stdio.h
) ( main :
) ( putchar ) ( getch
('&$% !
160
&'~
67
78
56
42
35
58
47
61
38
45
36
25
19
) (6.5.1
students
:
; ]int students [3][4
= 4 =. 3
161
B(C .=6
[email protected]
. .
). (6.5.1
) (6.5.2
:
#define N 3
#define M 4
)(main
{
]int students[3][4
}={78,58,45,56,47,36,42,61,25,35,38,19
162
, dept[3], sem[4], i, j;
char format[10]=" %d\t";
for ( i=0 ; i<N ; i++)
{
dept[i]=0;
for(j=0; j<M ; j++)
dept[i] += students[i][j];
}
for(j=0; j < M ; j++)
{
sem[j]=0;
for(i=0 ; i<N ; i++)
sem[j] += students[i][j];
}
for(i=0 ; i<N; i++)
{
printf("\n") ;
for(j=0 ; j<M; j++)
printf(format, students[i][j]) ;
printf(" %d",dept[i]);
}
printf("\n");
for(j=0; j<M ; j++)
printf(format,sem[j]);
}
( 6.5.2)
scanf
students
:
int studens [3][4] = { 78 , 58 , 45 , 56 , 47 , 36 ,
42 , 61 , 25 , 35 , 38 , 19 }
163
B(C .=6
[email protected]
.... .
array
. initialization
.
:
; " char format [10] = " %d \ t
format
" . " %d \ t
" " %d \ t
\ t . tab
\ t .
:
IGB
\a
(!R
bell
164
\b
('&$% !
back
space
\n
\t
6.6
.
.
" "Ahmed " " Ali
" "Ali " > "Ahmed
" "Ali
" . "Ahmed
strcmp
string comparison : :
165
s1 < s 2
s1 = s 2
if
if
s1 > s 2
if
B(C .=6
negative
strcmp(s1 , s2) = 0
positive
) A
( Z :
) if ( strcmp (name[i+1] , name[i] ) < 0
; { sorted = 0
; ) ]strcpy ( temp , name [i
; ) ]strcpy (name [i] , name [i+1
; ) strcpy ( name [i+1] , temp
}
strcpy
:
; ]temp = name [i
temp ] name [i . string
) (6.3.1 :
] name[N][L N
L .
166
. strcpy
. strcmp
#define N 5
#define L 12
main()
{
char name[N][L], temp[L] ;
int i ,j, k , sorted;
for(i=0; i<N ; i++)
{
printf("\nenter name[%d] ",i);
scanf("%s",&name[i]);
}
for(k=0; k<N; k++)
{
sorted=1;
for(i=0; i<N-1 ; i++)
{
if(strcmp(name[i+1] , name[i])<0 )
{
sorted = 0;
strcpy(temp, name[i] );
strcpy(name[i] , name[i+1]);
strcpy(name[i+1] , temp);
}
}
if(sorted) break;
}
printf("\n Here is the sorted array after %d iterations",k);
for(i=0; i<N ; i++)
B(C .=6
167
;)]printf("\n %s",name[i
}
) (6.6.1
:
old_array
. new_array
inserting )
( .
:
Old Array
Anas
Huda
Lubna
Omar
Suad
i
0
1
2
3
4
. Shada
:
('&$% !
168
New Array
Anas
Huda
Lubna
Omar
Shada
Suad
i
0
1
2
3
4
5
) (m new_name :
]old_array [m] < new_name < old_array [m+1
. m
new_array :
;]if i < m new_array[i] = old_array[i
;if i == m new_array[i] = new_name
;]if i > m new_array[i] = new_array[i-1
i= 0 , 1 , 2 , 3 , .......... N
) (6.6.3 :
6= B(C .
[email protected]
169
old_array new_array
:
char old_array [N][L] , new_array [N+1][L] , new_name[L] ;
: new_name
old_array[i] < new_name < old_array[i+1]
#define L 12
#define N 5
main()
{
char old_array[N][L], new_array[N+1][L] ,
new_name[L];
int i ,m ;
for(i=0; i<N ; i++)
{
printf("\nenter name[%d] ",i);
scanf("%s",&old_array[i]);
}
printf("\nPlease enter new name ") ;
scanf("%s",new_name);
for(i=0;i<N; i++)
if( strcmp( new_name , old_array[i]) > 0 &&
strcmp( new_name , old_array[i+1]) < 0 )
{ m=i+1 ; break ; }
170
('&$% !
:
strcmp ( new_name , old_array [i] ) > 0
&& strcmp (new_name , old_array [i+1] ) < 0
.
linked lists pointers
.
B(C .=6
171
old_array new_array
.
)
( .
.
.
6.7
.1
s = ( xI - xbar )2
I=0 .I=n-1 xbar
.x I
.n
. 2
yi+1 = ( 1+r ) yi
172
('&$% !
i r
. :
.
10.
. 10.
. .
1000 . r = 0.05
.3 10
.
.4
0 9 .
. 5
a , b , c , ..... z
A , B , C , ......... Z
B(C .=6
173
c 99
) ( .32
C .67
.6 .
..
. 7 xi
)(
.
.
.8 N M
.
.9 a N M b
M L c
c =a*b
:
174
('&$% !