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

Finals project due tonight


I have fully functioning code. I need your help refining it. I just need to know any glaring errors here. So far most things look ok but I don't have any idea about the last two functions. I know this is not terrific code but be easy please, I'm new.
CPP / C++ / C Code:
#include <iostream>//headers
#include <string>
#include <cmath>
using namespace std;
class newClass{//define class
public:
	newClass();
	void arraySize();
	void getNumbers(int);
	void printArray();
	void arrayLength();
	void sortAscending();
	void sortDescending();
	bool insertOrder(int, int);
	int get(int);
	bool deleteIndex(int);
	bool deleteVal(int);
	string toString();
	float average();
	int maxNumber();
	int minNumber();
	string range();
	int median();
	float sd();
private:
	const static int maxSize=20;
	int newArray[maxSize];
	int count;
};
int main(){
	newClass first;
	int num=0, insertAt=0, deleteAt=0;
	int choice;//menu 
do
	{
		cout << endl << endl << "Choose from:" << endl << endl;
		cout << "1. Enter up to twenty numbers." << endl;
		cout << "2. Print all entries in the Array." << endl;
		cout << "3. Return the length of the Array." <<endl;
		cout << "4. Sort the Array in ascending order."<<endl;
		cout << "5. Sort the Array in descending order."<<endl;
		cout << "6. Insert a number into the array."<<endl;
		cout << "7. Return the integer at the array location."<<endl;
		cout << "8. Delete Array index."<<endl;
		cout << "9. Delete numberical value."<<endl;
		cout << "10. Return a string with contents of Array."<<endl;
		cout << "11. Return the average of the Array."<<endl;
		cout << "12. Return the largest number."<<endl;
		cout << "13. Return the smallest number."<<endl;
		cout << "14. Return the range of numbers."<<endl;
		cout << "15. Return the median of the numbers."<<endl;
		cout << "16. Return the standard deviation."<<endl;
		cout << "17. Quit program."<<endl;
		cin >> choice;
		switch (choice)
		{
		case 1:
			cout<<endl<<"Press '0' to quit."<<endl;
			first.getNumbers(num);
			break;
		case 2:
			first.printArray();
			break;
		case 3:
			first.arrayLength();
			break;
		case 4:
			first.sortAscending();
			break;
		case 5:
			first.sortDescending();
			break;
		case 6:
			cout<<"Enter the number."<<endl;
			cin>>num;
			cout<<"Enter where you want to insert the number."<<endl;
			cin>>insertAt;
			first.insertOrder(num,insertAt);
			break;
		case 7:
			cout<<"Please enter the array index number."<<endl;
			cin>>num;
			cout<<first.get(num);
			break;
		case 8:
			cout<<"Which array index number would you like to delete?"<<endl;
			cin>>deleteAt;
			first.deleteIndex(deleteAt);
			break;
		case 9:
			cout<<"Enter the number you want deleted."<<endl;
			cin>>num;
			first.deleteVal(num);
			break;
		case 10:
			first.toString();
			break;
		case 11:
			cout<<first.average();
			break;
		case 12:
			cout<<first.maxNumber();
			break;
		case 13:
			cout<<first.minNumber();
			break;
		case 14:
			cout<<first.range();
			break;
		default:
		case 15:
			cout<<first.median();
			break;
		case 16:
			cout<<first.sd();
			break;
		case 17:
			break;
			cout << "invalid input!!! Try it again." << endl;
		}
	} while (choice != '17');



	return 0;
}
newClass::newClass(){//default constructor
	for (int i=0;i<maxSize;i++)
		newArray[i]=0;
	
}
void newClass::getNumbers(int x){//input numbers for array
	count =0;
	int i=0;
	do{
	cin>>x;
	newArray[i]=x;
	i++;
	count++;}
	while (x!=0);
	
}
void newClass::printArray(){//output array elements
	for (int i=0;i<maxSize;i++){
		if (newArray[i]!=0)
			cout<<newArray[i]<<endl;}
}
void newClass::arrayLength(){//give length of array
	cout<<count-1;
}
void newClass::sortAscending(){//sort array from smallest to largest
	for (int i=0;i<maxSize;i++){
		for (int j=0;j<maxSize;j++){
			if (newArray[i]<newArray[j])
				swap(newArray[i],newArray[j]);
		}
	}
}
void newClass::sortDescending(){//really, do I need to explain this one
	for (int i=0;i<maxSize;i++){
		for (int j=0;j<maxSize;j++){
			if (newArray[i]>newArray[j])
				swap(newArray[i],newArray[j]);
		}
	}
}

bool newClass::insertOrder(int x, int y){//insert new value in array
	int temp=0;
	if (y>count){ 
		cout<<"The array does not go that high."<<endl;
		return false;
	}

	else	
	temp=newArray[count-1];
	for (int i=count;i>y;i--)
		swap (newArray[i],newArray[i-1]);
	newArray[y-1]=x;
	count++;
	return true;
}


int newClass::get(int x){//return element value
	if (x>count-2){
		cout<<"There's no number there Dave.";
			return false;
	}
		
	int temp=0;
	temp=newArray[x];
	return temp;
}

bool newClass::deleteIndex(int x){//remove number at index and resort
	if (x>count-1){
		cout<<"The array doesn't go that high."<<endl;
			return false;
	}
	else
		for (int i=x;i<count-1;i++){
		swap (newArray[i],newArray[i+1]);
		}
	newArray[count-1]=0;
	return true;
}


bool newClass::deleteVal(int x){//replace value at index number and resort
	int temp=0;
	newArray[x]=0;
	for (int i=x;i<count-1;i++)
		swap (newArray[i],newArray[i+1]);
	return true;
}
string newClass::toString(){//convert to string
	string newString="\0";
	for (int i=0;i<count-1;i++)
		newString+=newArray[i];
	return newString;//not returning value
}
float newClass::average(){//return average
	float temp=0.0, average=0.0;
	for (int i=0;i<count-1;i++)
		temp+=newArray[i];
	average=temp/(count-1);
	return average;
}
int newClass::maxNumber(){//give largest number in array
	int temp=0;
	for (int i=0;i<count-1;i++){
		if (newArray[i]>temp)
			temp=newArray[i];
	}
	return temp;
}
int newClass::minNumber(){//return smallest number
	int temp=newArray[0];
	for (int i=0;i<count-1;i++){
		if (newArray[i]<temp)
			temp=newArray[i];
	}
	return temp;
}


string newClass::range(){//give largest and smallest number
	int min, max;
	string range;
	min=minNumber();
	max=maxNumber();
	range=min+" "+max;
	return range;//does not return value.
}
int newClass::median(){//return median(the number in middle index(s)
	float temp=0;
	int lmedian=0, rmedian=0, median=0;
	temp=(count-1)%2;
	if (temp==0){
		lmedian=newArray[(count-1)/2-1];
		median+=lmedian;//cannot add space here to return both numbers
		rmedian=newArray[(count-1)/2];
		median+=rmedian;
		return median;
	}
	if (temp==1){
		median=newArray[(count-1)/2];
		cout<<"temp=1"<<median<<endl;
	return median;
	}
}
float newClass::sd(){//return standard deviation I'm 99.9% sure this doesn't work
	float temp=0.0, average=0.0, devFromMean=0, squareRtVariance=0;
	for (int i=0;i<count-1;i++)
		temp+=newArray[i];
	average=temp/(count-1);
	return average;
	for (int i=0;i<count-1;i++)
		devFromMean=(newArray[i]-average)*(newArray[i]-average);
	squareRtVariance= sqrt (devFromMean);
	return squareRtVariance;
}
  #2  
Old 12-Dec-2008, 19:51
Allan Allan is offline
New Member
 
Join Date: Nov 2008
Posts: 14
Allan is on a distinguished road

Re: Finals project due tonight


chech/validate all inputs.

what if i enter a char type.
  #3  
Old 12-Dec-2008, 20:06
Allan Allan is offline
New Member
 
Join Date: Nov 2008
Posts: 14
Allan is on a distinguished road

Re: Finals project due tonight


my suggestion is to validate/checks all user inputs

put a defualt statement in the swith() after the last case

CPP / C++ / C Code:
 
default:
   cout << "invalid input" << endl;
   break;

this will catch all invalid input when selecting from your menu.
 
 

Recent GIDBlogProblems with the Navy (Chiefs) 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
Haptic project karet112 C Programming Language 1 08-Aug-2008 10:46
problem with visual studio project veronica_yick MS Visual C++ / MFC Forum 0 26-Feb-2007 01:22
project dependencies Dominic MS Visual C++ / MFC Forum 1 14-Nov-2005 03:25
Need help with Simple 1D Array coding project rho C++ Forum 2 27-Jun-2005 20:05
Community Project Proposal dsmith Miscellaneous Programming Forum 71 19-Feb-2005 13:26

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

All times are GMT -6. The time now is 19:23.


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