1 / 12

C Array

C Array. Continue one dimensional array. Passing array to function. Assume we have define this array: int hourlyTemp [ 24 ]; And we need to insert this array as parameter into function modifyArray ( );

niyati
Télécharger la présentation

C Array

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C Array Continue one dimensional array

  2. Passing array to function • Assume we have define this array: inthourlyTemp[ 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(inthotemp, int e );

  3. Passing array to function • 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.

  4. Passing array to function #include <stdio.h> 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

  5. Passing array to function Notes: • Entire array are passed by reference • But individual array elements are passed by value.

  6. Passing array to function /* 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 #include<stdio.h> #define SIZE 5 void modifyArray(int b[], int size); void modifyElement(int e); void main( ) { int a[SIZE]={0, 1, 2, 3, 4}; inti; printf(“\nThe values of original array are:\n”); for(i=0; i< SIZE; i++) { printf(“\n%d”, a[i]); }

  7. continue 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); }

  8. Passing array to function #include<stdio.h> 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 void tryToModifyArray( const int b[]) { b[0] /=2; //error • b[1] /=2; //error • b[2] /=2; //error }

  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 Sorting array: #include<stdio.h> #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]); }

  10. Searching array: #include<stdio.h> #define SIZE 100 intLinearSearch(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

  11. Continue Searching array: intLinearSearch(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

  12. ass • Modify searching array program for character التسليم بعد العيد

More Related