🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: alt.comp.lang.learn.c-c++
10 messages
10 total messages Started by richardcrane Wed, 14 Feb 2001 15:22
Arrays in C++
#664
Author: richardcrane
Date: Wed, 14 Feb 2001 15:22
6 lines
1482 bytes
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.  what method is the
best to start with as i dont have a clue.

cheers
Rick
Re: Arrays in C++
#665
Author: Ron Natalie
Date: Wed, 14 Feb 2001 15:27
10 lines
1506 bytes

richardcrane 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.  what method is the
> best to start with as i dont have a clue.
> 
As a beginner, you should be using std::vector<> not arrays, especially if
you need to track a variable size.
Re: Arrays in C++
#670
Author: Alan Donovan
Date: Wed, 14 Feb 2001 15:30
33 lines
1922 bytes
richardcrane 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.  what method is the
> best to start with as i dont have a clue.


std::vector is a resizable array class.

e.g.

typedef std::vector<std::string> strings;

strings tokenize(const char *line)
{
    strings res;
    ...
    res.push_back(str);
    ...
    return res;
}

strings r = tokenise("echo \"foo bar\" wiz");
assert(r.size() == 3);
assert(r[0] == "echo");
assert(r[1] == "foo bar");
assert(r[2] == "wiz");

HTH
alan

-- 
Alan Donovan, ARM Research, ARM Ltd.        alan.donovan@arm.com
Re: Arrays in C++
#667
Author: Mohammed Ansari
Date: Wed, 14 Feb 2001 15:42
17 lines
1432 bytes
just pass pointer to the first element of the array. for an array of char

f(char *array1ptr);

:)
richardcrane <richardcrane@ntlworld.com> wrote in article
<Ahxi6.6779$mO6.136359@news6-win.server.ntlworld.com>...
> 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.  what method is the
> best to start with as i dont have a clue.
> 
> cheers
> Rick
> 
> 
> 
Re: Arrays in C++
#666
Author: Karl Heinz Buchegger
Date: Wed, 14 Feb 2001 15:44
74 lines
2262 bytes

richardcrane 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.  what method is the
> best to start with as i dont have a clue.
> 

The C way:
**********

#include <stdio.h>

void Foo1( int* Array, int ArraySize )
{
  for( int i = 0; i < ArraySize; ++i )
    printf( "%d\n", Array[i] );
}

void Foo( int* Array, int ArraySize )
{
  Foo1( Array, ArraySize );
}

int main()
{
  const int Size = 10;
  int Test[Size];

  for( int i = 0; i < Size; ++i )
    Test[i] = 100 + i;

  Foo( Test, Size );

  return 0;
}

the C++ way:
************

#include <iostream>
#include <vector>

using namespace std;

typedef vector< int > IntArray;

void Foo1( IntArray& Array )
{
  for( int i = 0; i < Array.size(); ++i )
    cout << Array[i] << '\n';
}

void Foo( IntArray& Array )
{
  Foo1( Array );
}

int main()
{
  IntArray Test( 10 );

  for( int i = 0; i < Test.size(); ++i )
    Test[i] = 100 + i;

  Foo( Test );

  return 0;
}

-- 
Karl Heinz Buchegger
kbuchegg@gascad.at
Re: Arrays in C++
#669
Author: 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
Re: Arrays in C++
#668
Author: Ron Natalie
Date: Wed, 14 Feb 2001 15:53
24 lines
1640 bytes

Karl Heinz Buchegger wrote:
>
> 
> the C++ way:
> ************
> 
> #include <iostream>
> #include <vector>
> 
> using namespace std;
> 
> typedef vector< int > IntArray;
> 
> void Foo1( IntArray& Array )
> {
>   for( int i = 0; i < Array.size(); ++i )
>     cout << Array[i] << '\n';
> }

or
   for(vector<int>::iterator it = Array.begin(); it != Array.end(); ++it)
	cout << *it << '\n';
Re: Arrays in C++
#671
Author: Siper
Date: Wed, 14 Feb 2001 18:16
20 lines
1802 bytes
Arrays are always passed by reference not by value.  Even when you don't use
an ampersand it is still passed by reference.  I don't even think you can
add an ampersand.

--

Micah

{Eternity is not everlasting...}
"richardcrane" <richardcrane@ntlworld.com> wrote in message
news:Ahxi6.6779$mO6.136359@news6-win.server.ntlworld.com...
> 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.  what method is the
> best to start with as i dont have a clue.
>
> cheers
> Rick
>
>
Re: Arrays in C++
#672
Author: Bret Cline
Date: Wed, 14 Feb 2001 20:01
36 lines
2128 bytes
"Siper" <nospam@yahoo.com> wrote in message
news:sYzi6.95441$R5.4103496@news1.frmt1.sfba.home.com...
> Arrays are always passed by reference not by value.  Even when you don't
use
> an ampersand it is still passed by reference.  I don't even think you can
> add an ampersand.

Micah,

You can use the ampersand on an array (dereference it), but the method you
pass it to will probably complain.  It will probably say something about
passing an ArrayType** to a function that take an ArrayType*.

Bret

>
> --
>
> Micah
>
> {Eternity is not everlasting...}
> "richardcrane" <richardcrane@ntlworld.com> wrote in message
> news:Ahxi6.6779$mO6.136359@news6-win.server.ntlworld.com...
> > 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.  what method is
the
> > best to start with as i dont have a clue.
> >
> > cheers
> > Rick
> >
> >
>
>
Re: Arrays in C++
#673
Author: Phil Jackson
Date: Mon, 19 Feb 2001 16:16
3 lines
1378 bytes
He said hes a begginer, pointers are not for the newbie!

Phil Jackson
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads