Chapter 8 Exercises Answer Sheet
.pdf
keyboard_arrow_up
School
Wharton County Junior College *
*We aren’t endorsed by this school
Course
2304
Subject
Computer Science
Date
May 1, 2024
Type
Pages
4
Uploaded by ElderSummerBoar45 on coursehero.com
Chapter 8 Exercises Answer Sheet For each problem, please insert your scripts, commands, and answers. Please save your completed work as a PDF file and submit it to Blackboard. 1) Create the following cell array: >> ca = {'abc', 11, 3:2:9, zeros(2)} Use the reshape
function to make it a 2 x 2
matrix. Then, write an expression that would refer to just the last column of this cell array. Insert your answer here: >> ca = reshape(ca,2,2); >> ca{:,2} 2) Create a row vector cell array to store the character vector ‘xyz’, the number 33.3, the vector 2:6, and the logical
expression ‘a’ < ‘c’. Use the transpose operator to make this a column vector, and use reshape
to make it a 2 x 2 matrix. Use celldisp
to display all elements. Insert your answer here: >> mycell = {'xyz',33.3,2:6,'a' < 'c'} mycell = 1×4 cell array {'xyz'} {[33.3000]} {1×5 double} {[1]} >> mycell = mycell' mycell = 4×1 cell array {'xyz' } {[ 33.3000]} {1×5 double} {[ 1]} >> mycell = reshape(mycell,2,2) mycell = 2×2 cell array {'xyz' } {1×5 double} {[33.3000]} {[ 1]} >> celldisp(mycell) mycell{1,1} = xyz mycell{2,1} = 33.3000 mycell{1,2} = 2 3 4 5 6 mycell{2,2} = 1 3) Create a cell array that stores phrases, such as: exclaimcell = {'Bravo', 'Fantastic job'}; Pick a random phrase to print. Insert your answer here:
>> ranindex = randi([1, length(exclaimcell)]); >> fprintf('%s\n', exclaimcell{ranindex}) 4) Write a script that will prompt the user for character vectors and read them in, store them in a cell array (in a loop), and then print them out. Insert your answer here: Ch8Ex4.m % Prompt the user for char vectors and store in a cell array for i = 1:4 str = input('Enter a character vector: ','s'); strcell{i} = str; end strcell 5) Create a cell array that contains character vectors, e.g., pets = {'cat', 'dog', 'snake'}; Show the difference in the following methods of indexing: pets(1:2) pets{1:2} [p1 p2] = pets{1:2} Insert your answer here: >> pets(1:2) ans = 1×2 cell array {'cat'} {'dog'} >> pets{1:2} ans = 'cat' ans = 'dog' >> [p1 p2] = pets{1:2} p1 = 'cat' p2 = 'dog' 6) Create a cell array variable that would store for a student his or her name, university id number, and GPA. Print this information. Insert your answer here: >> studentca= {'Smith, Susan', 12345678, 3.5}; >> fprintf('Name: %s\nUID: %d\nGPA: %.2f\n', studentca{1}, ... studentca{2}, studentca{3}) Name: Smith, Susan UID: 12345678 GPA: 3.50
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
Related Questions
C++ Chapter 8 - 2D Array -Find the sum above or below the main diagonal in any n by n matrix, please solve the question with new solution, not the same as in questions bank
arrow_forward
(Check test scores) The answers to a true-false test are as follows: T T F F T. Given a two-dimensional answer array, in which each row corresponds to the answers provided on one test, write a function that accepts the two-dimensional array and number of tests as parameters and returns a one-dimensional array containing the grades for each test. (Each question is worth 5 points so that the maximum possible grade is 25.) Test your function with the following data: int score = 0;
arrow_forward
3."""Code _Write a function validSolution/ValidateSolution/valid_solution()that accepts a 2D array representing a Sudoku board, and returns trueif it is a valid solution, or false otherwise. The cells of the sudokuboard may also contain 0's, which will represent empty cells.Boards containing one or more zeroes are considered to be invalid solutions.The board is always 9 cells by 9 cells, and every cell only contains integersfrom 0 to 9.
(More info at: http://en.wikipedia.org/wiki/Sudoku)"""
# Using dict/hash-tablefrom collections import defaultdict
def valid_solution_hashtable(board): for i in range(len(board)): dict_row = defaultdict(int) dict_col = defaultdict(int) for j in range(len(board[0])): value_row = board[i][j] value_col = board[j][i] if not value_row or value_col == 0: return False if value_row in dict_row: return False else: dict_row[value_row] += 1.
arrow_forward
please answer all questions
You need to generate a (5x12) rain matrix (Rain[5][12]) that contains the average rainfall over 12 months for a 5 year period. You program should do the following steps:
1) Create a 5x12 integer rainfall matrix
2) Create a 5 element array of int pointers(int * Rain[5]) and assign each pointer to the address of the first element of each row
3) Populate the elements of the matrix with pseudo-random numbers having a range from 0-100 inches. Use the rand( ) function to generate your temperatures. The output of rand() should be mod with 100 (rand()%100) in order to get reasonable rainfall values.
Init the random number generator with long seed = srand(67)
This task should be performed by a function, void RainGen(int* Rain[ ], int R, int C), that populates all the matrix entries.
4) After generating all rainfall matrix entries, print the entire matrix using the function, void Print(int* Rain[ ], int R, int C);
5) Calculate the…
arrow_forward
Problem 1 – Adding contactsIn this problem, you will create the skeleton for your entire assignment. In the next problems, you will add more functionality to your program.Our goal on this problem is to create a Rolodex to store names and e-mails (using 1D or 2D arrays, you can choose). The Rolodex can store up to 256 contacts. The Rolodex will store the contact's name as one String and the contact's e-mail as another String.Your application will work as follows:1. Upon starting, it will greet the user and prompt the user to select an option (an integer)2. There are three options in problem 1:1 – to add contactnote: If the Rolodex is full, i.e. there are 256 contacts stored, this option should print: "Roldex is Full" and ask the user to input a new option5 – to print all contacts0 – to exitIf the user inputs an incorrect value, the program should prompt for an option againIf the users select option 1:1. The application will then ask for the contact's name and e-mail (see the example for…
arrow_forward
Correct the following wrong statements.
The following statement creates and initializes the 2-dimensional array values: short values[][] = {10, 20, 30, 40, 50};
arrow_forward
Array TypesObjective: Based on the given values and initializations, give what is being required of each statement.1. Given A[10], α=2000, esize=4 bytes:a) Find the number of elements.b) Find the address of the 6th element.c) Find the index no. of the 8th element.2. Given E[3][4], α=2020, esize=4 bytes:a) Find the total no. of elements.b) Find the address of the last element.c) Find the address of the 10th element.
arrow_forward
======= Office Pay Roll System =========
• Enter ‘r, to take information of all the employee into the Employee structure array of size N.
• Enter ‘s’, to search an employee by phone no. If found, display the info of employee. If not, display “Not Found”.
• Enter ‘t’, to display the information of all the employees in reverse index order in a tabular format.
• Enter ‘q’, quit/exit the menu system.
• For any other input, display “Invalid Input”
After the menu is displayed, the program will prompt the user “Enter your choice: “ to enter a choice from the menu. After user enters his choice, using switch case the program will call appropriate functions to do the task.
(a)(i) Create an Employee structure that will have the following members: name (string type), phoneNo (string type), and a salary (float type).
(ii) In the main function, create an array of size N of Employee structure type. This array will be used by all the functions. You will pass this array and # of employees as…
arrow_forward
1. Create a 2D char array called grades, to save the grades for three students for five courses (size 3 by 5). Write the nested for loop to initialize the first row with ‘A’, second row ‘B’, and third-row ‘C’.
2. Say the second student got C for the fifth course. The third student got an A for the first course. Make the changes to the above array.
3. Print out the array elements in the 2-dimensional format like the following. Make sure you print out the student # and course numbers so one can easily locate an array element. Format your output like the following.
arrow_forward
def large_position(matrix: list[list[int]], row: int, col: int) -> int: """ Returns the area of the largest rectangle whose top left corner is at position <row>, <col> in <matrix>.
You MUST make use of the helper <longest_chain> here as you loop through each row of the matrix. Do not modify (i.e., mutate) the input matrix.
>>> case100 = [[1, 0, 1, 0, 0], ... [1, 0, 1, 1, 1], ... [1, 1, 1, 1, 1], ... [1, 0, 0, 1, 0]] >>> large_position(case1, 0, 0) 4 """
Helper:
def longest_chain(lst: List[int]) -> int:
count = 0 # defining count variable as 0. Using it for counting the length of the sequence.i = 0 # defining i variable as 0. Using it for index positionwhile (i < len(lst) and lst[i] != 0): # iterating through out the list while i is lesser than the length of the list and until we encounter any 0i += 1 # increasing the index valuecount += 1 # increasing the countreturn…
arrow_forward
def large_position(matrix: list[list[int]], row: int, col: int) -> int: """ Returns the area of the largest rectangle whose top left corner is at position <row>, <col> in <matrix>.
You MUST make use of the helper <longest_chain> here as you loop through each row of the matrix. Do not modify (i.e., mutate) the input matrix.
>>> case100 = [[1, 0, 1, 0, 0], ... [1, 0, 1, 1, 1], ... [1, 1, 1, 1, 1], ... [1, 0, 0, 1, 0]] >>> large_position(case1, 0, 0) 4 """
Helper:
def longest_chain(lst: List[int]) -> int:
count = 0 # defining count variable as 0. Using it for counting the length of the sequence.i = 0 # defining i variable as 0. Using it for index positionwhile (i < len(lst) and lst[i] != 0): # iterating through out the list while i is lesser than the length of the list and until we encounter any 0i += 1 # increasing the index valuecount += 1 # increasing the countreturn…
arrow_forward
SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:9781305503922
Author:Patrick M. Carey
Publisher:Cengage Learning
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Related Questions
- C++ Chapter 8 - 2D Array -Find the sum above or below the main diagonal in any n by n matrix, please solve the question with new solution, not the same as in questions bankarrow_forward(Check test scores) The answers to a true-false test are as follows: T T F F T. Given a two-dimensional answer array, in which each row corresponds to the answers provided on one test, write a function that accepts the two-dimensional array and number of tests as parameters and returns a one-dimensional array containing the grades for each test. (Each question is worth 5 points so that the maximum possible grade is 25.) Test your function with the following data: int score = 0;arrow_forward3."""Code _Write a function validSolution/ValidateSolution/valid_solution()that accepts a 2D array representing a Sudoku board, and returns trueif it is a valid solution, or false otherwise. The cells of the sudokuboard may also contain 0's, which will represent empty cells.Boards containing one or more zeroes are considered to be invalid solutions.The board is always 9 cells by 9 cells, and every cell only contains integersfrom 0 to 9. (More info at: http://en.wikipedia.org/wiki/Sudoku)""" # Using dict/hash-tablefrom collections import defaultdict def valid_solution_hashtable(board): for i in range(len(board)): dict_row = defaultdict(int) dict_col = defaultdict(int) for j in range(len(board[0])): value_row = board[i][j] value_col = board[j][i] if not value_row or value_col == 0: return False if value_row in dict_row: return False else: dict_row[value_row] += 1.arrow_forward
- please answer all questions You need to generate a (5x12) rain matrix (Rain[5][12]) that contains the average rainfall over 12 months for a 5 year period. You program should do the following steps: 1) Create a 5x12 integer rainfall matrix 2) Create a 5 element array of int pointers(int * Rain[5]) and assign each pointer to the address of the first element of each row 3) Populate the elements of the matrix with pseudo-random numbers having a range from 0-100 inches. Use the rand( ) function to generate your temperatures. The output of rand() should be mod with 100 (rand()%100) in order to get reasonable rainfall values. Init the random number generator with long seed = srand(67) This task should be performed by a function, void RainGen(int* Rain[ ], int R, int C), that populates all the matrix entries. 4) After generating all rainfall matrix entries, print the entire matrix using the function, void Print(int* Rain[ ], int R, int C); 5) Calculate the…arrow_forwardProblem 1 – Adding contactsIn this problem, you will create the skeleton for your entire assignment. In the next problems, you will add more functionality to your program.Our goal on this problem is to create a Rolodex to store names and e-mails (using 1D or 2D arrays, you can choose). The Rolodex can store up to 256 contacts. The Rolodex will store the contact's name as one String and the contact's e-mail as another String.Your application will work as follows:1. Upon starting, it will greet the user and prompt the user to select an option (an integer)2. There are three options in problem 1:1 – to add contactnote: If the Rolodex is full, i.e. there are 256 contacts stored, this option should print: "Roldex is Full" and ask the user to input a new option5 – to print all contacts0 – to exitIf the user inputs an incorrect value, the program should prompt for an option againIf the users select option 1:1. The application will then ask for the contact's name and e-mail (see the example for…arrow_forwardCorrect the following wrong statements. The following statement creates and initializes the 2-dimensional array values: short values[][] = {10, 20, 30, 40, 50};arrow_forward
- Array TypesObjective: Based on the given values and initializations, give what is being required of each statement.1. Given A[10], α=2000, esize=4 bytes:a) Find the number of elements.b) Find the address of the 6th element.c) Find the index no. of the 8th element.2. Given E[3][4], α=2020, esize=4 bytes:a) Find the total no. of elements.b) Find the address of the last element.c) Find the address of the 10th element.arrow_forward======= Office Pay Roll System ========= • Enter ‘r, to take information of all the employee into the Employee structure array of size N. • Enter ‘s’, to search an employee by phone no. If found, display the info of employee. If not, display “Not Found”. • Enter ‘t’, to display the information of all the employees in reverse index order in a tabular format. • Enter ‘q’, quit/exit the menu system. • For any other input, display “Invalid Input” After the menu is displayed, the program will prompt the user “Enter your choice: “ to enter a choice from the menu. After user enters his choice, using switch case the program will call appropriate functions to do the task. (a)(i) Create an Employee structure that will have the following members: name (string type), phoneNo (string type), and a salary (float type). (ii) In the main function, create an array of size N of Employee structure type. This array will be used by all the functions. You will pass this array and # of employees as…arrow_forward1. Create a 2D char array called grades, to save the grades for three students for five courses (size 3 by 5). Write the nested for loop to initialize the first row with ‘A’, second row ‘B’, and third-row ‘C’. 2. Say the second student got C for the fifth course. The third student got an A for the first course. Make the changes to the above array. 3. Print out the array elements in the 2-dimensional format like the following. Make sure you print out the student # and course numbers so one can easily locate an array element. Format your output like the following.arrow_forward
- def large_position(matrix: list[list[int]], row: int, col: int) -> int: """ Returns the area of the largest rectangle whose top left corner is at position <row>, <col> in <matrix>. You MUST make use of the helper <longest_chain> here as you loop through each row of the matrix. Do not modify (i.e., mutate) the input matrix. >>> case100 = [[1, 0, 1, 0, 0], ... [1, 0, 1, 1, 1], ... [1, 1, 1, 1, 1], ... [1, 0, 0, 1, 0]] >>> large_position(case1, 0, 0) 4 """ Helper: def longest_chain(lst: List[int]) -> int: count = 0 # defining count variable as 0. Using it for counting the length of the sequence.i = 0 # defining i variable as 0. Using it for index positionwhile (i < len(lst) and lst[i] != 0): # iterating through out the list while i is lesser than the length of the list and until we encounter any 0i += 1 # increasing the index valuecount += 1 # increasing the countreturn…arrow_forwarddef large_position(matrix: list[list[int]], row: int, col: int) -> int: """ Returns the area of the largest rectangle whose top left corner is at position <row>, <col> in <matrix>. You MUST make use of the helper <longest_chain> here as you loop through each row of the matrix. Do not modify (i.e., mutate) the input matrix. >>> case100 = [[1, 0, 1, 0, 0], ... [1, 0, 1, 1, 1], ... [1, 1, 1, 1, 1], ... [1, 0, 0, 1, 0]] >>> large_position(case1, 0, 0) 4 """ Helper: def longest_chain(lst: List[int]) -> int: count = 0 # defining count variable as 0. Using it for counting the length of the sequence.i = 0 # defining i variable as 0. Using it for index positionwhile (i < len(lst) and lst[i] != 0): # iterating through out the list while i is lesser than the length of the list and until we encounter any 0i += 1 # increasing the index valuecount += 1 # increasing the countreturn…arrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
- New Perspectives on HTML5, CSS3, and JavaScriptComputer ScienceISBN:9781305503922Author:Patrick M. CareyPublisher:Cengage LearningProgramming with Microsoft Visual Basic 2017Computer ScienceISBN:9781337102124Author:Diane ZakPublisher:Cengage LearningMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:9781305503922
Author:Patrick M. Carey
Publisher:Cengage Learning
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,