labprog9-2010

labprog9-2010 - defined as the average of the two middle...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
Swinburne University of Technology Faculty of Engineering and Industrial Sciences HES1300 Robotics & Mechatronics Project 1 Laboratory assignment 9: Your task for this session is as follows: 1. Carefully study the following program and make sure you fully understand how it works. Pay particular attention to the way that an array has been passed to a function (two-way communication for arrays – discuss the issue with your lab tutor if you're not sure!). 2. Using the provided sort function, write a new function called myMedian which takes an array of integers of any size and the size of the array as inputs and returns its median. For this exercise, if there were odd number of elements in the array, then the median would be the middle number in the sorted array. However, if the number of elements were even, then the median would be
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: defined as the average of the two middle numbers (after sorting). 3. Write comments for all the functions (including main) in your program. #include <stdio.h> #include <stdlib.h> #include <time.h> void sort(int mynum, int size) { int pass, indx, hold; for(pass = 0; pass < size - 1 ; pass++) { for (indx = 0; indx < size - pass - 1; indx++) { if( mynum[indx] > mynum[indx + 1] ) { hold = mynum[indx]; mynum[indx] = mynum[indx + 1]; mynum[indx + 1] = hold; } } } } int main () { int i,mynum[10], mycopy[10]; srand((unsigned)time(NULL)); for(i = 0; i < 10; i++) { mynum[i] = rand(); mycopy[i] = mynum[i]; } sort(mynum, 10); printf("original numbers sorted numbers\n"); for(i = 0; i < 10; i++) { printf("%10d %17d \n",mycopy[i],mynum[i]); } return(0); }...
View Full Document

This note was uploaded on 09/29/2010 for the course C++ c taught by Professor Lauren during the Three '10 term at Swinburne.

Ask a homework question - tutors are online