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, 17:53
scuzzo scuzzo is offline
Awaiting Email Confirmation
 
Join Date: Jul 2007
Posts: 19
scuzzo is an unknown quantity at this point

Same array, new problem


So here is what I've got a problem with. I have created a function that generates a random number. I need to check the array for equal numbers (all between 1-20) and output the number that appears the most. My problem is that I'm getting odd outputs at both pulling out the repeating numbers, and at trying to output just one of the repeating numbers. My major problem is in the commonElement function.
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 maxElements();
	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::maxElements(){
//	}
	void myArray::commonElement(){
		int c=0;
		int newArray[20];
		for (int a=0;a<20;a++)//set all values to zero
			newArray[a]=0;
		for (int i=0;i<20;i++){
			for (int j=i+1;j<20;j++){//nested for loop to sort equal numbers
				if (A[i]==A[j]){//compare numbers
					newArray[c]=A[i];//assign equal numbers to new array
					c++;
			}
		}
		}
		for (int i=0;i<20;i++){
			for (int j=i+1;j<20;j++){
				if (newArray[i]==newArray[j]&&newArray[j]!=0)
					cout<<newArray[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;
		//case '5':
		//	
		//	break;
		//case '6':
		//	
		//	break;
		//case '7':
		//	
		//	break;
		//case '8':
		//	
		//	break;
		default:
			cout << "invalid input!!! Try it again." << endl;
		}
	} while (option != 5);
	
system ("pause");
return 0;
}

 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

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
where is the problem and can you fix it (php) oggie MySQL / PHP Forum 8 14-Apr-2008 16:08
Getting a line error in register oggie MySQL / PHP Forum 5 13-Apr-2008 17:16
Returning a 2 dimensional Array from a function vicky_brsh C++ Forum 1 04-Jan-2008 15:06
Loop problem on an array trp_be C Programming Language 1 06-Jan-2007 08:49
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 20:31

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:26.


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