Lecture9_handout

Lecture9_handout - Arrays EE140 Programming Concepts for...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 EE140: 0 Programming Concepts for Engineers Lecture 9 1 Multidimensional Arrays Arrays An array variable is a set of variables are a common name and type share a common name and type all linked together int age[30]; 30 elements: age[0], age[1], . .., age[29] index starts from 0 to n-1 (n is the size of the array) normally use with a loop 2 for (i=0; i<30; i++) { printf("enter a value: "); scanf("%d", &age[i]); } Example 1 Write a program to read in an array from keyboard. Calculate the mean and median value of the array. main( ) mean_array( ) median_array( ) bubble_sort( ) 3 median_array(): 1. call bubble_sort() to sort the array 2. the median is the middle element of sorted array: x[n/2] /*function to calculate the mean of the array*/ double mean_array (double x[ ], int n) { double sum =0.0, mean; int i; for (i=0; i<n; i++) sum = sum + x[i]; mean = sum/n; return (mean); } 4
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 /*Bubblesort function to sort the array */ void bubble_sort( double x[ ], int n) { int i, j; double temp; for( i = 0; i < n-1 ; i ++ ) { /* outer for loop */ o r(j=i+1 ;j<n;j+ +){ * inner for loop */ for( j = i + 1; j < n ; j ++ ) { /* inner for loop */ if( x[i] > x[j] ) {
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

Lecture9_handout - Arrays EE140 Programming Concepts for...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online