Can you tell me what this statement is doing?

  • Thread starter pairofstrings
  • Start date
In summary: This means that the result will be the integer part of the original calculation, rounded towards zero. This is used to determine which group the current value belongs to, since each group represents a range of 10 numbers.
  • #1
pairofstrings
411
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
  • #2
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.
 
  • #3
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:
  • #4
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.
 
  • #5


1 0to9 5
2 10to19 7
3 20to29 12
4 30to39 8
5 40to49 6
6 50to59 3
7 60to69 1
8 70to79 2
9 80to89 4
10 90to99 2
11 100to109 0

The first line of code, "for(i=0; i<MAXVAL; i++)" is a for loop that will iterate through the array "value" for a total of MAXVAL times. This means that the loop will run for as many times as the value of MAXVAL, which is 50 in this case.

The next line, "scanf("%f", &value);" is reading in a float value from the user and storing it in the array "value" at the index i. This is done for each iteration of the for loop, so a total of MAXVAL values will be read in.

The line "++group[(int)(value+0.5)/10]" is incrementing the value stored in the array "group" at the index corresponding to the range of the value read in from the user. This line is inside the for loop, so it will be executed for each value read in. The "(int)(value+0.5)/10" part is taking the value read in, adding 0.5 to it, casting it to an integer, and then dividing it by 10 to get the range of the value. This range is then used as the index for the "group" array to increment the frequency for that range.

The lines "printf("\n");" and "printf(" GROUP RANGE FREQUENCY\N\N");" are simply printing out some formatting for the output.

The lines "low = i*10;" and "if (i==10)" are setting the lower and upper bounds for each range. Since there are 11 ranges (0-9, 10-19, 20-29, etc.), the "low" variable is multiplied by 10 to get the lower bound. The "high" variable
 

Related to Can you tell me what this statement is doing?

1. What is the purpose of this statement?

This statement is used to perform a specific action or task in a computer program. It tells the computer what to do and helps to control the flow of the program.

2. How does this statement work?

This statement works by using a specific syntax or set of rules that the computer can understand. It may involve variables, functions, or other programming concepts to achieve its intended purpose.

3. Can you explain the syntax of this statement?

The syntax of a statement refers to its structure and format. It includes keywords, symbols, and other elements that must be used in a specific order for the statement to be valid. Different programming languages may have different syntax rules.

4. What does the statement do in relation to the rest of the program?

The statement is a part of the overall program and contributes to its functionality. It may interact with other statements, variables, or functions within the program to achieve a specific task or goal.

5. How can I modify this statement to achieve a different outcome?

To modify the statement, you can change its syntax or the values of its variables. Depending on the statement and the programming language, there may be different ways to modify it to achieve a different outcome.

Similar threads

  • Programming and Computer Science
Replies
4
Views
857
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
800
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
20
Views
2K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top