1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Beginner C++ Questions About Arrays (2 Questions)

Discussion in 'Software Development' started by rankbeginner, Aug 18, 2004.

Thread Status:
Not open for further replies.
Advertisement
  1. rankbeginner

    rankbeginner Thread Starter

    Joined:
    Jun 24, 2004
    Messages:
    106
    Hello Experts,

    Question 1:

    To initialize the 4 elements of integer array named numbers to the value of 0, is THIS

    int number [4] = { 0 };

    correct, and the exact same thing as saying this:

    int number [4] = { 0, 0, 0, 0 }; // ?

    Question 2:

    Assuming
    double x [ 6 ], y [ 14 ];

    What C++ statement could I write that would

    copy array named x into the first part of array named y. // ?

    Thanks again! ... RB
     
  2. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    Hi rankbeginner,

    I am not an C++ expert but I would do this to answer your questions :

    Question 1;

    for (int i = 0; i < 4; i++) {
    number = 0;
    }

    Question 2

    for (int i = 0; i < 6; i++) {
    y = x;
    }
     
  3. rankbeginner

    rankbeginner Thread Starter

    Joined:
    Jun 24, 2004
    Messages:
    106
    Thank you. I wish I knew if these were correct. Can anyone confirm this?
    --RB
     
  4. coderitr

    coderitr

    Joined:
    Oct 12, 2003
    Messages:
    3,015
    Chicon is correct.
     
  5. rankbeginner

    rankbeginner Thread Starter

    Joined:
    Jun 24, 2004
    Messages:
    106
    Thanks Chicon and thanks coderitr.
    --RB
     
  6. Shadow2531

    Shadow2531

    Joined:
    Apr 30, 2001
    Messages:
    2,636
    #1

    int number[4] = {0}; is O.K. to use.

    If you only initialize the first element of the array, the rest of the elements will be auto-initialized to zero.

    If you don't want it to happen automatically, then use the loop as suggested.
     
  7. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/263496

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice