Answer to Question #167502 in C++ for maduvha

Question #167502

1 Study the following descriptions and write only the statements to achieve the required

objectives in both algorithm and C++ syntax.

2.1.1 The array called quantity has 20 elements. Write statements that will help in counting

how many of these elements have a value of less than 50. Also determine the sum of

the values that are more than or equal to 50. Display these 2 answers.

2.1.2 The array called itemPrice has 150 elements. Write the statements to determine and

display the highest price. If there is more than one item with this price, the number of

items must also be displayed.



1
Expert's answer
2021-02-28T07:04:03-0500
#include <iostream>
#include <time.h>
using namespace std;


const int QUANTITY_ARRAY_SIZE = 20;
const int ITEM_PRICE_ARRAY_SIZE = 150;


int countLessValues(int* quantity, int value)
{
    int counter = 0;
    for(int i = 0; i < QUANTITY_ARRAY_SIZE; i++)
    {
        if(quantity[i] < value)
            counter++;
    }
    return counter;
}


int sumOfGreaterValues(int* quantity, int value)
{
    int sum = 0;
    for(int i = 0; i < QUANTITY_ARRAY_SIZE; i++)
    {
        if(quantity[i] >= value)
            sum+=value;
    }
    return sum;
}


int findHighestPrice(int* itemPrice)
{
    int highestPrice = itemPrice[0];
    for(int i = 1; i < ITEM_PRICE_ARRAY_SIZE; i++)
    {
        if(highestPrice < itemPrice[i])
            highestPrice = itemPrice[i];
    }
    return highestPrice;
}


int countHighestPrice(int* itemPrice, int highestValue)
{
    int counter = 0;
    for(int i = 0; i< ITEM_PRICE_ARRAY_SIZE; i++)
    {
        if(itemPrice[i] == highestValue)
            counter++;
    }
    return counter;
}


int main()
{
    int* quantity = new int[QUANTITY_ARRAY_SIZE];
    for(int i = 0; i < QUANTITY_ARRAY_SIZE; i++)
        quantity[i] = rand()%100 + 1;


    cout << "Elements: ";
    for(int i = 0; i < QUANTITY_ARRAY_SIZE; i++)
        cout << quantity[i] << " ";


    int lessThan50Values = countLessValues(quantity, 50);
    cout << "\n\nNumber of values that are less than 50: " << lessThan50Values << endl;


    int sumOfValuesGreaterThan50 = sumOfGreaterValues(quantity, 50);
    cout << "\nSum of values that are greater than or equals to 50: " << sumOfValuesGreaterThan50 << endl;
    delete[] quantity;


    int* itemPrice = new int[ITEM_PRICE_ARRAY_SIZE];
    for(int i = 0; i < ITEM_PRICE_ARRAY_SIZE; i++)
        itemPrice[i] = rand()%100 + 1;


    cout << "\nPrices: ";
    for(int i = 0; i < ITEM_PRICE_ARRAY_SIZE; i++)
        cout << itemPrice[i] << " ";


    int highestPrice = findHighestPrice(itemPrice);
    cout << "\n\nHighest price: " << highestPrice << endl;


    int countHighestPriceValues = countHighestPrice(itemPrice, highestPrice);
    cout << "Number of highest price values: " << countHighestPriceValues << endl;
    delete[] itemPrice;


    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment