Can you tell me what this statement is doing?

  • Thread starter Thread starter pairofstrings
  • Start date Start date
Click For Summary

Discussion Overview

The discussion centers around understanding specific parts of a C program that reads floating-point values, counts their frequency in defined groups, and prints a frequency table. Participants are examining the functionality of certain code segments and formatting in output statements.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Homework-related

Main Points Raised

  • Some participants inquire about the expression "++group[ (int) (value[i]+0.5)/10]", seeking clarification on its purpose and functionality.
  • One participant suggests that this expression can be understood as calculating an index for the counter and then incrementing the corresponding group count.
  • Another participant questions the use of the "(int)" cast in the expression, prompting a discussion about its role in converting a floating-point result to an integer.
  • Participants also discuss the formatting specifiers "%3d" and "%2d" used in the printf statements, indicating that these control how numbers are displayed in the output.

Areas of Agreement / Disagreement

Participants generally agree on the functionality of the code segments being discussed, but there are varying levels of understanding regarding the specific operations and formatting details. No consensus on a single interpretation is reached.

Contextual Notes

Some participants express uncertainty about the implications of casting to int and how it affects the calculation of group indices. There is also a lack of clarity on the overall flow of the program and how input data is processed.

Who May Find This Useful

This discussion may be useful for individuals learning C programming, particularly those interested in data processing and output formatting in coding exercises.

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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
8
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
6
Views
6K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 22 ·
Replies
22
Views
6K