a program that reads 30 pairs of integer values, storing the smallest value in a variable x and the largest in y

Asked

Viewed 76 times

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.

1 answer

2


I understood this problem a little differently:

a program that reads 30 pairs of integer values

That is, the program must read a pair of integer values (a, b) 30 times. Below is my solution, with comments for each step.

#include <stdio.h>
int main() {
    
    // Fazemos leitura e cálculo das entradas 30 vezes
    for (int i = 0; i < 30; ++i) {
        int a, b, soma = 0;
        
        // Primeiro, lemos um par de números
        printf("Primeiro número : ");
        scanf("%d", &a);
        printf("Segundo número : ");
        scanf("%d", &b);
        
        // Segundo, armazenamos esses números em variáveis x, y: x para o menor número, y para o maior
        int x = a > b ? b : a;
        int y = a < b ? b : a;
        printf("(x, y) = (%d, %d)\n", x, y);
        
        // Terceiro, fazmos a soma de todos os inteiros de x até y (incluindo x e y)
        while (x <= y) {
            soma += x;
            x++;
        }
        printf("Soma: %d\n", soma);
    }
    return 0;
}
  • last doubt, when you use "? b : a", what does it mean?

  • 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.

  • 1

    @This operator is called ternary operator and we can read as follows x ? y : z, where x indicates a certain condition (a > b, for example), and, if the condition in x is true, then y is executed, otherwise z 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.