I'm not finished with my program yet, but i've run into a few problems. The first main problem i see is that i seem to be doing something wrong to display the array that the user enters. It only shows one value, and it displays it twice. I cant check my other functions because that one doesn't work properly. Can anyone tell me what im doing wrong?
I also get 2 errors saying:
(37) : error C2065: 'max' : undeclared identifier
(40) : error C2065: 'min' : undeclared identifier
my code is below.
thx in advance for any help.

#include<iostream.h>

int get_array(int array[], int& size);
int display_array(int array[], int& size);
void reverse_array(int array[], int size);
int find_max(int array[], int size);
int find_min(int array[], int size);
void sort_array(int array[], int size);
int smallest_spread();
int largest_spread();
int positive_negative();

int index_of_smallest(const int array[], int start_index, int number_used);
void swap_values(int& v1, int& v2);


void main()
{
	int size;
	int original_array[20];
	int positive_array[20];
	int negative_array[20];

	get_array(original_array, size);
	cout << "\nOriginal";
	cout << display_array(original_array, size);
	
	reverse_array(original_array, size);
	cout << "\nReversed";
	cout << display_array(original_array, size);
	
	find_max(original_array, size);
	cout << "\nMaximum value = "<< max;

	find_min(original_array, size);
	cout << "\nMinimum value= " << min;

	sort_array(original_array, size);
	cout << "\nSorted";
	cout << display_array(original_array, size);
}


int get_array(int array[], int& size)
{
	
	cout << "Enter the array size: ";
	cin >> size;
	if(size > 20)
		cout << "Invalid size entered.";
	else
		for(int i=0; i < size; i++)
		{
			cout << "Enter element #" << i+1 << ": ";
			cin >> array[i];
		}
	return size;
}

int display_array(int array[], int& size)
{
	for(int i=0; i < size; i++)
	{
		cout << " array: " << array[i];
		return array[i];
			while(i<size)
			cout<< ", ";
	}
	return 0;
}

void reverse_array(int array[], int size)
{
	int temp;
	int j = size-1;
	for(int i = 0; i < size/2; i++, j--)
	{
		temp = array[i];
		array[i] = array[j];
		array[j] = temp;
	}
	
}

int find_max(int array[], int size)
{
	int max=array[0];
	for(int i=1; i < size; i++);
	{
		
		if(array[i]> max)
			max = array[i];
	}
	return max;
}

int find_min(int array[], int size)
{
	int min = array[0];
	for(int i=1; i < size; i++)
	{
		if(array[i] < min)
			min = array[i];
	}
	return min;
}

void sort_array(int array[], int size)
{
	int index_of_next_smallest;
	for(int i = 0; i < size-1; i++)
	{
		index_of_next_smallest= index_of_smallest(array, i, size);
		swap_values(array[i], array[index_of_next_smallest]);
	}
}
void swap_values(int& v1, int& v2)
{
	int temp;
	temp = v1;
	v1 = v2;
	v2 = temp;
}
int index_of_smallest(const int array[], int start_index, int number_used)
{
	int min = array[start_index],
		index_of_min = start_index;
	for(int i = start_index + 1; i < number_used; i++)
		if(array[i] < min)
		{
			min = array[i];
			index_of_min = i;
		}
	return index_of_min;
}

Recommended Answers

All 5 Replies

I also get 2 errors saying:
(37) : error C2065: 'max' : undeclared identifier
(40) : error C2065: 'min' : undeclared identifier

It means that in main, these two aren't declared!

What are they, integers, floats, an array, ...

Out of curiousity;
Tough I'm far from a programmer and I'm certain that I'll never be as good as most of you who post here, I find it hard to understand that, alltough you wrote this program, you fail to understand the simple errors that are shown to you while trying to compile.

Please, don't think that I'm trying to put you down, as I said, I'm not good at this myself, but such simple things as searching for errors while compiling certainly the ones you have are issues that you should be able to solve yourself, don't you think?

well i wasn't exactly asking for help with those 2 errors. i was more so looking for help with the array problem.

>void main()
This is not, and never has been, correct C++. main returns an integer.

>int display_array(int array[], int& size)
I notice that size is never modified, so the primary reason for passing a non-const reference doesn't apply. Because a reference is usually the same size as an int or bigger, there's no performance reasons. So why are you passing size by reference?

>It only shows one value, and it displays it twice.
That's because you print array when i is 0 then return that same value immediately, where the calling function prints it. Perhaps this would be more to your liking:

void display_array ( int array[], int size )
{
  for ( int i = 0; i < size - 1; i++ )
    cout<< array[i] <<", ";
  cout<< array[i] <<endl;
}

Then in main, change

cout << display_array(original_array, size);

to

display_array(original_array, size);

Just as a side note in case it wasn't obvious, a comma typically is not wanted after the last item, so my display loop prints all but the last item (array) with a comma in the loop body, then after the loop the last item is printed with a newline and the stream is flushed. At this point i would have the value of size - 1, so it all works out.

thank you so much. i had just noticed the comma problem ;) i've got it for the most part now. I just noticed that when it prints out the max, it prints the last value that was entered, yet the minimum value prints out correctly. any idea why? :-| i dont see why one would work and not the other.

int find_max(int array[], int size, int& max)
{
	max = array[0];
	for(int i=1; i < size; i++);
	{
		if(array[i] > max)
			max = array[i];
	}
	return max;
}

int find_min(int array[], int size, int& min)
{
	min = array[0];
	for(int i=1; i < size; i++)
	{
		if(array[i] < min)
			min = array[i];
	}
	return min;
}

How would I change the size of the positive and negative arrays so that it doesn't print out the total size of the original array? The way i have it set up now, it prints out the positive numbers, and the rest of the array is filled with 0's.
Here's a sample of my output:

Original array: -12, -8, 9, 4, 5
Reversed array: 5, 4, 9, -8, -12
Maximum value = 9
Minimum value= -12
Sorted array: -12, -8, 4, 5, 9
Smallest spread = 1
Largest spread = 12
Positive array: 4, 5, 9, 0, 0
Negative array: -12, -8, 0, 0, 0
Press any key to continue

This is my code as I have it now:

#include<iostream.h>

int get_array(int array[], int& size);
void display_array(int array[], int size);
void reverse_array(int array[], int size);
int find_max(int array[], int size, int& max);
int find_min(int array[], int size, int& min);
void sort_array(int array[], int size);
int smallest_spread(int array[], int size, int& s_spread);
int largest_spread(int array[], int size, int& l_spread);
void positive_negative(int array[], int size, int positive[], int negative[]);

int index_of_smallest(const int array[], int start_index, int number_used);
void swap_values(int& v1, int& v2);


void main()
{
	int size, biggest, smallest, small_spread, large_spread;
	int original_array[20];
	int positive_array[20];
	int negative_array[20];

	get_array(original_array, size);
	cout << "\nOriginal";
	display_array(original_array, size);
	
	reverse_array(original_array, size);
	cout << "\nReversed";
	display_array(original_array, size);
	
	find_max(original_array, size, biggest);
	cout << "\nMaximum value = "<< biggest;

	find_min(original_array, size, smallest);
	cout << "\nMinimum value= " << smallest;

	sort_array(original_array, size);
	cout << "\nSorted";
	display_array(original_array, size);

	smallest_spread(original_array, size, small_spread);
	cout << "\nSmallest spread = " << small_spread;

	largest_spread(original_array, size, large_spread);
	cout << "\nLargest spread = " << large_spread;

	positive_negative(original_array, size, positive_array, negative_array);
	cout << "\nPositive";
	display_array(positive_array, size);
	cout << "\nNegative";
	display_array(negative_array, size);


}


int get_array(int array[], int& size)
{
	
	cout << "Enter the array size: ";
	cin >> size;
	if(size > 20)
		cout << "Invalid size entered.";
	else
		for(int i=0; i < size; i++)
		{
			cout << "Enter element #" << i+1 << ": ";
			cin >> array[i];
		}
	return size;
}

void display_array(int array[], int size)
{
	cout << " array: ";
	for(int i=0; i < size; i++)
	{
		cout << array[i];
			if (i < size-1)
			cout<< ", ";
	}
}

void reverse_array(int array[], int size)
{
	int temp;
	int j = size-1;
	for(int i = 0; i < size/2; i++, j--)
	{
		temp = array[i];
		array[i] = array[j];
		array[j] = temp;
	}
	
}

int find_max(int array[], int size, int& max)
{
	max = array[0];
	for(int i=1; i < size; i++)
	{
		if(array[i] > max)
			max = array[i];
	}
	return max;
}

int find_min(int array[], int size, int& min)
{
	min = array[0];
	for(int i=1; i < size; i++)
	{
		if(array[i] < min)
			min = array[i];
	}
	return min;
}

void sort_array(int array[], int size)
{
	int index_of_next_smallest;
	for(int i = 0; i < size-1; i++)
	{
		index_of_next_smallest= index_of_smallest(array, i, size);
		swap_values(array[i], array[index_of_next_smallest]);
	}
}
void swap_values(int& v1, int& v2)
{
	int temp;
	temp = v1;
	v1 = v2;
	v2 = temp;
}
int index_of_smallest(const int array[], int start_index, int number_used)
{
	int min = array[start_index],
		index_of_min = start_index;
	for(int i = start_index + 1; i < number_used; i++)
		if(array[i] < min)
		{
			min = array[i];
			index_of_min = i;
		}
	return index_of_min;
}

int smallest_spread(int array[], int size, int& s_spread)
{
	int spread;
	s_spread = array[1] - array[0];
	for(int i = 1; i < size-1 ; i++)
	{
		spread = array[i+1] - array[i];
		if(spread < s_spread)
			s_spread = spread;
	}
	return s_spread;
}

int largest_spread(int array[], int size, int& l_spread)
{
	int spread;
	l_spread = array[1] - array[0];
	for(int i = 1; i < size-1 ; i++)
	{
		spread = array[i+1] - array[i];
		if(spread > l_spread)
			l_spread = spread;
	}
	return l_spread;
}

void positive_negative(int array[], int size, int positive[], int negative[])
{
	int i,p = 0,n = 0 ;
	for(i=0; i < size; i++)
	{
		positive[i]=0;
		negative[i]=0;
	}
	for(i=0; i < size; i++)
	{
		
		
		if(array[i] >= 0)
		{
			positive[p] = array[i];
			p++;
			
		}
		else
		{
			negative[n] = array[i];
			n++;
			
		}
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.