Chapter 8

¡Supera tus tareas y exámenes ahora con Quizwiz!

Which is better? A or B?

C

CodeLab is

an online set of interactive exercises with immediate feedback

Write the definition of a function named isSorted that receives three arguments: an array of int, an int that indicates the number of elements of interest in the array, and a bool. If the bool argument is true then the function returns true if and only if the array is sorted in ascending order. If the bool argument is false then the function returns true if and only if the array is sorted in descending order. In all other cases the function returns false. You may assume that the array has at least two elements.

bool isSorted(int a[], int n, bool status) { bool flag = true; if(status == true) { for (int i=0; i<n-1; i++) if (a[i] > a[i+1]) flag = false; } else { for (int i=n-1; i > 0; i--) if (a[i] > a[i-1]) flag = false; } return flag; }

You are given an int variable k, an int array zipcodeList that has been declared and initialized, an int variable nZips that contains the number of elements in zipcodeList, and a bool variable duplicates. Write some code that assigns true to duplicates if there are two adjacent elements in the array that have the same value, and that assigns false to duplicates otherwise. Use only k, zipcodeList, nZips, and duplicates.

duplicates = false; for (k=0; k<nZips; k++) { if (zipcodeList[k]== zipcodeList[k+1]) { duplicates=true; } }

You are given two int variables j and k, an int array zipcodeList that has been declared and initialized, an int variable nZips that contains the number of elements in zipcodeList, and a bool variable duplicates. Write some code that assigns true to duplicates if any two elements in the array have the same value, and that assigns false to duplicates otherwise. Use only j, k, zipcodeList, nZips, and duplicates.

duplicates=false; for (k=0; k<nZips; k++) { for (j=0; j<k; j++) { if ( zipcodeList[j]==zipcodeList[k]) { duplicates=true; } } }

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; }

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 number; for (int i=0; i<50; i++) { wasReadIn[i]=0; } for (int i=0; i<n; i++) { cin >> number; wasReadIn[number]++; } for(int i=0; i<50; i++) { if(wasReadIn[i]>0) { for(int j=0; j<wasReadIn[i]; j++) cout << i << " "; } }

Modify the solution to exercise 11186 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: 11186: 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. 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. Second, given a bool array wasReadin and int n, which have already been declared, initialize all elements in the array 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. 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.

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

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; temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;

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).

int temp; for(int index = 0; index < n-1; index++) { for (int count = 0; count < index; count++) { if (x[index] > x [count]){ temp = x[index]; x[index] = x[count ]; x[count] = temp; } } } if (n % 2==0) m=(x[(n/2)-1] + x[(n/2)]) / 2; else m=x[n/2];

Assume you have a int variable n that has already been declared and initialized. Its value is the number of integers that need to be read in from standard input and printed out in sorted (ascending) order, each on a line by itself. Furthermore, there are no duplicates in the input and every number to be read is a non-negative value that is less than n's value. In this exercise you may not use any array (or fancy STL collection such as a vector). You may declare a variable or two as needed. With these restrictions, read the n values and print them out as required onto standard output.

int x=0; for (int i=0; i<n; i++){ cin >> x; cout << i << "\n"; }

Given: an int variable k, an int array currentMembers that has been declared and initialized, an int variable nMembers that contains the number of elements in the array, an int variable memberID that has been initialized, and a bool variable isAMember, Write code that assigns true to isAMember if the value of memberID can be found in currentMembers, and that assigns false to isAMember otherwise. Use only k, currentMembers, nMembers, and isAMember.

isAMember = false; for (k=0; k<nMembers; k=k+1) { if (currentMembers[k] == memberID) { isAMember = true; } }

CodeLab replaces

none of the above

Given an int variable k, an int array incompletes that has been declared and initialized, an int variable nIncompletes that contains the number of elements in the array, an int variable studentID that has been initialized, and an int variable numberOfIncompletes, Write code that counts the number of times the value of studentID appears in incompletes and assigns this value to numberOfIncompletes. You may use only k, incompletes, nIncompletes, studentID, and numberOfIncompletes.

numberOfIncompletes=0; for (k=0; k<nIncompletes; k++) { if (incompletes[k] == studentID) { numberOfIncompletes ++; } }


Conjuntos de estudio relacionados

Priciniples Of Management Chapter 02

View Set

Cardiorespiratory Training: Programming and Progressions

View Set

Pharm Questions to study for final (ATI & Evolve Questions)

View Set

UNIT 5: BROKER-AGENT RELATIONSHIP

View Set

Community Nutrition - Chapter 14

View Set