[ create a new paste ] login | about

Link: http://codepad.org/IQAY7Y0K    [ raw code | output | fork ]

C, pasted on Sep 28:
/*
стр 83 зад 30
Дана вещ. квадратная матрица размерности N. Посчитать x1*xN + x2*xN-1 + ... +
xN*x1, где xk - наибольшее значение элементов k-ой строки матрицы
*/
 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
int main(void)
{
    float **a;
    float *max, sum;
    int i, j, N;
    
    
    srand(time(NULL));
    N = 3 + rand() % 3;//тут понятное дело нужен ввод но мы обойдёмся рандомом
    a = (float **)malloc(N*sizeof(float *));
    max = (float *)malloc(N*sizeof(float));
    
    for (i = 0; i < N; i++) 
    {
        a[i] = (float *)malloc(N*sizeof(float));
        for (j = 0; j < N; j++) 
        {
            a[i][j] = (rand() % 1000) / 10.0;
            printf("%3.1f ", a[i][j]);
        }
        printf("\n");
    }
    
    for (i = 0; i < N; i++) {
        max[i] = a[i][0];
        for (j = 1; j < N; j++) {
            if (a[i][j] > max[i]) {
                max[i] = a[i][j];
            }
        }
    }
    
    for (i = 0, sum = 0; i < N; i++) {
        sum += max[i]*max[N-1-i];
    }
    
    printf("Res = %.2f\n", sum);    
    getchar();
    return 0;
}


Output:
1
2
3
4
34.2 73.5 9.8 
33.3 16.7 96.0 
19.3 20.0 67.1 
Res = 19079.70


Create a new paste based on this one


Comments: