Can someone help me?

Please help me solving this problem in c++:

Mr. Dengklek has a farm of ducks. He has many types of ducks that varies in size. The ducks like to form lines, which need to be in the correct order. The ducks within the same type need to be behind or in-front of each other. Furthermore, in each types, the ducks are arranged from the little to the largest in size. The order of types of ducks is not too important.
Since Mr. Dengklek only has one hour left, he need to order his ducks quickly.

Please create a c++ program in Dev c++ that display the number of minimal steps required to arrange the ducks.
The first line will consist of a number inputted, C (the tens) and N (the unit). If the input is 21, so C is 2 and N is 1. C is the number of ducks' types and N is the number of how many ducks in each types.
Every C*N line after, a 2-digit number will be inputted, X (the tens) and Y (the unit). X is the type number and Y is the duck's size. FYI, there is no duck that has the same type and size.

Input Example:
22

21
12
11
22

Output Example: 2

Input Example:
32

32
22
11
31
21
12

Output Example: 2

Input Example:
41

21
31
11
41

Output Example: 0

Please help!
Thank you very much..
Sorry, we don't do homework assignments.

Make an attempt to solve the problem, post your code (using code tags) and we will try to help.


What I can generate is:

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

int main(){
             int a=0,b=0,c=0,d[100],e=0,n=0,x=0,y=0;

             cin>>a;

             x=(a/10);
             y=(a%10);

            b=x*y;

            for(c=0;c<b;c++){
                                       cin>>d[c];}

            for(c=0;c<b;c++){
                                        if(d[c]<d[c+1]){
                                                                e++;}

           cout<<e<<endl<<endl;

           system("pause");
           return 0;
}
Last edited on
That's the right idea, but you have a couple of problems.

You need a second loop in your sort.
You're not swapping the ducks in the array when you find them out of order.
The problem statement says to order from littlest to largest. Your comparison is counting the number of smaller ones in front of bigger ones.

This should help:
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
#include <iostream>
using namespace std;

int main()
{	int a,ducks[100];
	int num_types, ducks_per_type, num_ducks;
	int i, j, num_swaps = 0, temp;

	cout << "CN?";
    cin>>a;
	num_types=(a/10);
    ducks_per_type=(a%10);

    num_ducks = num_types * ducks_per_type;
	//	Populate the ducks
	for	(i=0;i<num_ducks;i++)
	{	cout << "XY?";
		cin >> ducks[i];
	}
	// sort the ducks 
    for(i=0;i<num_ducks-1;i++)
	{	for (j=i+1; j<num_ducks; j++)
		{	if (ducks[i] > ducks[j])
			{  // swap the two ducks
				temp = ducks[i];
				ducks[i] = ducks[j];
				ducks[j] = temp;
				num_swaps++;
			}	
		}
	}
	cout << "Ducks After sort" << endl;
	for (i=0;i<num_ducks; i++)
		cout << ducks[i] << endl;
	cout << "Number of swaps: " << num_swaps << endl;
	system("pause");
    return 0;
}


I renamed most of your variables names as I found them too confusing.

Could the number of swaps be reduced if order of the types is ignored in the sort criteria (sort same type together, but order of types doesn't matter per problem statement)? I'll leave that to you to discover.
Last edited on
Yeah, we could ignore the order of types. Thus, we can sort the same type together, but the order doesn't matter at all).

For example:

Input:
41

21
31
11
41

Output: 0


Because the duck already in the same type and ascending order (1,1,1,1) even though the type order is not (2,3,1,4 --> ok)

=====================================================

Btw, from your code I notice that you simply swamp the values when ordering the duck. However, can you help me in doing something like this:

Order duck(1):

32
22
11
31
21
12

Order duck(2):
*First move

31
32
22
11
21
12

Basically, when 31 move, the 21 behind it automatically fill that spot and when it gonna fill the first spot, the 32,22,11,21,12 automatically step back.
Therefore, they are not simply exchanging positions.

Thank you for your help! :) :D
Last edited on
Topic archived. No new replies allowed.