Hey I'm new here to these forums and I'm in need of some help with my program. I'm supposed to write a program that will allow the user to enter up to 100 values into an array. The program will then count how many values were entered and display the number counted along with the actual values themselves. To top it off, the program then needs to sort the values in ascending order and display them back. Could anyone help me with my code please?

#include <iostream>

using namespace std;
int Function1(double []);
void Printarray(double [], int );


int main()
{
	double array1[100];
	int count;

	count = Function1(array1);
	cout<<"There were "<<count<<" numbers entered into the array."<<endl;
	Printarray(array1, count);

	int i;
	int last = count - 2;
	int first = 0, pass = 0;
	bool sort = false;
	double fntArray[count];
	double temporary;
	
	while (!sort)
	{
		sort = true;
		for (i = first; i <= last; i++)
		{
			if (fntArray[i] > fntArray[i+1])
			{
				temporary = fntArray[i];
				fntArray[i] = fntArray[i+1];     
				fntArray[i+1] = temporary;
				sort = false;
				
			}
				
		}
		last = last - 1;
		pass++;
		cout<<pass<<endl;
		Printarray(array1, count);
	}

	return 0;
}


//This function counts the number of values entered by the user (up to 100) and then returns the number of values counted.
//
int Function1(double fntArray[])
{
	int i = 0;
	double somenumber = 0;
	do
	{
		cout<<"Please enter a number to enter into the array or a negative number to stop."<<endl;
		cin>>somenumber;
		if (somenumber >= 0)
		{
			fntArray[i] = somenumber;
			i++;
		}
	}while (i < 100 && somenumber >=0);
	
	return i;
}

//This function prints each value entered by the user and returns the array of values to the main function.
//
void Printarray(double fntArray[ ], int size)
{ for (int j = 0; j < size ; j++)
		{cout<<fntArray[j]<<endl;
		cout<<" "<<endl;
		}
}

Recommended Answers

All 4 Replies

Member Avatar for iamthwee

Frst of all try it without functions.
Then see if you can sort it without functions.
Then try it using functions.

Search the forums for bubble sort...

line 21: All that does is declare an array. Yet on line 27 you are attempting to sort it. Where are you copying the values of array1 into the array that was declared on line 21?

Also, you can't declare an array like that because count needs to be a const value. You have to use new to dynamically allocate the array double* fntArray = new double[count];

double fntArray[count];
work in gcc

double fntArray[count];
work in gcc

Yes, because the gcc people are getting ahead of themselves. That usage has been adopted for C, but has yet to be actually incorporated in the C++ standard, as I understand it all.

So such use is not going to be portable for some time t come.

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.