Tyler Joe
Tyler Joe

Reputation: 377

Print the total number and the indices of elements which are larger than a number

I want to find the total number and the indices of array elements which are larger than 100.

For example: The first row is the number of houses. The first column is the square of the houses and the second column is their price. I want to print out the houses which price is larger than 100.

6

42 15 

110 20

125 160 

166 180

42 10

110 39  

The output should look like this:

2 (total number of the houses)  3 (index of the first house) 4 (index of the second house) 

My code:

#include <iostream>
using namespace std;

int main()
{
   int house, square[100], price[100], counter=0;
   cin >> house;

   if ( price[i] > 100)
    counter++;
 }
    cout << counter << endl;
    return 0;
}

I don't know how to get the indices, can somebody help?

Upvotes: 1

Views: 231

Answers (4)

idzireit
idzireit

Reputation: 98

As I understand you question from looking at what your output should be and your code your allocating alot of unnecesary memory for your arrays.
6 (number of houses to compare)? houses 1 (42 15) instead of an array of 100 elements you only need 2 because each one is an integer and not a price square[2] which in memore would be addressed as such house[0] and house[1] house[0] would have the value 42 and house[1] would have the value 15.

Also it is looking like a 2 dimensional array so you could use houseprice[6][2]. That would give you a 2d array looking like this:

[0] [0] [1]

[1] [0] [1]

[2] [0] [1]

[3] [0] [1]

[4] [0] [1]

[5] [0] [1]

This make the first column the index and the 2nd and 3rd columns the houses to compare. Hope this helps your understanding of arrays and how to do what you want to do.

idzireit

Upvotes: 1

JeJo
JeJo

Reputation: 32847

While inputting the house and price, you can store the indexes to a vector array if price is greater than 100.

live demo

#include <iostream>
#include <vector>

int main()
{
    std::size_t size; std::cin >> size;
    std::vector<std::size_t> indexVec;  // vector array to store the indexes
    std::size_t no_houses = 0;          // to count while inputing
    for (std::size_t index = 0; index < size; ++index)
    {
        int house, price;
        std::cin >> house >> price; // user input
        if (price > 100) 
        {
            ++no_houses;  
            indexVec.emplace_back(index + 1);  // store the indexes
        }
    }
    std::cout << no_houses << " "; // just print
    for (const int index: indexVec)     std::cout << index << " ";
}

Upvotes: 1

Ankur Jain
Ankur Jain

Reputation: 48

first there is compilation error with your code, extra }

Second,to get index, just take one variable i and increment it after every loop.

#include <iostream>
using namespace std;

int main()
{
   int house, greter_prices[100], counter=0;
   cin >> house;

   int i = 0;
   int price_index=0;
   while(i< house){
    int size;
    cin >> size;
    int price;
    cin >> price;
    if ( price > 100){
      counter++;
      greter_prices[price_index++]=i;
    }
    i++;
   }

   cout << counter<<" ";
   i=0;
   while(i<price_index){
    cout << greter_prices[i]<<" ";  
    i++;
   }
    return 0;
}

Upvotes: 1

user3132457
user3132457

Reputation: 1019

Have you even compiled the code? There is an error - what is i?

You need to read to read the line (two numbers), then check if first number is greater than 100. If so, print its index, i.e. i.

Upvotes: 1

Related Questions