Concept explainers
Each of the following declarations and program segments has errors. Locate as many as you can.
A) int ptr*;
B) int x, *ptr;
&x = ptr;
C) int x, *ptr;
*ptr = &x;
D) int x, *ptr;
ptr = &x;
ptr =100; // Store 100 in x
cout << x << endl;
E) int numbers[] = {10, 20, 30, 40, 50};
cout << "The third element in the array is ";
cout << *numbers + 3 << endl;
F) int values[20], *iptr;
iptr = values;
iptr *= 2;
G) double level;
int dPtr = &level;
H) int *iptr = &ivalue;
int ivalue;
I) void doubleVal (int val)
{
*val *= 2;
}
J) int *pint;
new pint;
K) int *pint;
pint = new int;
pint = 100;
L) int *pint;
pint = new int[100]; // Allocate memory
.
.
Process the array
.
.
delete pint; // Free memory
delete pint; // Free memory
M) int *getNum()
{
int wholeNum;
cout << "Enter a number: ";
cin >> wholeNum;
return SwholeNum;
}
N) unique_ptr u = new unique_ptr(new int);
O) unique_ptr<int> u = make_unique<int>();
unique_ptr<int> v;
v = u;
P) unique_ptr<int>u(new int);
unique_ptr<int>v;
v.reset(u);
Want to see the full answer?
Check out a sample textbook solutionChapter 10 Solutions
Starting Out with C++: Early Objects (9th Edition)
Additional Engineering Textbook Solutions
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Programming in C
Computer Science: An Overview (12th Edition)
Computer Science: An Overview (13th Edition) (What's New in Computer Science)
Software Engineering (10th Edition)
Starting Out with Programming Logic and Design (4th Edition)
- Q 6:- Specify the local and Global variables used in the code below and express the reason r'. #include int a=10,b; void main() { printf("a = 8d and b=%d“,a,b) ; #include void main() { int x-23, y-4; printf("x = %d and y=sd“,x,y);arrow_forwardFilling the Pool (Deprecated) Write a program that calculates the time neccessary to completely fill an empty pool with water. We will assume that the pool is rectangular and the depth is uniform. All input values in this program will be integers. Prompt the user to enter the pool dimensions - length, width, and depth (unit: feet) - as well as the rate at which water can be put into the pool (unit: gallons per minute). Using these values, calculate and display the time (in minutes) needed to fill the pool from completely empty to completely full. Note: you should calculate the volume of the pool (as cubic feet) and then determine the rate (in cubic feet per minute) that water can be put into the pool. Assume that there are 7.48 gallons in one cubic foot. Your program should run like the examples shown below: Enter pool dimensions Length: 10 Width: 8 Depth: 7 Water entry rate: 14 The pool will fill completely in 299.2 minutesarrow_forwardI assume the following 2 codes express the same meaning? int myvar; int * myptr = &myvar; and int myvar; int * myptr; myptr = &myvar;arrow_forward
- Number guessing Game Write a C program that implements the “guess my number” game. The computer chooses a random number using the following random generator function srand(time(NULL)); int r = rand() % 100 + 1; that creates a random number between 1 and 100 and puts it in the variable r. (Note that you have to include <time.h>) Then it asks the user to make a guess. Each time the user makes a guess, the program tells the user if the entered number is larger or smaller than its number. The user then keeps guessing till he/she finds the number. If the user doesn’t find the number after 10 guesses, a proper game over message will be shown and the actual guess is revealed. If the user makes a correct guess in its allowed 10 guesses, then a proper message will be shown and the number of guesses the user made to get the correct answer is also printed. After each correct guess or game over, the user decides to play again or quit and based on the user choice, the computer will make…arrow_forwardWhen you perform arithmetic operations with operands of different types, such as adding an int and a float, ____________. C# chooses a unifying type for the result you must choose a unifying type for the result you must provide a cast you receive an error messagearrow_forwardMark the following statements as true or false: a. To use a predefined function in a program, you need to know only the name of the function and how to use it. (1) b. A value-returning function returns only one value. (2, 3) c. Parameters allow you to use different values each time the function is called. (2, 7, 9) d. When a return statement executes in a user-defined function, the function immediately exits. (3, 4) e. A value-returning function returns only integer values. (4) f. A variable name cannot be passed to a value parameter. (3, 6) g. If a C++ function does not use parameters, parentheses around the empty parameter list are still required. (2, 3, 6) h. In C + + , the names of the corresponding formal and actual parameters must be the same. (3, 4, 6) i. A function that changes the value of a reference parameter also changes the value of the actual parameter. (7) j. Whenever the value of a reference parameter changes, the value of the actual parameter changes. (7) k. In C++, function definitions can be nested; that is, the definition of one function can be enclosed in the body of another function. (9) l. Using global variables in a program is a better programming style than using local variables, because extra variables can be avoided. (10) m. In a program, global constants are as dangerous as global variables. (10) n. The memory for a static variable remains allocated between function calls. (11)arrow_forward
- Coin Toss Write a function named coinToss that simulates the tossing of a coin. When you call the function, it should generate a random number in the range of 1 through 2. If the random number is 1, the function should display "heads". If the random number is 2, the function should display "tails". Demonstrate the function in a program that sks the user how many times the coin should be tossed and the coin then simulates the tossing of the coin that number of times.arrow_forwardEach of the following definitions and program segments has errors. Locate as many as you can and correct the errors. a) void showValues (int nums) { for (int i = 0; i<8; i++) cout<arrow_forwardFunctions With Parameters and No Return Values Quiz by CodeChum Admin Create a program that accepts an integer N, and pass it to the function generatePattern. generatePattern() function which has the following description: Return type - void Parameter - integer n This function prints a right triangular pattern of letter 'T' based on the value of n. The top of the triangle starts with 1 and increments by one down on the next line until the integer n. For each row of in printing the right triangle, print "T" for n times. In the main function, call the generatePattern() function. Input 1. One line containing an integer Output Enter·N:·4 T TT TTT TTTTarrow_forwardLook at the following code. int x = 7; int *ptr = &x;What will be displayed if you send the expression *iptr to cout? What happens if you send the expression ptr to cout?arrow_forwardVoid functions do not return any value when they are called.arrow_forward#include #include #include #include #include using namespace std; int main (){ const double PI = 3.14159265359; return 0; }arrow_forwardarrow_back_iosSEE MORE QUESTIONSarrow_forward_ios
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,