Here is my code, I would need to fill in every element in the 2d array with the maximum size ( INT_MAX), however, my code was unable to work properly?
#include <iostream>
#include <string.h>
#include <limits.h>
using namespace std;
int main(){
int arr[10][10];
memset(arr,INT_MAX, sizeof (arr));
cout << arr[0][3];
//output = -1 instead of 2147483647
}
How do I modify my code such that I will be able to fill in an array with the max values?
C++ Standard Library has a function fill_n to fill arrays with n
values.
#include <algorithm>
...
std::fill_n(&arr[0][0], 100, INT_MAX);
The number of values may be brittle here - if you change the size of the array in future, you have to remember to change the hard-coded 100
to a new correct value.
It's good enough for a quick hack. If you need something with long-term value, use the idea in this answer to a similar question.