Odds are it will always be successful but if

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

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: between brackets, just as we do for haystack in find.c: int haystack[HAY_MAX]; 6 < 13 This is CS50. Harvard College Fall 2010 But when passing an array, you only specify its name, just as we do when passing haystack to sort in find.c: if (!sort(haystack, size)) { printf("Could not sort haystack.\n"); return 2; } (Why do we also pass in the size of that array separately?) When declaring a function that takes a one ­dimensional array as an argument, though, you don’t need to specify the array’s size, just as we don’t when declaring sort in helpers.h (and helpers.c): bool sort(int values, int n); Go ahead and implement sort so that the function actually sorts, from smallest to largest, the array of numbers that it’s passed, in such a way that its running time is in O(n), where n is the array’s size.3 Yes, this running time is possible, hax0r, because you may assume that each of the arrays’ numbers will be non ­negative and less than LIMIT, a constant defined in generate.c.4 However, realize that the array might contain duplicates. Take care, though, not to alter our declaration of sort. Its prototype must remain: bool sort(int values, int n); As this return type of bool implies, this function must not return a sorted array; it must instead “destructively” sort the actual array that it’s passed by moving around the values therein, thereafter returning true if and only if sorting was successful. (Odds are it will always be successful, but, if you employ certain tricks, it’s possible to, say, run out of memory.) As we’ll discuss in Week 4, arrays are not passed “by value” but instead “by reference,” which means that sort will not be passed a copy of an array but, rather, the original array itself. We leave it to you to determine how to test your implementation of sort. But don’t forget that printf and, now, gdb are your friends. And don’t forget that you can generate the same sequence of pseudorandom numbers again and again by explicitly specifying generate’s seed. Before you ultimately submit, though, be sure to remove any such calls to printf, as we like our programs’ outputs just they way they are! 3 Technically, because we’ve bou...
View Full Document

This note was uploaded on 03/22/2013 for the course COMP SCI CS-50 taught by Professor Malan during the Spring '10 term at Harvard.

Ask a homework question - tutors are online