Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include <iostream>
using namespace std;

const int CAPACITY=20;

void displayArray(int array[], int numElements);

void fill_array (int array[], int CAPACITY, int &numElements);

int main ()
{
    int NumArray[CAPACITY];	// an int array with a given CAPACITY
    int numElements=0;  // the array is initially empty, i.e., contains 0 elements
    
    fill_array(numArray[], numElements);
    
    return 0;

}

void fill_array (int NumArray[], int &numElements)
{
    cout << "Enter a list up to 20 integers or -1 to end the list" << endl;
    for (numElements=0; numElements<CAPACITY;numElements++)
    {
        cin >> NumArray[numElements];
    }
    
}

void displayArray(int array[], int numElements)
{
    for (int i = 0; i < numElements; i++)
        cout << array[i] << " ";
        cout << endl;
}


What I have tried:

Example Output:
Enter a list of up to 20 integers or -1 to end the list
3 2 4 8 12 5 6 9 0 11 10 13 -1
3 2 4 8 12 5 6 9 0 11 10 13
Enter a value and a position to insert: 18 5
3 2 4 8 12 18 5 6 9 0 11 10 13
Enter a value to delete from the array: 18
3 2 4 8 12 5 6 9 0 11 10 13
Enter a value to append: 15
3 2 4 8 12 5 6 9 0 11 10 13 15


I have to add all this part in the code:
Start with the simiplest function, e.g., the one to search the content of an array.
The suggested order to write the function is:
Function to search for a value in the array
Function to fill an array with positive integers
Function to delete an element from the array: after deletion, the array should have no holes.
Function to insert an element into the array
Write one function, test it to make sure it really works, and then move on to next function.
Write comments to 1) document your algorithms and design, 2) make your code readable, 3) debug code.
Posted
Updated 24-Sep-18 0:37am
v2

Quote:
Why doesn't this code work? I have to fill the array.

What makes you think that this code don't work ?

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
codingBlonde 23-Sep-18 13:08pm    
Hey, thank you. I just don't know how to do this problem. I having a problem with part 1. I don't how I'm supposed to take 20 integers and then when someone enters -1, it should stop? I'm new to c++.
Nelek 23-Sep-18 14:28pm    
What about checking if the input is -1 and then using break?
Try:
C++
#include <iostream>
using namespace std;

const int CAPACITY=20;

void displayArray(int array[], int numElements);

void fill_array (int array[], int &numElements);

int main ()
{
    int NumArray[CAPACITY]; // an int array with a given CAPACITY
    int numElements=0;  // the array is initially empty, i.e., contains 0 elements

    fill_array(NumArray, numElements);
    displayArray(NumArray, numElements);

    return 0;

}

void fill_array (int NumArray[], int &numElements)
{
    cout << "Enter a list up to 20 integers or -1 to end the list" << endl;
    for (numElements=0; numElements<CAPACITY;numElements++)
    {
        cin >> NumArray[numElements];
        if ( NumArray[numElements] == -1 ) break;
    }

}

void displayArray(int array[], int numElements)
{
    for (int i = 0; i < numElements; i++)
        cout << array[i] << " ";
        cout << endl;
}


Please note:
  • Your naming scheme is inconsistent.
  • You are using a out-parameter without reason (you may use function return value)
  • Using globals is not good
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900