Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Programmirovanie.docx
Скачиваний:
0
Добавлен:
01.03.2025
Размер:
187 Кб
Скачать
☆

Вариант №3

Дана матрица вещественных чисел A[N][M]. Сформировать массив Y[K] из положительных элементов матрицы

#include <iostream>

#define SIZE 100

using namespace std;

void Vvod(int n, int m, double arr[][SIZE])

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

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

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

cin >> arr[i][j];

} }

}

void Vivod(int n, int m, double arr[][SIZE])

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

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

cout << arr[i][j] << " "; }

cout<<endl;

}

cout<<endl;

}

void Vivod(int n, double *arr)

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

cout << arr[i] << " "; }

cout<<endl;

}

void main(){

double A [SIZE][SIZE], Y[SIZE];

int n,m, N=0;

cout << "ROW" << endl;

cin >> n;

cout << "COL" << endl;

cin >> m;

if((n > 100 && n <= 0) || (m > 100 && m <= 0 )){

cout<<"Error size"; system("pause"); return;

}

cout << "Massiv A" << endl;

Vvod(n,m, A);

cout << "\nMassiv A" << endl;

Vivod(n, m, A);

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

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

if( A[i][j] > 0){

Y[N++]=A[i][j];

}

} cout<<endl;

}

cout << "\nMassiv Y" << endl;

Vivod(N, Y);

cout << endl;

}

Ввести символьную строку. Посчитать количество слов в строке. Причем необходимо учесть, что строка может начинаться и заканчиваться пробелами, а также между словами могут быть несколько пробелов.

#include <stdio.h>

#include <iostream>

int getNum(char*);

void main()

{

setlocale(LC_ALL, "Russian");

char str[100];

puts("Введите несколько слов:");

gets(str);

printf("Количество слов: %d\n", getNum(str));

}

int getNum(char* str)

{

int i=0, k=0;

while(str[i]==' ')

{

++i;

}

for(i; i<strlen(str)-1; ++i)

{

if(str[i]==' ')

{

++k;

if(str[i+1]==' ')

--k;

}

}

return k+1;

}

Вариант №4

Дана матрица вещественных чисел A[M][N]. Сформировать массив Y[K] из отрицательных элементов матрицы, просматривая матрицу по строкам.

#include <iostream>

#define SIZE 100

using namespace std;

void Vvod(int n, int m,double arr[][SIZE])

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

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

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

cin >> arr[i][j];

} }

}

void Vivod(int n,int m,double arr[][SIZE])

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

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

cout << arr[i][j] << " "; }

cout<<endl;

}

cout<<endl;

}

void Vivod(int n, double *arr)

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

cout << arr[i] << " "; }

cout<<endl;

}

void main(){

double A [SIZE][SIZE], Y[SIZE];

int n,m, N=0;

cout << "ROW" << endl;

cin >> n;

cout << "COL" << endl;

cin >> m;

if((n > 100 && n <= 0) || (m > 100 && m <= 0 )){

cout<<"Error size"; system("pause"); return;

}

cout << "Massiv A" << endl;

Vvod(n,m, A);

cout << "\nMassiv A" << endl;

Vivod(n, m, A);

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

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

if( A[i][j] < 0){

Y[N++]=A[i][j];

}

}

cout<<endl;

}

cout << "\nMassiv Y" << endl;

Vivod(N, Y);

cout << endl;

}

Организовать ввод целых чисел с клавиатуры. Вычислить среднее значение чисел, которые делятся на 5 без остатка. При вводе числа 0 результат вывести на экран. Массив не использовать.

#include <iostream>

#include <windows.h>

int getInput();

int getNumber();

void getOut();

int sum = 0;

int count = 0;

void main()

{

setlocale(LC_ALL,".1251");

while(getInput() != -1)

{

}

getOut();

}

void getOut()

{ printf("\nСреднее значение делящихся на 5 без остатка чисел:\n%d\n", sum/count);

}

int getInput()

{

int num;

puts("Введите число");

num = getNumber();

if(num != -1)

{

sum += num;

}

return num;

}

int getNumber()

{ int num = 0;

scanf("%d", &num);

if(num == 0)

{

return -1;

}

if(num % 5 == 0)

{ count++;

return num;

}

else

{

return 0;

}

}