Array problem

Hello,

Currently, i was assigned to create an array, which i have no idea what array is.

The problem is this....

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

-Problem solved- Thanks!
Last edited on
Well to start with your going to use the const MAXSIZE as your array size.

double array1[MAXSIZE]

You need to create a loop that runs until it is == to MAXSIZE. If at any point during the loop a negative number is entere you can then break; out of the loop. You will need to validate each number entered to check for a negative. If it's less than 0 obviously it's a negative number... At the end of the function you should then return your loop counter variable which should increment each iteration of the loop. This would return the number of elements containing numbers.

That should get you started.
Last edited on
We are not going to do the entire problem for you. You need to show us your attempts and ask specific questions. It looks like you've been given a skeleton, so that's a start.

Read the following pages to understand arrays and input from the console:

http://www.cplusplus.com/doc/tutorial/arrays/
http://www.cplusplus.com/doc/tutorial/basic_io/

Post up your attempts at defining the funtions.
I have the same assignment but i don't understand what i am doing wrong. I can get it to read numbers that I input but i can't get it to print out the numbers.

hERE IS WHAT I HAVE

//add general program comments here



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

const int MAXSIZE =100;

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

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

PrintArray( myarray, count); //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[])
{
using namespace std;
cout << "Enter up to 100 nonnegative numbers.\n";
int next, index = 0;
cin >> next;
while (( next >= 0 ) && ( index < MAXSIZE ))
{

myarray[index]= next;
index++;
cin >> next;
}

return index;
}

//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 i =0; i < size; i++)
cout << a1[i] << " ";
cout<< endl;
}

Nevermind i figured it out it should be PrintArray( array1, count) not PrintArray( myarray, count).
Topic archived. No new replies allowed.