Can you tell me what this statement is doing?

  • Thread starter Thread starter pairofstrings
  • Start date Start date
AI Thread Summary
The discussion revolves around a C program that reads floating-point values, counts their frequency within defined groups, and prints a frequency table. The key line of code, `++group[(int)(value[i]+0.5)/10]`, increments the count of a specific group based on the input value. The expression `(int)(value[i]+0.5)/10` converts the floating-point number to an integer after rounding, determining which group the value belongs to. The use of `%3d` and `%2d` in the `printf` statements specifies the formatting of the output, ensuring numbers are displayed with a minimum width, aligning the output neatly. The discussion also clarifies that the integer cast is necessary to eliminate any fractional part from the division, allowing for accurate indexing into the `group` array.
pairofstrings
Messages
411
Reaction score
7
#define MAXVAL 50
#define COUNTER 11
main ()
{
float value[MAXVAL];
int i, low, high;
static group[COUNTER] = {0,0,0,0,0,0,0,0,0,0,0}

*/READING AND COUNTING*/

for(i=0; i<MAXVAL; i++)
{
/* READING OF VALUES*/

scanf("%f", &value);

/* COUNTING FREQUENCY OF GROUPS */

++group[ (int) (value+0.5)/10]
}

/* PRINTING OF FREQUENCY TABLE */

printf("\n");
printf(" GROUP RANGE FREQUENCY\N\N");

for(i=0; i< COUNTER; i++)
{
low = i*10;
if (i==10)
high =100;
else
high=low + 9;
printf( " %2d %3dto%3d %d)\n", i+1, low,high,group);

}

}

Can you expalin me what is ++group[ (int) (value+0.5)/10] and also what is %3d and %2d doing in the statements.output:
input data

GROUP RANGE FREQUENCY
 
Technology news on Phys.org
Can you expalin me what is ++group[ (int) (value+0.5)/10]

It is equivalent to
Code:
// calculate the number of the counter for this value.
index = (int) ( value[i]+0.5 ) / 10; 
 // increase the counter by one.
group[index] += 1;
(except that the integer variable index isn't defined in your piece of code, of course).

and also what is %3d and %2d doing in the statements.
That is the format in which the number should be displayed. Google for the printf command to find out more.
 
Can you help me trace the program? I am having little trouble in understanding this piece of code

/* COUNTING FREQUENCY OF GROUPS */

++group[ (int) (value+0.5)/10

// why are we using int in the middle of this statement?

}

/* PRINTING OF FREQUENCY TABLE */

printf("\n");
printf(" GROUP RANGE FREQUENCY\N\N");

for(i=0; i< COUNTER; i++)
{
low = i*10;
if (i==10)
high =100;
else
high=low + 9;
printf( " %2d %3dto%3d %d)\n", i+1, low,high,group);

}

}
 
Last edited:
pairofstrings said:
// why are we using int in the middle of this statement?

The calculation "(value+0.5)/10" is done using floats or doubles, which generally produces a result with a fractional part. The "(int)" casts (converts) the result to int, which discards the fraction.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top