not on right track

This is a discussion on not on right track within the C++ Programming forums, part of the General Programming Boards category; I am trying to write a program that repeatedly asks you to enter pairs of numbers until at least one ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    5

    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);

    cin.get();

    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)
    break;
    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
    Posts
    9,796
    >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.
    Code:
    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;
        cin>>high>>low;
        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.

    -Prelude
    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, 08: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, 05:23 PM
  5. F1 track analyser
    By Unregistered in forum Game Programming
    Replies: 1
    Last Post: 01-22-2002, 08:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21