CPPForSchool.com: Array - Single Dimension Set 1 Assignment 4
4.
Write a C++ program to find the largest and smallest element of an array.
When I attempted to find a solution for this assignment, I went on a wild goose chase. Admittedly, my approach was far too convoluted so I did not bother posting it. Mainly because it was flawed regardless of how many changes I made. Nevertheless, after reviewing the sample source code, I was able to not only complete the assignment, but I furthered my understandings of how to properly compare values with arrays without going overboard. The bit of code that stood out the most was to set the "small" and "large" to the lowest element of the array, specifically "Arr[0]". From there, it was a matter of comparing values within a for loop. My design included the largest/smallest int values by default and used that to compare. However, as stated, this approach was flawed because it did not understand what to do with the last values due to my improperly calculated algorithm. Still, I have learned a great deal with this assignment. Below is my source code and output along with the sample content.
When I attempted to find a solution for this assignment, I went on a wild goose chase. Admittedly, my approach was far too convoluted so I did not bother posting it. Mainly because it was flawed regardless of how many changes I made. Nevertheless, after reviewing the sample source code, I was able to not only complete the assignment, but I furthered my understandings of how to properly compare values with arrays without going overboard. The bit of code that stood out the most was to set the "small" and "large" to the lowest element of the array, specifically "Arr[0]". From there, it was a matter of comparing values within a for loop. My design included the largest/smallest int values by default and used that to compare. However, as stated, this approach was flawed because it did not understand what to do with the last values due to my improperly calculated algorithm. Still, I have learned a great deal with this assignment. Below is my source code and output along with the sample content.
Comments
Post a Comment