hi, this is the problem, i wrote what i could but i dont know what im doing wrong


1. 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.

2 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 have 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");
}

Recommended Answers

All 3 Replies

I would suggest that you descibe what is wrong...that is what are you experiencing so in the future it makes it easier for folks to help.

I would incorporate the comments you have describing what the program should do into the program itself, as your comments suggest you should do.

you obviously have problems in both functions but the most important one is in the Function1 where your loop has the wrong condition. You should probably be checking for count < MAXSIZE. In addition within your loop you should prompt the user to enter a number and immediately check if it is < 0, so you know to exit the loop and stop collecting numbers. Last and most importantly, whenever a program does not stop, examine any loops you have and ask yourself "what is the condition for stopping?" For example, if you change the loop condition to test the current count value against MAXSIZE you know you better have a statement in your loop that changes the value of count, e.g. ++count. I also suggest you have that function RETURN the value of count to the calling program, as in return count;

you will then have to change the PrintArray to use the size value and only print out the values from 0..count/size.

Hang in there.

Either you've posted the same problem twice under different user names, or someone's copying someone's homework. Earlier post by limengfang77 is exactly the same code.

It's obviously code they were given by the instructor, and they both want us to finish it for them.

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.