Консультация № 71982
21.01.2007, 22:21
0.00 руб.
0 1 1
Помогите решить задачу с использованием функций с указателями в качестве аргументов:
Даны вещественные массивы А[4][6], B[4][6]. Для каждого массива определить сумму и количество отрицательных элементов, расположенных в нечетных строках. И если можно то написать комментарии.

Обсуждение

Неизвестный
22.01.2007, 03:45
общий
это ответ
Здравствуйте, Lena_25!
Вот вариант решения задачи

Приложение:
#include <stdio.h>#include <stdlib.h>#include <time.h>void calculate(double **array, int row, int col, double *summ, int *negative_count) { int i, j; (*summ) = 0.0; (*negative_count) = 0; for(i=0; i<row; i+=2) { for(j=0; j<col; j++) { if(array[i][j] < 0) { (*summ) += array[i][j]; (*negative_count)++; } } }}int main(int argc, char* argv[]) { int N = 4; int M = 6; double **A; double **B; int i, j; A = new double*[N]; for(i=0; i<N; i++) A[i] = new double[M]; B = new double*[N]; for(i=0; i<N; i++) B[i] = new double[M]; int negative_count; double summ; srand((unsigned)time(NULL)); for(i=0; i<N; i++) { for(j=0; j<M; j++) { A[i][j] = (double)(rand()%100) + (double)(1./(double)(rand()%100)); if(rand()%2 == 0) A[i][j] = -A[i][j]; B[i][j] = (double)(rand()%100) + (double)(1./(double)(rand()%100)); if(rand()%2 == 0) B[i][j] = -B[i][j]; } } printf("Array A:\n"); for(i=0; i<N; i++) { for(j=0; j<M; j++) printf("%.2f ", A[i][j]); printf("\n"); } printf("\nArray B:\n"); for(i=0; i<N; i++) { for(j=0; j<M; j++) printf("%.2f ", B[i][j]); printf("\n"); } calculate(A, N, M, &summ, &negative_count); printf("\nSumm of negative element in array A: %.2f\n", summ); printf("Count of negative element in array A: %d\n\n", negative_count); calculate(B, N, M, &summ, &negative_count); printf("Summ of negative element in array B: %.2f\n", summ); printf("Count of negative element in array B: %d\n", negative_count); for(i=0; i<N; i++) delete[] A[i]; delete[] A; for(i=0; i<N; i++) delete[] B[i]; delete[] B; return 0;}
Форма ответа