![]() |
|
#1
|
|||
|
|||
Void functions with arraysHi guys, I need help with arrays and functions. I have to do the following:Write a C++ program that has a main function and one void function.
The void function is to be named sortof and will be called by the main function. The function sortof has two arguments, each of which are one-dimensional arrays of length four. The main program prompts the user for four numbers. These four numbers are stored in one of the two arrays. The function is then called and it sorts the numbers into their proper order, storing them in order (lowest to highest) in the second array. The main program then prints out the unsorted and sorted array to the screen. You don't have to use a fancy sorting routine, just determine the max, the min and then do the same for the two numbers left behind. ------------------------------------------------ what I've done: CPP / C++ / C Code:
Last edited by LuciWiz : 15-Oct-2009 at 00:53.
Reason: Please insert your C++ code between [cpp] & [/cpp] tags
|
|||
|
#2
|
||||
|
||||
Re: Void functions with arraysFirstly, your code is poorly formatted and your use of whitespace needs a lot of improvement. Poor coding style makes for difficult to read and understand code. Select an exisiting, accepted, good style and use it.
Secondly, you are being taught and/or are electively using bad variable names. "sortof" ...what in the world does THAT mean? In English, it is similar to "maybe" or "somewhat." Unless is is supposed to be some kind of acronym, it has no real meaning, at least not by someone reading its name alone and trying to figure out what it is supposed to do or mean. What about your variable names for your two arrays? st and sr? Which is which just from looking at the names? Which one is "store" and which is "sort?" Doesn't both of the "longer names" each have most of the same letters? I don't think that anyone likes to hear their "style" being "attacked," but I strongly recommend that you decide to improve it greatly or simply give up programming in your life. Some may consider this to be too harsh, but I believe it is that important. Obviously, you may choose to ignore any advice that you receive from any source, including whether or not to blindly follow the stupid assignment that the name of the function must be named "sortof." If it was up to me, I'd implement the ridiculously named function and then create a function pointer using an appropriate name and then call it, which, I'd argue, still meets the rather stringent requirements. Of course, there is something to be said about lemmings who blindly follow. The notion that "sortof" has only two arguments is yet another example of bad programming instruction and/or practices. It says that the integer arrays are of length 4 (assuming count of elements). However, there is no way to ensure that the user does not misuse the function by sending just two elements or two million elements. Of course, if we add the additional arguments for "length in number of elements," there is no guaranty that the user will provide the correct number of elements, all which contribute to undefined behavior for reasonable implementations. However, in C++, we say that the signature of a "function" is a "contract." That if someone gives us the arguments correctly, then we will produce the correct results. That "contract" is unenforceable when the arguments list is simply two int pointers. In other words, parts of the contract are UNWRITTEN and therefore are worthless. (If you prefer, the value of an unwritten contract is not worth the paper that it is written on.) It is VERY BAD to use "unwritten contracts" in code. Subsequent programmers and/or users of the code will not readily know that the "unwritten part" exists and will surely break the code or worse, it will continue "to work" without failing, becoming a timebomb waiting to happen. Someday it will blow up, and probably take a month of Sundays to figure out what went wrong. When an experienced programmer sees "bad code," one must suspect everything, regardless of how ingeniously it may solve a problem, bad traits and poor style give cause for justified alarm. How about this as a start? CPP / C++ / C Code:
Output: Code:
I'll leave it to you to decide how you want to complete the other "requirements." Also, if you wish to review the obvious issue with the fail/good test and the output to the user (has a bug!), it needs to be "fixed," but that is up to you to decide how to proceed. MxB |
|
#3
|
|||
|
|||
Re: Void functions with arraysYou could do what Bob says or you could do it the mortal way. That is, fixing what is broken learning as you go.
I'll try highlight the parts that most likely will give you trouble. CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
You could do it something like this. CPP / C++ / C Code:
Output: Code:
|
Recent GIDBlog
Not selected for officer school by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Assembly Tutorial? | zatora | Assembly Language | 76 | 22-Apr-2009 21:19 |
| Linked Lists advice request | promsan | C Programming Language | 74 | 23-May-2007 08:29 |
| triangle (polygon), drawing, sizing, and rotation programme using linked lists... | promsan | C Programming Language | 12 | 14-May-2007 14:03 |
| need help with a console menu system | BullBuchanan | C++ Forum | 6 | 20-Aug-2006 14:46 |
| Shapes Functions Version 2 - Arrays! | Cecil | C Programming Language | 1 | 09-Jul-2006 20:39 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The