Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- #include <cstdlib>
- #include <iomanip>
- using namespace std;
- #define N 5
- void print24(string);
- void fillArray(int**);
- void printArray(int **);
- void halfArray(int **);
- int non_zero(int *, int);
- int main()
- {
- char str[20];
- int ** arr = new int*[N];
- for (unsigned i = 0; i<N; i++) {
- arr[i] = new int[N];
- }
- fillArray(arr);
- printArray(arr);
- halfArray(arr);
- printArray(arr);
- int sz;
- srand(time(NULL));
- cout << "Vyvedete razmer na masiva: ";
- cin >> sz;
- int* arr2 = new int[sz];
- for (int i = 0; i < sz; i++) {
- arr2[i] = rand() % 20 - 10;
- cout << arr2[i] << " ";
- }
- cout << endl << non_zero(arr2, sz) << endl;
- for (unsigned i = 0; i<N; i++) {
- delete arr[i];
- }
- delete[] arr;
- return 0;
- }
- //zadacha 1
- void fillArray(int** arr) {
- srand(time(NULL));
- for (unsigned i = 0; i<N; i++) {
- for (unsigned j = 0; j<N; j++) {
- arr[i][j] = rand() % 10;
- }
- }
- }
- void printArray(int ** arr) {
- for (int i = 0; i<N; i++) {
- cout << " ";
- for (int j = 0; j < N; j++) {
- cout << setw(3) << arr[i][j];
- if (j<N - 1) cout << " ";
- }
- cout << " ";
- cout << endl;
- }
- cout << endl;
- }
- void halfArray(int ** arr) {
- for (int i = 0; i < N; i++) {
- int counter = 1;
- int cnt = N;
- for (int j = N-1; j >= 0; j--) {
- if (counter < i+1 ) {
- arr[i][j] = 0;
- }
- else {
- arr[i][j] = cnt--;
- }
- counter++;
- }
- }
- }
- //Zadacha 3
- int non_zero(int * arr, int sz) {
- if (sz > 0) return ((arr[sz - 1])>0) ? 1 + non_zero(arr, sz - 1) : non_zero(arr, sz - 1);
- return 0;
- }
Add Comment
Please, Sign In to add comment