user3182683
user3182683

Reputation: 101

How can I compare elements from different indexes in struct

#include<iostream>

using namespace std;

struct workspace {
    int ID;
    int price;
    int incoming_amount;
    int outgoing_amount;
    int date;
};

int main ()
{
    workspace works[5];
    string type_amount;
    int incoming_IDs[5];
    int incoming_IDs_counter = 0;
    for(int i = 0; i < 5; i++){
        cin >> works[i].ID;
        cin >> works[i].price;
        cout << "What type of amount (inc/out)?" << endl;
        cin >> type_amount;
        if(type_amount == "inc"){
            incoming_IDs[incoming_IDs_counter] = works[i].ID;
            incoming_IDs_counter++;
            works[i].outgoing_amount = 0;
            cin >> works[i].incoming_amount;
        }
        else if(type_amount == "out"){
            works[i].incoming_amount = 0;
            cin >> works[i].outgoing_amount;
        }
        cin >> works[i].date;
    }
    return 0;
}

This is my code so far, now I have to check the following:

So, I worked on the first one and I got this function:

bool INC_Exists(workspace &works, int &incoming_IDs_counter, int incoming_IDs[]){
    for(int i = 0; i < incoming_IDs_counter; i++){
        if(incoming_IDs[i] == works.ID){
            return true;
            break;
        }
    }
    return false;
}

It seems to work and does the job, but for the second one, I don't know how to work it out. How can I efficiently check if the price of the outgoing amount if smaller than the incoming amount for the same ID?

Upvotes: 1

Views: 74

Answers (3)

user2699298
user2699298

Reputation: 1476

bool Valid_Price(workspace *works, int& incoming_IDs_counter, int incoming_IDs[], workspace &workz){
    bool valid_price;
    for(int j = 0; j < incoming_IDs_counter; j++){
        if(incoming_IDs[j] == workz.ID){
            for(int k = 0; k < 5; k++){
                if(works[k].ID == workz.ID){
                    if(works[k].price < workz.price){
                        valid_price = true;
                    }
                    else if(works[k].price > workz.price){
                        valid_price = false;
                    }
                }
            }
        }
    }
    return valid_price;
}

Tested and it seems to work..

Upvotes: 3

Vlad from Moscow
Vlad from Moscow

Reputation: 310950

This function

bool INC_Exists(workspace &works, int &incoming_IDs_counter, int incoming_IDs[]){
    for(int i = 0; i < incoming_IDs_counter; i++){
        if(incoming_IDs[i] == works.sifra){
            return true;
            break;
        }
    }
    return false;
}

is invalid because structure workspace has no data member sifra,

If I have understood correctly the requirement

•Make sure the ID has an incoming amount added to it, before it can be an outgoing amount

you need to check whether object works is among elements of int incoming_IDs[]. The function could look the following way

bool INC_Exists( const int incoming_IDs[], int incoming_IDs_counter,  const workspace &works )
{
    int i = 0;

    while ( i < incoming_IDs_counter && works.ID != incoming_IDs[ i ] ) i++; 

    return ( i != incoming_IDs_counter );
}

As for this condition

•Make sure the price for incoming amount if smaller than the outgoing amount

then it can be represented by expression

works.price < works. outgoing_amount

But I don't know where this condition has to be used. From your description it is totally unclear.

Upvotes: 1

user2943407
user2943407

Reputation: 413

bool Valid_Price(workspace *works, int& incoming_IDs_counter, int incoming_IDs[], workspace &workz){
    for(int i = 0; i < 5; i++){
        for(int j = 0; j < incoming_IDs_counter; j++){
            if(incoming_IDs[j] == workz.ID){
                for(int k = 0; k < 5; k++){
                    if(works[k].price < workz.price){
                        return true;
                    }
                    else{
                        return false;
                    }
                }
            }
        }
    }
}

Try this code for the second part.

Upvotes: 1

Related Questions