Thread: not on right track

  1. #1
    Registered User
    Join Date
    Feb 2002

    Question not on right track

    I am trying to write a program that repeatedly asks you to enter pairs of numbers until at least one of the pair is zero. For each pair, the program should use a function to calculate the harmonic mean of the numbers the function should report the result. This is the code I have done but I am not sure I am on the right track. I just can't seem to get the right things together.

    #include <iostream.h>
    const int ArSize = 2;
    int fill_array(int ar[], int n);
    double hmean(double x, double y);

    int main(void)

    int numbers[ArSize];
    cout << "Please enter a pair of numbers\n";
    cout << "\t(q to quit)\n\n";
    int total = fill_array(int ar[], int n);
    hmean(total, numbers);


    return 0;

    int fill_array(int ar[], int n)
    int numbers;
    for (int i = 0; i < n; i++)
    cout << "Enter pairs #" << (i + 1) << ": ";
    while (!(cin >> numbers))
    if (i==0)
    return 0;
    ar[i] = numbers;
    return i;

    double hmean(int ar[], int n)
    { int x,y = 0;
    for (int i = 0; i < n; i++)
    return ar[i] = 2*x*y/(x+y);

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    >int total = fill_array(int ar[], int n);
    should be
    int total = fill_array(numbers, ArSize);

    A better algorithm would be to read in two integers from the user, check to see if one of them is zero, and then branch off.
    inline double hMean(int x, int y) 
      return (double)2 * x * y / (x+y);
    int main ( void )
      int high, low;
      double d;
      for ( ; ; ) { //forEVER
        cout<<"Enter two integers"<<endl;
        if ( high == 0 || low == 0 )
          return EXIT_SUCCESS;
        d = hMean(high, low);
        cout<<"The harmonic mean is "<< d <<endl;
      return EXIT_SUCCESS;
    /* Untested code */
    Throw an infinite loop around the processing in main for the program to run until the user enters a 0 as one of the integers.

    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to track every copy event
    By manav in forum Windows Programming
    Replies: 3
    Last Post: 06-05-2008, 10:37 AM
  2. Keeping track of words inputted
    By death_coder in forum C++ Programming
    Replies: 2
    Last Post: 01-11-2006, 09:05 PM
  3. Am I on the right track
    By romeoz in forum C++ Programming
    Replies: 12
    Last Post: 07-07-2003, 10:10 PM
  4. Keeping track of static external structure
    By pwilfred in forum C Programming
    Replies: 6
    Last Post: 03-13-2003, 06:23 PM
  5. F1 track analyser
    By Unregistered in forum Game Programming
    Replies: 1
    Last Post: 01-22-2002, 09:48 AM