Thread: Assigning the same value to every element of an array.

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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;
        }
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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-26-2008, 11:03 PM
  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, 10: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