Thread: Assignment due in 1 hour. 10 % of grade & having problems. Please help

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    7

    Assignment due in 1 hour. 10 % of grade & having problems. Please help

    seriously if you could help me i wil be in debt to you forever. I need to calculate area under a curve using provided x and y coordinates which are to be opened from a document called xydata.dat. Here is the data as writtten in the document:

    x values y values
    20.00 0
    20.02 15
    20.04 27
    20.06 39
    20.08 54
    20.10 65
    20.12 75
    20.14 84
    20.16 93
    20.18 101
    20.20 108
    20.22 113
    20.24 116
    20.26 115
    20.28 112
    20.30 107
    20.32 100
    20.34 92
    20.36 83
    20.38 74
    20.40 64
    20.42 53
    20.44 39
    20.46 27
    20.48 15
    20.50 0

    Here is the best ive done so far but its rudimentary and i just dont know where to go next



    Code:
    /*Program to take x and y values from a curve and add up the area of the 
    trapezoids they form. 
    input:x values and y values from document
    processing: 0.5(x1 – x2)(f(x2) + f(x3)) + 0.5(x3 – x2)(f(x2) + f(x3))+....
    output: area under the curve (sum of all trapezoids)
    */
    
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    using namespace std;
    
    int main()
    {
        ifstream datain;  
        ofstream infoout;
        int x1,y1,x2,y2;
        double area;
        double total=0;
    
        datain.open("xydata.dat"); 
        infoout.open("area.dat");  
    
            if (!datain.fail())   
        {
            datain.ignore(80,'\n');       
    
            while (!datain.eof())
            {
                datain>>x1;   
                datain>>y1;  
                datain>>x2;  
                datain>>y2; 
    
                area = 0.5*(x1-x2)*(y1+y2);
    
                infoout<<setw(6)<<fixed<<setprecision(5)<<area<<endl;    
    
                total = total + area;
            }
            
            infoout<<"\nThe area under the curve is "<<area;  
    
    
        }
    
        else
        {
            cout<<"Error opening file"<<endl;
        }
            return 0;
         
    }
    Last edited by kjs55; 10-17-2011 at 08:35 PM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,443
    what is the problem that you're having?

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    It says it failed to open the file lol cant even get it to do that. and also i doubt the above makes sense i completely guessed on a lot of it.
    The file is saved as xydata.dat on my desktop

    errors as follow:

    'Project2.exe': Loaded 'C:\Users\TheManBro\Documents\Visual Studio 2010\Projects\Project2\Debug\Project2.exe', Symbols loaded.
    'Project2.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file
    'Project2.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file
    'Project2.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file
    'Project2.exe': Loaded 'C:\Windows\SysWOW64\msvcp100d.dll', Symbols loaded.
    'Project2.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded.
    The program '[5024] Project2.exe: Native' has exited with code 0 (0x0).
    Last edited by kjs55; 10-17-2011 at 08:13 PM.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,443
    those are debugger errors, and have nothing to do with the running of your program. check to see if it created the area.dat file. it should be in the Project2 folder or the Debug folder if it was created.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    yeah it did but its completely blank

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,443
    make sure that the input file is in the folder where the program is looking for it. it's likely that it's not the folder that the compiled exe is in. whatever folder the area.dat file was created in, that's where the input file needs to be.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    thanks can u PLZ stick with me for 10 more mins maybe i can get this in on time!

    here is the new output
    -737869781502640770.00000
    -737869781502640770.00000
    -737869781502640770.00000
    -737869781502640770.00000
    -737869781502640770.00000
    -737869781502640770.00000
    -737869781502640770.00000
    -737869781502640770.00000
    -737869781502640770.00000
    -737869781502640770.00000
    -737869781502640770.00000
    -737869781502640770.00000

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,443
    I think you need to read in x1 and y1 before you start the while loop. read x2 and y2 at the top of the while loop and then at the bottom, copy x2 into x1 and copy y2 into y1.

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    still getting
    737869781502640770.00000
    737869781502640770.00000
    737869781502640770.00000
    737869781502640770.00000
    737869781502640770.00000
    737869781502640770.00000
    737869781502640770.00000
    737869781502640770.00000
    737869781502640770.00000
    737869781502640770.00000

    infinite amount of times - its an endless loop idk why

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    3,443
    datain.eof() is not a good way to decide if you're done reading the file. use
    Code:
    while (datain.good())
    instead.

  11. #11
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    now im just getting thearea under the curve is 0 lol...thanks for your help btw! cant explain how much i appreciate it. I think were close to getting it

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    3,443
    post your latest code so I can have a look.

  13. #13
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    Code:
     #include <iostream>
    #include <fstream>
    #include <iomanip>
    using namespace std;
    
    int main()
    {
        ifstream datain;  
        ofstream infoout;
        int x1,y1,x2,y2;
        double area=0;
        double total=0;
    
        datain.open("xydata.dat"); 
        infoout.open("area.dat");  
    
            if (!datain.fail())   
        {
            datain.ignore(80,'\n'); 
                datain>>x1;
                datain>>y1;
    
            while (datain.good())
            {
                
                datain>>x2;   
                datain>>y2;   
    
                area = 0.5*(x2-x1)*(y1+y2);   
    
                total = total + area;
    
                infoout<<setw(6)<<fixed<<setprecision(5)<<total<<endl;
    
                x1=x2;
                y1=y2;
            }
            
            infoout<<"\nThe area under the curve is "<<area;  
    
    
        }
    
        else
        {
            cout<<"Error opening file"<<endl;
        }
            return 0;
            system ("pause");
    }

  14. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    3,443
    why are you ignoring 80 characters at the beginning of the file? is it to ignore the first line? because there is a better way to do that. try using std::getline and a std::string.

    also, try declaring x1, y1, x2, and y2 as double instead of int. that might help with some of your problem.

  15. #15
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    The area under the curve is "total" not "area". You are calculating the discrete sum correctly though: just add up the trapezoid areas. Even if you lose 10%, you can rest assured that while you need to learn more about programming, you are at least up to pace with mathematics; whereas many decent programmers understand an alarmingly small amount about math.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-29-2011, 12:58 PM
  2. function returning hour in either 12 or 24 hour format
    By stanlvw in forum C Programming
    Replies: 4
    Last Post: 01-01-2008, 06:02 AM
  3. Big problems with assignment, please help!!
    By JFonseka in forum C Programming
    Replies: 12
    Last Post: 03-25-2007, 12:16 AM
  4. Roidian: Final Hour (72 hour gd compo results)
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 12-29-2004, 10:27 PM
  5. HELP!! i though addition problems were over after 3rd grade!
    By newbie2C++ in forum C++ Programming
    Replies: 1
    Last Post: 05-02-2002, 01:24 AM

Tags for this Thread