Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Last revision Both sides next revision
cs-142:array-fun [2014/10/14 09:10]
kseppi created
cs-142:array-fun [2014/10/16 09:21]
kseppi
Line 2: Line 2:
 #include <​string>​ #include <​string>​
 #include <​iostream>​ #include <​iostream>​
- +
 using namespace std; using namespace std;
-  + 
-int main ()+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()
 { {
- const int CAPACITY = 30; + int values[CAPACITY] = { 10, 20, 30, 40, 50, 60 }; // initial stuff in the list 
- int values[CAPACITY] = {10,​20,​30,​40,​50,​60};​ // initial stuff in the list + int current_length ​= 6;
- int currentsize ​= 6;+
  int input;  int input;
-  + 
-  + 
-  + 
-/* silly examples:+ /* silly examples:
  values[3] = 5;  values[3] = 5;
  cout << values[3] << endl;  cout << values[3] << endl;
Line 22: Line 52:
  cout << name;  cout << name;
  */  */
- +
  // Add more to the list  // Add more to the list
  cout << "input please:";​  cout << "input please:";​
  while (cin >> input)  while (cin >> input)
  {  {
- if (currentsize ​< CAPACITY)+ if (current_length ​< CAPACITY)
  {  {
- values[currentsize] = input; + values[current_length] = input; 
- currentsize++;+ current_length++;
  }  }
  }  }
- +
  // Print the current contents  // Print the current contents
  cout << "​\nHere are the current contents\n";​  cout << "​\nHere are the current contents\n";​
- for (int i = 0; i < currentsize; i++)+ for (int i = 0; i < current_length; i++)
  {  {
- cout << "at index " << i << " the value " << values[i] << " is stored"​ <<​endl;​+ cout << "at index " << i << " the value " << values[i] << " is stored"​ << endl;
  }  }
- +
  cout << "​\nRemove an item at location 2\n";  cout << "​\nRemove an item at location 2\n";
  int remove = 2;  int remove = 2;
- if (remove < currentsize)+ if (remove < current_length)
  {  {
- currentsize--; + current_length--; 
- for (int i = remove; i < currentsize; i++)+ for (int i = remove; i < current_length; i++)
  {  {
- values[i] = values[i+1];​+ values[i] = values[i + 1];
  }  }
  }  }
Line 55: Line 85:
  cout << "​bitter complaint: you may not remove items off the end!" << endl;  cout << "​bitter complaint: you may not remove items off the end!" << endl;
  }  }
- +
  cout << "​\nRemove the last item\n";​  cout << "​\nRemove the last item\n";​
- currentsize--; + current_length--; 
- +
  cout << "\nAdd an 8 at the end\n";​  cout << "\nAdd an 8 at the end\n";​
- values[currentsize] = 8; + values[current_length] = 8; 
- currentsize++; + current_length++
-  + print(values,​ current_length)
- cout << "\nPrint ​the current contents:​\n";​ + 
- for (int i = 0; currentsizei+++ cout << "\nTry to add one at 3:​\n";​ 
-+ if (insert(values,​ current_length,​ 3, 99)) { 
- cout << "​at ​index " << ​<< " the value " << values[i] << " ​is stored" <<endl;+ 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"​);​  system("​pause"​);​
  return(0);  return(0);
 } }
 </​code>​ </​code>​
cs-142/array-fun.txt · Last modified: 2015/10/14 11: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