![]() |
Salt Lake City Monday, 2008.09.08 08:50 MST [GMT-7] |
| Home - IT: IT Security - Programming - OS - HW - SW - Internet - IT News - Technology - Science - Communication - News: World - USA - USA States - Alternative - Business - Investment - more coming soon... | |
/**
* @title: dyma.cpp
* @date: 2004.07.12 - 9:55 pm
* @author: vinnie
* @ a Dynamic Memory Allocation
*/
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
void main( )
{ int choice = 0;
INIT:
cout << "\n Which program do you want to execute:\n\n\
1) Dynamic Allocation for an Array of integers\n 2) Dynamic\
Allocation for an Array of pointers to Arrrays of chars\n\
3) This program is disgusting, please let me get out or\n\
I'll sue you and your instructor\n\nPlease enter either 1 or 2 or 3: ";
cin >> choice;
switch (choice)
{ case 1: goto INTARR;
case 2: goto CHARARR;
case 3: goto EXIT;
default: cout << "\nHey clever boy, I said 1 or 2 or 3\n";
goto INIT;
}
// array of integers
INTARR:
{ int userIterations = 0;
cout << "\n How many numbers do you want to enter: ";
cin >> userIterations;
int *pArr = new int[userIterations];
long total = 0L;
for ( int i = 0; i < userIterations; i++)
{ cout << " Enter the " << (i+1) << "th number: ";
cin >> pArr[i];
total += pArr[i];
}
delete []pArr;
cout << "\n You introduced " << userIterations << " numbers whose Sum = " \
<< total << "\n with an average of " << \
((double)total)/((double)userIterations) << " each.\n";
}
goto INIT;
// array of pointers to arrays of chars
CHARARR:
{ int userIterations = 0;
cout << "\n How many names do you want to enter: ";
cin >> userIterations;
string *pStrArr = new string[userIterations];
for ( int i = 0; i < userIterations; i++)
{ char *pStr;
cout << " Enter the " << (i+1) << "th name: ";
cin >> pStr;
pStrArr[i] = pStr;
}
cout << "\n You introduced:\n";
for ( int i = 0; i < userIterations; i++)
cout << (i+1) << ") " << pStrArr[i] << "\n";
delete []pStrArr;
}
goto INIT;
EXIT:
cout << "done! :-)\n";
}
Vincenzo Maggio