some questions about arrays

This is a discussion on some questions about arrays within the C++ Programming forums, part of the General Programming Boards category; Hi guys, I have got some questions about arrays, eg if I have an array: Code: int tax[10]; int tax[10]={1.0,5.0,7.0}; ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    96

    some questions about arrays

    Hi guys,

    I have got some questions about arrays, eg if I have an array:

    Code:
    int tax[10];
    
    int tax[10]={1.0,5.0,7.0};
    Is this how we initialize element 1,5, and 7 in this array?
    Code:
    int tax[10]={1.0,5.0,7.0} ;
    if we want to initialize the entire array ,so do we write:
    Code:
    int tax[10]={0} ;


    Thanks

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,522
    Standard C++ does not support named parameter initialization for arrays. To initialize the first, fifth and seventh elements, you need to do something like:
    Code:
     int tax[10] = {0, 1, 0, 0, 0, 5, 0, 7 } ;
    If you fail to initialize all the elements of an array, what happens to the rest of the elements depends on the type. Plain old data will be zero initialized (as you can see) and user defined types have their default constructors called.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2,140
    Quote Originally Posted by student111 View Post
    Hi guys,

    I have got some questions about arrays, eg if I have an array:

    Code:
    int tax[10];
    
    int tax[10]={1.0,5.0,7.0};
    Is this how we initialize element 1,5, and 7 in this array?
    no. this would initialize the first three elements to 1, 5, and 7, respectively. if you compiled and ran the code, you would know that already though.

    Quote Originally Posted by student111 View Post
    Code:
    int tax[10]={1.0,5.0,7.0} ;
    if we want to initialize the entire array ,so do we write:
    Code:
    int tax[10]={0} ;
    try it and find out.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,560
    Code:
    int tax[10];
     
    int tax[10]={1.0,5.0,7.0};
    Is this how we initialize element 1,5, and 7 in this array?
    No, first the array is an int array, so you don't use a decimal point to initialize the elements. Second this would only initialize the first three elements. Third you can only use brace ( {} ) initialization when you define the array:
    Code:
    int tax[10]= {1,5,7};
    if we want to initialize the entire array ,so do we write:
    Code:
    int tax[10]={0} ;
    That will initialize the entire array to zero. If you want to initialize the array to some other value you must either initialize the array when you declare the array, or by assigning values to individual elements.

    Jim

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    96

    correction

    Thanks for your reply,

    So if we write:
    Code:
    int tax[10]={1,5,7};
    This will initialize the 1st 3 elements to 1,5 and 7?

    What if we want to just initialize element 1 ,3 and 7 in this array?how do we do it?
    Last edited by student111; 01-31-2012 at 10:39 AM. Reason: correcting

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,522
    I already showed you in my earlier reply. There is no other standard way, like I said. Keep up.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    96
    Ok, got it ,

    Thanks

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    2,140
    the C language allows you to explicitly initialize specific elements, but C++ does not, although some C++ compilers offer it as an extension. the syntax for this is as follows, but YMMV:
    Code:
    int array[10] = { [1] = 2, [5] = 7, [8] = 3 };

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    96
    Hi,

    Thanks for your reply but if we want to initialize the 1st, 5th and the 7th element of an array,so don't we write:


    Code:
    int tax[10]={1,0,0,0,5,0,7,0,0,0}
    Don't we count "0" as 1st element of an array?

    I hope I am clear

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    96
    Hi Elkvis,

    I didn't understand this:

    Code:
    int array[10] = {[1]=2, [5]=7,[8]=3};
    Can we use brackets inside curley braces?

    Is this initializing 1st element to 2 and so on?

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    2,140
    like I said, that's a C language feature, not necessarily supported in every C++ compiler, but offered by some as an extension to the C++ language.

  12. #12
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,522
    Quote Originally Posted by student111
    Don't we count "0" as 1st element of an array?
    I don't know, do we? I count zeroth, first, second... If you count something else, I don't care and I don't make the rules. Just make sure there are no consequences.

  13. #13
    Registered User
    Join Date
    Oct 2006
    Posts
    2,140
    array indices are the distance, in elements, from the beginning. the distance from the beginning to the first element is zero, therefore its index is zero.

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    FYI, you can actually initialise an entire array to zero by doing:
    Code:
    int tax[10] = {};
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  15. #15
    Registered User
    Join Date
    Nov 2011
    Posts
    96
    Hi,

    I have some questions, I just wanted to know that when we say initialize the 1st 3 elements of the array,does it mean to initialize starting from "0"?
    I mean is "0" = the 1st element?

    Thanks

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Questions regarding arrays and classes!!(amateur level)
    By linkstatic in forum C++ Programming
    Replies: 13
    Last Post: 12-01-2011, 11:04 AM
  2. Questions on Character Pointers and Arrays
    By 7heavens in forum C++ Programming
    Replies: 1
    Last Post: 02-07-2010, 03:00 AM
  3. questions about arrays basic collision detection
    By The_Hermit in forum Game Programming
    Replies: 2
    Last Post: 07-23-2008, 11:29 AM
  4. A couple of questions concerning arrays
    By stevedawg85 in forum C++ Programming
    Replies: 2
    Last Post: 03-22-2006, 05:47 PM
  5. questions about arrays
    By laasunde in forum C++ Programming
    Replies: 3
    Last Post: 08-14-2003, 05:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21