## tc121091

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:'(

## Ancient Dragon 5,243

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.

## VernonDozier 2,218

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.

## tc121091

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.

## VernonDozier 2,218

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.