Slight problem with grouping a set of numbers

In summary, the individual is having trouble with their code that is supposed to read numbers from a file and sort them into groups. They are not seeing the expected results and are unsure why. Suggestions were given to help troubleshoot the issue, including printing out the values being read and checking for errors. Additionally, the code itself has some issues, such as using a char value in a switch statement meant for int values and not setting the variable nmax. A revised example code was provided to address these issues.
  • #1
Cinimod
34
0
For some reason, the code I has compiles, but it just doesn't seem to do what it should. The code I have atm, involves opening a file of numbers, reading the numbers, and sorting them into groups.

Code:
int nmax;               // This is the number of lines in the file
int i;                     // Index
float num; groups[15];

// File is opened - I know that's not where the error is
FILE *file;
file = fopen("log.txt", "r");

for(i=0; i<nmax; i++)
{
fscanf(file, "%f\n", &num);

switch((int)num)
{
case '0': groups[0]++; break;
case '1': groups[1]++; break;
/* etc... */
}
}

Any ideas why it doesn't work. When I use another loop to print the values of groups to the screen, all the elements of the array are zero. which I think is rather odd.
 
Technology news on Phys.org
  • #2
First off, it would help if before switch()ing you'd printf out the value of num. That way you'd know at least whether the numbers are being read in correctly.

Second off, it kind of seems like you're mixing three different kinds of things. You're reading in a float, then casting it to an int (which is okay, but will cause you to always round decimal numbers down), but then in your switch statement you check against things like case '0' and case '1'? '0' is not equal to 0, it's a char ascii value and it's equal to like 65 or something.

Also I don't see nmax being set anywhere?!
 
Last edited:
  • #3
Coin said:
First off, it would help if before switch()ing you'd printf out the value of num. That way you'd know at least whether the numbers are being read in correctly.

Second off, it kind of seems like you're mixing three different kinds of things. You're reading in a float, then casting it to an int (which is okay, but will cause you to always round decimal numbers down), but then in your switch statement you check against things like case '0' and case '1'? '0' is not equal to 0, it's a char ascii value and it's equal to like 65 or something.

Also I don't see nmax being set anywhere?!

Coin's answers is very good!

to Cinimod :
there are no case matched if you use the switch (*some type of int*)
than case: '1'.
note that: '1' is a char who's size is ONE BYTE.
and an int type always occupise FOUR BYTES.
they are obviously different!
 
  • #4
Giving you code is not the best idea but there are several issues with your code.
This example reads freely from log.txt until it reaches either end of file or it has trouble converting the input. It does rudimentary error checking. It also assigns the group array in a more readable way. coin pointed out some of these issues.
Code:
#include <stdio.h>
#include <stdlib.h>
#define NMAX 25
#define ck(X) \
  if((X)==NULL){ perror("input file error"); \
  exit(1);}

int main()
{
	int groups[NMAX+1]={0};
	FILE *in=fopen("log.txt","r");
	float value=0.;
	
             ck(in);   /* make sure we have a file to read */
	while (fscanf(in,"%f\n", &value)> 0) /* read until EOF or problems */
	{		
		if((int)value<=NMAX) /* limit check the values so you do not exceed array bounds */
			groups[(int)value]++;
	}
	for(int i=0; i<NMAX; i++)
		printf("%3d groups: %3d\n", i, groups[i]);
	fclose(in);
	return 0;	
}
 

What does "grouping a set of numbers" mean?

Grouping a set of numbers refers to organizing and categorizing a set of numerical data into smaller, more manageable subsets based on certain criteria or characteristics.

Why is grouping a set of numbers important in scientific research?

Grouping a set of numbers allows scientists to make sense of large amounts of data and identify patterns or trends. It also helps in drawing meaningful conclusions and making accurate predictions based on the data.

What are some common methods used for grouping a set of numbers?

Some common methods for grouping a set of numbers include frequency distribution, histograms, scatter plots, and box and whisker plots. These methods help visualize and analyze the data in different ways.

How can outliers affect the process of grouping a set of numbers?

Outliers, or extreme values, can significantly skew the results of grouping a set of numbers. It is important to identify and address outliers before grouping the data to avoid inaccurate conclusions.

What are some challenges that scientists may face when grouping a set of numbers?

Some challenges that scientists may face when grouping a set of numbers include dealing with missing or incomplete data, choosing appropriate grouping criteria, and ensuring the accuracy and reliability of the data.

Similar threads

  • Programming and Computer Science
Replies
9
Views
712
  • Programming and Computer Science
Replies
22
Views
772
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
8
Views
883
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
1
Views
947
  • Programming and Computer Science
3
Replies
75
Views
4K
Back
Top