home Sandy, UT
Tuesday, 2017.11.21
10:26 MDT [GMT-7]
Brionews - Company Logo Condition: Partly Cloudy
Temperature: 8.8°C (47.8°F)
Barometer: 1028 mb and rising
IT
MIX
News
Low Cost Affordable C C++ PHP mySQL Perl Programs Development
Assignment for C++ class.
This one was pretty stupid. It was about dynamic allocation of arrays of integers and chars, the proper use of new and delete keywords, and related with the use of the I/O streams.
Download the executable: dyma.exe
July 12, 2004

Dynamic Allocation of Array of Integers and Chars

/**
  *  @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";
}

2004.07.12

Vincenzo Maggio