Thread: Array question..

  1. #1
    Registered User snapshooter's Avatar
    Join Date
    Sep 2004
    Posts
    37

    Array question..

    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..
    Thanks in advance!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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];
    My best code is written with the delete key.

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    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
    Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    52
    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
    Last edited by Siggy; 11-20-2004 at 10:36 AM.

  5. #5
    Registered User snapshooter's Avatar
    Join Date
    Sep 2004
    Posts
    37
    Thanks for the help guys..my program works fine!

  6. #6
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    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.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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> >

  8. #8
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    ahh.. a vector of vectors..
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Wanna guess what a 12th dimension vector would look like?

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  11. #11
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    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...?
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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);
    Last edited by Thantos; 11-23-2004 at 09:52 PM.

  13. #13
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Quote Originally Posted by Thantos
    Wanna guess what a 12th dimension vector would look like?
    Ack! That's hideous.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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;
    };
    Wanna guess what a 12th dimension vector would look like?
    Like this:
    Code:
    typedef typename vec_gen<12, int>::result ivec12;
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Cheater

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM