Untitled
unknown
c_cpp
2 years ago
2.5 kB
11
Indexable
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
// 4 8 1 2 0
// 3 6 8 7 1
// 9 0 4 1 6
// 8 6 0 3 7
// 1 3
// 4 8 1 2 0 3 6 8 7 1 9 0 4 1 6 8 6 0 3 7
// функция заполнения массива
void FillArr(int *arr, const int rows, const int cols );
// функция вывода массива
void PrintArr(const int *arr, const int rows, const int cols);
// функция возвращает минимальный элемент массива
int GetMin(const int *arr, const int rows, const int cols);
// функция возвращает среднее арифметическое элементов указанной строки
double GetAvgRow(const int *arr, const int numRow, const int rows, const int cols);
int main() {
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
srand(time(0));
const int rows = 4, cols = 5;
const int size = rows * cols;
int arr[ size ];
int numRow;
FillArr(arr, rows, cols); // имя массива - это указатель на первый элемент
PrintArr(arr, rows, cols);
printf("Минимальный эл-т массива = %d\n", GetMin(arr, rows, cols));
printf("Ввведите номер строки: ");
scanf("%d", &numRow);
--numRow;
printf("Среднее арфметическое = %d\n", GetAvgRow(arr, rows, cols));
// пользователь вводит номер строки для расчёта среднего арифметического
// ...
return 0;
}
void FillArr(int *arr, const int rows, const int cols ) {
const int size = rows * cols;
int i;
for( i = 0; i < size; ++i)
arr[ i ] = rand() % 11 - 5;
}
//int GetMin(const int *arr, const int rows, const int cols) {
//int i;
//int min = arr[0];
//for( i = 1; i < size; ++i)
// if (arr[i] < min)
// min = arr[i];
//return min;
//}
double GetAvgRow(const int *arr, const int numRow, const int rows, const int cols) {
int j;
double sum = 0;
for (j = 0; j < cols; j++) {
sum += arr[numRow * cols + j ];
}
double avg = avg / cols;
}
return avg;
void PrintArr(const int *arr, const int rows, const int cols) {
int i, j;
for( i = 0; i < rows; ++i ) {
for( j = 0; j < cols; ++j )
printf("%5d", arr[ i * cols + j ] );
puts("");
}
}Editor is loading...