Array Struct Not Being Sorted

When I sort the second time based on compnow, the struct is not being sorted (line 67).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;
struct data {
    string company;
    int last;
    int growth;
    int now;
} info [60000];
bool compgrowth (data a, data b) {
    if (a.growth!=b.growth)
    return a.growth>b.growth;
}
bool compnow (data a, data b) {
	if (a.now!=b.now)
    return a.now>b.now;
}
int main () {
    int n;
    int position;
    ifstream fin ("cow.in");
    ofstream fout ("cow.out");
    fin>>n;
    for (int i=0; i<n; i++) {
        fin>>info[i].company>>info[i].last;
    }
    string current[n];
    for (int i=0; i<n; i++) {
        fin>>current[i];
    }
    int n2=n;
    for (int i=0; i<n; i++) {
    int min=0;
    int max=n-1;
    int yesnt=0;
    while (min!=max) {
        int mid=(max+min)/2;
        if (current[i]<info[mid].company) 
        max=mid;
        else if (current[i]>info[mid].company)
        min=mid+1;
        else if (current[i]==info[mid].company){
            position=mid;
            yesnt=1;
            break;
        }
    }
    if (yesnt!=1) {
        info[n2].company=current[i];
        info[n2].growth=n-i;
        info[n2].last=n+1;
        info[n2].now=info[n2].last-info[n2].growth;
        n2++;
    }
    if (yesnt==1)
    info[position].growth=info[position].last-(i+1);
    info[position].now=info[position].last-info[position].growth;
    }
sort (info, info+n2, compgrowth);
int temp;
for (int i=0; i<n2; i++) {
temp=i+1;
    if (info[i].growth!=info[i+1].growth)
    break;
}
sort (info, info+temp, compnow);
for (int i=0; i<n2; i++)
cout<<info[i].company<<info[i].now<<endl;
}
Last edited on
Turn error-checking on in your compiler:
In function 'bool compgrowth(data, data)':
14:1: warning: control reaches end of non-void function [-Wreturn-type]
 In function 'bool compnow(data, data)':
18:1: warning: control reaches end of non-void function [-Wreturn-type]
 In function 'int main()':
67:31: warning: 'temp' may be used uninitialized in this function [-Wmaybe-uninitialized]
21:9: warning: 'position' may be used uninitialized in this function [-Wmaybe-uninitialized]


In this instance, those are all errors, not just warning.
Hi! Thanks for the reply! Even though there is an error, the compgrowth function is actually working. How do I fix these errors?
Fix
may be used uninitialized
by initializing it (obviously!).

Fix
control reaches end of non-void function
by making sure that the function has a return value for all values of its arguments.



Even though there is an error, the compgrowth function is actually working

Quite.
Thanks! What does quite mean?
In colloquial English, "quite" as a statement on its own means "I agree", but it carries an overtone of "this is something I already knew".

It can be softened by saying "Well, quite" rather than just "Quite".
Last edited on
it can also mean 'good' (or inverse if used sarcastically):
that guy is quite the programmer
this may be misuse of the word, but it is used that way (or was long ago)
Last edited on
afatperson wrote:
Thanks! What does "quite" mean?

Ah, it was meant to be ironic. That the two things
"there is an error",
"the compgrowth function is actually working"
seem to be contradicting each other here, and there wasn't strong evidence for either statement.

@afatperson, could you do a few things, please:
- fix the indentation in your code; it's confusing;
- supply us with a short input file so that we can run it (or fake it with a stringstream input instead)
- explain what your code is supposed to do;
- make sure that all your variables are initialised whichever branch of an if-statement they take, and all your functions return an appropriate answer regardless of whether their enclosed if-statement is true or not;
- change that naughty line
string current[n];
as (standards-compliant) c++ won't like it.


Back to code:

1
2
3
4
5
6
7
8
bool compgrowth (data a, data b) {
    if (a.growth!=b.growth)
    return a.growth>b.growth;
}
bool compnow (data a, data b) {
	if (a.now!=b.now)
    return a.now>b.now;
}


These two need work.

Never fall into the trap of thinking, "well, it works, so...."

First, what does the function do if a.growth == b.growth? You may never present that condition in your testing, and perhaps it never happens in practice, but is this a reason to make this test?

No.

There's no problem skipping the "!=" test in these functions.

The most common test is lessthan (<) for sorting. If you want a reverse order, then make it greaterthan. That much is fine.

So, the function should return true if that condition is met, and false under all other circumstances.

So, both of these are configured to report > (greaterthan) as true. Just test for that, and ditch the "!=" - it doesn't really do anything at all.

If that test fails, return false. Now, you'd have a proper comparison.

Consider what happens if this code compiles and executes. What if the two parameters are neither > or ==?

The next step is undefined. The function could return anything...random results. It should return false, but without an actual explicated return of false, the calling code might just as easily receive a non-zero return (interpreted as true).

How would the algorithm function if these functions returned true when the two tests written both were false?

To say the least, it wouldn't be reliable.


Hey guys, thanks for all the input! It really means a lot to me. This is my solution to a competitive programming problem, which is below.

President of Babylon country prepares Forte Ones List (FOL) every year, top n companies of the country.
This year, the government wants also to see the top growing company, the company which has increased
its position the most.

Question: Write a program that is going to read Company Identification Numbers (CIN) for n companies
for last year and this year. Then, the program will find out the most growing company.

Input specification
You will be given an integer number (n), the number of companies in the lists where 1 ≤ n ≤ 60,000.
Then in the following n lines, you will have two information for each company: CIN and the position
in the last year FOL. And, the list for last year is in ascending order of CINs.
Then, the following n lines will give the list for this year. This second list is sorted according
their orders in this year. That is, first appearing company is the top company in this year.
Company Identification Numbers start with a letter and contain up to 8 digit numbers.

Output specification:
Show the CIN of the top growing company.
Note:

There can be many companies sharing the same position in a year.
Some companies may not be present in the previous year list or may be present in the last year's
list but not present in this year's list. In this case, all companies missing in the list of last
year's FOL are counted as having the (n+1)th position.
If there are several companies which has climbed the same number of steps up, then show the company
with the smallest position in this year.

Sample Input
5
E7182 3
S4673 5
S9990 4
X6509 2
Z8998 1
E7182
S4673
Z8998
N2793
X6509

Sample Output
S4673

Output Details: There are two companies which has climbed up: E7182 and S4673. And, S4673 is the top
most growing company.

E7182 was in the 3rd position and has climbed 2 steps up
S4673 was in the 5th position and has climbed 3 steps up
Z8998 was in the 1st position and it has fallen 2 steps down
N2793 was not in the list that is it has climbed 2 steps up
X6509 was in the 2nd position and it has fallen 3 steps down


My compnow function deals particularly with the "If there are several companies which has climbed the same number of steps up, then show the company
with the smallest position in this year.". Essentially, I sort the companies tied for first place growth based on their current position this year.
Update: I have solved the issue, it seems all I had to do was change the < in compnow to >.
Registered users can post here. Sign in or register to post.