Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ОПАМ.doc
Скачиваний:
0
Добавлен:
01.07.2025
Размер:
326.66 Кб
Скачать

Int main()

{

cout<<"Merge sort"<<endl;

cout<<"Enter number of the components (<10) "<<endl;

cin>>n;

input(masif,n);

cout<<"Generated array"<<endl;

Output(masif,n);

Sort(1,n);

cout<<"Sorted array"<<endl;

Output(masif,n);

system("pause");

}

Приклад 7.14

/* Використання масивів і покажчиків */

#include <iostream>

using namespace std;

int n;

typedef int array [10];

array *ptr;

//======================= введення масиву ========================

array* input(int size)

{

cout<<"input array"<<endl;

ptr=new array[10];

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

cin>>*ptr[i];

return ptr;

}

//====================== виведення масиву ========================

void output(array* p)

{

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

cout<<*p[i]<<" ";

cout<<"\n";

}

//============= сортування масиву методом Шелла =================

void sort(array* p)

{

for (int k=n/2; k>0; k/=2)

for (int i=k; i<n; i++)

{

for (int j=i-k; j>=0&&*p[j]>*p[j+k]; j-=k)

{

int temp=*p[j];

*p[j]=*p[j+k];

}

}

}

//=================== головна програма ============================

Void main()

{

cout<<"Using pointer and array"<<endl;

cout<<"Number of array's members? ";

cin>>n;

ptr=input(n);

cout<<"Entered array"<<endl;

output(ptr);

sort(ptr);

cout<<"Sorted array"<<endl;

output(ptr);

system("pause");

}

Приклад 7.15

/* Пошук максимального елемента матриці */

#include <iostream>

#include <stdlib.h>

#include <time.h>

using namespace std;

int n=5;

int a[5][5];

int Max;

int imax, jmax;

//============= генерація значень елементів матриці ===============

void input()

{

srand((unsigned)(time(NULL)));

cout<<"Maximum of matrix"<<endl;

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

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

a[i][j]=rand()%30;

}

//=================== виведення матриці ===========================

void output()

{

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

{

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

cout<<a[i][j]<<" ";

cout<<endl;

}

}

//===== пошук максимального елемента матриці та його індексів =====

void maximum()

{

Max=a[0][0];

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

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

if (Max < a[i][j])

{

Max=a[i][j];

imax=i;

jmax=j;

}

}

//===================== головна функція =============================

Int main()

{

input();

cout<<"Generated matrix"<<endl;

output();

maximum();

cout<<" max="<<Max<<" imax="<<imax<<" jmax="<<jmax<<endl;

system("pause");

}

Приклад 7.16

/* Видалення заданого рядка матриці */

#include <iostream>

#include <stdlib.h>

#include <time.h>

using namespace std;

int n,m;

int a[5][5];

//================ генерація матриці =======================

void input()

{

srand((unsigned)(time(NULL)));

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

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

a[i][j]=rand()%20;

}

//=============== виведення матриці ========================

void output()

{

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

{

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

cout<<a[i][j]<<" ";

cout<<endl;

}

}

//================= головна функція ========================