Question: Computer Programming 2 Lab 3 This assignment tests the concepts of functions arrays loops Objective Todays lab is a bit different than the last few.

Computer Programming 2

Lab 3

This assignment tests the concepts of

  • functions
  • arrays
  • loops

Objective

Todays lab is a bit different than the last few. We will only be implementing a few functions. The provided main.cpp file will be used to TEST your functions.

It is recommended that for each function you get the simplest case working and add handling of the other cases once everything else works. This is called stepwise refinement.

Implement each function prototyped in functions.h that has a TODO. I recommend you start with the starter code for functions.cpp. Read the provided preconditions and postconditions, ask questions if you do not understand them. Your implementation will be checked for compliance to these conditions. You may assume the precondition is ALWAYS met, you do not need to confirm the precondition is true.

You may modify the main function, however, you will only hand in functions.cpp. You will want to avoid modifying functions.h, since modifications here may make your code unable to be compiled by the grader.

starter functions.cpp

#include  #include  #include  #include  #include "functions.h" double standardDeviation(const double nums[], unsigned int count) { return 0; //TODO: remove this stub when implemented } void printArray(const double nums[], unsigned int count, unsigned short precision/*=3*/ ) { } bool isInteger(const std::string &s) { return true; //TODO: remove this stub when implemented } long long stringToLong(const std::string& s) { return 0; //TODO: remove this stub when implemented } void swapNumbers(long& x, long& y) { }

functions.h

#ifndef LAB_FUNCTIONS_H #define LAB_FUNCTIONS_H #include  //Precondition: nums is an array of count size. //Postcondition: The values in the array are filled with random values from min to max // THIS FUNCTION IS IMPLEMENTED IN main.cpp void fillArrayWithRandoms(double nums[], unsigned int count, long min, long max); //Precondition: none //Postcondition: base to the power of exponent is returned. // pow in c++ returns a double. Due to this, we cannot use pow on long long numbers. // The longPow function will allow this to work, however, will work only for positive values (to make the code easier) // THIS FUNCTION IS IMPLEMENTED IN main.cpp long long longPow (unsigned long long base, unsigned long exponent); //Precondition: Not any //Postcondition: Returns true if the passed string represents a integer with or without commas // If the integer has at least one comma, the entire integer has commas in groups of 3. // Supports negative numbers as long as no padding with spaces. // All strings containing non numeric digits, hyphen in more than just the first position, or commas // in the incorrect spots will return false. // TODO: Student will implement in functions.cpp bool isInteger(const std::string&); //Precondition: Not any //Postcondition: The value in x is copies to y and y gets the former value of x // TODO: Student will implement in functions.cpp void swapNumbers(long& x, long& y); //Precondition: Passed string would pass the isInteger test //Postcondition: Returns the integer represented by the string // TODO: Student will implement in functions.cpp long long stringToLong(const std::string&); //Precondition: nums is an array of count size. //Postcondition: Returnes the calculated standard deviation of all the numbers in the array // Help: https://www.mathsisfun.com/data/standard-deviation.html // TODO: Student will implement in functions.cpp double standardDeviation(const double nums[], unsigned int count); //Precondition: nums is an array of count size. //Postcondition: Prints out the array in a format simular to the way Python outputs a list // If the array was 1, 4, 6, it would output // [1, 4, 6] // Without any newlines or spaces before and after the brackets! // The numbers will output with fixed precision set to the passed precision value // TODO: Student will implement in functions.cpp void printArray(const double nums[], unsigned int count, unsigned short precision=3 ); #endif //LAB_FUNCTIONS_H

main.cpp

#include  #include  #include  #include  #include "functions.h" using namespace std; int main() { //Seed the random number generator based on number of seconds since 1970 srand(time(0)); const long MIN=-10000, MAX=10000; long a=123, b=423; //Testing of swap cout << "(" << a << ", " << b << ")" << endl; swapNumbers(a, b); cout << "(" << a << ", " << b << ")" << endl; //Standard deviation test of the mathisfun example double stddevExample[] = {600, 470, 170, 430, 300}; cout << "Standard Deviation of "; printArray(stddevExample, 5, 1); cout << ": " << standardDeviation(stddevExample, 5) << endl; //Random std dev test const unsigned long sd2Size = 10; double sd2[sd2Size]; fillArrayWithRandoms(sd2, sd2Size, MIN, MAX); cout << "Standard Deviation of "; printArray(sd2, sd2Size); cout << ": " << standardDeviation(sd2, sd2Size) << endl; //Test of isInteger cout << "123,000 isInteger: " << boolalpha << isInteger("123,000") << endl; //true cout << "123000 isInteger: " << boolalpha << isInteger("123000") << endl; //true cout << "122,123,000 isInteger: " << boolalpha << isInteger("122,123,000") << endl; //true cout << "122,123000 isInteger: " << boolalpha << isInteger("122,123000") << endl; //false cout << "122123,000 isInteger: " << boolalpha << isInteger("122123,000") << endl; //false cout << "2apple isInteger: " << boolalpha << isInteger("2apple") << endl; //false cout << "-123,000 isInteger: " << boolalpha << isInteger("-123,000") << endl; //true cout << "-123000 isInteger: " << boolalpha << isInteger("-123000") << endl; //true cout << "-123-000 isInteger: " << boolalpha << isInteger("-123-000") << endl; //false cout << "12,300 isInteger: " << boolalpha << isInteger("12,300") << endl; //true cout << "-12,300 isInteger: " << boolalpha << isInteger("-12,300") << endl; //true //all of these should work cout << "123000 stringToLong: " << stringToLong("123000") << endl; cout << "123,000 stringToLong: " << stringToLong("123,000") << endl; cout << "12345 stringToLong: " << stringToLong("12345") << endl; cout << "12,345 stringToLong: " << stringToLong("12,345") << endl; cout << "2345 stringToLong: " << stringToLong("2345") << endl; cout << "2,345 stringToLong: " << stringToLong("2,345") << endl; cout << "345 stringToLong: " << stringToLong("345") << endl; cout << "-123000 stringToLong: " << stringToLong("-123000") << endl; cout << "-123,000 stringToLong: " << stringToLong("-123,000") << endl; cout << "-12345 stringToLong: " << stringToLong("-12345") << endl; cout << "-12,345 stringToLong: " << stringToLong("-12,345") << endl; cout << "-2345 stringToLong: " << stringToLong("-2345") << endl; cout << "-2,345 stringToLong: " << stringToLong("-2,345") << endl; cout << "-345 stringToLong: " << stringToLong("-345") << endl; cout << "9,223,372,036,854,175,807 stringToLong: " << stringToLong("9,223,372,036,854,175,807") << endl; cout << "-9,223,372,036,854,175,807 stringToLong: " << stringToLong("-9,223,372,036,854,175,807") << endl; return 0; } long long longPow (unsigned long long base, unsigned long exponent) { long long result = 1; for ( unsigned long i = 0 ; i < exponent ; ++i ) result *= base; return result; } void fillArrayWithRandoms(double nums[], unsigned int count, long min, long max) { const long precision = 1000; for ( unsigned long i = 0 ; i < count ; ++i ) { nums[i] = (rand() % (max-(min*precision)) + min * precision)/static_cast(precision); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!