#include <string>
#include <iostream>
 
using namespace std;
 
const int CAPACITY = 30; // ok?? oh, I guess.
 
void print(int myarray[], int current_length) {
 
	cout << "Current contents:\n";
	for (int i = 0; i < current_length; i++)
	{
		cout << "at index " << i << " the value " << myarray[i] << " is stored" << endl;
	}
}
 
int insert(int myarray[], int &current_length, int location, int value)
{
	int return_code = 0;
 
	if ( (location >= CAPACITY)
		|| (location > current_length)
		|| (CAPACITY > current_length)
		|| (location < 0)) {
			return_code = 1;
	}
	else {
		current_length++;
		for (int i = current_length; i > location; i--) {
			myarray[i] = myarray[i-1];
		}
		myarray[location] = value;
	}
	return return_code;
}
 
int main()
{
	int values[CAPACITY] = { 10, 20, 30, 40, 50, 60 }; // initial stuff in the list
	int current_length = 6;
	int input;
 
 
 
	/*	silly examples:
	values[3] = 5;
	cout << values[3] << endl;
	cout << values[4] << endl;
	values[40] = 7;
	cout << values[40] << endl;
	cout << name;
	*/
 
	// Add more to the list
	cout << "input please:";
	while (cin >> input)
	{
		if (current_length < CAPACITY)
		{
			values[current_length] = input;
			current_length++;
		}
	}
 
	// Print the current contents
	cout << "\nHere are the current contents\n";
	for (int i = 0; i < current_length; i++)
	{
		cout << "at index " << i << " the value " << values[i] << " is stored" << endl;
	}
 
	cout << "\nRemove an item at location 2\n";
	int remove = 2;
	if (remove < current_length)
	{
		current_length--;
		for (int i = remove; i < current_length; i++)
		{
			values[i] = values[i + 1];
		}
	}
	else
	{
		cout << "bitter complaint: you may not remove items off the end!" << endl;
	}
 
	cout << "\nRemove the last item\n";
	current_length--;
 
	cout << "\nAdd an 8 at the end\n";
	values[current_length] = 8;
	current_length++;
	print(values, current_length);
 
	cout << "\nTry to add one at 3:\n";
	if (insert(values, current_length, 3, 99)) {
		cout << "hey the insert failed!!\n";
	}
	else {
		cout << "hey IT WORKED.\n";
	}
	print(values, current_length);
 
	cout << "\nTry to add one at 0:\n";
	if (insert(values, current_length, 0, 98)) {
		cout << "hey the insert failed!!\n";
	}
	else {
		cout << "hey IT WORKED.\n";
	}
	print(values, current_length);
 
	cout << "\nTry to add one at -4:\n";
	if (insert(values, current_length, -4, 97)) {
		cout << "hey the insert failed!!\n";
	}
	else {
		cout << "hey IT WORKED.\n";
	}
	print(values, current_length);
 
	cout << "\nTry to add one at 8:\n";
	if (insert(values, current_length, 8, 96)) {
		cout << "hey the insert failed!!\n";
	}
	else {
		cout << "hey IT WORKED.\n";
	}
	print(values, current_length);
 
	cout << "\nTry to add one at 7:\n";
 
	if (insert(values, current_length, 7, 95)) {
		cout << "hey the insert failed!!\n";
	}
	else {
		cout << "hey IT WORKED.\n";
	}
	print(values, current_length);
 
 
	cout << "\nTry to add one at 30:\n";
 
	if (insert(values, current_length, 30, 94)) {
		cout << "hey the insert failed!!\n";
	}
	else {
		cout << "hey IT WORKED.\n";
	}
	print(values, current_length);
 
	cout << "\nempty it!\n";
	current_length = 0;
	print(values, current_length);
 
	cout << "\nTry to add one at 0:\n";
	if (insert(values, current_length, 0, 93)) {
		cout << "hey the insert failed!!\n";
	}
	else {
		cout << "hey IT WORKED.\n";
	}
	print(values, current_length);
 
	system("pause");
	return(0);
}

Mr. Medulla: Dreadful technique! You've confused rays with beams! D! Minus! I'd give you an F, but that would only mean having to see you in summer school.

cs-142/array-fun.txt · Last modified: 2015/10/14 17:48 by kseppi
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0