Here is the question : http://www.sendspace.com/file/qu6bpi

I upload the code to my school checking system,
and it said that I can only pass 6 hidden cases out of 10.
I don't know where the problem is. :'(

Here is my code : (can only use looping,if,array,pointer)

``````#include<stdio.h>
#include<math.h>
#define N 200
#define B 100
void swap(int *p,int *q,int swap1,int swap2)
{
int temp;
temp=p[swap1];
p[swap1]=q[swap2];
q[swap2]=temp;
return;
}
void main()
{
int n,i,j,min=60,k,s,m;
int a[N]={},b[B]={};
int *p=NULL,*q=NULL;
double t;
scanf("%d",&n);
t=floor(1.2*n);
for (i=0;i<t;i++)
{
scanf("%d",&a[i]);
if (a[i]>80 || a[i]<6)
goto end;
for (k=0;k<n;k++)
{
if (a[k]>10 && a[k]<60)
{
m=1;
break;
}
}
switch(m)
{
case 1:
{
if (i>=n && (a[i]<11 || a[i]>59))
{
for (k=0;k<n;k++)
{
if (a[k]>10 && a[k]<60)
{
if (a[k]<min)
{
min=a[k];
s=k;
b[i]=s;
}
}
}
swap(a,a,s,i);
min=60;
for(k=s+1;k<n;s++,k++)
swap(a,a,s,k);
}
else b[i]=i;
break;
}
default: b[i]=i;break;
}
for (j=n;j<=i;j++)
{
for (k=j+1;k<=i;k++)
{
if (b[j]>b[k])
{
swap(a,a,j,k);
swap(b,b,j,k);
}
}
}
printf("\nsitting ");
for (j=0;j<=i && j<n;j++)
printf("%d ",a[j]);
printf("\n\n");
if (i>=n)
{
printf("standing ");
for (j=n;j<=i && j<t;j++)
printf("%d ",a[j]);
printf("\n\n");
}
}
if (i==t)
printf("\n\nbus full\n\n");
end:;
}``````

Thanks:'(

Edited by tc121091: n/a

3
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by VernonDozier

Your link wants me to download and install some unknown rar file on my computer. Ain't going to happen in this lifetime. If you want us to see the instructions you were given all you hve to do in attach the text file to your post using the Advanced Editor and Manage Attachments button.

Edited by Ancient Dragon: n/a

Attachments
``````Task Description

Imagine there is a queue of passengers waiting at a bus station for a bus that goes from Hong
Kong to Shenzhen. You are to write a program to do the seat arrangement for the passengers.
Some assumptions and the seating rules are given as follows:

1. The bus has a number of seats. Each seat can take one passenger. The bus can also allow
some passengers to stand. We assume that if the bus has n seats, it can allow at most 0.2n
passengers to stand. E.g., if n=58, it can take 69 passengers, including both sitting and
standing (58*1.2=69.6 and we round down). We assume that 1n100. You need not
handle any bus of size outside this range.

2. When a bus arrives, passengers get on the bus one by one in the order they queue up in the
line. If there are no more seats, the passenger will have to stand. However, as a courtesy,
young passengers who are sitting will give seats to the children and elderly. More
precisely, we assume each passenger has age between 6 and 80, inclusively. We classify
passengers into 3 groups according to their age:
children : age 6-10
young: age 11-59
elderly: age 60-80
When a passenger, who is a child or elderly, gets on the bus and there are no more seats,
and if there are young passengers sitting, the youngest among these sitting young
passengers will give his/her seat to the child/elderly. If there are more than one youngest
sitting passengers, the one who gets on the bus earlier will give seat.

3. If the bus has already taken 1.2n passengers, no more passengers will be accepted.

Program Requirements

Your program should first input the number of seats, n, in the bus. You can assume that
1n100.

Then it inputs a sequence of integers, each (except the last one) representing the age of a
passenger. The last item in the sequence is an integer outside the range [6..80]. This
represents the end of input.

For each passenger, your program should print the list of sitting passengers in the order they
get on the bus, and then the list of standing passengers (if any), also in the order they get on
the bus.

When the last integer (i.e., the integer outside the range [6..80]) of the sequence is read, your
program should stop. If the bus has already taken 1.2n passengers, your program should print
a bus full message and then halt without reading any more inputs.
Below are two samples of input/output. User inputs are shown in boldface.

Sample 1:
5
6
sitting 6
45
sitting 6 45
20
sitting 6 45 20
77
sitting 6 45 20 77
20
sitting 6 45 20 77 20
80
sitting 6 45 77 20 80
standing 20
bus full

Sample 2:
4
11
sitting 11
60
sitting 11 60
10
sitting 11 60 10
81``````

Rule 1 for readability. Pick some good variable names and throw in some comments.
Rule 2 for readability. When you ask the user to enter in information, tell them what you want them to do. All I get is a cursor waiting for me to...who knows? Tell me!

``````scanf("%d",&n);
t=floor(1.2*n);``````

Much better...

``````int numSeats;
int maxNumPassengers;
printf ("Enter the number of seats on the bus : ";
scanf("%d",&numSeats);
maxNumPassengers=(int)(floor(1.2*numSeats)); // be careful of roundoff error here though!  May not be an issue.  I'm not sure.``````

Note that I changed the maximum number of passengers to an int. You either take a full passenger or none of it. There's no such thing as a bus having a capacity of 67.8 passengers. Doesn't make any sense. Make it an int. Why would you take the floor and store it as a double? That could make sense in some other programs, but not here.

You aren't following the spec in line 21. Who says the bus will necessarily fill up? Read till you get input that tells you to stop reading. That could happen right away or that could take quite some time. You need a loop redesign. Right now you have a goto, but it's "going to" the wrong place. I'll leave the "never use goto" lecture for someone else since I'm not a subscriber to that philosophy (if there's never an appropriate place to use goto, why's it still in the language?), but you certainly don't need/want it the way you have it.

Then it inputs a sequence of integers, each (except the last one) representing the age of a
passenger. The last item in the sequence is an integer outside the range [6..80]. This
represents the end of input.

Thanks the above suggestion.

I tried to use "break" instead of "goto"
Cause I don't know the actual use of a goto, seems many people never use it also.
And I stop the loop when the bus full or inputing a stop value

``````for (i=0;i<N;i++)
{
if (i==maxNumPassengers)
{
printf("\nbus full\n");
break;
}
scanf("%d",&a[i]);
if (a[i]>80 || a[i]<6)
break;
}``````

However, the logical problems are still exist
Cause I cannot pass all the hidden case (just 6 out of 10)
I don't know if there is a miss or a wrong logic.

Thanks.

Before you do anything else, I think you need to...

1. Format the code with consistent indentation. Right now it's impossible to match the closing bracket with the opening bracket, so the code is extremely hard to read.
2. Pick some better variable names.
3. Get rid of the goto.
4. Check every variable that you use and make sure it is initialized.

The swap function seems more than a bit odd. It may work just fine, but generally its spec would be this...

``void swap(int* num1, int* num2)``

Swap functions should swap two numbers. They should not care at all whether those numbers are part of an array or not. You figure out which two numbers need to swapped and pass the address. Thus the address calculation occurs in the function CALL, not the function itself. The way you do it is not an error, but is generally not how it's done.

Too many variables, variable names not named descriptively, and no comments leads to major problems debugging. I'm sure I could go through the spec and match variable names to what each variable does, but the point is if I have to do that, that means that the code isn't designed well in the first place. The solution is not to debug the existing code, but to start over in my opinion.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.