HELP

Given a set of data for example.

apple=$5
orange=$20
watermelon=$22
banana=$24
kiwi=$25


How do I use a program to show the fruits with the top 3 prices and also the fruits with price more than $20.

anyway I'm using #include<stdio.h>

Last edited on
One option would be to create a struct called Fruit with a name and a price and store the fruits in a vector or array. In this way you can easily sort them or find a specific one.
1
2
3
4
5
struct Fruit
{
  string name;
  int price;
};
"Using stdio.h" states that you are programming with C rather than C++. Is that correct?


Logically, your data is a table with two columns: name and price.
You want to sort the rows of that table by the price column. Then you want to print three rows.
On the second part you want to print every row, where price criteria is true

You do need a data-structure for storing the data in memory. Such structure that supports the necessary operations.
Yes I'm using c

Anyway how do I set price criteria and display if the data meets the criteria?

Thank you for your help :)
Even in C you can use structs - like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct Fruit
{
  char *name;
  int price;
};

struct Fruit fruits[] =
{
  "apple", 5,
  "orange", 20,
  "watermelon", 22,
  "banana", 24,
  "kiwi", 25
};



To sort you can use quicksort.
how do I set price criteria

You said:
with price more than $20

That looks like a criteria.

Thomas shows array 'fruits' above. Surely you can loop through all elements of an array?

IF current element has price more than 20 THEN print its name and price
Topic archived. No new replies allowed.