GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Dec-2008, 16:00
scuzzo scuzzo is offline
Awaiting Email Confirmation
 
Join Date: Jul 2007
Posts: 19
scuzzo is an unknown quantity at this point

Find duplicate numbers in an array


I'm trying to write a program that has an array in a class. That array will take in random numbers then I will find the duplicate numbers and output the duplicate numbers(but only one of each dup). I'm getting an unhandled exception when I try to call the commonElement function. As it stands right now I only have the function outputting so I can see that I have the correct numbers being sorted out of the array.
CPP / C++ / C Code:
 
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <cmath>

using namespace std;
class myArray{
public:
	myArray();
             void setRandomNum();
	void getPrint();
	void commonElement();

private:
	int n;
	int A[20];
};
	myArray::myArray(){//constructor
		n=0;
	}
	void myArray::setRandomNum(){
		for (int i=0;i<=20;i++){
			n=rand()%20+1;
			A[i]=n;
		}
	}
	void myArray::getPrint(){
	for (int i=0;i<=20;i++)
		cout<<A[i]<<endl;
	}

	void myArray::commonElement(){
		for (int i=0;i<=20;i++){
			for (int j=i+1;i<=20;j++){

				if (A[i]==A[j]){
					cout<<"The common numbers are:"<<endl<<A[i]<<endl;}
			}
		}
	}
int main(){
 myArray one;



	char option;//set menu
do 	{
		cout << endl << endl << "Choose from:" << endl << endl;
		cout << "1. Set Array" << endl;
		cout << "2. Output Array" << endl;
		cout << "3. Output Common number." << endl;
		//cout << "4. Compare Second to First Word" << endl;
		//cout << "5. Stop Program" << endl;
		//cout << "6. " << endl;
		//cout << "7. " << endl;
		//cout << "8. " << endl;

		cin >> option;

		switch (option)
		{
		case '1':
			cout<<"The Array is initialized";
			one.setRandomNum();
			break;
		case '2':
			cout<<"The Array Elements";
			one.getPrint();
			break;
		case '3':
			cout<<"The largest common elements are:"<<endl;
			one.commonElement();		
		//	break;
		//case '4':
		//	cout<<newString.compareOddStrings(first, second);
		//	break;
				default:
			cout << "invalid input!!! Try it again." << endl;
		}
	} while (option != 5);
	
system ("pause");
return 0;
}
  #2  
Old 03-Dec-2008, 16:24
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,464
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: array problem


At least one potential problem that I notice...

The array only has 20 elements, the loop conditions <= 20 will be problematic (will go out of bounds, common reason for an exception), those conditions should be < 20.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #3  
Old 03-Dec-2008, 16:34
scuzzo scuzzo is offline
Awaiting Email Confirmation
 
Join Date: Jul 2007
Posts: 19
scuzzo is an unknown quantity at this point

Re: array problem


There is nothing that makes me feel like a douche nozzle more than dumb-assing all over something as simple as this. So I fixed the i/j problem in the second for loop in the commonElement function, but I still am not seeing the proper output. It is skipping results and I don't know what else. I'm working on it though. Thanks for the help.
  #4  
Old 03-Dec-2008, 17:43
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,464
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: array problem


Can you post the latest code change(s)?

EDIT:
Also, a couple of other things:
1. The 'break' for case 3 appears to have been commented-out by mistake, which will 'fall through' to the default.
2. In the commonElement() function, the inner-loop (with 'j') is not necessary. Rather, use the loop with i, make it one less in the condition, and use it this way:
CPP / C++ / C Code:
	for (int i=0;i<(20-1);i++){ // use < 19 for 'looking ahead'
		if (A[i]==A[i+1]){
			cout<<"The common numbers are:"<<endl<<A[i]<<endl;
	       }
      }
3. using the rand(), know that it could be possible to not find common values!
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Array Problem lief480 C++ Forum 3 26-Oct-2008 09:12
where is the problem and can you fix it (php) oggie MySQL / PHP Forum 8 14-Apr-2008 15:08
Getting a line error in register oggie MySQL / PHP Forum 5 13-Apr-2008 16:16
Returning a 2 dimensional Array from a function vicky_brsh C++ Forum 1 04-Jan-2008 15:06
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 20:31

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:15.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.