Need help with an array assignment

This is a discussion on Need help with an array assignment within the C Programming forums, part of the General Programming Boards category; Hey guys im having problems with this assignment. Thanks to all of you that helped with the sort. Now I ...

  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);
    
    }
    Attached Images Attached Images   

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 03:39 PM
  2. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 01:20 PM
  3. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-12-2004, 12:15 AM
  4. array assignment question
    By threahdead in forum C Programming
    Replies: 2
    Last Post: 08-17-2003, 06:36 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21