آرایه Array
آرایه مجموعه اى از متغیرهاى از یک نوع است که با یک نام مشترک تحت استفاده
قرار مى گیرند.
یک عنصر بخصوص در یک آرایه با یک index ( اندیس ) مورد دستیابى قرار مى گیرد.
در C تمام عناصر آرایه از مکانهاى مجاور ( بهم چسبیده ) حافظه تشکیل مى گردند .
کوچکترین آدرس ، آدرس عنصر اول است . بزرگترین آدرس متناظر آخرین عنصر است.
آرایه ها مى توانند یک بعدى یا چند بعدى باشند .
آرایه هاى یک بعدى
فرم کلى آرایه یک بعدى به این صورت است :
type array-name[ size ] ;
همان نوع پایه است که نوع هر عنصر آرایه است . type
اندازه ( size ) مى گوید آرایه چند عنصر درون خود خواهد داشت .
اندیس اولین عنصر هر آرایه ، صفر مى باشد .
مثال : اگر بگویید
int x[10] ;
x به عنوان یک آرایه با 10 عنصر معرفى شده لذا عناصرش
x[0] , x[1] , x[2] , … , x[9]
مى باشد .
عنصر
آدرس
0 1 2 3 4 5 6 7
1000 1002 1004 1006 1008 1010 1012 1014
عنصر
آدرس
8 9
1016 1018
اگر x در حافظه از آدرس 1000 شروع شده باشد آنگاه :
مثال :
#include <stdio.h>
main( )
{
int x[10] ;
int t ;
for ( t = 0 ; t < 10 ; ++t ) x[ t ] = t ;
for ( t = 0 ; t < 10 ; ++t ) printf(“x[%d]= %dn”,t, x[t]);
}
sizeof(type) * length of array
total bytes =
char interest[10] ;
کل اندازه آرایه از فرمول زیر بدست مى آید :
مثال :
int id[4] ;
1 x 10 = 10
مثال :
2 x 4 = 8
Sizeof1.C برنامه
مثال :
#include <stdio.h>
#include <conio.h>
main() {
int x[10], num ;
clrscr();
printf(“size of int is %d n”, sizeof(int));
printf(“size of float is %d n”, sizeof(float));
printf(“size of double is %d n”, sizeof(double));
printf(“size of long double is %d n”, sizeof(long double));
printf(“size of long int is %d n”, sizeof(long int));
printf(“size of num is %d nn”, sizeof(num));
printf(“size of x is %d nn”, sizeof(x) );
}
size of int is 2
size of float is 4
size of double is 8
size of long double is 10
size of long int is 4
size of num is 2
size of x is 20
size of int is 2
نتیجه اجرا
x
X[0]
X[1]
X[8]
y
y[0]
y[1]
y[8]
x + y
X[0] + y[0]
X[1] + y[1]
X[8] + y[8]
#define MAX 100
#include <stdio.h>
#include <conio.h>
main() { /* sum of two vectors */
int i , n;
double x[MAX] , y[MAX] ;
printf(“ n ? n”);
scanf(“%d”, &n) ;
printf(“please enter the values of the first vector:n”) ;
for (i = 0 ; i < n ; i++ ) {
printf(“x[%d] ? t “ , i ) ;
scanf(“%lf” , &x[i]) ;
}
printf(“please enter the values of the second vector:n”) ;
for (i = 0 ; i < n ; i++ ) {
printf(“y[%d] ? t “ , i ) ;
scanf(“%lf” , &y[i]) ; }
برنامه جمع دو بردار
for (i = 0 ; i < n ; i++ )
printf(“sum[%d] = %.0f n” , i , x[i] + y[i] ) ;
}
toupper(ch) =
اگر ch یک حرف کوچک باشد
معادل بزرگ ch
در غیر اینصورت
ch
#include <ctype.h>
tolower(ch) =
اگر ch یک حرف بزرگ باشد
معادل کوچکch
در غیر اینصورت
ch
#include <ctype.h>
#define MAX 100
#include <ctype.h>
#include <dos.h>
#include <stdio.h>
#include <conio.h>
main() { /* sum of two vectors */
int i , n , cyesno ;
double x[MAX] , y[MAX] ;
for(cyesno = ‘Y’ ; cyesno == ‘Y’ ; ) {
clrscr();
printf(” n ? n”);
scanf(“%d”, &n) ;
printf(“please enter the values of the first vector:n”) ;
for (i = 0 ; i < n ; i++ ) {
printf(“x[%d] ? t ” , i ) ;
scanf(“%lf” , &x[i]) ;
}
printf(“please enter the values of the second vector:n”) ;
for (i = 0 ; i < n ; i++ ) {
printf(“y[%d] ? t ” , i ) ;
scanf(“%lf” , &y[i]) ;
}
for (i = 0 ; i < n ; i++ )
printf(“sum[%d] = %.0f n” , i , x[i] + y[i] ) ;
printf(“do you want to sum other vectors ? Y/Nt”);
cyesno = toupper(getche());
}
}
آرایه هاى داراى ابعاد بالاتر :
type array-name [ size1 ] [ size2 ] …. [size n ] ;
مثال :
int a[2][3] ;
a00
a01
a02
a10
a11
a12
مثال :
int b[2][2] ;
b00
b01
b10
b11
در زبان C ماتریس دو بعدى در حافظه بصورت سطرى نگهدارى مى شود :
نکته :
a00
a01
a02
a10
a11
a12
مثال :
برنامه محاسبه جمع دو ماتریس :
#include <stdio.h>
#define MAX 100
main ( ) {
int m , n , i , j ;
double a[MAX][MAX] , b[MAX][MAX] ;
1- شروع
2- m را از ورودی دریافت کن
3- n را از ورودی دریافت کن
4- مقادیر ماتریس اول ( A ) را از ورودی دریافت کن
5- مقادیر ماتریس دوم ( B ) را از ورودی دریافت کن
6- مقادیر ماتریسهای A و B را با هم جمع و چاپ کن
7- پایان
printf ( “ number of rows of matrices : m ?t “ ) ;
scanf(“%d” , &m ) ;
printf ( “ number of columns of matrices : n ?t “ ) ;
scanf(“%d” , &n ) ;
printf(“ Enter aij ‘s members of the first matrix :A n”) ;
for ( i = 0 ; i < m ; i++ )
for ( j = 0 ; j < n ; j++ ) {
printf( “ a[%d][%d] ? t” , i , j ) ;
scanf( “%lf” , &a[i][j] ) ;
}
printf(“ Enter bij ‘s members of the second matrix :B n”) ;
for ( i = 0 ; i < m ; i++ )
for ( j = 0 ; j < n ; j++ ) {
printf( “ b[%d][%d] ? t” , i , j ) ;
scanf( “%lf” , &b[i][j] ) ;
}
for ( i = 0 ; i < m ; i++ )
for ( j = 0 ; j < n ; j++ )
printf( “ sum[%d][%d] = %lfn” , i , j , a[i][j] + b[i][j] ) ;
}
انواع خطاها Kinds of Errors
1- Syntax error خطاى نحوى
2- linker error خطاى اتصال دهنده
3- Run-time error خطاى زمان اجرا
4- Logical error خطاى منطقى
Debugرفع اشکال
Bug اشکال
Moduleبسته ، تکه
Maintance
Support
تابع function
آرایه Array
خیلی خیلی خیلی مهم
خیلی خیلی مهم
sin
п
0
return
ضابطه
Function عملکرد
f(x) = sin(x)
f
x
x2
f(x) =
x2
f(t) =
t2
به x یا t پارامتر ظاهری تابع می گویندFormal parameter
به п پارامتر واقعی تابع می گویندActual parameter
تابع Function
sin sqrt(4) √4
cos square root
log10(100) 2
tan return value مقدار بازگشتی
asin
acos
atan
atan2(y,x)
cast
2 5 2 * 2 * 2 * 2 * 2
5
basen base * base * base … * base
n
#include <math.h>
#include <stdio.h>
main()
{
int n;
do {
scanf(“%d” , &n);
if( n < 0 )
printf(“Enter a positive integer number: ”); }
while( n < 0 )
printf(“%lf” , sqrt( (double) n); }
#include <stdio.h>
int power( int , int);
/* test power function */
main( )
{
int i ;
for ( i = 0 ; i < 10 ; i++ )
printf(“%d %d %dn” , i , power(2 , i) , power(-3 , i) );
return 0 ;
}
Function Call فراخوانى تابع
insert mode
مثال :
تابع توان
تابع Function
/* power : raise base to n-th power ; n >= 0 */
int power (int base , int n )
{
int i , p;
for ( p = 1 , i = 1 ; i <= n ; ++i )
p = p * base ;
n = 20;
return p;
}
double sqrt(double x)
return-type function-name(parameter declaration,if any)
{
declarations
statements
}
تعریف تابع :
Actual parameters َ پارامترهاى واقعى arguments آرگومان
Formal parameters پارامترهاى رسمى parameter
Call by value فراخوانى توسط مقدار
pow(x , y)
function prototype
نمونه اولیه
2 ** 3
2 ^ 3
math.h
Mathematics
#include <math.h>
Character Ascii Code
0 48 ‘0’
1 49 ‘1’
2 50
9 57 ‘9’
#include <stdio.h>
/* count digits, White space, others */
main( )
{
int c , i , nwhite, nother ;
int ndigit[10] ;
nwhite = nother = 0 ;
for ( i= 0; i < 10 ; ++i )
ndigit[ i ] = 0 ;
while ( ( c = getchar() ) != EOF )
if ( c >= ‘0’ && c <= ‘9’ )
++ndigit[ c – ‘0’ ] ;
else if ( c == ‘ ‘ || c== ‘n’ || c== ‘t’ )
++nwhite ;
else
++nother ;
printf(”digits = “ ) ;
for ( i = 0 ; i < 10 ; ++i )
printf(“ %d” , ndigit[ i ] );
printf( “ , white space = %d , other = %dn” ,
nwhite , nother ) ;
}