In: Computer Science
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.
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: