MyProgrammingLab Chapter 7.2-7.4

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

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 array[], int num, bool valid) { bool flag = true; if(valid == true) { for(int i = 0; i < num - 1; i++) { if(array[i] > array[i + 1]) { flag = false; } } } else { for(int i = num - 1; i > 0 ;i--) { if(array[i] > array[i - 1]) { flag = false; } } } return flag; }

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(j = 0; j < nZips; j++) { for(k = 0; k < j; k++) { if(zipcodeList[j] == zipcodeList[k]) { duplicates = true; break; } } }

Given a two-dimensional array of integers named q, with 2 rows and 4 columns, write some code that puts a zero in every element of q. Declare any variables needed to help you.

for(int i = 0; i < 2; i++){ for(int j = 0; j < 4; j++) { q[i][j] = 0; }}

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.

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

Write the definition of a function printArray, which has two parameters. The first parameter is an array of integers and the second is an int, the number of elements in the array. The function does not return a value. The function prints out each element of the array, on a line by itself, in the order the elements appear in the array, and does not print anything else.

void printArray(int array[], int j) { for(int i = 0; i < j; i++) { cout << array[i] << endl; } }

Write a statement that declares a prototype for a function printArray, which has two parameters. The first parameter is an array of element type int and the second is an int, the number of elements in the array. The function does not return a value.

void printArray(int[], int);

Write the definition of a function reverse, whose first parameter is an array of integers and whose second parameter is the number of elements in the array. The function reverses the elements of the array. The function does not return a value.

void reverse(int array[], int n) { int temp; for(int k = 0; k < n/2; k++) { temp = array[k]; array[k] = array[n - k-1]; array[n - k-1] = temp; } }

Given a two-dimensional array x of element type int, write an expression whose value is the sum of the element in the 3rd row/4th column and the element in the 5th row/1st column.

x[2][3] + x[4][0]

Given a two-dimensional array x of element type int with 5 rows and 4 columns, write an expression whose value the last element in the array (the last column of the last row).

x[4][3]

Given a two-dimensional array x of element type double, and two integer variables i and j, write an expression whose value is the i-th element in the j-th row.

x[i][j]

Given a two-dimensional array x of doubles, write an expression whose value is twice the value of the element in the 3rd row and the 2nd column.

2 * x[2][1]

You are given a 6x8 (6 rows, 8 columns) array of integers, x, already initialized and three integer variables: max, i and j. Write the necessary code so that max will have the largest value in the array x.

max = x[0][0]; for(i = 0; i < 6; i++) { for(j = 0; j < 8; j++) { if(x[i][j] > max) { max = x[i][j]; } } }

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

printArray is a function that has two parameters. The first parameter is an array of element type int and the second is an int, the number of elements in the array. The function prints the contents of the array parameter; it does not return a value. inventory is an array of ints that has been already declared and filled with values. n is an int variable that holds the number of elements in the array. Write a statement that prints the contents of the array inventory by calling the function printArray.

printArray(inventory, n);

Declare a 8x8 two-dimensional array of strings named chessboard.

string chessboard[8][8];

Write the definition of a function named sumArray that receives two parameters: an array of element type int and an int that contains the number of elements of the array. The function returns the sum of the elements of the array as an int.

int sumArray(int array[], int n) { int sum = 0; for(int i = 0; i < n; i++) { sum += array[i]; } return sum; }

Declare a 3x3 two-dimensional array of integers named tictactoe.

int tictactoe[3][3];

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

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

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


Kaugnay na mga set ng pag-aaral

LCSW_ Assessment, Diagnosis, and Treatment Planning

View Set

2.3 Financial Markets Instruments

View Set

Chapter 8 Lecture Culturelle (v/f + answer the question)

View Set

Sales contracts and purchase agreements

View Set

Medical Terminology: Circulatory System Exercises

View Set

Chapter 3: Time Value of Money: An Introduction

View Set

Nutrition Final - Dietary Supplements

View Set

Structure of the generalized cells (with image)

View Set