Can't solve it

I have wrote this code to solve this problem:

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
#include <iostream>
using namespace std;

int main () 
{
	int d, n, t, a, b;						//d=expenses, t=weeks, n=saved euros 
	int count = 0; 		 					//a=weekly allowance, b=balance
	cin >> d >> n >> t;						//count=number of weeks with positive balance
	if ( t == 0 and n >= d )				//if there are no weeks i just have to compare
	{										//saved euros and expenses
		cout << 1 << endl;
	}	
	else
	{
		for ( int i = 0; i < t; ++i )		
		{									
			cin >> a;
			b = n + a - d;					//calculate the balance of every week till week "t"
		
			if ( b >= 0 )					//if it is positive, then add 1 to the counter
			{
				++count;
			}
		b = 0;
		}	
	cout << count << endl;
	} 			
}


We use a tool to "judge" if the code solves the problem or not. This tool executes the code with public and private samples. The code passes the public ones but not the private ones. I have tried different things as being certain values 0 but privates say wrong answer. I do not know what else is missing in this code. Some kind of help would be thanked!

"
You have saved n euros. Additionally, every Monday of the next t weeks, you will be given a weekly allowance of a1, a2, ..., at euros, respectively. Every week, you have a fixed amount of expenses that sums up to d euros.


Write a program that counts how many weeks you end up with a strictly positive balance.

Input

The input consists of three naturals d ≥ 0, n ≥ 0 and t> 0, which represent fixed weekly expenses, the initial savings, and the number of weeks with allowance, respectively.


Following, there are the quantities corresponding to the t weekly allowances a1, ..., at . Every week allowance is a natural number ai≥ 0.

Output

The output is a natural number indicating the number of weeks which end up with a strictly positive balance, after paying the weekly expenses.


Your program must meet the output format described in the examples an it should follow a right programming style. You may also decide to include comments, if appropriate.
"

Last edited on
//Have you worked with the mod "%" symbol yet??
//Hint : run this code
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

using namespace std;

int main() {
	for (int i = 2; i <= 20; i++) {
		if (i % 2 == 0) { //note the use of  the mod "%" symbol.. it returns a value of 1 for remainder and 0 for no remainder
			cout << i << " is Positive \n";
		}
		else
			cout << i << " is Negative. \n";
	}
}
Last edited on
I have worked with it and know what it does.

I think you should change positive for even and negative for odd.

Still can't see how you can implement the mod symbol in the program above (?)
You are on the right track but since I messed up you can have this as an example:
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
#include <iostream>

using namespace std;

int main() {
	int expenses, weeks, saved, allowance, balance, count = 0; //variables
	cout << "Expenses: ";
	cin >> expenses;
	cout << "\nSaved: ";
	cin >> saved;
	cout << "\nWeeks: ";
	cin >> weeks;

	if(weeks == 0){
		if (saved >= expenses)
			cout << "True";
		else
			cout << "False";
	}
	else {

		for (int i = 0; i < weeks; i++) {
			cout << "\nAllowance: ";
			cin >> allowance;

			balance = (allowance + saved) - expenses;

			if (balance >= 0) { //if balnce is greater than 0
				cout << balance << " is Positive \n";
				count++;
			}
			else { //if balance is less than 0
				cout << balance << " is Negative. \n";
			}
		}


	}
	return 0;
}


Go over your logic in your if statements.
I have seen what your program does and run it. The output you give is the weekly balance and if it is positive or negative.

The problem requires to output how many weeks you have a positive balance and that is it. Have you tried to run my program? It does that. But when the tool we use to correct programs says that there is/are some case/s where my program does not work. I have been thinking about other case/s but i can not get them so that is why i posted this here. Btw, thanks!
Topic archived. No new replies allowed.