# Thread: could someone help me with my code

1. ## could someone help me with my code

Hello everyone, I'm new to this board and fairly new to programming and I'm trying to understand some concepts on arrays so I'm trying to make this little program. I have a future assignment coming up that has some of these concepts so I'm trying to understand them so I can apply it to my assignments.

I'm trying to delete a number from a list of numbers in an array
and the program is also suppose to delete duplicates but i have no idea how to do it in my for loop
the numbers of the list are these

0,1,4,4,5,2,1,7,7

so if the user puts in 0 first

the list becomes 1,4,4,5,2,1,7,7

if they put 4 instead

the list becomes

0,1,5,2,1,7,7

this is my code here
can somebody help me with what is going on i tried 4 but the output is wrong i got
0,1,4,5,5,2,1,7 instead of 0,1,5,2,1,7,7

Code:
```#include <iostream>
#include <string>

using namespace std;

void Get_Number(int &Number_to_Delete){

cout << endl << "Enter number to delete."<<endl;
cin >> Number_to_Delete;

}

void Delete_Number(int Number_to_Delete, int array[],int &count){

cout<< "The number " <<Number_to_Delete<< " is going to be deleted." <<endl;

for(int i=0;i<count;i++){

if(Number_to_Delete==array[i]){

array[i]=array[i+1];

}

}

count--;
}

void Display_Numbers(int array[],int &count){

for(int i=0;i<count;i++){

cout <<array[i];

}

}

int main(){

int list[9]={0,1,4,4,5,2,1,7,7};
int Number_to_Delete;
int count = 9;

Display_Numbers(list,count);

Get_Number(Number_to_Delete);

cout << "You want to delete the number "<< Number_to_Delete << endl;

Delete_Number(Number_to_Delete,list,count);

Display_Numbers(list,count);

system("pause");

}```

My output for this code

014452177
Enter number to delete.
4
You want to delete the number 4
The number 4 is going to be deleted.
01455217
Press any key to continue . . .

2. you're replacing a[2] with a[3], where a[2] = a[3] = 4, so it seems like the 4 is not going anywhere. Then the code replace a[3] with a[4] which replicate the "5" in a[4] and give the "55" in the sequence. The array size is the same, but you decrement count by 1, so the last 7 went awol.

The easiest way is to use two array of the same size. Iterate over the first array, if the deleted value equals an element of the first array, don't copy it into the second array, else push the value into the end of the second array.

Another way is to use a linked list, and delete all the nodes that store the "delete_value"

3. In your function Delete_Number() you iterate through the array with only one variable. You should use two variables; one for the reading position and another one for the writing position. They both would start at 0 and the reading position (i) would be incremented every time, but the writing position (j) would be incremented only when there's something worth to be written:

Code:
```    for (i=j=0; i<count; i++)
if (array[i] != Number_to_Delete)
array[j++] = array[i];```
After this, you should set the size of the array to j.

4. You might consider a vector instead of c style arrays. Also i am not fond of function names with underscores in them, but thats just personal preference

Popular pages Recent additions