MPL chap 8-10

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Assume the variable diff has already been declared . Write an expression whose value is the address of diff

&diff

Write an expression that retrieves the value in the location immediately after the location pointed to by the integer pointer variable ip

*(ip + 1)

Assume that ip has been declared to be a pointer to int and that result has been declared to be an array of 100 elements . Assume further that ip has been initialized to point to an element in the first half of the array . Write an expression whose value is the element in the array after the element that ip points to

*(ip +1)

Assume that ip has been declared to be a pointer to int and that result has been declared to be an array of 100 elements . Assume further that ip has been initialized to point to an element in the first half of the array . Write a statement that makes ip point to the next element in the array

*(ip++);

Assume that ip has been declared to be a pointer to int and that result has been declared to be an array of 100 elements . Assume further that ip has been initialized to point to an element in the first half of the array . Write an expression whose value is the sum of the element that ip points to plus the next two elements

*ip + *(ip + 1) + (*ip + 2)

Write an expression that evaluates to 1 more than the value in the location pointed to by the integer pointer variable ip. Thus, if ip points to a location containing the value 3, the expression should evaluate to 4

*ip + 1

Assume that ip1, ip2, and ip3 have already been declared to be of type "pointer to int ". Assume further that each of these pointer variables have been initialized -- each points to some int variable . Write a statement that computes the sum of the variables that ip1 and ip2 point to, and assigns that value (the sum ) to the variable that ip3 points to

*ip3=*ip1+*ip2;

Assume that strikeCounter has already been declared to be a "pointer to int ". Assume further that strikeCounter has been initialized -- its value is the address of some int variable . Write an expression whose value is four (4) times the value of the variable that strikeCounter is pointing to

*strikeCounter * 4

Assume that strikeCounter has already been declared to be a "pointer to int ". Assume further that strikeCounter has been initialized -- its value is the address of some int variable . Write a statement that adds 22 to the value of the variable that strikeCounter is pointing to

*strikeCounter +=22;

Assume that strikeCounter has already been declared to be a "pointer to int ". Assume further that strikeCounter has been initialized -- its value is the address of some int variable . Write a statement that assigns the value 27 to the variable that strikeCounter is pointing to

*strikeCounter = 27;

Write an expression that evaluates to true if and only if the C-string s equals the C-string "end".

strcmp(s, "end") == 0

Write an expression that evaluates to true if and only if the C-string s does not equal "end"

strcmp(s,"end")!=0

Write an expression that evaluates to true if the value of the C-string s1 is greater than the value of C-string s2.

strcmp(s1, s2) > 0

Given the string variable address, write an expression that returns the position of the first occurrence of the string "Avenue" in address

address.find("Avenue")

The variables arr1 and arr2 have been declared as pointers to integers . An array of 10 elements has been allocated, its pointer assigned to arr1, and the elements initialized to some values . Allocate an array of 20 elements , assign its pointer to arr2, copy the 10 elements from arr1 to the first 10 elements of arr2, and initialize the remainder of the elements of arr2 to 0.

arr2=new int[20]; for (int i=0;i<10;i++) { arr2[i]=arr1[i]; }

Declare a variable , bp, as a pointer to bool, dynamically allocate memory for a single boolean value , assign the resulting pointer to bp and initialize the value to true

bool* bp; bp=new bool(true);

Declare a variable cp that can be assigned the address of an char variable . In other words, declare cp to be of type "pointer to char "

char *cp

Write a function max that has two C string parameters and returns the larger of the two

char *max(char *a, char *b) { return strcmp(a,b)>0? a: b; }

Write a function min that has three C string parameters and returns the smallest

char *min(char *a, char *b, char *c) { char *min1 = a; if (strcmp(b,min1)<0) min1 = b; if (strcmp(c,min1)<0) min1 = c; return min1; }

In molecular biology the "alphabet" of genes consists of four chemicals (called nucleotides) represented by the letters A C G T. A triad is a sequence of three nucleotides (for example AGA) and specifies an amino acid, a building block for proteins. A gene consists of a very, very long sequence of A-C-G-T combinations. Assume the input consists of a long sequence of A-C-G-T letters representing a gene. Write the code necessary to skip over the first 7 letters and then read the next 4 triads, printing each out on its own line

char acid[4]; cin.get(acid, 7); cin.ignore(); for(int i = 0; i < 4; i++) { cin.get(acid, 4); for(int j = 0; j < 3; j++) cout << acid[j]; cout << endl; }

Declare a two-dimensional character array named "gasgiants" that is capable of storing the names of the gas giant planets of our solar system: jupiter, saturn, uranus, and neptune. Do not make the array any larger than it needs to be

char gasgiants[4][8];

Declare a char array named line suitable for storing C-strings as large as 50 characters , and write a statement that reads in the next line of standard input into this array . (Assume no line of input is 50 or more characters .)

char line[50]; cin.getline(line,50);

Declare and initialize a two-dimensional character array named "stooges" to store the names "moe", "larry" and "curly" (in that order). Do not make the array any larger than it needs to be

char stooges[3][6] = {"moe", "larry", "curly"};

Given a char variable c that has already been declared , write some code that repeatedly reads a value from standard input into c until at last a 'Y' or 'y' or 'N' or 'n' has been entered

cin >> c; while (c != 'y' && c != 'Y' && c != 'N' && c != 'n') cin >> c;

Assume that an int variable counter has already been declared . Assume further a variable counterPointer of type "pointer to int " has also already been declared . Write a statement that makes counterPointer "point" to counter.

counterPointer=&counter;

Assume that an int variable diff has already been declared . Assume further a variable diffPointer of type "pointer to int " has also already been declared . Write a statement that assigns the address of diff to diffPointer

diffPointer = &diff;

divide is a function that takes four arguments and returns no value . The first two arguments are of type int . The last two arguments arguments are pointers to int that are set by the function to the quotient and remainder of dividing the first argument by the second argument . The function does not return a value . x and y are two int variables that have been declared and initialized . q and r are two int variables that have been declared . Write a statement that sets the value of q to the quotient of x divided by y, and sets the value of r to the remainder of x divided by y, by calling divide

divide(x, y, &q, &r);

Declare a variable dp that can be assigned the address of a double variable . In other words, declare dp to be of type "pointer to double "

double *dp

The variable dp is to refer to an array of double . Assuming the integer variable n has been assigned a value , declare dp appropriately, allocate an array of n doubles and assign the resulting pointer to dp.

double *dp; dp = new double[n];

doubleIt is a function that takes one argument and returns no value . The argument is a pointer to int . The function doubles the value that the argument points to and stores it back. savings is an int variable that has been declared and initialized . Write a statement that doubles the value stored in savings by invoking the function doubleIt. For example, if the value in savings was 7 before your statement , after your statement its value would be 14

doubleIt(&savings);

Assume that word is a variable of type string that has been assigned a value . Assume furthermore that this value always contains the letters "dr" followed by at least two other letters. For example: "undramatic", "dreck", "android", "no-drip". Assume that there is another variable declared , drWord, also of type string . Write the statements needed so that the 4-character substring word of the value of word starting with "dr" is assigned to drWord. So, if the value of word were "George slew the dragon" your code would assign the value "drag" to drWord

drWord = word.substr(word.find("dr"), 4);

Given the string variables name1 and name2, write a fragment of code that assigns the larger of the two to the variable first (assume all three are already declared and name1 and name2 assigned values )

first = name1; if (strcmp(name2,first)>0) first = name2;

Assume that sentence is a variable of type string that has been assigned a value . Assume furthermore that this value is a string consisting of words separated by single space characters with a period at the end. For example: "This is a possible value of sentence." Assume that there is another variable declared , firstWord, also of type string . Write the statements needed so that the first word of the value of sentence is assigned to firstWord. So, if the value of sentence were "Broccoli is delicious." your code would assign the value "Broccoli" to firstWord

firstWord = sentence.substr(0,sentence.find(" "));

Write a fragment of code that reads in integers from standard input, until end-of-file and sends their (floating point ) average to standard output . If no numbers are input, the message "no values to average" is printed instead (while loops , end-of-file processing, mixed-mode arithmetic, basic conditional )

float number=-1; float count=0; float sum=0; while(cin.eof( )==false) { cin >> number; sum += number; count++; } if(number==-1) { cout << "no values to average"; } else{ if (count>0) cout << sum/count; }

A U.S. social security number consists of a string of 9 digits, such as "444422333". Assume that input consists of a sequence of 9-digit social security numbers with no intervening spaces or separators of any kind: 111223333444422333123456789987654321... Assume that a char array named ssn suitable for storing a social security number as a C-string has been declared . Use this array to read in 3 successive social security numbers and print each one out on a line by itself

for (int count = 0; count < 3; count++) { cin.get(ssn, 10); cout << ssn << endl; }

The variable ip_arr has been declared as an array of 20 pointers to integer (i.e., each element of ip_arr is a pointer to an integer ). Allocate 20 integer values and assign their pointers to the elements of ip_arr

for (int i = 0; i < 20; i++) ip_arr[i] = new int;

The variable cp_arr has been declared as an array of 26 pointers to char . Allocate 26 character values , initialized to the letters 'A' through 'Z' and assign their pointers to the elements of cp_arr (in that order) Hint: Capital 'A' in ascii is 65

for (int i = 0; i < 26; i++) { cp_arr[i] = new char('65' + i); }

Assume that scotus is a two-dimensional character array that stores the family names (last names ) of the nine justices on the Supreme Court of the United States. Assume no name is longer than twenty characters . Assume that scotus has been initialized in alphabetical order. Write some code that will print each to standard output , each on a separate line with no other characters

for (int i=0; i<9; i++) cout << scotus[i] << endl;

Assume that given , middle and family are three variables of type string that have been assigned values . Write an expression whose value is a string consisting of the first character of given followed by a period followed by the first character of middle followed by a period followed by the first character of family followed by a period: in other words, the initials of the name . So if the values of these three variables were "John" "Fitzgerald" "Kennedy", then the expression 's value would be "J.F.K."

given.substr(0,1)+"." +middle.substr(0,1)+ "."+family.substr(0,1)+ "."

You are given an array x of int elements along with an int variable n that contains the number of elements in the array . There are no duplicates in this array . You are also given an int variable m that has been declared . Assign to m the median value of the array . NOTE: The median of a set of numbers is the number in the set such that there are as many numbers above that value as there are below. If the set has an even number of members, the median is the average of the pair of numbers such that there are as many numbers above the pair as there are below. EXAMPLE 1: Given 5 8 1 9 7 the median is 7 because there are two numbers below (1 5) and two numbers above (8 9). EXAMPLE 2: Given 3 7 1 4 6 9 the median is the average of 4 and 6 because there are two numbers above 4 and 6 (7 9) and two numbers below (3 1).

if ((n&1)==0) m = (x [n/2-1] + x[n/2])/2; else m=x[n/2];

Declare a variable ip that can be assigned the address of an int variable . In other words, declare ip to be of type "pointer to int "

int *ip

The integer variables first and last have been declared and assigned values (with last >= first). Allocate an integer array that can hold the squares of the numbers between first and last (inclusively), assign the resulting pointer to the variable squares (which you must declare ), and initialize the array to the squares of the numbers from first to last (in that order)

int *squares = new int[last-first+1]; for (int i = first; i <= last; i++) squares[i-first] = i * i;

The variables xp and yp have both been declared as pointers to integers , and have been assigned values (i.e., they are each pointing to an integer value ). Write the code to exchange the values of these two variables (so that after the swap xp points to what yp originally pointed to and vice-versa-- in other words, in this exercise, you are swapping the pointers). Declare any necessary variables

int *temp = xp; xp = yp; yp = temp;

The variables xp and yp have both been declared as pointers to integers , and have been assigned values . Write the code to exchange the two integers (so that after the swap xp still points at the same location, but it now contains the integer value originally contained in the location pointed to by y; and vice versa-- in other words, in this exercise you are swapping the integers , not the pointers). Declare any necessary variables

int *temp = xp; xp = yp; yp = temp;

Given an array arr of type int , along with two int variables i and j, write some code that swaps the values of arr[i] and arr[j]. Declare any additional variables as necessary.

int temp=arr[i]; arr[i]=arr[j]; arr[j]=temp;

Modify the solution: int x; for (int i=0; i<50; i++) wasReadIn[i] = false; for (int i=0; i<n; i++) { cin >> x; wasReadIn[x] = true; } for (int i=0; i<50; i++) if (wasReadIn[i]) cout << i << " " So that it can handle duplicates in the input. Hint: change the bool array to an array of int . The instructions for 11186 are repeated below:

int x; for (int i=0; i<50; i++) wasReadIn[i] = 0; for (int i=0; i<n; i++) { cin >> x; wasReadIn[x] += 1; } for (int i=0; i<50; i++) for (int j=0; j<wasReadIn[i]; j++) cout << i << ' ';

Assume that name is a variable of type string that has been assigned a value . Write an expression whose value is a string containing the second character of the value of name . So if the value of name were "Smith" the expression 's value would be "m"

name.substr(1,1)

Assume that name is a variable of type string that has been assigned a value . Write an expression whose value is a string containing the last character of the value of name . So if the value of name were "Smith" the expression 's value would be "h"

name.substr(name.length()-1)

Assume that name is a variable of type string that has been assigned a value . Write an expression whose value is the first character of the value of name . So if the value of name were "Smith" the expression 's value would be 'S'

name[0]

Assume that name is a variable of type string that has been assigned a value . Write an expression whose value is the second character of the value of name . So if the value of name were "Green" the expression 's value would be 'r'

name[1]

In this exercise, you will write some code that reads n unique (no duplicates!) non-negative integers , each one less than fifty (50). Your code will print them in sorted order without using any nested loops-- potentially very efficient! We'll walk you through this: First, assume you are given an int variable n, that contains the number of integers to read from standard input. Also assume you are given an array , named wasReadIn, of fifty (50) bool elements and initialize all the elements to false . Third, read in the n integers from the input, and each time you read an integer , use it as an index into the bool array , and assign that element to be true -- thus "marking" in the array which numbers have been read. Lastly the "punchline": write a loop that traverses the bool array : every time it finds an element that is true it prints out the element 's INDEX -- which was one of the integers read in. Place all the numbers on a single line, separated by a single spaces. Note: this technique is not limited to 50 elements -- it works just as well for larger values . Thus, for example you could have an array of 1,000,000 elements (that's right-- one million!) and use it to sort numbers up to 1,000,000 in value !

int x; for (int i=0; i<50; i++) wasReadIn[i] = false; for (int i=0; i<n; i++) { cin >> x; wasReadIn[x] = true; } for (int i=0; i<50; i++) if (wasReadIn[i]) cout << i << " "

Assume that ip has been declared to be a pointer to int and that enrollment has been declared to be an array of 20 elements . Write a statement that makes ip point to the first element in the array

ip = &enrollment[0];

Assume that ip has been declared to be a pointer to int and that enrollment has been declared to be an array of 20 elements . Write a statement that makes ip point to the last element in the array

ip = &enrollment[19];

Assume that ip has been declared to be a pointer to int and that enrollment has been declared to be an array of 20 elements . Assume also that section has been declared to be an int and has been initialized to some value between 5 and 10. Write a statement that makes ip point to the element in the array indexed by section

ip = &enrollment[section];

Given the variable ip, already declared as a pointer to an integer , write the code to dynamically allocate memory for a single integer value , assign the resulting pointer to ip, and initialize the integer value to 27

ip = new int; *ip = 27;

Assume that ip and jp have both been declared to be pointers to int and that result has been declared to be an array of 100 elements . Assume further that ip has been initialized to point to an element in the first half of the array . Write a statement that makes jp point to the element in the array just after the one that ip points to

jp = ip + 1;

Assume that ip and jp have both been declared to be pointers to int and that result has been declared to be an array of 100 elements . Assume further that ip has been initialized to point to an element in the first half of the array . Write a statement that makes jp point to the element in the array 5 positions after the one that ip points to

jp = ip + 5;

Given the char * variables name1, name2, and name3, write a fragment of code that assigns the largest value to the variable max (assume all three have already been declared and have been assigned values )

max = name1; if (strcmp(name2,max)>0) max = name2; if (strcmp(name3,max)>0) max = name3;

minMax is a function that takes five arguments and returns no value . The first three arguments are of type int . The last two arguments are pointers to int that are set by the function to the largest and smallest of the values of the first three parameters , respectively. x, y and z are three int variables that have been declared and initialized . big and small are two int variables that have been declared . Write a statement that sets the value of big to the largest of x, y, z and sets the value of small to the smallest of x, y, z by calling minMax

minMax (x, y, z, &big, &small);

Assume that name is a variable of type string that has been assigned a value . Write an expression whose value is a string containing the first character of the value of name . So if the value of name were "Smith" the expression 's value would be "S"

name.substr(0,1)

Write an expression that whose value is the fifth character of the string name

name[4]

Assume that name is a variable of type string that has been assigned a value . Write an expression whose value is the last character of the value of name . So if the value of name were "Blair" the expression 's value would be 'r'

name[name.length()-1]

Write a sequence of statements that finds the first comma in the string line, and assigns to the variable clause the portion of line up to, but not including the comma. You may assume that an int variable pos, as well as the variables line and clause, have already been declared

pos=line.find(","); clause=line.substr(0,pos);

Write an expression that results in a string consisting of the third through tenth characters of the string s

s.substr(2,8)

Assume that scotus is a two-dimensional character array that stores the family names (last names ) of the nine justices on the Supreme Court of the United States. Assume no name is longer than twenty characters . Assume that scotus has been initialized in alphabetical order. Write an expression whose value is the last name stored in the array

scotus[8]

Assume that sentence is a variable of type string that has been assigned a value . Assume furthermore that this value is a string consisting of words separated by single space characters with a period at the end. For example: "This is a possible value of sentence." Assume that there is another variable declared , secondWord, also of type string . Write the statements needed so that the second word of the value of sentence is assigned to secondWord. So, if the value of sentence were "Broccoli is delicious." your code would assign the value "is" to secondWord

secondWord = sentence.substr(sentence.find(' ') + 1); secondWord = secondWord.substr(0, secondWord.find(' '));

Write an expression that evaluates to true if the value of C-string lastName is greater than the C-string "Dexter"

strcmp(lastName, "Dexter") > 0

Assume that ip, jp, and tp have all been declared to be pointers to int and that result has been declared to be an array of 100 elements . Assume further that ip has been initialized to point to an element in the first half of the array and that jp has been initialized to point to an element in the second half of the array . Write some code that makes jp point to the element that ip was pointing to and that makes ip point to the element that jp was pointing to

tp = ip; ip = jp; jp = tp;

tripleIt is a function that takes one argument and returns no value . The argument is a pointer to int . The function triples the value that the argument points to and stores it back. penalty is an int variable that has been declared and initialized . Write a statement that triples the value stored in penalty by invoking the function tripleIt. For example, if the value in penalty was 7 before your statement , after your statement its value would be 21

tripleIt(&penalty);

Write the definition of a function divide that takes four arguments and returns no value . The first two arguments are of type int . The last two arguments arguments are pointers to int and are set by the function to the quotient and remainder of dividing the first argument by the second argument . The function does not return a value . The function can be used as follows: int numerator=42, denominator=5, quotient, remainder; divide(numerator,denominator,&quotient,&remainder); /* quotient is now 8 */ /* remainder is now 2 */

void divide (int n, int d, int *q, int *r) { *q = n/d; *r=n%d; }

Write a statement that declares a prototype for a function divide that takes four arguments and returns no value . The first two arguments are of type int . The last two arguments arguments are pointers to int that are set by the function to the quotient and remainder of dividing the first argument by the second argument . The function does not return a value

void divide(int, int, int *, int *);

Write a statement that declares a prototype for a function doubleIt, which has a single parameter of type pointer to int

void doubleIt(int *);

Write the definition of a function doubleIt, which doubles the value of its argument but returns nothing so that it can be used as follows: int x=5; doubleIt(&x); /* x is now equal to 10 */

void doubleIt(int *x) {*x = *x*2;}

Write a statement that declares a prototype for a function minMax, which has five parameters . The first three parameters are integers . The last two are parameters that are set by the function to the largest and smallest of the values of the first three parameters . The function does not return a value

void minMax (int, int, int, int *, int *);

Write the definition of a function minMax that has five parameters . The first three parameters are integers . The last two are set by the function to the largest and smallest of the values of the first three parameters . The function does not return a value . The function can be used as follows: int a=31, b=5, c=19 big, small; minMax(a,b,c,&big,&small); /* big is now 31 */ /* small is now 5 */

void minMax(int a, int b, int c, int *big, int *small){ *big = *small = a; if (b < *small){ *small = b; } if (c < *small){ *small = c; } if (b>*big){ *big = b; } if (c>*big){ *big = c; } }

Write the definition of a function tripleIt, which triples its argument but returns nothing so that it can be used as follows: int x=5; tripleIt(&x); /* x is now equal to 15 */

void tripleIt (int * x) { *x = *x * 3; }

Write a statement that declares a prototype for a function tripleIt, which can be used as follows: int x=5; tripleIt(&x); /* x is now equal to 15 */

void tripleIt(int *);

Write the definition of a function zeroIt, which is used to zero out a variable . The function is used as follows: int x=5; zeroIt(&x); /* x is now equal to 0 */

void zeroIt (int * x) { *x = 0; }

Write a statement that declares a prototype for a function zeroIt, which can be used as follows: int x=5; zeroIt(&x); /* x is now equal to 0 */

void zeroIt(int *);

Assume that word is a variable of type string that has been assigned a value . Write an expression whose value is a string consisting of the first three characters of the value of word. So if the value of word were "dystopia" the expression 's value would be "dys"

word.substr(0,3)

Assume that word is a variable of type string that has been assigned a value . Write an expression whose value is a string consisting of the last three characters of the value of word. So if the value of word were "biggest" the expression 's value would be "est"

word.substr(word.length()-3,3)

zeroIt is a function that takes one argument and returns no value . The argument is a pointer to int . The function stores the value 0 back into the variable pointed to by the argument . x is an int variable that has been declared . Write a statement that sets the value stored in x to zero by invoking the function zeroIt

zeroIt(&x);


Kaugnay na mga set ng pag-aaral

Chemistry Unit 6 TEST States of matter

View Set

APUSH Period 1 Critical Thinking Questions

View Set

RN Pharmacology Online Practice 2023B

View Set

Chapter 14: Oligopoly and Monopolistic competition

View Set