Need help with the logic of the program

This is a discussion on Need help with the logic of the program within the C++ Programming forums, part of the General Programming Boards category; I need logic to the following program: The input is like 2, 3.4, 5, 2.2, 4.6, 5.7, 3.6 ..... The ...

  1. #1
    unregistered
    Guest

    Need help with the logic of the program

    I need logic to the following program:
    The input is like
    2, 3.4, 5, 2.2, 4.6, 5.7, 3.6 .....
    The output should be sorted out as:

    0-2 :1
    2-4: 3
    4-6: 3
    6-8: 0

    The numbers are checked if they are in a particular range and the counter is increased accordingly.
    How can this be implemented?I need some help with the logic.

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    You can use the if-construction. This construction is:

    Code:
    if (condition)
    {
        /* the code here will be executed */
    }
    else
    {
        /* the code here will be executed */
    }
    You could use this construction like this:

    Code:
    if (number > 2 && number < 4)
    {
        ..
    }
    else if (number >= 4 && number < 6)
    {
        ...
    }
    
    etc.

  3. #3
    Unregistered
    Guest
    Hi,
    Thanks, I tried this method and it works but I need some other method because the if-else method is too lengthy and if I have the range set to
    0-1:
    1-2:
    2-3:
    3-4: and so on then there are so many if statements to be used so I was thinking of some other method. If there is some other way to do it please let me know.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    try something like:

    double input;
    int numbers[RANGE];
    cin >> input;
    int value = (int)floor(input);
    numbers[value]++;

    then after you've inputted all the values

    for(int i = 0; i < RANGE; i++)
    {
    cout << i << " - " << i + 1 << " : " << numbers[i] + numbers[i+1] << endl;
    i++; // incremet again
    }


    this hasn't been compiled,it's intended to give you an idea. if you still have problems.. post again.

    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  5. #5
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    the value declaration looks weird, plz explain it to me. Just curious. That int thing in parentesis looks like typecasting, but why would you typecast an int with an int?

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    look at the floor() function, you'll figure it out.

    hint: what does floor() return??
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  7. #7
    Unregistered
    Guest
    Thanks Uraldor

    After making some changes the program worked fine.Thanks for the logic.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 09:49 PM
  2. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 08:48 AM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  4. logic on a program
    By officegirl in forum C Programming
    Replies: 0
    Last Post: 10-13-2001, 10:41 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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