Download presentation
Presentation is loading. Please wait.
Published byLorin Little Modified over 9 years ago
1
C A RRAY Continue one dimensional array 1
2
Assume we have define this array: int hourlyTemp[ 24 ]; And we need to insert this array as parameter into function modifyArray( ); So, we need two parameters one for name of the array without brackets and the other is length of array. Then the above function look like this: void modifyArray(int hotemp, int e ); 2
3
C automatically passes arrays to function by reference [ the called functions can modify the element values in the callers ] original arrays. The name of the array is actually the address of the first element of the array. Because the starting address of the array is passed, the called function knows precisely where the array is stored. In the next example we illustrate if we print array, &array and &array[0] are the same if we print the address by using %p. 3
4
#include void main( ) { char array[5]; printf(“\n array = %p \n&array[0]= %p\n&array= %p \n”, array, &array[0], &array); } Output: array= 0012FF78 &array[0]= 0012FF78 &array= 0012FF78 4
5
Notes: Entire array are passed by reference But individual array elements are passed by value. 5
6
#include #define SIZE 5 void modifyArray(int b[], int size); void modifyElement(int e); void main( ) { int a[SIZE]={0, 1, 2, 3, 4}; int i; printf(“\nThe values of original array are:\n”); for(i=0; i< SIZE; i++) { printf(“\n%d”, a[i]); } 6 /* pass array a to modifyarray by reference*/ modifyArray(a, SIZE); printf(“\nThe values of modifyArray are:\n”); for(i=0; i< SIZE; i++) { printf(“\n%d”, a[i]); } printf(“\nOutput value of a[3] befor calling modifyElement= %d\n”. a[3]); /* pass value a[3] to modifyElement by value*/ modifyElement(a[3]); printf(“\nOutput value of a[3] after calling modifyElement = %d\n”. a[3]); }// end main
7
void modifyArray(int b[], int size) { int j; for(j=0; j< size; j++) { b[j] *= 2; } void modifyElement(int e) { printf(“\nOutput value of a[3] in modifyElement= %d\n”. e *= 2); } 7
8
#include void tryToModifyArray( const int b[]); void main( ) { int a[]={10, 20, 30}; tryToModifyArray( a ); printf(“%d %d %d\n”, a[0],a[1], a[2]); }//end main 8 void tryToModifyArray( const int b[] ) { b[0] /=2; //error b[1] /=2; //error b[2] /=2; //error } void tryToModifyArray( const int b[] ) { b[0] /=2; //error b[1] /=2; //error b[2] /=2; //error }
9
#include #define SIZE 10 void main( ) { int a[SIZE]= {2,6,4,8,10,12,89,68,45,37}; int pass, i, hold; printf(“\nthe original data array:\n ”); for(i=0; i< SIZE; i++) { printf(“\n%d”, a[i]); } 9 //Buble sort for(pass=1; pass< SIZE; pass++) { for(i=0; i< SIZE-1; i++) { if( a[i]> a[i+1]) { hold = a[i]; a[i] = a[i+1]; a[i+1] = hold; } //end if } //end for i }//end for pass printf(“\nData items in assending order:\n”); for(i=0; i< SIZE; i++) { printf(“\n%d”, a[i]); } } //end main
10
#include #define SIZE 100 int LinearSearch(const int array, int key, int size); void main( ) { int a[SIZE]; int x, searchkey, element; for(x=0; x< SIZE; x++) { a[i]=2*x; } printf(“\nEnter integer search key: ”); scanf(“%d”, &searchkey); element = LinearSearch(a, searchkey, SIZE); if(element != -1) printf(“element found in %d”, element); else printf(“\n not found”); }//end main 10
11
int LinearSearch(const int array, int key, int size) { int n; for(n=0; n< size; ++n) { if( array[n]== key) { return n;} //end if } //end for n return -1; // if key is not found } //end function 11
12
ASS Modify searching array program for character التسليم بعد العيد 12
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.