Program Error ... Help Needed

I wrote a program for this problem with pointers and went through it and can't find the problem of why it is shifting certain numbers and then at some point will just replace the rest of the remaining numbers with the same number ... Not sure if that explains the problem but hopefully someone can understand what I am trying to say here ... I have included the question and my code ... If anyone could help it would be greatly appreciated ... Thank you

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*Write tableDelete, a function that deletes all occurrences 
of a particular item from an array of int. It does the delete 
by shifting array elements. First write it using array 
subscripting and then using pointers.*/

#include <stdio.h>
#include <iostream>
#include<time.h>

using namespace std;

int tableDelete (int Array[], int i); //Deletes all integers of one and shifts the array down

int main()

{
	srand (time(NULL));

	int Array[] = {rand()%3, rand()%3, rand()%3, rand()%3, rand()%3, rand()%3, rand()%1};

	for (int i = 0; i < 6; i++)
		{
			printf("Array #%i: %i\n", i + 1, Array[i]);
		}

	printf("\n-----------------------------------------\n\nDeleting all integers of 1 the final array is: \n\n");

	

	int sum = 0;

	for (int i = 5; i >= 0; i--)
		{
			int tableDelete (int Array[], int i);

			sum = sum + tableDelete (Array, i);
		}

	for (int k = 0; k <= 5 - sum; k++)
		{
			printf("New Array #%i: %i\n", k + 1, Array[k]);
		}

	printf("\n");
}


int tableDelete (int Array[], int i)

{
	int *p;

	p = &Array[i];
	
	if (*p == 1)
			{
				for (int j = i; i <= 5; i++)
				{
					*p = *(p + 1);
					
					Array[i] = *p;

				}

				return 1;
			}

		else
		{
			return 0;
		}
}
Last edited on
From what I see

You are using for loop to call tabelDelete and argument you are passing is changes along with the loop.
the pointer you use in tableDelete is pointing address to the array whose index is the argument you send.
Once you get in for loop that pointer is deference and values are assigned to the original array., which is changing as the new arguments are passed.
Trying to compare it to what you are mentioning but I still don't see the issue @sk9
why the useless line 34?
The problem is in lines 59 to 61:
replace
1
2
3
*p = *(p + 1);
					
Array[i] = *p;

with
Array[j] = Array[j+1];

However, to not reinvent the wheel, you can use the standard function std::remove instead of your function tableDelete.
#include <algorithm>
1
2
3
4
5
6
	auto end = std::remove(std::begin(Array), std::end(Array), 1);
	int new_size = end - std::begin(Array);
	for (int k = 0; k < new_size; k++)
	{
		printf("New Array #%i: %i\n", k + 1, Array[k]);
	}

@fcantoro the question I was given says I need to create that function as while using pointers ... so using the remove function is not allowed to create this program ... That is why I can not use the replacement with an array which I have already done on the first part of the question and as a separate code which I have not included since I got the program to work
Last edited on
Topic archived. No new replies allowed.