Thread: Need help with an array assignment

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    13

    Need help with an array assignment

    Hey guys im having problems with this assignment. Thanks to all of you that helped with the sort. Now I have to put in a search function. The function I used works and the whole program works, the only problem I have is when you enter a two digit number into the array (I used 10) the search function will not return the right value. I have included my source code along with a couple of screen shots form the out put windows. In the first screen shot you can see that the search function works in the second screen shot I substitute 10 for 0 and the search function does not work. Any suggestions would be greatly appreciated.




    Code:
    // arr.cpp : Defines the entry point for the console application.
    //
    
    #include<stdio.h>
    #include<stdafx.h>
    #include"conio.h"
    #define n 10
    #define NOT_FOUND -1 
    
    void sort_arr(int arr[]);
    
    int search(const int arr[]);
    
    int main()
    {
           int  arr[n];
    	   printf("Enter the numbers in the array:\n ");
           for(int k=0; k<n; k++)
                   scanf("%d", &arr[k]);
    
           
           for(int t=0; t<n; t++)
                   {
    				   printf("%d", arr[t]);
                   }
           printf("\n");
           
           sort_arr(arr);
    
           for(int t=0; t<n; t++)
                   {
    				   printf("%d", arr[t]);
                   }
    
    	printf("\n");
    	printf("Please enter the target value:\n");
        search(arr);
    
    	
    
    	   
    	   
    	   
    	   
    	   getch();
           return (0);
    }
    
    
    void sort_arr(int arr[])
    {
    int a, b, low;
    
    for(a=0; a<n; a++)
           {
                   b=a+1;
                   for(b; b<n; b++)
                           if(arr[a]>arr[b])
                                   {
                                           low=arr[a];
                                           arr[a]=arr[b];
                                           arr[b]=low;
                                   }
           }
    }
    
    
    int search(const int arr[])     
    {
          int i, found = 0, target, where; 
    
    	 
    		  scanf("%d", &target);
    
          
          i = 0;
          while (!found && i < n) {
              if (arr[i] == target)
                    found = 1;
              else
                    ++i;
          }
    
          /* Returns index of element matching target or NOT_FOUND	*/
          if (found)
                where = i;
          else
                where = NOT_FOUND;
    	
    
    	printf("%d", where);
            
    	getch();
    	return (where);
    
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    i do not see a problem, in the first sample 5 is 6th number so its index is 5
    in the second sample 7 is the 7th number and its index is 6

    that's exsactly the output of the program.
    Where is the problem?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  3. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  4. array assignment question
    By threahdead in forum C Programming
    Replies: 2
    Last Post: 08-17-2003, 05:36 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM