Assigning the same value to every element of an array.

This is a discussion on Assigning the same value to every element of an array. within the C++ Programming forums, part of the General Programming Boards category; Beginner question here, I need to assign the same value to every element of an array, and have the computer ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Location
    Los Gatos, California, United States
    Posts
    19

    Assigning the same value to every element of an array.

    Beginner question here, I need to assign the same value to every element of an array, and have the computer output that value to the console. After I accomplish this, I will be adding another function that will randomly assign a different value to each element of the array, but I haven't got there yet. This program outputs the location of the element, instead of its value.
    Code:
    #include <iostream>
    using namespace std;
    class List{ 
    private:
        int myarray [99];
    public: List();
            void print();
    };
    int main(){
        List storesamevalue;
        storesamevalue.print();
        return 0;
    }
    List::List(){
        int myarray [99]=99;
    }
    
    
    void List::print(){
        cout<<myarray[1];
    }
    How can I change the assignment statement in the Constructor's definition, or change the output statement in the print function to output the value of the element.

    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    The way that I would do that is to #include <algorithm> then write:
    Code:
    std::fill_n(myarray, 99, 99);
    However, the way probably expected of you is to use a loop. Note that you appear to be declaring a local variable named myarray in the constructor; you should likely be using the member variable instead.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2011
    Location
    Los Gatos, California, United States
    Posts
    19
    Thanks for your response. I am indeed supposed to use a "for" loop, but am having difficulty understanding what difference it makes. I updated the constructor to
    Code:
    List::List(){
    	for (int a=0;a<=99;a++)
    		myarray[a];
    }
    This still returns a string of random numbers which I understand to be the memory location of the array. Sorry for the beginner question here, but how do I modify that 'for' loop to assign a constant value of 99 to each individual element of the array?

    Thanks again.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    Ah, but now you forgot to actually assign to the elements of the array. Also, if your array has 99 elements, then myarray[99] does not exist, so your loop condition should be a < 99, not a <= 99.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2011
    Location
    Los Gatos, California, United States
    Posts
    19
    I can assign a value to a single element of the array, for example, this will return "99" to the console, but if I change the final cout statement to a different element, it will not:
    Code:
    List::List(){
        for (int a=0;a<99;a++)
            myarray[a];
        myarray[10]=99;
    }
    void List::print(){
            cout<<myarray[10];
    }
    I need to apply the value 99 to every element in the array, and am not particularly interested in listing each element and setting it equal to 99, I can't seem to find a command or create a function or loop that will assign the same value to every element without having to list them all.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    Quote Originally Posted by RRTT
    I can assign a value to a single element of the array,
    Yes, so what I am saying is to do that for each element of the array, like this:
    Code:
    List::List() {
        for (int a = 0; a < 99; ++a) {
            myarray[a] = 99;
        }
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size of an array poited by array element
    By mitofik in forum C Programming
    Replies: 7
    Last Post: 12-24-2010, 12:09 AM
  2. 2D Array's, assigning chars.
    By gman89 in forum C Programming
    Replies: 9
    Last Post: 04-27-2008, 12:03 AM
  3. assigning a color to the element of an array ...
    By twomers in forum C++ Programming
    Replies: 0
    Last Post: 02-06-2006, 02:40 AM
  4. Assigning the value of a const array to a normal array
    By Accident Prone in forum C++ Programming
    Replies: 6
    Last Post: 08-11-2003, 11:40 PM
  5. assigning a rand number to each element in an array
    By agerealm in forum C++ Programming
    Replies: 3
    Last Post: 12-15-2002, 01:12 PM

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