/*
* Copyright 2023, Ali Arya
* Anyone Can Code: Algorithmic Thinking
* Example 5.3c
*/

#include <iostream>
#include "stdlib.h"

using namespace std;

//shifting up the members from index1 to index2 (inclusive)
void ShiftUp(int Data[], int index1, int index2)
{
	for (int i = index2; i >= index1; i--)
	{
		Data[i + 1] = Data[i];	//possible error if index2 is the last member of the array
	}
}
//shifting down the members from index1 to index2 (inclusive)
void ShiftDown(int Data[], int index1, int index2)
{
	for (int i = index1; i <= index2; i++)
	{
		Data[i - 1] = Data[i];	//possible error if index1 is the first member of the array
	}
}
void Insert(int Data[], int len, int index, int value)
{
	//move up all members from insertion point
	//this copies the member at index to index+1
	ShiftUp(Data, index, len - 1);
	//now add the new value
	Data[index] = value;
}
void Delete(int Data[], int len, int index)
{
	//move down all members after insertion point
	//this overwrites the member at index by index+1
	ShiftDown(Data, index + 1, len - 1);
}
void Print(int Data[], int len)
{
	for (int i = 0; i < len; i++)
	{
		cout << Data[i];
		cout << " ";
	}
	cout << "\n";
}

int main()
{
	int data[] = { 1, 2, 3, 4, 5, 6, 7 };
	Print(data, 7);
	ShiftUp(data, 2, 4);   //1,2,3,3,4,5,7
	Print(data, 7);
	ShiftDown(data, 3, 5); //1,2,3,4,5,5,7
	Print(data, 7);
	Delete(data, 7, 5);    //1,2,3,4,5,7,7
	Print(data, 7);
	Insert(data, 7, 5, 6); //1,2,3,4,5,6,7
	Print(data, 7);

	//randomize
	for (int i = 0; i < 7; i++)
		data[i] = rand() % 50;
	Print(data, 7);

	return 0;
}
