# Array question..

• 11-20-2004
snapshooter
Array question..
:eek: I have wrote this program to calculate the 5 elements of an array..
Code:

```#include <iostream.h> #include <stdlib.h> #include <iomanip.h> using namespace std; int main(int argc, char *argv[]) { int n[5],i; for(i=0;i<5;i++) {     cout<<"Value for n["<<i<<"]:";     cin>>n[i]; }    for(i=0;i<5;i++) { cout<<i<<":"<<n[i]<<endl; } int sum=n[0]+n[1]+n[2]+n[3]+n[4]; cout<<"The sum is:"<<sum<<endl;   system("PAUSE");          return 0; }```
could someone tell me (if it is) a way to calculate mora than 4 elements,,lets say the array have 100 elements..
• 11-20-2004
Prelude
>could someone tell me (if it is) a way to calculate mora than 4 elements
Yes, but it's error prone and not very scalable. Have you considered using a loop instead?
Code:

```int sum = 0; for ( int i = 0; i < 5; i++ )   sum += n[i];```
• 11-20-2004
The Brain
Got a few warnings when I compiled this:

C:\MySource>bcc32 argc.cpp
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
argc.cpp:
Warning W8057 argc.cpp 27: Parameter 'argc' is never used in f
Warning W8057 argc.cpp 27: Parameter 'argv' is never used in f
• 11-20-2004
Siggy
You could do this.

Code:

```int n[100], i, sum; sum = 0; for(i = 0; i < 100; i++) {     cout << "Value for n["<<i<<"]:";     cin >> n[i];     sum +=  n[i]; }    for(i=0; i < 100; i++) {     cout << i << ":" << n[i] << endl; }```
Im pretty sure thatll work...Didnt test it but wrote it up in word, hope it helps
• 11-21-2004
snapshooter
Thanks for the help guys..my program works fine!
:D
• 11-23-2004
The Brain
I was just thinking about arrays.. and then i started thinking aboot vectors.. and then I was like, "hmm.. is there a way in c++ to make multi-dimensional vectors..?" I've never seen code for this type of implementation but i think it would be a cool data structure.
• 11-23-2004
Thantos
Quote:

Originally Posted by The Brain
I was just thinking about arrays.. and then i started thinking aboot vectors.. and then I was like, "hmm.. is there a way in c++ to make multi-dimensional vectors..?" I've never seen code for this type of implementation but i think it would be a cool data structure.

std::vector< std::vector<T> >
• 11-23-2004
The Brain
ahh.. a vector of vectors.. :)
• 11-23-2004
Thantos
Wanna guess what a 12th dimension vector would look like? :)
• 11-23-2004
hk_mp5kpdw
Quote:

Originally Posted by snapshooter
could someone tell me (if it is) a way to calculate mora than 4 elements,,lets say the array have 100 elements..

A possible way to do that:

Code:

```#include <numeric> ... int n[100]; ... // Stuff 100 values into array ... // Calculate sum of all 100 elements int sum = std::accumulate(n,n+100,0);```
• 11-23-2004
The Brain
Quote:

Wanna guess what a 12th dimension vector would look like?

Just out of curiosity.. how would you pushback( ) to let's say.. the 5th dimension of a 12 dimension vector...? :confused:
• 11-23-2004
Thantos
Very very very carefully. Basically you'll have to use an iterators to get to the 5th dimension and then use push_back() on that.

edit:
Or:

Code:

```vector < (insert other vectors) > > > > > > > > > > > > vec; vector < (insert 4 other vectors) bleh; vec[0][0][0][0][0].push_back(bleh);```
• 11-23-2004
Zach L.
Quote:

Originally Posted by Thantos
Wanna guess what a 12th dimension vector would look like? :)

• 11-24-2004
CornedBee
Code:

```template <unsigned int depth, typename type> struct vec_gen {   typedef std::vector < typename vec_gen<depth - 1, type>::result > result;   typedef char[depth - 1] null_protection; }; template <typename type> struct vec_gen<1> {   typedef std::vector <type> result; };```
Quote:

Wanna guess what a 12th dimension vector would look like?
Like this: :)
Code:

`typedef typename vec_gen<12, int>::result ivec12;`
• 11-24-2004
Thantos
Cheater ;)