Three sequences X , Y , Z of n real numbers each are given ( n < 200 ). Calculate the value of (а,a) - (b,c) , where a denotes that of the sequences X , Y , Z , which has the largest minimal element, and b and c denote two other sequences.

I don’t understand what is (a,a) - (b,c) , so for the beginning I decided to write a program that she would simply choose a .

The question is: how to make an array with the largest minimal element be taken as a ?
My code is:

 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<conio.h> #include<locale.h> #define n 200 void main() { setlocale(LC_CTYPE, "Russian"); float X[n], Y[n], Z[n]; int k, i, j, l; printf("введите k \n"); scanf("%d", &k); { printf("\n Введите значения массива X\n"); for (i = 0; i < k; i++) scanf("%f", &X[i]); int min = X[0]; for (i = 1; i < k; i++) if (X[i] < min) min = X[i]; printf("\n Минимальный элемент массива X: %d", min); } { printf("\n Введите значения массива Y\n"); for (j = 0; j < k; j++) scanf("%f", &Y[j]); int min2 = Y[0]; for (j = 1; j < k; j++) if (Y[j] < min2) min2 = Y[j]; printf("\n Минимальный элемент массива Y: %d", min2); } { printf("\n Введите значения массива Z\n"); for (l = 0; l < k; l++) scanf("%f", &Z[l]); int min3 = Z[0]; for (l = 1; l < k; l++) if (Z[l] < min3) min3 = X[l]; printf("\n Минимальный элемент %d", min3); } _getch(); } 

2 answers 2

Well, for example, like this:

 // Вычисление минимального элемента массива float min_el(float* a, int n) { float min = a[0]; for(int i = 1; i < n; ++i) if (a[i] < min) min = a[i]; return min; } // Выбор массива с максимвльным минимальным элементом. float* max_min_el(float*a, float*b, float*c, int n) { float ma = min_el(a,n); float mb = min_el(b,n); float mc = min_el(c,n); // Выбираем a float m = ma; float*p = a; if (m < mb) // Если b больше - сохраняем его { p = b; m = mb; } if (m < mc) // Если c еще больше - выбираем его p = c; return p; } 

Well, if (a, a) is really a scalar product, then

 float product(float*a, float*b, int n) { float s = 0.0; for(int i = 0; i < n; ++i) s += a[i]*b[i]; return s; } float task(float*a, float*b, float*c, int n) { float * m = max_min_el(a,b,c,n); if (m == a) return product(a,a,n) - product(b,c,n); if (m == b) return product(b,b,n) - product(a,c,n); return product(c,c,n) - product(b,a,n); } 

Like that.

  • And how did I write to calculate the minimum array element possible? And to select the maximum minimum element already create a subroutine? - Alexander

In your program, you first declared arrays of variable length, without initializing the variable n , and then by mistake enter the value of the variable k

 float X[n], Y[n], Z[n]; ^^^ ^^^ ^^^ int k, i,j,l; printf("введите k \n"); scanf("%d", &k); 

Therefore, your program has an undefined behavior. You must first enter a positive value for the variable n , and then declare arrays of variable length.

As for your question

The question is: how to do it so that a would take an array with the largest minimal element?

then you can do this using pointers to the first elements of the arrays. To simplify the work, you can declare a structure that has two fields: the minimum value in the array and a pointer to the array. Then simply declare an array of these structures, corresponding to the number of source arrays, and sort it.

Below is a demo program. It does not look for the minimal element of the arrays, but assumes that it has already been found. As a result of the program, the array of structures will be sorted according to the maximum minimum element, as can be seen from the output of the program. For simplicity, the example also declares arrays of a fixed size.

 #include <stdio.h> #define N 3 int main( void ) { int a[N] = { 2, 2, 2 }; int b[N] = { 3, 3, 3 }; int c[N] = { 1, 1, 1 }; int a_min = 2; int b_min = 3; int c_min = 1; struct Pair { int min; int *p; } min[] = { { a_min, a }, { b_min, b }, { c_min, c } }; if (min[0].min < min[1].min) { struct Pair tmp = min[0]; min[0] = min[1]; min[1] = tmp; } if (min[1].min < min[2].min) { struct Pair tmp = min[1]; min[1] = min[2]; min[2] = tmp; } if (min[0].min < min[1].min) { struct Pair tmp = min[0]; min[0] = min[1]; min[1] = tmp; } for (size_t i = 0; i < 3; i++) { printf("minimum %d: ", min[i].min); for (size_t j = 0; j < N; j++) { printf("%d ", min[i].p[j]); } printf("\n"); } } 

Output to console:

 minimum 3: 3 3 3 minimum 2: 2 2 2 minimum 1: 1 1 1 
  • > In your program, you first declared variable-length arrays, without having initialized the variable n, and then mistakenly entering the value of the variable k. So I have #define n 200, isn't it possible? - Alexander
  • @ Alexander I did not notice. Preprocessor names are happy to announce in capital letters. Then they are immediately fashionable to distinguish from variables. - Vlad from Moscow
  • Okay. Can I have a couple of questions about your program? Don't quite understand what "tmp" is? - Alexander
  • @ Alexander This is an "intermediate" structure object used to exchange values ​​between two objects of this structure, for example, min [0] and min [1] - Vlad from Moscow