Question

In: Computer Science

1. Starting problems These first problems are all based on the mathematical concept of a matrix...

1. Starting problems

These first problems are all based on the mathematical concept of a matrix because it's a great fit for doing multi-dimensional arrays in C++ and is a very common use of this structure! Don't worry if you're a bit rusty on matrices, we explain everything below.

Definition: a matrix is a rectangular array of data. It can be square or rectangular.

Examples:

1 1 3
1 0 4

This matrix has 2 rows and 3 columns

1 0
0 1

This matrix has 2 rows and 2 columns (and is an Identity matrix, see below)

1 2
4 5
7 8
9 0

This matrix has 4 rows and 2 columns

Note that there are no empty cells, just zero cells, in these numerical examples.

1-1. Given a matrix of integers, return the sum of the elements along the main diagonal (top left bottom right)

Signature: int diagonal(int array[4][4])

(Note. This function should be placed in function-1-1.cpp and the matching main function in main-1-1.cpp)

1-2. Given a matrix of integers with 10 rows and 10 columns, write a function and program to determine if the matrix is an identity matrix or not. Return a 1 if it is an identity matrix and a 0 if not. (Definition: An identity matrix is one with 1's down the main diagonal and 0's elsewhere)

Signature: int identity(int array[10][10])

1-3 Given a matrix of integers, count the amount of times each number 0-9 appears. Print out your results on one line in the following form:

0:number of zeros;1:number of ones;2:number of twos;3:number of threes;4:number of fours;5:number of fives;6:number of sixes;7:number of sevens;8:number of eights;9:number of nines;

For example, if you are passed an identify matrix, it contains 12 zeros and 4 ones and no other numbers from 0 to 9, your output would be:

0:12;1:4;2:0;3:0;4:0;5:0;6:0;7:0;8:0;9:0;

Note the colons and semi-colons.

Signature: void count_numbers(int array[4][4])

1-4 Given a matrix, print out the scaled version of the matrix, where you multiply every element in the matrix by the same numeric value. Elements should be printed by row, with a space separating elements on a row, and a newline character separating rows.

Example:

int scale = 3 ;
int threebythree[3][3] = {{0,1,2},{3,4,5},{6,7,8}} ;
print_scaled_matrix(threebythree,scale) ;

This should produce the following output:

0 3 6
9 12 15
18 21 24
Signature: void print_scaled_matrix(int array[3][3],int scale)

1-5 Given two two-dimensional arrays, add them together and print the result as in 1-4 above. What relationship needs to be between the sizes of the two matrices?

Example:

int matrix1[3][3] = {{0,1,2},{3,4,5},{6,7,8}} ;
int matrix2[3][3] = {{0,0,0},{2,2,2},{-5,-4,8}} ;
print_summed_matrices(matrix1,matrix2) ;

This should produce the following output:

0 1 2
5 6 7
1 3 16
Signature: void print_summed_matrices(int array1[3][3],int array2[3][3])

2. Medium problems

2-1. Given a positive decimal number, write a function that transforms it into binary, and prints out the result. You may assume that the string contains at least 1 and no more than 9 decimal digits. The output should only include the binary digits followed by a new line.

Hint: You might need to convert a string to integer. Have a look at the stoi(string) in the C++11 string library. If you need to compile with C++11, include the compilation flag -std=c++11 in your compile command.

Signature: void print_as_binary(std::string decimal_number)

2-2. Given a binary number represented as an array, write a function that takes the array and its size as a parameter, and returns the integer value. You may assume that there are at least 1 and no more than 30 numbers in the array and that all the values are either 0 or 1. The array is ordered with most significant binary digit at the start (index 0) and the least significant digit at the end.

Signature: int binary_to_number(int binary_digits[], int number_of_digits)

Definition: a palindrome is a sequence that reads the same backwards as forwards. Hence, 101, 120021 and 1 are all numerical palindromes. A palindrome array would be of the form [1,2,2,1] for example. An empty array is a palindrome by definition.

2-3. Given an array of integers, write a function to calculate the sum of the elements if it is a palindrome array. If it is not a palindrome array, your function must return -2. Your function must call separate functions to check whether or not the array is a palindrome and to calculate the sum of its elements. If the length is 0 or negative each function must return -1 or false as its result.

Signature: int sum_if_a_palindrome(int integers[], int length)
Signature: bool is_a_palindrome(int integers[], int length)
Signature: int sum_elements(int integers[], int length)

2-4. Given an array of integers, write a function to determine its maximum and minimum elements and then return their sum. Your function must call separate functions to identify the maximum and minimum elements. If the length is 0 or negative each function must return -1 as its result.

Signature: int sum_min_and_max(int integers[], int length)
Signature: int max_integer(int integers[], int length)
Signature: int min_integer(int integers[], int length)

3. Tricky problems

With these problems, we are giving you a free hand at writing the signature of your functions. Follow the templates from the easier problems, and ask for help if unsure! The following two problems are set in the context of a supermarket checkout. There is only one checkout with a single line of customers each with items they are wanting to buy.

Note: The web submission system will ignore these two programs so no marks will be awarded for them even if you submit them.

3-1. Write code to capture the scenario: at the checkout, the customer who is first in the queue pays an amount equal to the value of his/her products and then leaves the supermarket. The next customer in the queue is then served. For each customer in the queue (you have initialised the queue with 10 customers), print out how much they have to pay.

3-2. Write code to capture the scenario: you have been serving customers for a while and your queue no longer has 10 customers in it. A new customer arrives at the checkout.

Solutions

Expert Solution

Please go through code and output.

CODE:

#include <iostream>

using namespace std;

//1-1

int diagonal(int array[4][4])

{

int sum = 0;

for(int i = 0; i<4; i++)

{

for(int j=0; j<4; j++)

{

sum += array[i][j];

}

}

return sum;

}

//1-2

int identity(int array[10][10])

{

for(int i=0; i<10; i++)

{

for(int j=0; j<10; j++)

{

if(i==j)

{

if(array[i][j] != 1)

return 0;

}

else

{

if(array[i][j] != 0)

return 0;

}

}

}

return 1;

}

//1-3

void count_numbers(int array[4][4])

{

int num[10] = {0};

for(int i=0; i<4; i++)

{

for(int j=0; j<4; j++)

{

switch(array[i][j])

{

case 0:

num[0] += 1;

break;

case 1:

num[1] += 1;

break;

case 2:

num[2] += 1;

break;

case 3:

num[3] += 1;

break;

case 4:

num[4] += 1;

break;

case 5:

num[5] += 1;

break;

case 6:

num[6] += 1;

break;

case 7:

num[7] += 1;

break;

case 8:

num[8] += 1;

break;

case 9:

num[9] += 1;

break;

}

}

}

for(int i=0; i<10; i++)

{

cout << i << ":" << num[i] << ";";

}

cout << endl;

}

//1-4

void print_scaled_matrix(int array[3][3], int scale)

{

for(int i=0; i<3; i++)

{

for(int j=0; j<3; j++)

{

cout << array[i][j]*scale << " ";

}

cout << endl;

}

}

void print_summed_matrices(int array1[3][3],int array2[3][3])

{

for(int i=0; i<3; i++)

{

for(int j=0; j<3; j++)

{

cout << array1[i][j]+array2[i][j] << " ";

}

cout << endl;

}

}

int main()

{

//1-1

int diagonal_v = 0;

int fourbyfour[4][4] = {{0,1,2,1},{3,4,5,1},{6,7,8,1},{3,4,5,1}};

diagonal_v = diagonal(fourbyfour);

cout << "Diagonal : " << diagonal_v << endl << endl;

//1-2

int tenbyten[10][10] = {{1,0,0,0,0,0,0,0,0,0},

{0,1,0,0,0,0,0,0,0,0},

{0,0,1,0,0,0,0,0,0,0},

{0,0,0,1,0,0,0,0,0,0},

{0,0,0,0,1,0,0,0,0,0},

{0,0,0,0,0,1,0,0,0,0},

{0,0,0,0,0,0,1,0,0,0},

{0,0,0,0,0,0,0,1,0,0},

{0,0,0,0,0,0,0,0,1,0},

{0,0,0,0,0,0,0,0,0,1},

};

if(identity(tenbyten) == 1)

{

cout << "Matrix is Identity" << endl<< endl;

}

else

{

cout << "Matrix is Not Identity" << endl<< endl;

}

//1-3

count_numbers(fourbyfour);

cout << endl;

//1-4

int scale = 3 ;

int threebythree[3][3] = {{0,1,2},{3,4,5},{6,7,8}} ;

print_scaled_matrix(threebythree,scale) ;

cout << endl;

//1-5

int matrix1[3][3] = {{0,1,2},{3,4,5},{6,7,8}} ;

int matrix2[3][3] = {{0,0,0},{2,2,2},{-5,-4,8}} ;

print_summed_matrices(matrix1,matrix2) ;

}

OUTPUT:


Related Solutions

For each of these problems, please use first a mathematical formula to solve the problem. Second...
For each of these problems, please use first a mathematical formula to solve the problem. Second use Excel spreadsheet to also solve the problem. You are thinking about leasing a car. The purchase price of the car is $30,000. The residual value (the amount you could pay to keep the car at the end of the lease) is $15,000 at the end of 36 months. Assume the first lease payment is due one month after you get the car. The...
declare a struct named matrix and typedef the struct to type name Matrix. A mathematical matrix...
declare a struct named matrix and typedef the struct to type name Matrix. A mathematical matrix is a two-dimensional, rectangular data structure. The matrix struct should have three fields (in this order): an unsigned variable named "rows", an unsigned variable named "columns", and a pointer to a pointer to double (i.e. double**) named "data". (Code #including matrix.h should be able to declare Matrix variables.) In matrix.c, implement the create_matrix function. The Matrix should be filled with 0.0 values. data[i] should...
Do the following problems: a. Use mathematical induction to show that 1 + 2 + 22...
Do the following problems: a. Use mathematical induction to show that 1 + 2 + 22 +···+ 2n = 2n+1 − 1 for all non-negative integers n. b. A coin is weighted so that P(H) = 2/3 and P(T) = 1/3. The coin is tossed 4 times. Let the random variable X denote the number of heads that appear. (x) Find the distribution of X; (xx) Find the expectation E(X). c. Show a derivation of Bayes’ Theorem
In each of Problems 16 through 25, find all eigenvalues and eigenvectors of the given matrix....
In each of Problems 16 through 25, find all eigenvalues and eigenvectors of the given matrix. 16) A= ( 1st row 5 −1 2nd row 3 1) 23) A= (1st row 3 2 2, 2nd row 1 4 1 , 3rd row -2 -4 -1)
What is matrix notation and all the matrix operations?
What is matrix notation and all the matrix operations?
1. The correct mathematical models for the first four seconds of the motion map below are:...
1. The correct mathematical models for the first four seconds of the motion map below are: ( can have multiple answers) http://tinypic.com/view.php?pic=20u4v4o&s=8#.U62fWF6aL1o << graph that goes to the question A) x(m)=1/2(-10m/s^2)t^2(s^2)+(10m/s)t(s), 0 < t <3s B) v(m/s)=(-10m/s^2)t(s)+(20m/s), 0 < t <3s C) x(m)=(-10m/s) delta t(s) +5m ,3 < t <4s D) v^2(m^2/s^2) = 2(-10m/s^2) delta x(m) +(400m^2/s^2), 0 < t <3s 2. Examine the velocity versus time graph below. Select the mathematical models that COULD describe the graph from the...
What is similarity and difference between first principle induction mathematical and second principle induction mathematical ?...
What is similarity and difference between first principle induction mathematical and second principle induction mathematical ? When to use each principle? are there characteristics that distinguish the issue to be solved by the first or second principle?!
In parts a-d evaluate the following determinants. show all steps. a. 2x2 matrix the first row...
In parts a-d evaluate the following determinants. show all steps. a. 2x2 matrix the first row being 1 and 2 the second row being -3 and 4. b. 3x3 matrix, the first row being 2,1, 5, the second row being 0, 3, 2, the third row being 0, 0, 4. c. 3x3 matrix, the first row being 3, -1, 4, the second row being 2, -2, 3, the third row being 1, -1, 2 d. 4x4 matrix, the first row...
Matrix Calculator Goals Write, compile and test a matrix calculator program Review the concept of pointers,...
Matrix Calculator Goals Write, compile and test a matrix calculator program Review the concept of pointers, and multi-dimensional dynamic arrays Create a simple makefile Write a robust input validation. The purpose of this lab is to get familiar with the software environment we will be using this term, and review some C++ concepts to get prepared for future projects and labs. This lab might be very time-consuming since there are a lot of concepts you need to pick up. For...
Let B equal the matrix below: [{1,0,0},{0,3,2},{2,-2,-1}] (1,0,0 is the first row of the matrix B)...
Let B equal the matrix below: [{1,0,0},{0,3,2},{2,-2,-1}] (1,0,0 is the first row of the matrix B) (0,3,2 is the 2nd row of the matrix B (2,-2,-1 is the third row of the matrix B.) 1) Determine the eigenvalues and associated eigenvectors of B. State both the algebraic and geometric multiplicity of the eigenvalues. 2) The matrix is defective. Nonetheless, find the general solution to the system x’ = Bx. (x is a vector)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT