My compsci professor is very kind, however... he is not the best professor and many of us are struggling with the concepts, seeing as we went from visual basic (where everything was basically handed to us) to C++. I am trying very hard to learn on my own but I am falling behind. I would appreciate some help! I am including the code I have been working on with each question.
1. Implement a function that returns an integer that is one greater than the integer passed into it.
Write test code with assertions to verify that your function is correctly implemented.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <iostream>
#include <cassert>
using namespace std;
int k;
int addOne; // adds one to a number
int main () {
cout << "Enter an integer." << endl;
cin >> k;
int addOne(int k);
cout << addOne << endl;
return 0;
}
|
I don't exactly know what he means by "test code".
2. Implement a function that increases the value of an integer passed into it by 1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
#include <iostream>
#include <cassert>
using namespace std;
// creates a function for addOne that will
void addOne(int & x) {
x = (x + 1);
}
int main() {
int x;
cout << "Input an integer.\n";
cin >> x;
//Calls the function addOne(x).
addOne(x);
//Displays the value the user inputted added by one.
assert (==(x+1));
cout << x << endl;
return 0;
}
|
3. Write a function named isEven that takes an integer argument n and returns a boolean value. Implement the function so that it returns true when n is even and false when n is odd.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <iostream>
#include <cassert>
using namespace std;
//Creates Function for isEven
bool isEven(int n) {
if (n%2 == 0) return true;
else return false;
}
int main()
{
int n;
cout << "Input an integer to declare if it is Even or Odd.\n";
cin >> n;
//States that if the integer has a remainder of 0, it is true
bool (isEven(n));
//Confirms that the program is running correctly
n;
}
|
4. Write a program that prompts the user for a first name and then prompt the user for a last name. Have the program display the user's initials. A person's initials is the first letter of their first name, followed by a period. For example, the initials for David Turner are D.T.
As part of your solution, define a function named initials, which takes 2 arguments: the first name and the last name. The function returns the person's initials as a string.
Because the function arguments don't need to be changed and are complex data types, you should pass them into the function as const references.
Hint: use the substr function or the index operator of the string data type.
5. Write a program that prompts the user for a lower bound and an upper bound. After the user inputs these values, the program should call your randomInteger function to get a random integer between the lower and upper bounds. The program displays this value to the user and then terminates.
Implement a function named randomInteger that takes as arguments a lower bound a and an upper bound b of an interval [a, b]. The function computes and returns a random integer that falls inside this interval.
For 4 and 5, I have no idea where to start. If any of you can let me know how, it would be greatly appreciated.