🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.comp.lang.learn.c-c++
Article #669

Re: Arrays in C++

#669
From: Jon Bell
Date: Wed, 14 Feb 2001 15:51
83 lines
3165 bytes
In article <Ahxi6.6779$mO6.136359@news6-win.server.ntlworld.com>,
richardcrane <richardcrane@ntlworld.com> wrote:
>can somebody help me iam a beginner in C++.  I want to pass an array between
>3 different functions that are a user definable size.

I take it you mean the array size is defined by the user when the program 
runs?  Maybe this will give you the idea:

  #include <iostream>

  using namespace std; // quick and dirty namespace hack

  void ReadData (double d[], int size)
  {
      for (int k = 0; k < size; ++k)
      {
          cout << "Enter d[" << k << "]: ";
          cin >> d[k];
      }
  }

  void DisplayData (double d[], int size)
  {
      for (int k = 0; k < size; ++k)
      {
          cout << "d[" << k << "] = " << d[k] << endl;
      }
  }

  int main ()
  {
    cout << "How many data items? ";
    int howmany;
    cin >> howmany;
    double *data = new double[howmany]; // allocate the array dynamically
    ReadData (data, howmany);  // must pass the size, too
    DisplayData (data, howmany);
    delete [] data;  // clean up after yourself when done
    return 0;
  }


This is easier using vectors instead of arrays:


  #include <iostream>
  #include <vector>

  using namespace std; // quick and dirty namespace hack

  void ReadData (vector<double>& d)
  {
      cout << "Enter data items one by one, hit ^D when finished...\n";
      double x;
      while (cin >> x)
      {
          d.push_back(x);  // vector automatically expands as needed
             // and keeps track of how much data it contains
      }
  }

  void DisplayData (vector<double>& d)
  {
      for (int k = 0; k < d.size(); ++k)
      {
          cout << "d[" << k << "] = " << d[k] << endl;
      }
  }

  int main ()
  {
    // no need to ask for how much data
    // declare vector as zero-length initially
    vector<double> data;
    ReadData (data);  // vector keeps track of its own size
    DisplayData (data);
    // vectors clean up after themselves for you
    return 0;
  }

-- 
Jon Bell <jtbell@presby.edu>                        Presbyterian College
Dept. of Physics and Computer Science        Clinton, South Carolina USA
Technical Details
Message-ID: <G8r819.8yK@presby.edu>
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!sn-xit-01!supernews.com!newsfeed.stanford.edu!presby.edu!not-for-mail
References: <Ahxi6.6779$mO6.136359@news6-win.server.ntlworld.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10b7bb,4b7be81e9b0e1dc7
X-Google-Attributes: gid10b7bb,public
X-Google-Thread: 109fba,4b7be81e9b0e1dc7
X-Google-Attributes: gid109fba,public
X-Google-Thread: 114592,4b7be81e9b0e1dc7
X-Google-Attributes: gid114592,public
X-Google-ArrivalTime: 2001-02-14 08:06:04 PST
Newsgroups: alt.comp.lang.learn.c-c++,comp.lang.c++,ctdl.lang.c++
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!sn-xit-01!supernews.com!newsfeed.stanford.edu!presby.edu!not-for-mail
From: jtbell@presby.edu (Jon Bell)
Subject: Re: Arrays in C++
Originator: jtbell@presby.edu (Jon Bell)
Sender: news@presby.edu (Usenet News)
Message-ID: <G8r819.8yK@presby.edu>
Date: Wed, 14 Feb 2001 15:51:09 GMT
X-Nntp-Posting-Host: cs2
References: <Ahxi6.6779$mO6.136359@news6-win.server.ntlworld.com>
Organization: Presbyterian College, Clinton SC, USA
X-Newsreader: trn 4.0-test74 (May 26, 2000)
Lines: 83