Arrays, how to find if some number is the element of an array.

This is a discussion on Arrays, how to find if some number is the element of an array. within the C++ Programming forums, part of the General Programming Boards category; Well im trying to figure out arrays and such and working on a program to find if a number inputed ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    494

    Arrays, how to find if some number is the element of an array.

    Well im trying to figure out arrays and such and working on a program to find if a number inputed by the user belongs to a particular array. this is what i have so far and im kinda stuck as i got no idea how to make the next step.

    So i have initialized 2 arrays with different numbers and im asking the user to find a number. When the user lets say, enters 4. The program should say 4 is in ArrayA, if the user enters 7 then the program should say 7 is in ArrayB. Any suggestions?
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	
    
    	int ArrElement;
    	int *pArrElement =&ArrElement;
    
    	int ArrayA[8];
    	int ArrayB[8];
    
    	for (int i =0; i<5; i++)
    	{
    		ArrayA[i] = i+1;
    		cout << "ArrayA["<< i << "]: " << ArrayA[i] << "\n";
    		cout <<"\n";
    	}
    
    		for (int j=5; j<10; j++)
    		{
    			ArrayB[j] = j+1;
    			cout << "ArrayB["<< j << "]: " << ArrayB[j] << "\n";
    			cout <<"\n";
    		}
    
    		cout << "Find a number between 1 and 10 \n";
    		cin >> ArrElement;
    		cout << *pArrElement << " belongs to ArrayA\n";
    
    
    	return 0;
    }
    When no one helps you out. Call google();

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    In theory, one solution is binary search. In practice, check out STL.

    Kuphryn

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Is there any reason you're using arrays instead of lists or vectors?

    If you're going to use arrays, loop through one element at a time and use an if to compare that element with the value you are matching to.

    Code:
    for(int x=0; x!=arraySize; ++x)
    {
       if (array[x] == matchValue)
          // Do something
    
    }

  4. #4
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    brute force

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    	int num;
    	int arrayA[4] = {0,1,2,3};
    	int arrayB[5] = {4,5,6,7,8};
    	cout << "Enter a number 0-8";
    	cin >> num;
    	for (int i = 0; i < sizeof(arrayA)/sizeof(*arrayA); i++)
    	{
    		if (arrayA[i] == num)
    			cout << "The number is in arrayA";
    	}
    	for (int i = 0; i < sizeof(arrayB)/sizeof(*arrayB); i++)
    	{
    		if (arrayB[i] == num)
    			cout << "The number is in arrayB";
    	}
    }

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    in not very familiar with lists or vectors yet so thats why. I search for Binary search and it looks promising, as it is better than brute force, but now i gotta figure out how it works and not just what it does.

    Oh another question. The book im studying from Sams Teach yourself C++, says that you can combine 2 arrays and make a 3rd array. But it doesnt explain how that is done. my other step on this would be to find 1 element from each array, delete those values from the arrays that they belong and save them into the 3rd array. Any suggestion, or if there is any other better way to do this?
    When no one helps you out. Call google();

  6. #6
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    you do realise your code will crash?
    Quote Originally Posted by InvariantLoop
    Code:
    	int ArrayB[8];
    
    		for (int j=5; j<10; j++)
    		{
    			ArrayB[j] = j+1; // oh dear, accessing ArrayB[9] on an 8 element array
    besides that you want std::find
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    Quote Originally Posted by ChaosEngine
    you do realise your code will crash?


    besides that you want std::find
    it doesnt crash, ive already tested it, i get 0 errors, 0 warnings.
    When no one helps you out. Call google();

  8. #8
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    it doesnt crash, ive already tested it, i get 0 errors, 0 warnings.
    It does crash for me, and I would suspect for most compilers

    0 errors or warnings has nothing to do with it

  9. #9
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    It will either crash or give you a funky result, either way, its a semantic error not an error a compiler will normally catch.

    If you set ArrayB[j+1] = anything, you will find some bad things happening. You are going outside your bounds of array. If you run from 5-10, i.e. 5,6,7,8,9 and you defined ArrayB as ArrayB[10], the elements will go from 0,1,2,3,4,5,6,7,8,9 which is 10 elements. At j == 10, you are going to the 11th element in the array. Sound like the right thing to you?

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    it doesnt crash, ive already tested it, i get 0 errors, 0 warnings.
    The point isn't whether your program crashes or not. When you access an index that is out of bounds, the behavior is undefined and therefore doing so will cause unpredicatable results. An array of size 7 has index values that run from 0-6.

    my other step on this would be to find 1 element from each array, delete those values from the arrays that they belong and save them into the 3rd array. Any suggestion, or if there is any other better way to do this?
    Omit the delete part, and you've got it
    Last edited by 7stud; 03-15-2006 at 07:10 PM.

  11. #11
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    i understand what you mean.
    When no one helps you out. Call google();

  12. #12
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    >it doesnt crash, ive already tested it, i get 0 errors, 0 warnings.
    it will crash when u try running it.

  13. #13
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Quote Originally Posted by qqqqxxxx
    >it doesnt crash, ive already tested it, i get 0 errors, 0 warnings.
    it will crash when u try running it.
    Like 20 people already said the same thing before you so shut up!

  14. #14
    Registered User
    Join Date
    Mar 2006
    Posts
    3
    >Like 20 people already said the same thing before you so shut up!

    please refrain from such nonsense .

  15. #15
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Well, if you use STL iterators, you can stop worrying about overflows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. using realloc for a dynamically growing array
    By broli86 in forum C Programming
    Replies: 10
    Last Post: 06-27-2008, 06:37 AM
  3. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 03:31 PM
  4. can u help me with pointers?plzzzzzzzz
    By itcs in forum C Programming
    Replies: 7
    Last Post: 07-11-2003, 02:29 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

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