Is this correct ?

This is a discussion on Is this correct ? within the C++ Programming forums, part of the General Programming Boards category; The program works...however for the question it states "The function should print true if the value is found in the ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    24

    Is this correct ?

    The program works...however for the question it states

    "The function should print true if the value is found in the array or false if the value is not found in the array."

    Would this be the way of doing it:

    Code:
    bool isMember(int a[], unsigned int size, int val);
    
    using namespace std;
    
    #include <stdio.h>
    #include <iostream>
    #include <stdlib.h>
    
    int main()
    {
    	int numbers[10] = {1,2,4,7,9,11,14,15,17,19};
    	int guess;
    
    	std::cout<<"Enter Any Number Between 1-20: ";
    	std::cin>>guess;
    
    	std::cout<<("\n");
    
    	if (isMember(numbers, 10, guess))
            {
    	       std::cout << "The number was found\n";
            }
            else
            {
    	       std::cout << "The number was not found\n";
            }
    }
    
    bool isMember(int a[], unsigned int size, int val)
    {
    	int i;
    
    	for (i = 0; i < size; i++ )
    	{
    		if (a[i] == val)
    		{
    			return true;
    		}
    	}
    
    	return false;
    
    }
    Last edited by devilsknight; 06-26-2006 at 09:13 AM. Reason: changing code

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    The program is strange, you are mixing C and C++, and for some reason, flag isn't a bool.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    You are basically doing a linear search, which is a correct way of doing it if your array is not guaranteed to be sorted. That said, if the function in question is supposed to return true, then it should return a bool. It would also be more flexible if you passed the size of the array.

    If the array will definitely be in sorted order, then you should use binary search instead.

    Incidentally, I notice that isMember() takes an int to search for, but then you are passing an array of floats. When comparing floating point values for equality, you need to account for a possible small margin of error, using operator == may sometimes fail for values that should be considered equal.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    24
    Quote Originally Posted by citizen
    The program is strange, you are mixing C and C++, and for some reason, flag isn't a bool.
    lol yah...k just for the record then, where am i using C and where am i using C++ ? Our prof never quite told us when we swtiched over to C++ ?

    Thanks for your help.

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    24
    Quote Originally Posted by laserlight
    You are basically doing a linear search, which is a correct way of doing it if your array is not guaranteed to be sorted. That said, if the function in question is supposed to return true, then it should return a bool. It would also be more flexible if you passed the size of the array.

    If the array will definitely be in sorted order, then you should use binary search instead.

    Incidentally, I notice that isMember() takes an int to search for, but then you are passing an array of floats. When comparing floating point values for equality, you need to account for a possible small margin of error, using operator == may sometimes fail for values that should be considered equal.
    i changed over the array from float to int...what do you mean by boolean ? how would i return a boolean ?

    thank you for your help

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    where am i using C and where am i using C++
    printf is from <stdio.h>, which is from C.

    what do you mean by boolean ? how would i return a boolean ?
    You would return true or return false.

    With an argument for the size of the array, your function prototype would then be:
    Code:
    bool isMember(int a[], unsigned int size, int value);
    By the way, <iostream.h> is pre-standard, write #include <iostream>

    This would also mean that you have to write std::cout instead of cout, and std::cin instead of cin. Alternatively, since this is a toy program, you can write:
    Code:
    using namespace std;
    somewhere at the top.
    Last edited by laserlight; 06-26-2006 at 07:06 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    24
    Quote Originally Posted by laserlight
    printf is from <stdio.h>, which is from C.


    You would return true or return false.

    With an argument for the size of the array, your function prototype would then be:
    Code:
    bool isMember(int a[], unsigned int size, int value);
    By the way, <iostream.h> is pre-standard, write #include <iostream>

    This would also mean that you have to write std::cout instead of cout, and std::cin instead of cin. Alternatively, since this is a toy program, you can write:
    Code:
    using namespace std;
    somewhere at the top.
    ok i changed the code around, but what would i use now inside of the function isMemeber to return true or false ? sorry i'm still very new at this.

    and what does std::... do exactly ?

    if you could help me with the isMember now that would be great thanks for your help.

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    and what does std::... do exactly ?
    Instructs the compiler that whatever follows is a name declared in the std namespace.

    try this:

    Code:
    #include <iostream>
    
    namespace test {
        int i;
    }
    
    int main()
    {
        int i;
    
        i = 0;
        test::i = 1;
        std::cout << i << " " << test::i << std::endl;
    
        return 0;
    }
    Note: This is bad form. Just an example. It's almost never a good idea to declare a local variable with the same name as a global one.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    what would i use now inside of the function isMemeber to return true or false ?
    Well, you would write something like:
    Code:
    bool isMember(int a[], unsigned int size, int value)
    {
    	for (/* etc */)
    	{
    		if (/* etc */)
    		{
    			return true;
    		}
    	}
    	return false;
    }
    and what does std::... do exactly ?
    std::cout means that cout belongs to, or is in the scope of, std. You can read up on C++ namespaces.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jun 2006
    Posts
    24
    Quote Originally Posted by laserlight
    Code:
    bool isMember(int a[], unsigned int size, int value)
    {
    	for (/* etc */)
    	{
    		if (/* etc */)
    		{
    			return true;
    		}
    	}
    	return false;
    }
    and where would i place the output ex: "The number was found" ?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    and where would i place the output ex: "The number was found" ?
    In the main() function:
    Code:
    if (isMember(numbers, 10, guess))
    {
    	std::cout << "The number was found\n";
    }
    else
    {
    	std::cout << "The number was not found\n";
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Jun 2006
    Posts
    24
    Ok i changed the code at the top. How does that look, i don't have a compiler here at work.

    Thanks for everything guys...appreciate it.

    devilsknight

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    Looks okay, but it is best that you actually test it by compiling and running the program when you have a compiler available. I would put the function prototype and using directive after the includes.

    Oh, and you should write:
    Code:
    std::cout << "\n";
    The parentheses around "\n" are useless in this case.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux for GNU/Linux is not correct?
    By password636 in forum Linux Programming
    Replies: 8
    Last Post: 03-31-2009, 08:30 PM
  2. Is this correct : passing strings?
    By socket in forum C Programming
    Replies: 15
    Last Post: 11-25-2008, 01:03 PM
  3. correct order to insert new node?
    By campermama in forum C++ Programming
    Replies: 1
    Last Post: 06-16-2004, 07:51 PM
  4. Replies: 1
    Last Post: 05-26-2004, 12:58 AM
  5. Loop until enter correct value
    By jchanwh in forum C Programming
    Replies: 2
    Last Post: 11-27-2001, 12:23 AM

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