A real number x
is given. Determine the value of the given piecewise continuous function y(x)
, if it exists, or display a message about the non-existence of the function for the given x
.
- Given a natural number
n
. Calculate the value of a given formula. - To solve the problem write two programs:
- the first program must use nested loops to calculate the formula;
- the second program must perform the calculation of the formula using a single cycle using the method of dynamic programming.
- Calculate the number of operations for each of the algorithms according to the method described in the lecture, adding to it the calculation of the number of calls to standard functions.
- The program must correctly solve the problem for any given for which the result of the calculation can be correctly represented by type double.
- Output the resulting data in a format with seven characters after the dot.
- Given real numbers
a
,b
, integern
and one-dimensional array of real numbersY[n]
. - Obtain a one-dimensional array of real numbers
Z[n]
, the elements of which arez_i = f(y1, y2,…, yn)
, wheref
is a given function, andy_i
are the elements of the vectorY[n]
. - Obtain the resulting value
R = g(z1, z2,…, zn)
, where g is a given function, andz_i
are the elements of the vectorZ[n]
. - The values of the variables
a
,b
andn
are the initial data, which are chosen independently so that the functionf
exists for the given values of these variables. - The program must correctly solve the problem with the input data
a
,b
,n
. - The value of the initial array
Y[n]
, the intermediate arrayZ[n]
and the resulting valueR
to display in a format with three characters after the dot.
The matrix of int numbers A[m,n]
is given. Determine the presence among all elements of the matrix of any of the numbers in the range [0,5] and its location (coordinates) by the method of binary search, if the elements of each column are separately arranged in descending order.
Implementation:
The file lab5_1.c
contains the code developed on requirements to the task. Here, a standard one-dimensional array binary search was applied to each row of our matrix. But this method is not very effective.
However, if we consider a binary search for a matrix and not for a one-dimensional array, then for this example the binary search should be performed in a different form, and not as suggested in the condition.
The following is an effective solution that works in O(2ln(n+m))
time (provided that n>=2 and m>=2, we have: log(n+m) < log(nm) = log(n) + log(m) < log(n+m) + log(n+m) = 2log(n+m)
, for a square matrix - O(n)
).
Let x
= the element we are trying to find in the matrix, e
= the current element that we are processing in the array.
- Let's start with the upper right element (provided that we have a sorted matrix).
- Compare this element with
x
:- if
e = x
, then return the position e, because we foundx
in a given matrix. - if
e > x
, then move to the left to check the elements smaller thane
(if you went beyond the matrix - go out of the loop). - if
e < x
, then move below to check the elements larger thane
(if they went beyond the matrix - we leave the cycle).
- if
- Repeat three conditions above until we find the element or return a message that the desired element was not found.
The following is my variation on this problem:
- Array elements are not sorted to sort them, make a nested loop.
- In the nested loop, each element will be compared with all the elements below it.
- If the element is larger than the element below it, they are interchangeable.
- After executing the nested loop, we get an array in ordered elements in nondecreasing order.
Program implementation
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
void main()
{
int i, j, k, a, m, n, x;
printf("Enter the dimension of the matrix (with a space): \n");
scanf("%d %d", &m, &n);
int Z[m][n];
srand(time(NULL));
printf("---------------------------\n");
printf("Matrix before sorting: \n");
for(i=0;i<m;++i) {
for(j=0;j<n;++j) {
Z[i][j]=20+rand()%10;
printf("%d ", Z[i][j]);
}
printf("\n");
}
printf("---------------------------\n");
printf("After sorting in nondecreasing order by rows: \n");
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
for (k =(j + 1); k < n; ++k) {
if (Z[i][j] > Z[i][k]) {
a = Z[i][j];
Z[i][j] = Z[i][k];
Z[i][k] = a;
}
}
}
}
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
printf(" %d", Z[i][j]);
}
printf("\n");
}
printf("---------------------------\n");
printf("After sorting in nondecreasing order by columns: \n");
for (j = 0; j < n; ++j) {
for (i = 0; i < m; ++i) {
for (k = i + 1; k < m; ++k) {
if (Z[i][j] > Z[k][j]) { // Comparison of other elements of the array
a = Z[i][j]; // Use a temporary variable to store the last value
Z[i][j] = Z[k][j]; // change the places of the variable
Z[k][j] = a; // save the last value
}
}
}
}
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
printf(" %d", Z[i][j]);
}
printf("\n");
}
printf("---------------------------\n");
int my_val = 0;
for (x = 20; x <= 25; ++x) {
int l = 0, t = n-1;
while ( l < n && t >= 0 ) {
if ( Z[l][t] == x ) {
printf("Element %d found at position (%d, %d)\n", x, l, t);
my_val ++;
}
if (Z[l][t] > x)
t--;
else
l++;
}
}
if (my_val == 0)
printf("Items from the range [20, 25] were not found in the matrix ...\n");
}
Program implementation
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
void swap(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void main() {
int i, j, m, n;
printf("Enter the dimension of the matrix (with a space): \n");
scanf("%d %d", &m, &n);
int Z[m][n];
srand(time(NULL));
printf("--------------------------\n");
printf("Matrix before sorting:\n");
for(i = 0; i < m; ++i){
for(j = 0; j < n; ++j){
Z[i][j] = 10 + rand()%90;
printf("%d ", Z[i][j]);
}
printf("\n");
}
for(j = 0; j < m; ++j) {
int left = 0,
right = n - 1;
while (left < right) {
for (int i = left; i < right; i++) {
if (Z[j][i] > Z[j][i + 1])
swap(&Z[j][i], &Z[j][i+1]);
}
right--;
for (int i = right; i > left; i--) {
if (Z[j][i - 1] > Z[j][i])
swap(&Z[j][i], &Z[j][i-1]);
}
left++;
}
}
printf("--------------------------\n");
printf("Matrix after shaker sorting:\n");
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
printf(" %d", Z[i][j]);
}
printf("\n"); }
}