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
Directions: Read each item carefully. Create command/codes based on the information requested on each item. (R programming)
Create a list that contains a numeric vector from (1 to 30, 34, 45, 47, and 50 to 70), a character vector that repeat the elements A, B, and C 30 times, and a 2 x 3 matrix that contains a number from 101 to 106 filled by rows.
arrow_forward
5 نقاط
Q1. Write a code to find the
summation of all numbers that
are less than or equal to 10 in
the array. C= {-10, 35, 10, -5, 6,
16, -23, 94, -22, 12, 202, -52O, 0,
.-3}
arrow_forward
Q1: -Write a program that reads from the user a matrix Nx4. The user has to
enter students' data in four columns, the first column gets students' names, the
second column contains gender, the third column has ages of students, and the
fourth column contains the education level. See the example in the figure below.
Note that the type of the matrix is string.
Ali
Ahmed
Noor
Fatimah
www.
Male
Male
Female
Female
17
21
20
25
1st
4th
3rd
3rd
24
**
1. Find How many females and males are in the matrix.
2. Find how many male students their ages are above 25 years.
3. Find how many students in the 1" year who their ages are 17 years.
arrow_forward
True or False The subscript of the last element will always be one less than the array’s Length property.
arrow_forward
. Create an integer type array whose size to be entered by the user using calloc function. After entering the data elements, modify its size using realloc function and then entered the remaining elements in the array. Find the total number of even and odd elements in the final array.
arrow_forward
O 43%
4G
17:20 m
MajorAssignments3.pdf
TPG111T 2020 Semester 1 Major Assignment 1
1
Question 2
[12]
The question:
You are required to develop an application to determine the maximum value in a 2-dimension array as
displayed in figure 1. You are required to:
Declare a 4-by-5 array.
The array should be initialized with the values as shown in Table 1.
Display all the array values as per example in Figure 1.
Display the maximum (highest) value as well as the maximum values row and column subscript
as per example in Figure 1.
Note: Your code should work even if the array is initialized with different values.
15
20
97
4
18
25
4
101
98
105
6
17
2
45
33
88
72
188
34
Table 1
15
20
97
18
25
4.
101
98
105
6
17
2
45
33
88
72
188
34
The maximum value is: 188
This value is found in row 3 column 3
Press any key to continue. ..-
Figure 1
Question 3
[43]
arrow_forward
Q3: a- Write a program to pass a two dimensional square matrix as a parameter to a function; this
function is used to convert the elements of the first row of the matrix to the average elements of the
second and fourth rows; finally display the matrix after change.
arrow_forward
City Location (definition of simple cell array)
The city of Istanbul, Turkey is located at a latitude of 41.0082 degrees north and longitude of 28.9784 degrees east.
Write a script to do the following:
1. Define a character array variable named CityName and assign the value 'Istanbul, Turkey' to it.
2. Define a single precision two-element row vector named LatLong and assign the latitude of Istanbul to the first element and the longitude of Istanbul to the second element.
3. Define a 1 x 2 cell array named CityLocation. Assign the character array 'Istanbul, Turkey' to the first cell. Assign the single precision row vector of Istanbul's latitude and
longitude to the second cell.
Script>
1% defining character array variable
2 CityName = 'Istanbul, Turkey';
3
4 % defining 2 element row vector
5 LatLong = [41.0082 28.9784];
6
7% defining 1 x 2 cell array
8 CityLocation = {CityName; Lat Long};
9 end
Save
My Solutions >
C Reset
MATLAB Documentation
?
arrow_forward
q3
=
range (30)
# change q3 to a np.array
# add 1 to each element in q3
# add 2 to the last 5 elements in q3
# subtract 10 to the first element in q2
arrow_forward
Match each data operator with its function:
OFFSET
Returns the number of the elements in an array.
TYPE
Returns the size of a variables byte.
SIZEOF
Returns the memory adress of a variable.
LENGTHOF
Returns number of bytes used in an array.
arrow_forward
Q1. Answer the following
bläi 5
Q1. Write a code to find the
summation of all numbers that
are less than or equal to 10 in
the array. C= {-10, 35, 10, -5, 6,
16, -23, 94, -22, 12, 202, -520, O,
.-3}
ث إضافة ملف
arrow_forward
Q3
using the column operator (:), create
a column vector that contain all the
column of A
3
A=
4
5
6.
7
13
14
15
16
17
arrow_forward
Matrix Multiplication• Write a multiplication function that accepts two 2D numpy arrays,and returns the product of the two. This function should test that thearrays are compatible (recall that the number of columns in the firstmatrix must equal the number of rows in the second matrix). Thisfunction should test the array sizes before attempting themultiplication. If the arrays are not compatible, the function shouldreturn -1.
solve in python
arrow_forward
c) .Use loops to create a matrix in which the value of each element is two times its row
number minus three times its column number. For example, the value of element (2,5)
2*2-3*5=-11
AAT --
arrow_forward
1. Display Function
Implement a function to display the contents of the patient_list array.
Add code to call this function. Note that there are multiple places where this function needs to be called. Look for the // TODO comments to find the correct locations.
2. Sorting the array by Age
Implement the code to sort the contents of the patient_list array based on the value stored in the age field. To do this you will need to implement code that relies on the qsort function from the C Standard library
A function that compares two patient elements, based on the value stored in the age field.
A call to the qsort function, which includes the array to be sorted, the number of elements in the array, the size of each array element and the function used to compare the array elements.
Add code to call the qsort function, using the age comparison function that you implemented. This code should be placed just under the appropriate // TODO comment in main().
Sorting the array by…
arrow_forward
4. Array code
Fill in the method below to have it create and fill in an array of 4 doubles. The first ele-
ment should be set to the average of the first quarter of the array passed to data, the
second element should be set to the average of the second quarter of the array, and so
on.
If input data is [1, 2, 3, 4, 5, 6, 7, 8] result should be [1.5, 3.5, 5.5, 7.5]
public static double[] quartileAverages(int [] data)
{
arrow_forward
part a- 2point) for 3 students find the average of a student by asking him how many grades do you have. The answer to this question, determine the size of the array(list of grades) and then ask the user to enter grades(the number of the grades is equal to the size of the array) then calculate the average for each student. Hence, you will calculate 3 averages. to calculate the average, use the array accumulator. Also when you get each grade validate it. validation for grade means that you check whether a grade is between 0 and 100. if the grade is negative or a grade is more than 100 then it is not valid and you have to ask the user to enter a grade again.
Part b- 1 point) Find the maximum and minimum grades for each student.
Part c- 1 point) Print the sorted list of the grades for each student.
example: if grades user entered are: 60 78 98 23 45
then sorted list is:23 45 60 78 98
(Array not vector)
arrow_forward
Date Printer Write a program that reads a char array from the user containinga date in the form mm/dd/yyyy. It should print the date in the form March 12, 2014.
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
Declare an array of ints named scores and assign it the values 90, 80, 70 using static initialization.
Important: Please do not use any blank spaces between elements in your array, or any unnecessary
spaces, or your answer will be marked incorrect.
arrow_forward
To refer to a particular location or element within an array, we specify the name of thearray and the value of the particular element.
true or false
arrow_forward
Write down the code to traverse the array until X located
arrow_forward
Part 1: Create an array consist of 10 students marks and display the highest and lowest marks in
given array.
arrow_forward
You can assign the contents of one array to another by using
the assignment operator.
the equality operator.
both array names.
A and C together.
none of the above.
arrow_forward
SEE MORE QUESTIONS
Recommended textbooks for you
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning
Related Questions
- Directions: Read each item carefully. Create command/codes based on the information requested on each item. (R programming) Create a list that contains a numeric vector from (1 to 30, 34, 45, 47, and 50 to 70), a character vector that repeat the elements A, B, and C 30 times, and a 2 x 3 matrix that contains a number from 101 to 106 filled by rows.arrow_forward5 نقاط Q1. Write a code to find the summation of all numbers that are less than or equal to 10 in the array. C= {-10, 35, 10, -5, 6, 16, -23, 94, -22, 12, 202, -52O, 0, .-3}arrow_forwardQ1: -Write a program that reads from the user a matrix Nx4. The user has to enter students' data in four columns, the first column gets students' names, the second column contains gender, the third column has ages of students, and the fourth column contains the education level. See the example in the figure below. Note that the type of the matrix is string. Ali Ahmed Noor Fatimah www. Male Male Female Female 17 21 20 25 1st 4th 3rd 3rd 24 ** 1. Find How many females and males are in the matrix. 2. Find how many male students their ages are above 25 years. 3. Find how many students in the 1" year who their ages are 17 years.arrow_forward
- True or False The subscript of the last element will always be one less than the array’s Length property.arrow_forward. Create an integer type array whose size to be entered by the user using calloc function. After entering the data elements, modify its size using realloc function and then entered the remaining elements in the array. Find the total number of even and odd elements in the final array.arrow_forwardO 43% 4G 17:20 m MajorAssignments3.pdf TPG111T 2020 Semester 1 Major Assignment 1 1 Question 2 [12] The question: You are required to develop an application to determine the maximum value in a 2-dimension array as displayed in figure 1. You are required to: Declare a 4-by-5 array. The array should be initialized with the values as shown in Table 1. Display all the array values as per example in Figure 1. Display the maximum (highest) value as well as the maximum values row and column subscript as per example in Figure 1. Note: Your code should work even if the array is initialized with different values. 15 20 97 4 18 25 4 101 98 105 6 17 2 45 33 88 72 188 34 Table 1 15 20 97 18 25 4. 101 98 105 6 17 2 45 33 88 72 188 34 The maximum value is: 188 This value is found in row 3 column 3 Press any key to continue. ..- Figure 1 Question 3 [43]arrow_forward
- Q3: a- Write a program to pass a two dimensional square matrix as a parameter to a function; this function is used to convert the elements of the first row of the matrix to the average elements of the second and fourth rows; finally display the matrix after change.arrow_forwardCity Location (definition of simple cell array) The city of Istanbul, Turkey is located at a latitude of 41.0082 degrees north and longitude of 28.9784 degrees east. Write a script to do the following: 1. Define a character array variable named CityName and assign the value 'Istanbul, Turkey' to it. 2. Define a single precision two-element row vector named LatLong and assign the latitude of Istanbul to the first element and the longitude of Istanbul to the second element. 3. Define a 1 x 2 cell array named CityLocation. Assign the character array 'Istanbul, Turkey' to the first cell. Assign the single precision row vector of Istanbul's latitude and longitude to the second cell. Script> 1% defining character array variable 2 CityName = 'Istanbul, Turkey'; 3 4 % defining 2 element row vector 5 LatLong = [41.0082 28.9784]; 6 7% defining 1 x 2 cell array 8 CityLocation = {CityName; Lat Long}; 9 end Save My Solutions > C Reset MATLAB Documentation ?arrow_forwardq3 = range (30) # change q3 to a np.array # add 1 to each element in q3 # add 2 to the last 5 elements in q3 # subtract 10 to the first element in q2arrow_forward
- Match each data operator with its function: OFFSET Returns the number of the elements in an array. TYPE Returns the size of a variables byte. SIZEOF Returns the memory adress of a variable. LENGTHOF Returns number of bytes used in an array.arrow_forwardQ1. Answer the following bläi 5 Q1. Write a code to find the summation of all numbers that are less than or equal to 10 in the array. C= {-10, 35, 10, -5, 6, 16, -23, 94, -22, 12, 202, -520, O, .-3} ث إضافة ملفarrow_forwardQ3 using the column operator (:), create a column vector that contain all the column of A 3 A= 4 5 6. 7 13 14 15 16 17arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Programming with Microsoft Visual Basic 2017Computer ScienceISBN:9781337102124Author:Diane ZakPublisher:Cengage Learning
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning