Thread: Need help(Don't understand a few things)

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

    Need help(Don't understand a few things)

    Hello. I found this program in a book I read the explanation but I don't undersatnd a few things I hope that you guys can help me.

    Code:
    #include <stdio.h>
    #define SIZE 100
    
    int linearSearch(int [], int, int);
    
    int main()
    {
    	int a[SIZE], x, searchkey, element;
    
    	for(x=0; x<=SIZE-1; x++)
    	{
    		a[x] = 2*x;
    
    	}
    
    	printf("Enter the integer search key: ");
    	scanf("%d", &searchkey);
    	element = linearSearch(a, searchkey, SIZE);
    
    	if (element != -1)
    	{
    		printf("Found value in element %d\n", element);
    	}
    
    	else
    		
    		printf("Value not found\n");
    	
    
    		return 0;
    }
    
    int linearSearch(int array[], int key, int size)
    {
    		int n;
    
    		for(n=0; n <=SIZE-1; n++)
    			{
    				if (array[n] == key)
    				return n;
    			}
    
    			return -1;
    }
    1.
    Code:
    int linearSearch(int [], int, int);
    
    int linearSearch(int array[], int key, int size)
    Why is the function prototype different to the actual function declearation. I thought they were both meant to be the same. And is the program declearing and initilising the variable key, size and the array"array" in the function declearation.

    2.
    Code:
    element = linearSearch(a, searchkey, SIZE);
    I totally don't understand this line of code what is it doing and whay is it doing it?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by HAssan
    1.
    Code:
    int linearSearch(int [], int, int);
    
    int linearSearch(int array[], int key, int size)
    Why is the function prototype different to the actual function declearation. I thought they were both meant to be the same. And is the program declearing and initilising the variable key, size and the array"array" in the function declearation.
    It isn't different, in the "eyes" of the compiler. Variable names are irrelevant in the declaration. The parameters must have the same type, however.

    Quote Originally Posted by HAssan
    2.
    Code:
    element = linearSearch(a, searchkey, SIZE);
    I totally don't understand this line of code what is it doing and whay is it doing it?
    The function linearSearch is called with parameters a (the array), searchkey (the search key), and SIZE (the size of the array a). The value returned by the function is assigned to element.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Suggestions for things to study
    By Mastadex in forum Windows Programming
    Replies: 5
    Last Post: 08-18-2008, 09:23 AM
  2. Question about atheists
    By gcn_zelda in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 08-11-2003, 11:50 AM
  3. things to think about... [from www.nuclearwastesite.com]
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 11-11-2001, 12:47 AM
  4. What is your favorite things about programming?
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 10-21-2001, 12:13 PM
  5. Help with these three things...
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 08-26-2001, 07:05 AM