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;
}
}
|