Purchase Solution

C++ programming and conceptual questions

Not what you're looking for?

Ask Custom Question

12. Write a program that reads in an integer from the keyboard and displays its value doubled, using two functions that you write. The reading function should be int and the displaying function should be void.

13. How does the use of inline speed up the execution of a program? What is the disadvantage of using inline?

14. What is the output produced by the following program? Explain.
#include <iostream>
using namespace std;
void testfunc(int x, int y = 10) {
cout << x + y;
}
int main () {
testfunc(3);
return 0;
}

15. What is the output of the following program? Explain.
#include <iostream>
using namespace std;
void f(int i, int &j) {
i = 5;
j = j + i;
cout << "f: i = " << i << endl;
cout << "f: j = " << j << endl;
}
int main () {
int i = 15;
int j = 30;
f(i, j);
cout << " main: i = " << i << endl;
cout << " main: j = " << j << endl;
}

16. Write a program that reads in two integers from the keyboard and displays their sum. The reading function should read in both integers at the same time. Both the reading and displaying functions should be separate void functions.

17. What is the output of the following program? Explain.
#include <iostream>
using namespace std;
float someNum = 3.0;
int main () {
int someNum = 2;
cout << someNum << endl;
cout << ::someNum << endl;
return 0;
}

18. When two or more functions have the same name, how does the compiler determine which one to use for a particular function call?

19. Write a void function called swap that takes two integer parameters and swaps their contents. Write another swap function that takes two double arguments. Write a short main function that calls both swap functions and demonstrates that the contents have been swapped. Cout statements should only appear in function main.

20. Why do we have both debug and release builds?

21. What is the difference between step over, and step into?

22. Find and describe the errors in the following program. Use the debugger.
//this program computes how much money will
//accumulate after so many years of investing
#include <iostream>
using namespace std;
double Balance = 0.0;
double Interest;
double YearlyCont;
int NumYears;
//this function computes one year of investment
double newBalance(double balance);
int main () {
cout << "How much money will you deposit"
<< "each year?" << endl;
cin >> YearlyCont;
cout << "What interest rate will you get"
<<"(enter 5% as .05)?" << endl;
cin >> Interest;
cout << "How many years will you invest?"
<< endl;
cin >> NumYears;
for(int i = 1; i <= NumYears; i++);
Balance = newBalance(Balance);
cout << "Money at end of investment: "
<< Balance << endl;
return 0;
}
double newBalance(double balance) {
//add in this year's deposit
balance = balance + YearlyCont;
//add in this year's interest
balance = (Interest + 1.0) * Balance;
return balance;
}

Purchase this Solution

Solution Summary

C++ programming and conceptual questions are examined.

Solution Preview

Please find the solution in the attached file.

12. Write a program that reads in an integer from the keyboard and displays its value doubled, using two functions that you write. The reading function should be int and the displaying function should be void.
SOLUTION 12
#include<iostream.h>
#include<conio.h>
int read()
{
int n;
cout<<"nEnter an integer";
cin>>n;
return n;
}

void display(int value)
{
cout<<"nnNumber entered by user is "<<value;
cout<<"nnAfter doubling the value number becomes "<<2*value;
}

int main()
{
int number=read();
display(number);
getch();
}

13. How does the use of inline speed up the execution of a program? What is the disadvantage of using inline?
SOLUTION 13
The use of inline speeds up the execution of a program. Instead of calling the function every time it is invoked, the compiler replaces the function call with a copy of the function body. If it's a small function which gets called a lot, this can sometimes speed things up.
Disadvantage of using inline
Since the compiler will copy the entire function body every time the function is called, if it is a large function (more than three or four lines), inlining can increase the size of your executable program significantly.

14. What is the output produced by the following program? Explain.
#include <iostream>
using namespace std;
void testfunc(int x, int y = 10) {
cout << x + y;
}
int main () {
testfunc(3);
return 0;
}
SOLUTION 14
OUTPUT is 13
Function testfunc(int x, int y = 10) has two parameters "x" and "y" out of which "y" is a default parameter whose value is 10 .
A default parameter is a function parameter that has a default value provided to it. If the user does not supply a value for this parameter, the default value will be used. If the user does supply a value for the default parameter, the user-supplied value is used.
In main as testfunc is called with only one argument value '3', thus that value is assigned to "x" and "y" takes the default value i.e. 10.
Therefore, x + y = 3 + 10 = 13

15. What is the output of the following program? Explain.
#include <iostream>
using namespace std;
void f(int i, int &j) {
i = 5;
j = j + i;
cout << "f: i = " << i << endl;
cout << "f: j = " << j << endl;
}
int main () {
int i = 15;
int j = 30;
f(i, j);
cout << " main: i = " << i << endl;
cout << " main: j = " << j << endl;
}

SOLUTION 15
f: i = 5
f: j = 35
main: i = 15
main: j = 35
Here in the above given program, from the main function, i is passed "by-value" and j is passed "by-reference" to the function f(int i,int &j).
In the main function, the initial values of i and j are 15 and 30 respectively at the time of calling function f(i,j).Now in the function f(i,j), i is initialized to 5 and j = j+ i which is equal to 30+5 = 35. Therefore, the values printed in the ...

Purchase this Solution


Free BrainMass Quizzes
C# variables and classes

This quiz contains questions about C# classes and variables.

Basic Networking Questions

This quiz consists of some basic networking questions.

Javscript Basics

Quiz on basics of javascript programming language.

Word 2010: Tables

Have you never worked with Tables in Word 2010? Maybe it has been a while since you have used a Table in Word and you need to brush up on your skills. Several keywords and popular options are discussed as you go through this quiz.

C++ Operators

This quiz tests a student's knowledge about C++ operators.