# Thread: Get Position in array

1. ## Get Position in array

Code:
```typedef struct  {   int CourseID;
int Prereq1_CourseID;
int Prereq2_CourseID;
} PREREQ;

#define TOTAL 20

//The list of courses and their prerequisites
PREREQ Prereq[TOTAL]={
{107,-1,-1},	// The course 107 has no prerequisite.
{108,-1,-1},	// ...
{109,-1,-1},
{117,-1,-1},
{118,-1,-1},
{119,-1,-1},
{127,-1,-1},
{128,-1,-1},
{207,107,117},	// To take 207, student must finish either 107 or 117 first
{209,109,119},
{218,-1,-1},
{219,-1,-1},
{228,108,118},
{308,-1,-1},
{309,109,119},
{318,218,228},
{319,209,219},
{407,207,127},
{408,308,318},
{409,309,319}};

int GetPositionInArray(int course_id)
{
//Description:
//Given a course ID, find whether its information exists in the Prereq array.
//If it exists, return its position in the array (ie. 0,1,2,..TOTAL-1).
//Otherwise return -1

int pos;

for (pos=0; i<TOTAL; i++)
{	if (course_id == Prereq[pos].CourseID)
return pos;
else
return -1;
}
return 0;
}

void main()
{
int target1;
int target2;
printf("Please enter id of first target course: ");
scanf("%d",&target1);
printf("Please enter id of second target course: ");
scanf("%d",&target2);
printf("\n");

//Check whether target1 exists
{	int PositionInArray;
PositionInArray=GetPositionInArray(target1);
if (PositionInArray==-1)
exit(0);
}
else printf("%d is found as the %dth one in the array.\n",target1,PositionInArray);
}```
When i input course id 407, but output is "The course id 407 is not found. What's wrong about my program, thanks for your help!!

2. What's wrong about my program

Conversely, write the smallest and simplest program that demonstrates your problem. Explain what your program is supposed to do, and what is the problem with it, as well as your attempts to fix the problem.

3. You return after the first comparison, try checking to the end of the array then return success/fail.

Oh, and main returns int (see avatar for details)

4. Code:
```int GetPositionInArray(int course_id)
{
//Description:
//Given a course ID, find whether its information exists in the Prereq array.
//If it exists, return its position in the array (ie. 0,1,2,..TOTAL-1).
//Otherwise return -1

int pos;

for (pos=0; i<TOTAL; i++)
{	if (course_id == Prereq[pos].CourseID)
return pos;
else
return -1;
}
return 0; // <- is never reached. This loop never iterates
}```

5. Code:
```for (pos=0; i<TOTAL; i++)
if (course_id == Prereq[pos].CourseID)
return pos;

return -1;```

6. where you have declared "i" ?
why you are incrementing "i" instead of "pos"?

if u have answers for the above questions, ur problem is over
Code:
```for (pos=0; i<TOTAL; i++)
if (course_id == Prereq[pos].CourseID)
return pos;

return -1;```

7. OMGMSNMSNMSN

Try to use "your" and "you" instead of "ur" and "u".
Oh and if it takes a while to send a reply, don't click the button twice.

8. Code:
```	int pos;

for (pos=0; i<TOTAL; i++)
{	if (course_id == Prereq[pos].CourseID)
return pos;
else
return -1;
}
return 0; // <- is never reached. This loop never iterates
}```
Well, the return 0 would be reached if TOTAL was 0.