C++ task (prob for you very easy) I just need lift

Hello,
I have one task that i can't solve. I know that it's not hard compared to other task posted on this forum. I'm currently learning just in school so if someone can help that will be awesome :D btw im not speaking English, sry for grammar mistakes.

On ship, packages are transfering with given masses with
cart (capacity of cart is given and capacity is bigger or
equal to mass of packages). Beacuse of that packages can't be split.
When some package can't get into cart beacuse of current off
limit of cart capacity,we use new cart and put that
package in new cart .Write a program in C++ that shows in order,
for each cart , number of packages and total mass of packages
that were transfered with cart .

-Input
In first line is entering capacity of cart N (10 <= N <= 500).
In every other line enters mass of package M (1 <= M <= N) that needs
to be transfered. Input ends when we enter 0 (1 <= lines <= 50 000).

-Output
On first there's number of packages on cart and mass of packages
put on carts (separated by space), and then on last line serial
number of cart with biggest number of items.

For exaple:
-Input
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
30
5
10
8
12
3
5
15
13
5
15
2
4
22
9
0

-Output
1
2
3
4
5
6
7
3 23
3 20
2 28
4 26
1 22
1 9
4



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
int main(int argc, char const *argv[])
{
	int n, x[500], s = 0;
	int xt, array1[100], array2[100], array3[50];
	cin >> n;
	
	for(int i = 0; i <= n; i++){
		cin >> x[i];
		if(x[i] == 0) break;
	}

	do{
		for (int i = 0; i <= x[i]; i++){
			xt = x[i] + x[i+1]; 
			if(xt >= 30){
				array1[i] = xt - x[i-1]; //23
				array3[0+i] = x[i-1];
				array2[i] = array3[0+1]; //12
				cout << array1[i] << endl;
			}
		}
	}while(s == 0);

	return 0;
}

my output is litteraly nothing..
thanks!
Last edited on
So add some debugging, and follow what's going on.
1
2
3
4
5
6
7
8
9
10
		for (int i = 0; i <= x[i]; i++){
			xt = x[i] + x[i+1];
                        cout << "i=" << i << ", xt=" << xt << endl;
			if(xt >= 30){
				array1[i] = xt - x[i-1]; //23
				array3[0+i] = x[i-1];
				array2[i] = array3[0+1]; //12
				cout << array1[i] << endl;
			}
		}


This is the output.
i=0, xt=15
i=1, xt=18
i=2, xt=20
i=3, xt=15

And that's all she wrote.

For example, your i <= x[i] is a weird loop condition. It stops when you get a package smaller than it's position in the array.

Also, you need to be VERY careful with all those x[i-1] and x[i+1] subscripts, to make sure you don't fall of either end of the array.

> while(s == 0);
s is always 0, so your code will loop forever.

> Input ends when we enter 0 (1 <= lines <= 50 000).
You're only allowing for 500 entries at the moment, not 50,000.
You don't need to remember all the items. You can just process them one at a time. Perhaps the hardest part about doing it this way is to remember to handle the last cart at the end of the loop.
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
#include <iostream>
using std::cin;
using std::cout;


int
main()
{
    int cartCapacity;
    int itemWeight;
    int currentCartWeight=0;
    int numPackages = 0;
    int maxPackages = 0;
    
    cin >> cartCapacity;

    while (cin >> itemWeight && itemWeight != 0) {
	if (currentCartWeight+itemWeight > cartCapacity) {
	    // It won't fit. Output what's on the cart
	    // and then empty the cart
	    cout << numPackages << ' ' << currentCartWeight << '\n';
	    if (numPackages > maxPackages) {
		maxPackages = numPackages;
	    }
	    numPackages = currentCartWeight = 0;
	}
	// Add the item to the cart
	++numPackages;
	currentCartWeight += itemWeight;
    }

    // Now output info about the last cart. Be sure to check the last
    // cart for max items too.
    cout << numPackages << ' ' << currentCartWeight << '\n';
    if (numPackages > maxPackages) {
	maxPackages = numPackages;
    }

    // Output the max number of packages
    cout << maxPackages << '\n';
}

thank you very much helped me a lot
cheers!
Topic archived. No new replies allowed.