You are here:

C++/about finding geometric mean of a sequence of numbers in an input file


i want to make a program that reads a sequence of real numbers in an input.dat file and finds the harmonic and geometric mean of those real numbers.
In the input.dat file, all the numbers are written line by line.
I tried to use an character array to do the work.
It works fine for harmonic mean except that i could only convert the data type "character array" to "integer" to make the calculations. So, the numbers in the input.dat file can only be integers, not real numbers.
Another problem is when i tried to find the geometric mean, the result is always "0.000000".
I think it is because of the multiplying process to find geometric mean.
I am new at programming and if you can help me i will really appreciate it.
this is the part of the code for geometric mean:

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <fstream>
using namespace std;

// Define a constant for the number of lines to read

#define MAX_VALUES 10000

int main() {
   // Array of line numbers each line being no more than 100 chars
   char thearray[MAX_VALUES][100];
   int counter = 0;
   float geometric = 0;
   // Open our file
   ifstream inFile("c:input.dat",ifstream::in);

   // If we can read good
   if (inFile.good()) {

       // Read through file and load into array
       while (!inFile.eof() && (counter < MAX_VALUES)) {

       // Loop through the array which we just put together and find the geometric mean
       for (int i = 0; i < counter; i++) {

         geometric = geometric *  atoi(thearray[i]);   // when i change "atoi" with "float" it gives an error
         // about pointing value and a floating point value


   printf("Geometric Mean is : %f", pow(geometric, float (1.0/counter)));


   return 0;

Comments that may help you debug and resolve the problems:

1. Since geometric is initialized to 0, the line:

geometric = geometric *  atoi(thearray[i]);

is always 0.  I don't know geometric means, but I know the result here will always be 0.

2. Use atof(thearray[i]) to get the float value from the lines being read in.

3.  I believe you want:

char thearray[100][MAX_VALUES];

you want MAX_VALUES of 100 char arrays, right?



All Answers

Answers by Expert:

Ask Experts


Bill A


I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.


I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

BS Computer Engineering

©2017 All rights reserved.