Writing programs using functions

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.
1 test code is sometimes called a driver. Generally, what this means, is make "main" call your function and print out the result so you 'test' it by running it a few times on a few values.

this one is so basic Ill do it for you against policy (how did you manage to not write a function here, and get #2 and 3 correct? )

int addone(int k)
{
return k+1;
}
int main()
{
cout << addone(2) << endl;
}


2) looks good, but you can do x+=1; this is shorthand for x=x+1; Its not really better, its just alternative to save typing. You will see this a lot, so learn it.

3) also looking good, but you don't need else. It works, but its not needed. Do you see why? Its generally considered better looking to not put the if and the statement after it all on one line.

4) ... you've done very well so far. keep going! If you really can't do it, youll get help here.



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
19
#include <iostream>
#include <cassert>

using namespace std;

int AddOne(int num)
{
  return num + 1;
}

int main()
{
  int num = 1;
  int res = AddOne(num);
  assert(res == num + 1);
  cout << "Result = " << res << " as expected\n\n";
  system("pause"); // remove if not using Visual Studio
  return 0;
}
Topic archived. No new replies allowed.