there's bug when two digits is used

1. Write a program that asks the user to type 10 integers of an array and an integer value V. The program must search if the value V exists in the array and must remove the first occurrence of V, shifting each following element left and adding a zero at the end of the array. The program must then write the final array.

this is what i've done

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<iostream>
using namespace std;
 
int main()
{
    int array[10];
    int input, ctr=0, move;
    for (int x=0; x<10; x++)
    {
        cout<<"Enter a number on index ["<<x<<"]: "; cin>>array[x];
    }
 
    int v;
    cout<<"Enter the value of 'v': "; cin>>v;
 
    for (int y=0; y<10; y++)
    {
        if (array[y]==v)
        {
            if (ctr==0)
            {
                ctr=y;
            }
        }
    }
    if (ctr==0)
    {
        cout<<endl<<"***Cannot find Value of v is not in the array***"<<endl;
    }
    else if (ctr!=0)
    {
    move=array[ctr];
    for (long int z=ctr; z<10; z++)
        {
            array[z]=array[move];
            move++;
        }
    array[9]=0;
    for (int d=0; d<10; d++)
        {
            cout<<array[d]<<", ";
        }
    }
 
    return 0;
}


the last time i check,
theres bug when i use 2 digits

any suggestions how to fix this will be much appreciated :)
Last edited on
What is the bug? Explain the problem more clearly.
What if array[0] == v?

Edit:
1
2
3
4
5
6
    move=array[ctr];                 // if array[ctr] >= 10 then 
    for (long int z=ctr; z<10; z++)
        {
            array[z]=array[move];    //array[move] is out of bounds
            move++;
        }
Last edited on
On line 32, you set move, which is the number of the array you want to remove, to the contents of the array you want to remove.

i.e. Let's say you want to remove the number 17, and you have found it in array[4]. You should set move to 4, but you are setting it to 17.

You actually don't need to use move , and you're making the code more complicated (and thus harder for you to9 fix bugs). Try this instead:

1
2
3
4
5
6
7
8
9
10
11
12
.
.
.
 else if (ctr!=0)
    {
   
    for (long int z=ctr; z<9; z++)
        {
            array[z]=array[z+1];
.
.
.
Last edited on
Topic archived. No new replies allowed.