I am told to write a definition after the main program that takes numbers from the keyboard and stores them in an array. I'm slightly confused on how to do this step. Help would be majorly appreciated. This is what I have so far with the program.

#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;

void input (int i, int element[]);

int main()
{

int i, element[10], max;
i = 0;

return 0;
}

Ok now you need to use a for loop. Have you used it before? Give it a try.
Inside the for loop, you ask the user for a number and insert it into the array.

is this the correct for loop?

for(i = 0; i < 10; i++)
cout<<"Please enter numbers to be store in the array";

something like this?

Almost. First you need the curly braces so that you can include multiple statements like so :

for(i = 0; i < 10; ++i){
 //code goes here
}

Now you can put that "cout << "Please ..." inside the loop. But you also have to get the user input by using cin.

Edited 6 Years Ago by firstPerson: n/a

oh yea sorry my bad but how does the function know how big the array is? our array has 10 elements

You need to tell it like so :

void inpit(int array[], int arraySize){
  for(int i = 0; i < arraySize; ++i){
    //code goes here
  }
}
int main(){
 const int ARRAY_SIZE = 10; //create a constant number 
 int array[ARRAY_SIZE] = {0}; //create an array of ARRAY_SIZE and initialize it to 0
 input(array, ARRAY_SIZE);
}

I have added the suggested changes and the output only gives me one error...
it says i have an (undefined reference to 'input(int*, int)') on line 26

#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;

void input (int array[], int arraySize);

int main()
{
    const int arraySize = 10; //create a constant number
    int array[arraySize] = {0}; //create an array of ARRAY_SIZE and initialize it to 0
    input(array, arraySize);

    int i, element[10], max;
    i = 0;
    for (i = 0; i < 10; i++)
    {
        cin>> element[i];
        if (element[10] > max)
        max = 10;
    }

    cout<<"The numbers are"<<element[i]<<endl;

    return 0;
}

You need to define the input function. Put the for loop inside the input function like so :

void input (int array[], int arraySize){
  //for loop code
}

oh my gosh, that fixed it! yay! I have just one more question. the program is supposed is supposed to print all the values to the main screen and i need to add pre- and post conditions to the function prototype. I have no idea what pre and post conditions are, let alone how to add them to the prototype. any ideas?

oh my gosh, that fixed it! yay! I have just one more question. the program is supposed is supposed to print all the values to the main screen and i need to add pre- and post conditions to the function prototype. I have no idea what pre and post conditions are, let alone how to add them to the prototype. any ideas?

I'm not sure exactly what you are trying to accomplish here. Can you explain again?

I'm supposed to be calling the function in my main program (which i did...i think) and then in the main program print all the values to the screen. Lastly I need to add pre and post conditions to my function prototype. But I'm unclear about the last two things

First to print all the values, you can just use a for loop and print out each element inside the array. And for the pre and post condition, what are the condition to be meet Is this an assignment? Post the exact problem.

Here is the problem:

The basics - declaring an array and then reading input into its elements.
1. In the main program, declare 1 array variable:
o an integer array with 10 elements
o initialize all array elements to 0

2. Write the declaration for a void function to read user input into the first array.
o Return data type: void
o Function name: input
o Parameters: 1 array parameter, 1 call-by-value parameter

3. Write the definition of the same function: (after main)
o This function should read in numbers from keyboard and store them in the array.
o The input should end when the array is full. (How does the function know how big the array is?)

4. Call the function from your main program.

5. In the main program, print all values to the screen.

6. Test your code; make sure there are no errors and that it compiles fine. Also, now would be a good time to add the pre- and post-conditions to the function prototype.

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