Need help on this array problem,
thebolded part is what I'm really confused, How can I check if the user entered a negative value?
and How can I return the count to the previous cout? thanks

2. Write a function to read in numbers and put them into the array. The function should continue to read in numbers until either: the user types a negative number, or 5 numbers (hint: MAXSIZE) have been read. The function should also return the number of elements in the array. Write comments before the function that describe what the function does, what it needs passed to it, what it sends back to the function call, and how it accomplishes its task.

3. Then write a second function that will accept an array and print out the contents of the array. This function should accept an array and the number of elements that are to be printed. Before writing the code for this function, write the comments.

//this is what I tried so far

#include <iostream>
using namespace std;
int Function1(double []);
void PrintArray(const double [], int);

const int MAXSIZE =5;

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

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

	PrintArray(array1, 5);//function call to function that will print the array


	return 0;
}

//Write the function here to read in numbers into the array.  The array is called myarray.
//add comments describing what this function does, what is passed to it, what it sends back, etc.
int Function1(double myarray[])
{ 
	cout<<"Please enter the number"<<endl;
int count = 0, next;
	while (myarray>=0)
	{cin>>next;
	myarray[count]=next;
	
 }
return 0;
}

//Write the new function here along with comments
//add comments describing what this function does, what is passed to it, what it sends back, etc.
void PrintArray(const double a1[], int size)
{
for (int x = 0; x < 5; x++ )
{
		cout << a1[ x ] << endl;
}
system("pause");
}

Edited 6 Years Ago by limengfang77: n/a

This article has been dead for over six months. Start a new discussion instead.