0
someone can help me develop this problem?
Present a program that reads 30 pairs of integer values, storing the lowest value in a variable x and the highest in y, and for each pair of values, calculate the sum from x to y integers inclusive. So far I’ve managed to do so far:
#include <stdio.h>
int main() {
int v[30];
int x = 0;
int y = 0;
int soma = 0;
for (int i = 0; i < 30; ++i) {
scanf("%d", &v[i]);
if (v[i]>y) {
y = v[i];
} else {
x = v[i];
}
}
printf("\nVetor: \n");
for (int i = 0; i < 30; ++i)
{
printf("[%d]\n", v[i]);
}
return 0;
}
someone can give me a light how I should do "for each pair of values, calculate the sum of the integers from x to y, inclusive." I’m not getting it exactly. I managed to catch the biggest and smallest, however, this not catching the biggest and smallest of the pairs as for example: v[0] and v[1], v[2] and v[3] and so on, if anyone can help me thank.
last doubt, when you use "? b : a", what does it mean?
– natalia
this was the only part I did not understand, at the time you used: "int x = a > b ? b : a;", had never used in this way.
– natalia
@This operator is called ternary operator and we can read as follows
x ? y : z
, wherex
indicates a certain condition (a > b
, for example), and, if the condition inx
is true, theny
is executed, otherwisez
is executed. Basically, it works as a simplified form of if/Else and the result of the transaction can be used in a value assignment. Therefore, in the case of that code,int x = a > b ? b : a
we can interpret how declare and enter the value of b, if a is greater than b or the value of a otherwise or assign x the lowest value between a and b.– Jobert