0

I'm trying to make an ASCII art using C++, and having some problems in arrays.

Is there any way to set multiple array variables at the same time?

Let me be more specific.

When you initialize an array, you can do this way.

int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

By the way shown above, you can set 10 array variables at the same time.

However, I want to (re) set some of the array variables like this?

a[1] = 3;
a[4] = 2;
a[5] = 2;
a[7] = 2;

Since there is NO rule in the variables, I can't do

for(int i=0; i<10; i++) a[i] = i+1;
fill(n);

I can't use an for statement or the fill, fill_n function, since there is no regularity.

To sum up, Is there any way to set more than 1 array variables at the same time? (Like the second code snipplet above?

9

4 Answers 4

1

Given a index-value mapping list, and assign it one by one.

template<typename T, size_t N>
void Update(T(&arr)[N], const std::vector<std::pair<size_t, T>>& mappings)
{
    for (const auto& mapping : mappings) 
        if(mapping.first < N)
            arr[mapping.first] = arr[mapping.second];
}

int main()
{
    int arr[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    Update(arr, { {1, 3}, {4, 2}, {5, 2}, {7, 2} });
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

0

As far as I'm aware without a pattern a control structure is kind of redundant, you might be better served reading from a file.

// for user input
    int arr[10] = { 0,1,2,3,4,5,6,7,8,9 };
    for (int i = 0; i < 10; i++) {
        cout << "Please input your value for array index " << i << endl;
        cin >> arr[i];
    }

    // for manual input in initalization
    int arr[10] = { 0, 3, 2, 2, 2, 5, 6, 7, 8, 9 };

However a better approach might be to read it from a file, http://www.cplusplus.com/forum/general/58945/ Read "TheMassiveChipmunk"'s post there for exactly how to do it.

Comments

0

Assuming you know which indices you will be changing upfront you can use a separate index array:

int ind[4]= {1,4,5,7};

..and an accompanying array with values

int new_val[4] = {3,2,2,2};

The you can use the following for loop to assign the values:

for (int i=0; i<4; i++)
    arr[ind[i]] = new_val[i];

You should also use some variable signifying the number of indices to be changed like int val_num = 4 instead of plain number 4.

Comments

0

Changes that are defined in runtime to an array can be easily implemented by using a list to save tuples that represent the changes you want to make. As an example, we can write:

 #include <tuple>                                                                
 #include <list>                                                                 
 #include <iostream>                                                             

 using namespace std;                                                            
 typedef tuple <int, int> Change;                                                

 int main() {                                                                    
     int a[5] = {1,2,3,4,5};                                                     
     list<Change> changes;                                                       

     //represents changing the 2-th entry to 8.                                  
     Change change(2,8);                                                         
     changes.push_back(change);

     for(auto current_change: changes)                                           
         a[get<0>(current_change)] = get<1>(current_change);                                     
     cout << a[2] << '\n';                                                       
 }  

Prints 8.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.