Skip to content

Methodical material. Parsing the topic of c++ pointers. Writing assignments and tests for students

License

Notifications You must be signed in to change notification settings

DaniilVdovin/Material-Pointer-Cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Material-Pointer-Cpp

Methodical material. Parsing the topic of c++ pointers. Writing assignments and tests for students

Pointers

  1. ✅ Write a function that takes the side of a square as arguments and returns its perimeter, area, and diagonal. Write a program to demonstrate how this function works.
    #include <iostream>
    #include <math.h>
    using namespace std;
    void fun(float* Perimeter, float* Square, float* Diagonal, float* Side) {
    *Perimeter = *Side * 4;
    *Diagonal = sqrtf(2 * *Side * *Side);
    *Square = powf(*Side, 2.0);
    }
    int main() {
    float Perimeter, Square, Diagonal, Side;
    cin >> Side;
    fun(&Perimeter, &Square, &Diagonal, &Side);
    cout.precision(2);
    cout << endl << fixed << Perimeter << " " << Diagonal << " " << Square << endl;
    return 0;
    }
  2. ✅ Write a function that takes three sides of a triangle as arguments and returns its perimeter and area. Write a program to demonstrate how this function works.
    #include <iostream>
    #include <math.h>
    using namespace std;
    void fun(float* Perimeter, float* Square, float* Side1, float* Side2, float* Side3) {
    *Perimeter = *Side1 + *Side2 + *Side3;
    *Square = sqrtf(*Perimeter/2 * (*Perimeter/2 - *Side1) * (*Perimeter/2 - *Side2) * (*Perimeter/2 - *Side3));
    }
    int main() {
    float Perimeter, Square, Side1, Side2, Side3;
    cin >> Side1 >> Side2 >> Side3;
    fun(&Perimeter, &Square, &Side1, &Side2, &Side3);
    cout.precision(2);
    cout << fixed << endl << Perimeter << " " << Square << endl;
    return 0;
    }
  3. ✅ Write a function that takes the radius of the circle as arguments and returns its area and the length of the arc. Write a program to demonstrate how this function works.
    #include <iostream>
    #define _USE_MATH_DEFINES
    #include <math.h>
    using namespace std;
    void fun(float* Perimeter, float* Square, float* Radius) {
    *Perimeter = 2 * M_PI * *Radius;
    *Square = M_PI*powf(*Radius,2.0);
    }
    int main() {
    float Perimeter, Square, Radius;
    cin >> Radius;
    fun(&Perimeter, &Square, &Radius);
    cout.precision(2);
    cout << fixed << endl << Perimeter << " " << Square << endl;
    return 0;
    }
  4. ✅ Write a function that takes the legs of a right triangle as arguments and returns its angles. Write a program to demonstrate how this function works.
    #include <iostream>
    #define _USE_MATH_DEFINES
    #include <math.h>
    using namespace std;
    void fun(float* A, float* B, float* Side1, float* Side2) {
    *B = (atanf(*Side2 / *Side1)*180)/M_PI;
    *A = 90 - *B;
    }
    int main() {
    float A, B, Side1, Side2;
    cin >> Side1 >> Side2;
    fun(&A, &B, &Side1,&Side2);
    cout.precision(2);
    cout << fixed << endl << A << " " << B << " " << 90.00 << endl;
    return 0;
    }
  5. ✅ Write a function that takes three sides of a parallelepiped as arguments and returns its volume and surface area. Write a program to demonstrate how this function works.
    #include <iostream>
    #define _USE_MATH_DEFINES
    #include <math.h>
    using namespace std;
    void fun(float* A, float* B, float* H, float* Volume, float* Square) {
    *Volume = *A * *B * *H;
    *Square = 2*(*A**B + *A**H + *B**H);
    }
    int main() {
    float A, B, H, Volume, Square;
    cin >> A >> B >> H;
    fun(&A, &B, &H, &Volume,&Square);
    cout.precision(2);
    cout << fixed << endl << Volume << " " << Square << " " << endl;
    return 0;
    }
  6. ✅ Write a function that takes two numbers as arguments and returns the greatest and least common divisors. Write a program to demonstrate how this function works.
    #include <iostream>
    #include <math.h>
    using namespace std;
    int NOD(int n1, int n2)
    {
    int div;
    if (n1 == n2) return n1;
    int d = n1 - n2;
    if (d < 0) { d = -d; div = NOD(n1, d); }
    else div = NOD(n2, d);
    return div;
    }
    int NOK(int n1, int n2){return n1 * n2 / NOD(n1, n2);}
    void fun(int* N1, int* N2, int* max_nod, int* min_nod) {
    *min_nod = NOK(*N1, *N2);
    *max_nod = NOD(*N1, *N2);
    }
    int main() {
    int N1 , N2, min_nod , max_nod;
    cin >> N1 >> N2;
    fun(&N1, &N2, &min_nod, &max_nod);
    cout.precision(2);
    cout << fixed << endl << min_nod << " " << max_nod << endl;
    return 0;
    }
  7. ✅ Write a function that takes the legs of a right triangle as arguments and returns the value of the hypotenuse, the radius of the inscribed and circumscribed circle. Write a program to demonstrate how this function works.
    #include <iostream>
    #include <math.h>
    using namespace std;
    void fun(float* ROS, float* RIS, float* G, float* Side1, float* Side2) {
    *G = sqrtf(powf(*Side1, 2.0) + powf(*Side2, 2.0));
    *RIS = (*Side1 + *Side2 - *G)/ 2;
    *ROS = *G / 2;
    }
    int main() {
    float A, B, G, ROS, RIS, Side1, Side2;
    cin >> Side1 >> Side2;
    fun(&ROS, &RIS, &G, &Side1, &Side2);
    cout.precision(2);
    cout << fixed << endl << G << " " << RIS << " " << ROS << endl;
    return 0;
    }
  8. ✅ Write a function that takes the height and radius of the cylinder as arguments, and returns its volume and surface area. Write a program to demonstrate how this function works.
    #include <iostream>
    #define _USE_MATH_DEFINES
    #include <math.h>
    using namespace std;
    void fun(float* R, float* H, float* Volume, float* Square) {
    *Volume = M_PI * powf(*R,2.0) * *H;
    *Square = 2 * M_PI * *R * (*R+*H);
    }
    int main() {
    float R, H, Volume, Square;
    cin >> H >> R;
    fun(&R, &H, &Volume, &Square);
    cout.precision(2);
    cout << fixed << endl << Volume << " " << Square << " " << endl;
    return 0;
    }
  9. ✅ Write a function that takes two numbers as arguments and returns a reduced fraction (25 15, 5/3). Write a program to demonstrate how this function works.
    #include <iostream>
    #define _USE_MATH_DEFINES
    #include <math.h>
    using namespace std;
    int gcd(int a, int b) {
    a = abs(a), b = abs(b);
    if (b == 0)return a;
    else return gcd(b, a % b);
    }
    void fun(int* N1, int* N2) {
    int nod = gcd(*N1, *N2);
    *N1 /= nod;
    *N2 /= nod;
    }
    int main() {
    int N1 , N2;
    cin >> N1 >> N2;
    fun(&N1, &N2);
    cout.precision(2);
    cout << fixed << endl << N1 << "/" << N2 << endl;
    return 0;
    }
  10. ✅ Write a function that takes three sides of a triangle as arguments and returns the perimeter of the triangle and the area calculated using the Heron formula. Write a program to demonstrate how this function works.
    #include <iostream>
    #include <math.h>
    using namespace std;
    void fun(float* A, float* B, float* C, float* Perimeter, float* Square) {
    *Perimeter = *A + *B + *C;
    float p = (*A + *B + *C) / 2;
    *Square = sqrtf(p * (p - *A)* (p - *B)* (p - *C));
    }
    int main() {
    float A, B, C, Perimeter, Square;
    cin >> A >> B >> C;
    fun(&A, &B, &C, &Perimeter, &Square);
    cout.precision(2);
    cout << fixed << Perimeter << " " << Square << " " << endl;
    return 0;
    }
  11. ❌ Write a function that takes the value of the acute angle of a right-angled triangle and the length of the hypotenuse as arguments, and returns the length of the legs, the area of the triangle and the radius of the circumscribed circle. Write a program to demonstrate how this function works.
  12. ❌ Write a function that takes the base of an isosceles trapezoid as arguments and returns the perimeter, area and height of the trapezoid. Write a program to demonstrate how this function works.
  13. ❌ Write a function that takes age (number of years) as an argument and returns the number of months, days, hours and minutes lived.
  14. ❌ Write a function that takes as an argument the value of the first and second elements of an arithmetic progression and returns the difference of this progression, the 15th element and the sum of its first 50 members.
  15. ❌ Write a function that takes as an argument the value of the first and second element of a geometric progression and returns the denominator of this progression, the 15th element and the sum of its first 50 members.

Dynamic arrays

  1. ✅ Write a program that generates a dynamic array of all primes before N and displays them on the screen.
    #include <iostream>
    using namespace std;
    bool simpl(int* n) {
    for (int i = 2; i <= sqrt(*n); i++)
    if (*n % i == 0) return false;
    return true;
    }
    int main() {
    int N,Size=0,j=0;
    cin >> N;
    for (int i = 2; i < N; i++)if(simpl(&i))Size++;
    int* array = new int[Size];
    for (int i = 2; i < N; i++) {
    if (simpl(&i)) {
    array[j] = i;
    cout << array[j] << ' '; j++;
    }
    }
    delete(array);
    return 0;
    }
    /*
    5 = 2 3
    10 = 2 3 5 7
    20 = 2 3 5 7 11 13 17 19
    */
  2. ✅ Write a program that, given a natural number n, prints all its prime natural divisors, taking into account the multiplicity. The running time of the program must be proportional to the root of n.
    #include <iostream>
    using namespace std;
    int main() {
    int n, d{ 2 }, k{ 4 },s=0;
    cin >> n;
    int* rs{(int*)calloc(s, sizeof(int))};
    while (k <= n)
    {
    while (n%d == 0)
    {
    s++;
    rs = (int*)realloc(rs,(s+1)*sizeof(int));
    rs[s-1] = d;
    n /= d;
    }
    k += 2 * d + 1; d += 1;
    }
    rs[s] = n;
    for (int i = 0; i <= s; i++) cout << rs[i] << ' ';
    free(rs);
    return 0;
    }
    /*
    6 = 2 3
    66 = 2 3 11
    132 = 2 2 3 11
    */
  3. ✅ Given a number N. Form a dynamic array consisting of the first N Fibonacci numbers (recall that they are given by the recurrent formula Fn = Fn-1 + Fn-2, F1 = F2 = 1).
    #include <iostream>
    using namespace std;
    int Fib(int n) {
    return n == 1 || n == 2 ? 1 : Fib(n - 1) + Fib(n - 2);
    }
    int main() {
    int n;
    cin >> n;
    int* rs{ (int*)calloc(n, sizeof(int)) };
    for (int i = 1; i <= n; ++i) rs[i] = Fib(i);
    for (int i = 0; i <= n; i++) cout << rs[i] << ' ';
    free(rs);
    return 0;
    }
    /*
    5 = 0 1 1 2 3 5
    10 = 0 1 1 2 3 5 8 13 21 34 55
    15 = 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
    */
  4. ❌ Given an array of integers, consisting of N elements. Fill it out from the keyboard. Count the number of odd elements before the minimum, if there are several of them, form a new array of them.
  5. ✅ Given an array consisting of N elements and an array consisting of M elements. Fill them out from the keyboard. Create an array consisting of the positive elements of the input arrays.
    #include <iostream>
    using namespace std;
    int main() {
    int n, m,rs=0,i=0;
    cin >> n >> m;
    int *N { (int*)calloc(n , sizeof(int)) },
    *M { (int*)calloc(m , sizeof(int)) },
    *RS{ (int*)calloc(rs, sizeof(int)) };
    for (i = 0; i < n; ++i) cin >> N[i];
    for (i = 0; i < m; ++i) cin >> M[i];
    for (i = 0; i < n; ++i){
    if (N[i] > 0) {
    rs++; RS = (int*)realloc(RS, rs * sizeof(int));
    RS[rs - 1] = N[i];
    }}
    for (i = 0; i < m; ++i) {
    if (M[i] > 0) {
    rs++; RS = (int*)realloc(RS, rs * sizeof(int));
    RS[rs - 1] = M[i];
    }}
    for (i = 0; i < rs; i++) cout << RS[i] << ' ';
    free(N); free(M); free(RS);
    return 0;
    }
    /*
    2 2 -1 -3 5 1 = 5 1
    2 2 -1 5 3 -5 = 5 3
    4 2 1 2 -3 4 -1 -2 = 1 2 4
    */
  6. ✅ Given an array consisting of N elements and an array consisting of M elements. Fill them out from the keyboard. Create an array consisting of even elements of the input arrays.
    #include <iostream>
    using namespace std;
    int main() {
    int n, m,rs=0,i=0;
    cin >> n >> m;
    int *N { (int*)calloc(n , sizeof(int)) },
    *M { (int*)calloc(m , sizeof(int)) },
    *RS{ (int*)calloc(rs, sizeof(int)) };
    for (i = 0; i < n; ++i) cin >> N[i];
    for (i = 0; i < m; ++i) cin >> M[i];
    for (i = 0; i < n; ++i){
    if (N[i] % 2 == 0) {
    rs++; RS = (int*)realloc(RS, rs * sizeof(int));
    RS[rs - 1] = N[i];
    }}
    for (i = 0; i < m; ++i) {
    if (M[i] % 2 == 0) {
    rs++; RS = (int*)realloc(RS, rs * sizeof(int));
    RS[rs - 1] = M[i];
    }}
    for (i = 0; i < rs; i++) cout << RS[i] << ' ';
    free(N); free(M); free(RS);
    return 0;
    }
    /*
    2 2 2 4 5 1 = 2 4
    2 3 1 2 5 6 8 = 2 6 8
    4 2 1 2 3 4 9 6 = 2 4 6
    */
  7. ✅ Given an array of N elements. Create a new array consisting of the positive elements of the input array.
    #include <iostream>
    using namespace std;
    int main() {
    int n,rs=0,i=0;
    cin >> n;
    int *N { (int*)calloc(n , sizeof(int)) },
    *RS{ (int*)calloc(rs, sizeof(int)) };
    for (i = 0; i < n; ++i) cin >> N[i];
    for (i = 0; i < n; ++i){
    if (N[i] > 0) {
    rs++; RS = (int*)realloc(RS, rs * sizeof(int));
    RS[rs - 1] = N[i];
    }}
    for (i = 0; i < rs; i++) cout << RS[i] << ' ';
    free(N); free(RS);
    return 0;
    }
    /*
    3 -1 -2 3 = 3
    4 -2 -10 1 55 = 1 55
    4 2 10 -1 -55 = 2 10
    */
  8. ✅ Given an array of N elements. Create a new array consisting of the negative elements of the input array.
    #include <iostream>
    using namespace std;
    int main() {
    int n,rs=0,i=0;
    cin >> n;
    int *N { (int*)calloc(n , sizeof(int)) },
    *RS{ (int*)calloc(rs, sizeof(int)) };
    for (i = 0; i < n; ++i) cin >> N[i];
    for (i = 0; i < n; ++i){
    if (N[i] < 0) {
    rs++; RS = (int*)realloc(RS, rs * sizeof(int));
    RS[rs - 1] = N[i];
    }}
    for (i = 0; i < rs; i++) cout << RS[i] << ' ';
    free(N); free(RS);
    return 0;
    }
    /*
    3 -1 -2 3 = -1 -2
    4 -2 -10 1 55 = -2 -10
    4 2 10 -1 -55 = -1 -55
    */
  9. ❌ Given a dynamic array, the size of which is entered from the keyboard. Fill the array with random numbers. Remove K elements from it, starting from the middle of the array.
  10. ❌ Write a program that creates a dynamic array and remove an element from the number N from it, add K elements, starting from the number N. N is entered from the keyboard
  11. ✅ Given an array of integers, consisting of N elements. Fill it out from the keyboard. Create an array consisting of elements whose values are greater than the value of the previous element (starting from the second).
    #include <iostream>
    using namespace std;
    int main() {
    int n,rs=0,i=0;
    cin >> n;
    int *N { (int*)calloc(n , sizeof(int)) },
    *RS{ (int*)calloc(rs, sizeof(int)) };
    for (i = 0; i < n; ++i) cin >> N[i];
    for (i = 1; i < n; ++i){
    if (N[i] > N[i-1]) {
    rs++; RS = (int*)realloc(RS, rs * sizeof(int));
    RS[rs - 1] = N[i];
    }}
    for (i = 0; i < rs; i++) cout << RS[i] << ' ';
    free(N); free(RS);
    return 0;
    }
    /*
    5 1 2 3 2 1 = 2 3
    5 1 2 3 2 3 = 2 3 3
    7 7 5 6 1 3 4 5 = 6 3 4 5
    */
  12. ✅ Given an array consisting of N elements and an array consisting of M elements. Fill them out from the keyboard. Find the minimum in the array N, and the maximum in the array M. Form an array consisting of N array elements preceding the minimum element and M array elements following the maximum.
    #include <iostream>
    using namespace std;
    int MaxIndex(int* Ar,int n) {
    int mv = 0, mi = 0;
    for (int i = 0; i < n; i++){
    if (mv < Ar[i]) {
    mv = Ar[i]; mi = i;
    }}
    return mi;
    }
    int MinIndex(int* Ar, int n) {
    int mv = 1000, mi = 0;
    for (int i = 0; i < n; i++) {
    if (mv > Ar[i]) {
    mv = Ar[i]; mi = i;
    }}
    return mi;
    }
    int main() {
    int n, m, rs = 0, i = 0;
    cin >> n >> m;
    int * N { (int*)calloc(n , sizeof(int)) },
    * M { (int*)calloc(m , sizeof(int)) },
    * RS{ (int*)calloc(rs, sizeof(int)) };
    for (i = 0; i < n; ++i) cin >> N[i];
    for (i = 0; i < m; ++i) cin >> M[i];
    for (i = 0; i < MinIndex(N,n); ++i) {
    rs++; RS = (int*)realloc(RS, rs * sizeof(int));
    RS[rs - 1] = N[i]; }
    for (i = MaxIndex(M, m) + 1; i < m; ++i) {
    rs++; RS = (int*)realloc(RS, rs * sizeof(int));
    RS[rs - 1] = M[i]; }
    for (i = 0; i < rs; i++) cout << RS[i] << ' ';
    free(N); free(M); free(RS);
    return 0;
    }
    /*
    3 3 3 2 1 3 2 1 = 3 2 2 1
    4 4 2 5 4 6 9 5 8 7 = 5 8 7
    2 2 1 2 2 1 = 1
    */
  13. ✅ Change the dynamic array of integers so that its positive elements become negative and vice versa.
    #include <iostream>
    using namespace std;
    int main() {
    int n, i = 0;
    cin >> n;
    int* N{ (int*)calloc(n , sizeof(int)) };
    for (i = 0; i < n; ++i) cin >> N[i];
    for (i = 0; i < n; ++i) N[i]*=-1;
    for (i = 0; i < n; i++) cout << N[i] << ' ';
    free(N);
    return 0;
    }
    /*
    5 1 -2 3 -4 5 = -1 2 -3 4 -5
    6 -1 -2 -0 5 3 -9 = 1 2 0 -5 -3 9
    2 0 -0 = 0 0
    */
  14. ✅ Given an array consisting of N elements and an array consisting of M elements. Fill them in with random numbers. Create an array consisting of the positive elements of the N array and the negative elements of the M array.
    #include <iostream>
    using namespace std;
    int main() {
    int n, m, rs = 0, i = 0;
    cin >> n >> m;
    int* N{ (int*)calloc(n , sizeof(int)) },
    * M{ (int*)calloc(m , sizeof(int)) },
    * RS{ (int*)calloc(rs, sizeof(int)) };
    for (i = 0; i < n; ++i) {
    cin >> N[i];
    if (N[i] > 0) {
    rs++; RS = (int*)realloc(RS, rs * sizeof(int));
    RS[rs - 1] = N[i];
    }}
    for (i = 0; i < m; ++i) {
    cin >> M[i];
    if (M[i] < 0) {
    rs++; RS = (int*)realloc(RS, rs * sizeof(int));
    RS[rs - 1] = M[i];
    }}
    for (i = 0; i < rs; i++) cout << RS[i] << ' ';
    free(N); free(M); free(RS);
    return 0;
    }
    /*
    4 4 5 -4 2 -2 -1 2 -3 4 = 5 2 -1 -3
    2 2 -1 2 -2 1 = 2 -2
    2 2 -1 1 1 -1 = 1 -1
    */
    And Random Variant. Unfortunately it cannot be tested.
    #include <iostream>
    using namespace std;
    int main() {
    int n, m, rs = 0, i = 0;
    cin >> n >> m;
    int* N{ (int*)calloc(n , sizeof(int)) },
    * M{ (int*)calloc(m , sizeof(int)) },
    * RS{ (int*)calloc(rs, sizeof(int)) };
    for (i = 0; i < n; ++i) {
    N[i] = int((-100 + rand()/100));
    if (N[i] > 0) {
    rs++; RS = (int*)realloc(RS, rs * sizeof(int));
    RS[rs - 1] = N[i];
    }}
    for (i = 0; i < m; ++i) {
    M[i] = int((-100 + rand()/100));
    if (M[i] < 0) {
    rs++; RS = (int*)realloc(RS, rs * sizeof(int));
    RS[rs - 1] = M[i];
    }}
    for (i = 0; i < rs; i++) cout << RS[i] << ' ';
    free(N); free(M); free(RS);
    return 0;
    }
  15. ❌ Given an array consisting of N elements and an array consisting of M elements. Fill them in with random numbers. Create an array consisting of prime numbers of arrays N and M. The numbers in the new array cannot be repeated.

About

Methodical material. Parsing the topic of c++ pointers. Writing assignments and tests for students

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Languages