1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Can anyone help me with c++?

  1. Dec 9, 2006 #1
    please help me solving errors in this problem .

    #include <iostream>
    #include <string>
    #include <fstream>
    #include <cmath>
    #include <cstdlib>
    using namespace std;
    const size_t CAPACITY(8);

    // struct needed for this program.
    struct Points {
    double x, y;
    };

    // function prototype to open the user file and make array.
    void makeArray(Points[], size_t&);
    // function prototype to calculate distance of points from origin.
    void distance(Points[], const Points, size_t&);
    double calculateAverage(Points[], double);
    void outputFile(Points[], double, ofstream&);

    int main()
    {
    Points array[CAPACITY];
    Points dis;
    size_t size(0);
    ofstream outFile;
    double average(0);
    makeArray(array, size);
    distance(array, dis, size);
    average = calculateAverage(array, average);
    outputFile(array, average, outFile);


    }

    // function to open the user choice of input file and fill in the array.
    void makeArray(Points array[], size_t& size)
    {
    string fileName;
    ifstream inFile;

    // ask the user for input file.
    cout << "Enter a filename:";
    cin >> fileName;

    // open the user input file.
    inFile.open(fileName.c_str());

    // check if the file exists or not.
    if(inFile.fail())
    {
    cout << "Invalid filename.";
    cout << "Please enter a filename again:";
    cin >> fileName;
    inFile.clear();
    inFile.open(fileName.c_str());
    }
    Points temp;

    // read the points from the file.
    while(inFile >> temp.x >> temp.y )
    {
    array[size] = temp;
    size++;
    }


    }
    // function to calculate the distance of the points from the origin.
    void distance(Points array[], const Points dis, size_t& size)
    {

    // for loop to calculate the distance of each point from origin.
    for(int i = 0; i < size; i++)
    {
    array = sqrt(static_cast<double>(pow( dis.x,2)+pow(dis.y,2) )) ;

    }

    }

    // function to calculate the average of all the distance of points.
    double calculateAverage(Points array[], double average)
    {
    int howMany(0);
    int total(0);

    // for loop to calculate the sum.
    for(int i = 1; i < howMany; i++)
    {
    total += array;
    howMany++;
    }

    // calculate the average of distances.
    average = total/howMany;

    return average;
    }

    // function to output if average is greater than the points.
    void outputFile(Points array[], double average, ofstream& outFile)
    {
    if(average > array)
    {
    outFile.open("Output.txt");
    outFile << array<<endl;
    }
    }
     
  2. jcsd
  3. Dec 9, 2006 #2

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Hi jimmy007,

    We're not going to do your homework for you. We will not provide any assistance unless you show your work.

    If you have some specific questions about this program, please describe what you're tried so far and why you are stuck. We'll try to point you in the right direction.

    - Warren
     
  4. Dec 9, 2006 #3
    what i am asking is that i cannot figure out what i did wrong. this is not actually a homework. please tell me what did i do wrong. the errors i get are c:\Documents and Settings\narinderpal singh\My Documents\Visual Studio Projects\rec13exam\rec13exam.cpp(96): error C2677: binary '+=' : no global operator found which takes type 'Points' (or there is no acceptable conversion)
    c:\Documents and Settings\narinderpal singh\My Documents\Visual Studio Projects\rec13exam\rec13exam.cpp(81): error C2679: binary '=' : no operator found which takes a right-hand operand of type 'double' (or there is no acceptable conversion)
    c:\Documents and Settings\narinderpal singh\My Documents\Visual Studio Projects\rec13exam\rec13exam.cpp(112): error C3861: 'i': identifier not found, even with argument-dependent lookup

    these are the erros i get please help me.
     
  5. Dec 9, 2006 #4

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Well, let's look at the errors, one at a time. In the first error (which appears at line 91 to me), you're trying to add a Points structure to an integer. This, of course, doesn't make a lick of sense.

    - Warren
     
  6. Dec 9, 2006 #5
    so how do i fix it. should i use static_cast<>
     
  7. Dec 9, 2006 #6

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    It does not make logical sense to add a structure, composed of two doubles, to an integer. That's like trying to add five to an elephant.

    I don't really know what your program is intending to do, so I cannot suggest what you should change.

    Futhermore, I frankly don't believe that this is not homework. You do not need to lie to us to get help, and we don't really appreciate it.

    - Warren
     
  8. Dec 9, 2006 #7
    this is actually a lab that i have to do.

    Like the Blue Book question from the last test:

    Read a user chosen file of points (use the struct definition below for this) and store them in an array.
    Keep prompting the user until the file can be opened.
    Assume the file contains two ints per line which represent the x and y axis values for a point.

    Create an output file containing all the points that are further than the average distance of all points from the origin.

    You must use our code for the distance function.
    Here is it's prototype (declaration) :

    double distance2origin( const Point &, const Point & );

    Here's Point:

    struct Point {
    double x, y;
    };

    Since we do not give you the definition of our function, you can only compile your code.

    Your lab worker must check that your code compiles and will read your code to see that you correctly broke the problem into subproblems and wrote functions for these subproblems. Your main must be "clean": create variables, arrays and so forth to hold the data and call functions using these data structures to solve the problem.



    this is what my program is about.
     
  9. Dec 9, 2006 #8

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Thanks for your honesty. Here on PF, we place a strong emphasis on academic intregrity.

    Tell me what you intend for the calculateAverage() function to do. Is it supposed to calculate the average distance of all the points from the origin?

    - Warren
     
  10. Dec 9, 2006 #9
    yea you are right.
     
  11. Dec 9, 2006 #10

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Then I think it's curious that your calculateAverage() function doesn't reference your distance() function. Don't you?

    Futhermore, I think it's curious that your distance() seems to be taking an array of points, but then is trying to stick the distance to those points into the same array. That seems odd to me, doesn't it to you?

    - Warren
     
  12. Dec 9, 2006 #11
    yea then i should have different array right? so any suggestions.
     
  13. Dec 9, 2006 #12

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Sounds like a good plan, yes. Or, perhaps you don't need to store the distances at all, and can just calculate the distances as you're computing the average.

    - Warren
     
  14. Dec 9, 2006 #13
    should the other array be type of points or int
     
  15. Dec 9, 2006 #14

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    It's a distance, right? I'd say it's neither a point, nor an int. Turn on your brain for a minute.

    - Warren
     
  16. Dec 9, 2006 #15
    i don't get it . i better contact my TA.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Can anyone help me with c++?
  1. Can anyone help (Replies: 3)

Loading...