Term
myFile.dat contains a list of integers. Write a program that will read from myFile.dat and output to the file out.dat the positive integers. |
|
Definition
#include
using namespace std;
int main() { ifstream infile; ofstream outfile; infile.open("myFile.dat"); outfile.open("out.dat"); int number; while (infile >> number) { if (number > 0) outfile << number << endl; }
infile.close(); outfile.close(); return 0; } |
|
|
Term
The following is the definition for a C++ function.
int calc(int c, int b, int a) { int d; d=a-(c/b)%a; return d; }
What value is assigned to y in the following statement:
y=calc(10,2,3); |
|
Definition
|
|
Term
Write a C++ function definition that receives two integer values, returns the greater of the product or sum of the two values. |
|
Definition
int greater (int num1, int num2) { int sum, product;
sum = num1 + num2 product = num1 * num2;
if (product > sum) return product; else return sum; } |
|
|
Term
Write a C++ function definition that receives two integer values, num1 and num2, divides num1 by num2 (if num2 is not zero), and returns the integer portion of the quotient and the remainder of the quotient. If num2 is zero, return -1 for the quotient and remainder. |
|
Definition
void divide(int num1, int num2, int ", int &rem) { if (num2 == 0) { quot = -1; rem = -1; } else { quot = num1 / num2; rem = num1 % num2; } } |
|
|
Term
Write a function definition named getit that prompts a user to input from the keyboard the length and width of a rectangle as doubles and passes the length and width back to main. |
|
Definition
void getit(double &length, double &width) { cout << "Enter length and width" << endl; cin >> length >> width; } |
|
|
Term
Write a function definition named swapit that is passed two integer numbers and exchanges the two numbers when they are passed back. |
|
Definition
void swapit(int &num1, int &num2) { int temp; temp = num1; num1 = num2; num2 = temp; } |
|
|
Term
Given the following C++ segment, what will be displayed?
void calc (int &, int &, int);
int main() { int a = 4, b = 3, c = 2; calc(c, b, a); cout << a << b << c << endl; return 0; }
void calc (int &x, int &y, int z) { x = y + z; y = x + z; z = x * y; cout << x << y << z << endl; } |
|
Definition
Local variables in main: a = 4, b = 3, c = 2 Parameters for calc: x is an alias for c, y is an alias for b, z is a copy of a. x = y + z; // y is 3, z is 4, sets x and c to 3 + 4 = 7 y = x + z; // x is 7, z is 4, sets y and b to 7 + 4 = 11 z = x * y; // x is 7, y is 11, sets z to 7 * 11 = 77 cout << x << y << z << endl; // outputs 71177 cout << a << b << c << endl; // outputs 4117 |
|
|
Term
Please check all that applies. Which of the following would be considered legal overloaded functions of the function prototype void calc (int x);
____ void calculate (int x); ____ void calc (int y); ____ void calc (int x, int y); ____ int calc (int x); ____ void calc (double x); |
|
Definition
Original prototype: void calc (int x); ____ void calculate (int x); // different name ____ void calc (int y); // same signature _X_ void calc (int x, int y); // different # of params ____ int calc (int x); // same signature _X_ void calc (double x); // different type param |
|
|
Term
Given the following function prototype indicate if the following function calls are legal. If the call is not legal, briefly explain why it is not legal.
// Function prototype void fone(int& , int& , int =0);
// Function calls (assume n, p and r are ints) m = fone (n, p, r); _______________________________ fone (n, p, r); ___________________________________ fone (n, r); ______________________________________ fone (p, r, 2); ___________________________________ fone (2, p, r); ___________________________________ |
|
Definition
// Function prototype void fone (int &, int &, int = 0);
// Function calls (assume n, p and r are ints) m = fone(n, p, r); not legal - fone is void function fone(n, p, r); legal________________________________ fone(n, r); legal___________________________________ fone(p, r, 2); legal________________________________ fone(2, p, r); not legal - 2 is not an lvalue_______ |
|
|
Term
Declare an array of 100 integers and set the value of each element equal to the index. |
|
Definition
const int SIZE = 100; int arr[SIZE];
for (int i = 0; i < SIZE; i++) arr[i] = i; |
|
|
Term
Declare an array with 10 columns and 20 rows with integers and set each element equal to 15 using nested for loops only. |
|
Definition
const int ROWS = 20, COLS = 10; int nums[ROWS][COLS]; for (int i = 0; i < ROWS; i++) for (j = 0 ; j < COLS; j++) nums[i][j] = 15; |
|
|
Term
Write a function definition that is passed an array of integers and the number of elements in the array and returns the smallest value in the array. |
|
Definition
int smallest (int arr[], int size) { int min = arr[0]; for (int i = 1; i < size; i ++) if (arr[i] < min) min = arr[i]; return min; } |
|
|
Term
Write a C++ program that will read a file called in.dat that contains integers (one number per record). There are exactly 100 records. Store the nonzero integers in an array and output to the terminal the number of integers stored |
|
Definition
#include
#include
using namespace std;
int main()
{
ifstream inputFile;
const int SIZE = 100;
int nums[SIZE];
int readnum, count = 0;
inputFile.open("in.dat");
// read integers from file
for (int i = 0; i < SIZE; i++)
{
inputFile >> readnum;
// only store nonzero integers in array
if (readnum != 0)
{
nums[i] = readnum;
count++; // counts nonzero integers
}
}
cout << "The number of nonzero integers = ";
cout << count << endl;
inputFile.close();
return 0;
} |
|
|
Term
Write a C++ program that will read a file called in.dat that contains integers (one number per record). There may be up to 100 records. Store all of integers in an array and call a function that will output to the terminal the integers in reverse order when the array and the number of elements are passed to the function. |
|
Definition
#include
#include
using namespace std;
// function prototype
void displayReverse(int[], int);
int main()
{
ifstream inputFile;
const int SIZE = 100;
int nums[SIZE];
int count = 0;
inputFile.open("in.dat");
// read numbers from file into array
while(inputFile >> nums[count])
count++;
// display numbers in reverse order
displayReverse(nums, count);
inputFile.close();
return 0;
}
void displayReverse(int nums[], int size)
{
// go from last element to first
for (int i = size - 1; i >= 0; i--)
cout << nums[i] << endl;
} |
|
|
Term
Given the following array of integers:
10 20 30 40 50 60 70 80 90
and a search value of 78, give the first, last and middle index after each pass of the binary search algorithm. |
|
Definition
Array: 10 20 30 40 50 60 70 80 90 Search value: 78
Pass First Last Middle 1 0 8 4 (50 < 78, so search 2nd half) 2 5 8 6 (70 < 78, so search 2nd half) 3 7 8 7 (80 > 78, so search 1st half)
stops at Pass 4 because last = first = 7 |
|
|
Term
Given the following array of integers:
45 35 65 87 22 61
give the result of each pass using a. Selection sort b. Bubble sort |
|
Definition
Array: 45 35 65 87 22 61
a. Selection Sort
Pass 1: 22 35 65 87 45 61 Pass 2: 22 35 65 87 45 61 Pass 3: 22 35 45 87 65 61 Pass 4: 22 35 45 61 65 87 Pass 5: 22 35 45 61 65 87
b. Bubble Sort
Pass 1: 35 45 65 22 61 87 Pass 2: 35 45 22 61 65 87 Pass 3: 35 22 45 61 65 87 Pass 4: 22 35 45 61 65 87 Pass 5: 22 35 45 61 65 87 |
|
|
Term
What output would the following C++ program produce?
#include
int main()
{
int a[5];
int sum=0;
for(int j = 0; j < 5; ++j)
a[j] = 2 * j - 3;
int i = 0;
while(i < 4)
{
sum += a[i];
if(a[i] > 0)
cout << i << 2 * a[i];
i++;
}
cout << sum;
return 0;
} |
|
Definition
For loop: a[0] = -3 a[1] = -1 a[2] = 1 a[3] = 3 a[4] = 5
While loop: i = 0, a[i] = -3, sum = -3, a[i] <= 0 i = 1, a[i] = -1, sum = -4, a[i] <= 0 i = 2, a[i] = 1, sum = -3, a[i] > 0, output: 22 i = 3, a[i] = 3, sum = 0, a[i] > 0, output: 36 i = 4, (exit loop)
Output after loop: 0 Complete output: 22360 |
|
|
Term
|
Definition
a collection of a fixed number of components all of the same data type |
|
|