We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,514 Members — Technology Publication meets Social Media

# Array simple question

hey guys reading deitel book and have a little problem with the code that I cant understand

``````#include <iostream>

using std::cout;
using std::endl;

#include <iomanip>

using std::setw;

int main()
{
// define array sizes
const int responseSize = 40;   // size of array responses
const int frequencySize = 11;  // size of array frequency

// place survey responses in array responses
int responses[ responseSize ] = { 1, 2, 6, 4, 8, 5, 9, 7, 8,
10, 1, 6, 3, 8, 6, 10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7,
5, 6, 6, 5, 6, 7, 5, 6, 4, 8, 6, 8, 10 };

// initialize frequency counters to 0
int frequency[ frequencySize ] = { };

// for each answer, select value of an element of array
// responses and use that value as subscript in array
// frequency to determine element to increment

// display results
cout << "Rating" << setw( 17 ) << "Frequency" << endl;

// output frequencies in tabular format
for ( int rating = 1; rating < frequencySize; rating++ )
cout << setw( 6 ) << rating
<< setw( 17 ) << frequency[ rating ] << endl;

return 0;  // indicates successful termination

} // end main
``````

OK , please tell me about these things , what happents when we say int frequenct[frequencysize] = {} . does it do a thing or if it doesent why should the writer put that bracket open and bracket close , he could have not writen it , and the second part is ++frequent [ responses[answer]] two things about it first of all what will be ++ and a little talking about it , like which element in array will change ? thats all tnx

3
Contributors
4
Replies
3 Hours
Discussion Span
7 Months Ago
Last Updated
5
Views
Question
kshahnazari
Light Poster
45 posts since Jul 2011
Reputation Points: 10
Skill Endorsements: 0

what happents when we say int frequenct[frequencysize] = {} . does it do a thing or if it doesent why should the writer put that bracket open and bracket close

Read the comment for that line. It says "initialize frequency counters to 0". That should give you a hint of what the empty braces do. To be more specific, if you have an initializer where the number of items in the initializer is less than the number of items in the array, the remaining items in the array will be default initialized. So this:

``````int a[10] = {22, 33, 44};
``````

Will initialize `a[0]` to 22, `a[1]` to 33, `a[2]` to 44, and all other elements to 0 since 0 is the default value for int.

and the second part is ++frequent [ responses[answer]] two things about it first of all what will be ++ and a little talking about it , like which element in array will change ?

Let's simplify first, because using an array item to index another array can be visually overwhelming if you're not used to it (and sometimes even if you are):

``````{

++frequent[index];
}
``````

Does that help make what's happening more clear?

deceptikon
Challenge Accepted
3,425 posts since Jan 2012
Reputation Points: 822
Skill Endorsements: 56

line 22 tels the compiler to initialize an empty array. Line 28 does not make any sense to me. If you want to increase the element that you find you would use the post increment operator not the pre increment operator.

NathanOliver
Posting Virtuoso
1,513 posts since Apr 2009
Reputation Points: 269
Skill Endorsements: 3

If you want to increase the element that you find you would use the post increment operator not the pre increment operator.

The prefix operator is a better practice when the result of the expression doesn't matter. The reason for this is because conceptually prefix increment and decrement are faster due to no need for a temporary. Prefix increment works conceptually like this (not valid C++):

``````int& operator++()
{
value = value + 1;
return value;
}
``````

While postfix increment works conceptually like this (still not valid C++):

``````int operator++(int)
{
int temp = value;
value = value + 1;
return temp;
}
``````

If you need the result then the difference matters because it's the difference between evaluating to the unincremented value versus the incremented value. But for statements like `++value;` versus `value++;`, the result is ignored. Therefore it's best to use the version that has potential to be faster: `++value;`.

For built in types it's largely a moot point because even the first C compiler optimized away the conceptual temporary. I think even Miracle C (the canonical "bad" compiler) performs this optimization. However, C++ introduces user-defined types, where the temporary may not be optimized away. You'll notice that people will recommend prefix increment or decrement for iterators rather than postfix, and this is the reason why.

deceptikon
Challenge Accepted
3,425 posts since Jan 2012
Reputation Points: 822
Skill Endorsements: 56
Question Answered as of 7 Months Ago by deceptikon and NathanOliver

@ deceptikon. Thanks for that. I had a total brain fart this mornging. Glad you were around to set me straight.

NathanOliver
Posting Virtuoso
1,513 posts since Apr 2009
Reputation Points: 269