Home > Back-end >  How to find largest element from an array?
How to find largest element from an array?

Time:01-28

I'm trying to solve this assignment in C which has a given code and I have to complete. I'm stuck at the 2nd part, I cant find out what the while loop is doing there and don't know how I can write the code in there in order to find the largest element.

In this case I need largest element which I would get from the price precios, and then print out the code of the product which contains this price codigoProducto. As you can see codigoProducto is being counted in order to identify it by product 1, product 2, etc.

Can you please give me an idea of how I can accomplish this? thank you! (sorry for the Spanish)

int main() {
    double precios[5];
    int cantidades[5];

    int codPrecioMasAlto;
    int codCantidadMasBaja;
    double total = 0;
    int codigoProducto = 0;

    // 1.  ingreso de datos
    for (int i = 0; i < 5 ; i  ) {
        cout << "Ingresar precio de producto " 
             << codigoProducto   << endl;
        cin >> precios[i];
        cout << "Ingresar cantidad del producto" << endl;
        cin >> cantidades[i];
    }

    // 2. cual de los productos tiene el precio mas alto
    while (codigoProducto < 5) {
        // completar el codigo
    }

    cout << "El producto con mayor precio es el Producto " 
         << codPrecioMasAlto << ", cuyo precio es " 
         << endl;

CodePudding user response:

Since this is C , you can make use of the algorithms provided by the standard library.

    #include<algorithm>
    #include<iterator>
    
    ...

    double *precioMasAlto = std::max_element(std::begin(precios), std::end(precios));

This will give a pointer precioMasAlto to the largest element of the array. Thus the max price is *precioMasAlto.

If the index is needed, it can be calculated as the difference in position between the found element and the first element of the array (thanks to Jarod42 for noticing this solution):

std::distance(std::begin(precios), precioMasAlto);

CodePudding user response:

As I understand it, you want to input information on five products, and then print out info on the highest priced item.

That might look something like:

#include <iostream>
#include <string>

int main() {
    std::string productNames[5];
    double productPrices[5];

    double maxPrice = 0.0;
    size_t indexForMaxPrice = 0;

    for (size_t i = 0; i < 5;   i) {
        std::cout << "Input info for product " << (i   1) << std::endl;
        std::cout << "Name: ";

        std::cin >> productNames[i];

        std::cout << "Price: ";

        std::cin >> productPrices[i];

        if (productPrices[i] > maxPrice) {
            maxPrice = productPrices[i];
            indexForMaxPrice = i;
        }
    }

    std::cout << "Most expensive item: " << productNames[indexForMaxPrice]
              << " at $" << productPrices[indexForMaxPrice]
              << std::endl;

    return 0;
}

But, if we use a struct to group those related pieces of information, a naive approach might be:

#include <iostream>
#include <string>

struct Product {
    std::string name;
    double price;
};

int main() {
    Product products[5];

    Product *maxPriceProduct = nullptr;

    for (size_t i = 0; i < 5;   i) {
        std::cout << "Input info for product " << (i   1) << std::endl;
        std::cout << "Name: ";

        std::cin >> products[i].name;

        std::cout << "Price: ";

        std::cin >> products[i].price;

        if (maxPriceProduct == nullptr || products[i].price > maxPriceProduct->price) {
            maxPriceProduct = &products[i];
        }
    }

    std::cout << "Most expensive item: " << maxPriceProduct->name
              << " at $" << maxPriceProduct->price
              << std::endl;

    return 0;
}
  •  Tags:  
  • Related