#c++ BASIC

1 messages · Page 1 of 1 (latest)

runic wind
#

can we do this ??

1.) int arr[20] = {};
i mean the array i said is of 20 int, but i made it empty

2.) int arr1[20];
int arr2[5] = {1,2,3,4,5}

arr1 = arr5;
will the arr1 get equal to 5?

3.) if i make a map and mapped the frequency of every integer

is there any command in c++ to know which has the highest frequency or i will have to loop over and do

int highest = INT_MIN;
while (it != map.end()) { 
if (it->second > highest) highest = it->second;
}

and now i will get the highest frequency integer stored in variable highest

woven mirageBOT
#

<@&987246683568103514> please have a look, thanks.

woven mirageBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

past oxide
#

A correct way would be a for-loop for example. Something like:

int Ar1[5]={1,2,3,4,5};
int Ar2[20];
for(int i=0;i<4;i++){
Ar2[i]=Ar1[i];
}
#

so you gotta do it for each Array Index, one by one

#

there are some other ways tho.

runic wind
formal cliff
#

1.) the syntax = {}; means "initialize to default", so you will get an array containing 20 integers equal to zero
this works for other types too, e.g. int x = {}; is the same as int x = 0;, and if you have a class with a default constructor then Class x = {}; is the same as Class x = Class(); calling the default constructor

2.) you cannot do arr1 = arr2;, because arrays are not assignable. this gives a compile error "Array type int[5] is not assignable"

3.) in <algorithm> there is std::max_element:

int highest = std::max_element(
  map.begin(), map.end(), [] (auto p1, auto p2) {
    return p1.second < p2.second;
  })->second;
woven mirageBOT
runic wind
formal cliff
#

you cannot assign arrays

#

initialization with an equals is fine

#

but once it has been declared you cannot assign an entire new array value

runic wind
woven mirageBOT
formal cliff
#

no it is not valid

#

you cannot assign arrays

#

arr1 = arr2; is never valid if arr1 is an array

runic wind
formal cliff
#

yes, if they are of equal size you still cannot do it