VB icon

Array Fun

Email
Submitted on: 8/16/2016 7:57:15 PM
By: Ben  
Level: Beginner
User Rating: Unrated
Compatibility: C++ (general), Microsoft Visual C++, UNIX C++
Views: 1435
 
     A small project using many things you can do with an array of numbers. /Get Max, Min, Sum, Sort and more. hope you find the code usfull.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Array Fun
// Description:A small project using many things you can do with an array of numbers.
/Get Max, Min, Sum, Sort and more. hope you find the code usfull.
// By: Ben
//**************************************

//A small project using many things you can do with an array of numbers.
//Get Max, Min, Sum, Sort and more.
#include <iostream>
using namespace std;
using std::cout;
using std::endl;
int range(int data[],int size){
	int i = 0;
	int number = data[0];
	int r = number;
	int count = 1;
	int cnt = 1;
	while(i < size){
		if(data[i] == number){
			//INC current number count found.
			count++;
		}else{
			//Check for next numbers
			if(count > cnt){
				cnt = count;
				//Mode is the number that gets found.
				r = number;
			}
			//Reset counter
			count = 1;
			//Get next number.
			number = data[i];
		}
		//INC counter
		i++;
	}
	//Return number found most times.
	return r;
}
int middle(int data[],int size){
	int m = (size/2);
	return data[m];
}
int _find(int data[],int size, int findwhat){
	int i = 0;
	int idx = -1;
	while(i < size){
		if(data[i] == findwhat){
			idx = i;
			break;
		}
		i++;
	}
	return idx;
}
void _reverse(int data[], int size)
{
	int i = 0;
	int t = 0;
	
	for(i = 0;i<(size/2);i++){
		//Get temp item
		t = data[size-i-1];
		data[size-i-1]=data[i];
		data[i]=t;
	}
}
void _sortnums(int data[], int size){
	int i = 0;
	int j = 0;
	for(i = 0;i<size;i++){
		for(j = i + 1;j<size;j++){
			if(data[i] > data[j]){
				swap(data[i],data[j]);
			}
		}
	}
}
void print_array(int data[], int size){
	for(int i = 0;i<size;i++){
		if(i == size - 1){
			std::cout << data[i];
		}
		else{
			std::cout << data[i] << ",";
		}
	}
	std::cout << endl;
}
int average(int data[], int size){
	int ret = 0;
	for(int i = 0; i < size;i++){
		ret+=data[i];	
	}
	return (ret / size);
}
int sum(int data[], int size){
	int ret = 0;
	for(int i = 0; i < size;i++){
		ret+=data[i];	
	}
	return ret;
}
int max(int data[], int size){
	int ret = data[0];
	//Find the max value in data
	for(int i = 1;i<size;i++){
		if(ret < data[i]){
			ret = data[i];
		}
	}
	return ret;
}
int min(int data[], int size){
	int ret = data[0];
	//Find the min value in data
	for(int i = 1;i<size;i++){
		if(ret > data[i]){
			ret = data[i];
		}
	}
	return ret;
}
int main(){
	//Add some random ints
	int nums[] = {5,8,3,3,11,10,62,62,62,10,184,2,6,3,0,196,96,3,10,10,10,1};
	int len = sizeof nums / sizeof(nums[0]);
	system("title ArrayFun");
	cout << "Range of nums unsorted ";
	print_array(nums,len);
	cout << endl;
	cout << "Items in nums is: " << len << endl;
	cout << "Largest number in nums is : " << max(nums,len) << endl;
	cout << "Smallest number in nums is : " << min(nums,len) << endl;
	cout << "Total sum of nums is: " << sum(nums,len) << endl;
	cout << "Middle of nums is : " << middle(nums,len) << endl;
	cout << "Average of nums is : " << sum(nums,len) << "/" << len 
		<< "=" << average(nums,len) << endl;
	cout << "158 was found at index : " << _find(nums,len,3) << endl << endl;
	//Reverse nums
	_reverse(nums,len);
	cout << "Reverse nums ";
	print_array(nums,len);
	cout << endl;
	//Short nums
	_sortnums(nums,len);
	cout << "Sorted nums ";
	print_array(nums,len);
	cout << endl;
	//Make sure to sort the numbers first
	cout << "Number most often found: " << range(nums,len) << endl;
	system("pause");
	return 1;
}


Other 50 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.