Thread: arithmetic operations

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    72

    arithmetic operations

    Hello,

    I want to calculate the average, median, mode and range of some numbers. It's supposed to read the numbers from a file.

    The file got:

    Code:
    int numbers[]={53,2,3,5,59,7,11,13,17,41,53,41,19,23,29,31,53,7,37,53,41,43,47,53}
    I am stuck with the average.....
    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    using namespace std;
    
    int main() {
        int average;
        int median;
        int mode;
        int range;
        int numbers;
        ifstream inFile;
        
        inFile.open("C:\Users\Sven\Desktop\data_for_proj1.txt");
        if (!inFile) {
            cout << "Unable to open file";
            exit(1); // terminate with error
        }
        
        
            average = numbers /24;
        
        
        inFile.close();
        cout << "The average is: " << average << endl; 
        return 0;
    }
    Gives me an error for the "while {" line. No idea if this would work. anyway. Any input is appreciated.


    EDIT:

    I just edited the while out. Now it should be better...I guess. But it still gives me tis error:

    29:17 C:\Users...........Lab_16.cpp incomplete universal character name \U
    29:17 C:\Users\..........Lab_16.cpp [Warning] unknown escape sequence '\S'
    Last edited by XodoX; 06-28-2009 at 05:41 PM.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    So you're telling me that you can't even construct a while loop but you want to write a program to calculate the average, median, mode and range of a set of numbers? Here's an idea. Get a hold of a few of C++ programming books and read them from cover to cover. You need to understand the basic syntax and a get feel for the language before you start writing programs. That's just common sense, no?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    to solve your compiler errors, you have to escape the slash, using "\\" in place of all quoted "\"'s

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    72
    Quote Originally Posted by nadroj View Post
    to solve your compiler errors, you have to escape the slash, using "\\" in place of all quoted "\"'s

    At least one helpful comment, thanks. It's working now.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    sure.

    but i hope by "It's working now" you don't mean its done, you just mean its compiling. of course the variable "numbers" was never initialized, so the program as is will produced an undefined result. i imagine you want to read a number from the file, summing each one, and finally dividing to get your average.

    also, i hope you know that putting "int numbers[]={53,2,3,5,59,7,11,13,17,41,53,41,19,23,29,31,53,7 ,37,53,41,43,47,53}" in a file doesnt do what you are expecting it to do.

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    72
    Yes, that's what I wanted to do...

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by XodoX View Post
    The file got:
    Code:
    int numbers[]={53,2,3,5,59,7,11,13,17,41,53,41,19,23,29,31,53,7,37,53,41,43,47,53}
    If that's really the exact contents of the file you have then you're probably supposed to #include it, not open it at runtime as a datafile.
    Of course there is the issue with the missing semicolon at the end, though in theory that can be put in the cpp file instead, right after the include (yuck).
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Resolve Arithmetic operations
    By louis_mine in forum C Programming
    Replies: 3
    Last Post: 11-13-2006, 09:14 PM
  2. doing floating operations using integer operations
    By ammalik in forum C Programming
    Replies: 10
    Last Post: 08-15-2006, 04:30 AM
  3. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM
  4. Replies: 1
    Last Post: 11-19-2001, 04:45 PM