Thread: Get Position in array

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    20

    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
    
    	//Your task: Finish this NON-RECURSIVE function
    	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)
    		{	printf("The course id %d is not found.\n",target1);
    			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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What's wrong about my program
    It doesnt work, and you should ask your teacher why.

    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    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
    
    	//Your task: Finish this NON-RECURSIVE function
    	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. #5
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Code:
    for (pos=0; i<TOTAL; i++)
    		if (course_id == Prereq[pos].CourseID)
    			return pos;
    			
    	return -1;
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    12
    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. #7
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    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.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  2. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 05:16 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM
  5. mode of an array
    By Need Help in forum C Programming
    Replies: 15
    Last Post: 09-17-2001, 08:03 AM