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

#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()
{
	//create empty array
	int data[] = { 0,0,0,0,0,0,0 };

	//create as many new members as we want (here assume 10)
	for (int j = 0; j < 10; j++)
	{
		//create new random number
		int num = rand() % 50 +1; //1 to 50
		cout << "new number is ";
		cout << num;
		cout << "\n";

		//find the right place assuming the array is descending order
		int index = 0;
		for (int i = 0; i < 7; i++)
			if (num > data[i])
			{
				index = i;
				break;
			}
		//write in the found location
		Insert(data, 7, index, num);

		//see results
		Print(data, 7);
	}
	return 0;
}
