LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-30-2013, 03:51 PM   #1
shifter
Member
 
Registered: May 2006
Distribution: Slackware, DragonFly
Posts: 233

Rep: Reputation: 30
average calculus in C language


Hi all, I've a problem in average calculus with a program in C language.

Below I wrote some snippet of code.

In funzioni.c
Code:
float voto_medio(studente stud[], int dim) {

  int i, somma = 0;
  float vmedio;
  
  for (i = 0; i < dim; i++) {
    somma = stud[i].voto + somma;
  }

  vmedio = somma/dim;
  
  return (vmedio);
  
}
and in funzioni.h I have
Code:
#define MAX 50

typedef char STRINGA[30];

typedef struct {
  STRINGA nome;
  STRINGA cognome;
  int voto;
} studente;

void inserisci_studenti(studente*, int);
void visualizza_studenti(studente*, int);
float voto_medio(studente*, int);
void ordina(studente*, int);
void visualizza_studenti_con_giudizio(studente*, int);
void salva_su_file(studente*, int);
My problem is that average calculus in voto_medio function is not correct.
I'm not able to make average with float numbers. For example, with 30, 28 and 24 I have as result 27.000 whereas I must 27.333...

Where is my errore in voto_medio function?

Thanks in advance.

Last edited by shifter; 06-30-2013 at 03:54 PM.
 
Old 06-30-2013, 04:10 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
somma and dim are ints, so somma/dim is performing an integer division. You'll need to re-cast them as floats in order to do a floating point division.
 
Old 06-30-2013, 04:22 PM   #3
shifter
Member
 
Registered: May 2006
Distribution: Slackware, DragonFly
Posts: 233

Original Poster
Rep: Reputation: 30
That is to say
Code:
(float)somma/dim;
modifying voto_medio in the following way:
Code:
float voto_medio(studente stud[], int dim) {

  int i, somma = 0;
  
  for (i = 0; i < dim; i++) {
    somma = stud[i].voto + somma;
  }
  
  return (float)somma/dim;
  
}
In this way I have average in floating point.

Thank you very much!!
 
Old 07-01-2013, 03:18 AM   #4
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
How accurate does the average need to be ? If it needs to be reasonably accurate, use 'double' instead of 'float'.
 
Old 07-02-2013, 11:04 AM   #5
shifter
Member
 
Registered: May 2006
Distribution: Slackware, DragonFly
Posts: 233

Original Poster
Rep: Reputation: 30
It doesn't matter, I think float is sufficient.
 
Old 07-02-2013, 07:16 PM   #6
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by shifter View Post
It doesn't matter, I think float is sufficient.
"float" is only guaranteed to be 32 bits long. About the only time you would use it in preference to "double" is when you are dealing with massive arrays of real numbers.
 
Old 07-02-2013, 08:55 PM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by psionl0 View Post
"float" is only guaranteed to be 32 bits long. About the only time you would use it in preference to "double" is when you are dealing with massive arrays of real numbers.
Or if you don't care about precision beyond 7 sig figs...
 
Old 07-03-2013, 12:14 AM   #8
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by suicidaleggroll View Post
Or if you don't care about precision beyond 7 sig figs...
You might not care whether you use float or double but you would seldom prefer float without a special reason.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
new theme song: Captain Calculus hydraMax General 0 02-02-2013 08:34 PM
GNU Calculus icecubeflower General 17 11-23-2009 05:34 AM
intro calculus question nadroj General 9 03-03-2007 08:51 PM
Decent calculus software geniarse Linux - Software 3 10-11-2004 11:27 AM
calculus software splinter889 Linux - Software 4 01-10-2004 03:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration