Need help (ASAP)

hey again i have small problem i need help i want to print the "year with greatest and smallest increase and decrease" the years are from 2000 to 2009. i know how to print the greatest and smallest number in the array but i don't know how to print the year. here is half of the code of the program the one that's important for you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void maxIncDecYears(int annualIncome[],int arraySize){
    year = 2000;

    int max = annualIncome[0],min = annualIncome[0];

    for(int z = 1; z < arraySize; z++){
        if(max < annualIncome[z]){
            max = annualIncome[z];
        }
        year++;
    }
    cout << "Year with maximum income increased: " << year << endl;

    year = 2000;

    for(int x = 1; x < arraySize; x++){
        if(min > annualIncome[x]){
            min = annualIncome[x];
        }
        year++;
    }
    cout << "Year with maximum income decreased: " << year << endl;
}
I'm not sure what you want... you ARE printing the year.

If by "smallest increase and decrease" you mean the largest (smallest) difference between two years, then just replace annualIncome[x] with annualIncome[x] - annualIncome[x-1]?
yes i am but the problem is that the year is not correct the year should be 2000 and 2004 but instead it prints 2003 and 2001.

EDIT: and if your confused about the program on what it dose. it asks the user to enter a company's annual income for 10 years from 2000 to 2009 and then shows the increase and decrease for every year then it displays the percentage of the increase and decrease and then at last it has to show the "year with the greatest increase in the income" and the "year with the greatest decrease in the income"
Last edited on
Ah I see what you're doing wrong. Your "year" variable will ALWAYS be the last year, because you increase it no matter what (it's outside the if-statement).

Instead, you should do this:
1
2
3
4
5
6
7
 
for(int z = 1; z < arraySize; z++){
        if(max < annualIncome[z]){
            max = annualIncome[z];
            year = 2000+z; 
        }
}

That way, if annualIncome[8] is max, year will become 2000+8 = 2008.
hey it worked thanks mate appreciated.
Topic archived. No new replies allowed.