Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Last C++ help thread - Reading txt into arrays and plotting.
New Posts  All Forums:Forum Nav:

Last C++ help thread - Reading txt into arrays and plotting.

post #1 of 13
Thread Starter 
Last Homework, I promise. Now down to business.

We are supposed to write a program that reads data from txt files into arrays and then plot them using 2D arrays.

What do I have so far? Well, not much. I found a program online that does most of what I'm trying to do, in terms of reading the values, and decided to go from there.

Code:
#include<iostream>
#include<math.h>
#include<iomanip>
#include<fstream>
#include<string>

using namespace std;

int main(void){

    string fileName=" ";
    ifstream file;
    int rows(0), termsOne[rows], termsTwo[rows], columns(0), limitOne(0); 
    int limitTwo(0), i(0);
    double valueX[rows], sinX[rows], sinOne[rows], errorOne[rows];
    double sinTwo[rows], errorTwo[rows];
    
    cout<<"filename?  >>  ";
    cin>>fileName;
    
    file.open(fileName.c_str());
    
    if(file.fail())
        cout<<"File did not open properly.";
        
    file>>rows>>columns>>limitOne>>limitTwo;
    
    for(i=0;i<rows;i++){
         file>>valueX[i]>>sinX[i]>>sinOne[i]>>termsOne[i]>>errorOne[i]
         >>sinTwo[i]>>termsTwo[i]>>errorTwo[i];//I think the problem is here

         cout<<valueX[i]<<"  "<<sinX[i]<<"  "<<sinOne[i]<<"  "
         <<termsOne[i]<<"  "<<errorOne[i]<<"  "<<sinTwo[i]
         <<"  "<<termsTwo[i]<<"  "<<errorTwo[i]<<endl;
    }
    
    system("pause");
    file.close();
    return(0);
    
}
Apparently, it kept crahsing for the dude who posted it and the solution was:
"I declared the arrays after the value for rows and now it works".

I can't seem to get this to work. I've moved the declaration about for a while. The program reads the txt file in, not all of it mind you, and then crashes.

I'll attach the txt file I'm trying to read.


Why is it crashing?


Please scroll to the bottom for my latest inquiry. Helpful posts will be rewarded.
Edited by MeeMoo220 - 3/29/11 at 10:06pm
post #2 of 13
Quote:
Originally Posted by MeeMoo220 View Post
Why is it crashing?
Quote:
Originally Posted by MeeMoo220 View Post
I found a program online that does most of what I'm trying to do
This is why. Write your own code for homework, finding some junk online only hurts you.

More specifically, this line is wrong and it should be obvious why.
Code:
    int rows(0), termsOne[rows], termsTwo[rows], columns(0), limitOne(0);
What size are the declared arrays?
½
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD A64 3500+ Winchester DFI nF4 SLi-DR EVGA 7800GT OCZ 4000VX 
Hard DriveOptical DriveOSMonitor
Maxtor 300Gb 16Mb Buffer Spinney one XP Pro SOYO LCD 
KeyboardPowerCaseMouse
Broken Somewhat OCZ PowerStream 520W None Old 
Mouse Pad
Pad? AHAAHAHAH 
  hide details  
Reply
½
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD A64 3500+ Winchester DFI nF4 SLi-DR EVGA 7800GT OCZ 4000VX 
Hard DriveOptical DriveOSMonitor
Maxtor 300Gb 16Mb Buffer Spinney one XP Pro SOYO LCD 
KeyboardPowerCaseMouse
Broken Somewhat OCZ PowerStream 520W None Old 
Mouse Pad
Pad? AHAAHAHAH 
  hide details  
Reply
post #3 of 13
Thread Starter 
Quote:
Originally Posted by The Bartender Paradox View Post
This is why. Write your own code for homework, finding some junk online only hurts you.

More specifically, this line is wrong and it should be obvious why.
Code:
    int rows(0), termsOne[rows], termsTwo[rows], columns(0), limitOne(0);
What size are the declared arrays?
I'm not trying to laze out of my homework, I'm just looking for working templates to analyze/build on. That was the closest I've found.

As for why it isn't working, I'm guessing it's because they are declared at size zero?
post #4 of 13
Thread Starter 
Ahh, I see. It's cuz I am trying to change rows later on although it is started out as a size 0, which means I can't access it.

Any ideas?
post #5 of 13
Quote:
Originally Posted by MeeMoo220 View Post
I'm not trying to laze out of my homework, I'm just looking for working templates to analyze/build on. That was the closest I've found.
Code you find online is almost always wrong (as in this case) and will cause you more headaches then writing it yourself. Especially with something this small.

Quote:
Originally Posted by MeeMoo220 View Post
As for why it isn't working, I'm guessing it's because they are declared at size zero?
Yes. Also you aren't using 2D arrays, they are declared as array[x][y].


EDIT: you posted again, I'll respond, hold on.
EDIT2: The most simple way of doing this is knowing in advance what size array you need (or at least the minimum size). Then you declare the array at a static size, like this:
Code:
//pick your own size
    int row = 50, column = 50;
    int array[row][column];

Edited by The Bartender Paradox - 3/29/11 at 9:10pm
½
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD A64 3500+ Winchester DFI nF4 SLi-DR EVGA 7800GT OCZ 4000VX 
Hard DriveOptical DriveOSMonitor
Maxtor 300Gb 16Mb Buffer Spinney one XP Pro SOYO LCD 
KeyboardPowerCaseMouse
Broken Somewhat OCZ PowerStream 520W None Old 
Mouse Pad
Pad? AHAAHAHAH 
  hide details  
Reply
½
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD A64 3500+ Winchester DFI nF4 SLi-DR EVGA 7800GT OCZ 4000VX 
Hard DriveOptical DriveOSMonitor
Maxtor 300Gb 16Mb Buffer Spinney one XP Pro SOYO LCD 
KeyboardPowerCaseMouse
Broken Somewhat OCZ PowerStream 520W None Old 
Mouse Pad
Pad? AHAAHAHAH 
  hide details  
Reply
post #6 of 13
Thread Starter 
Quote:
Originally Posted by The Bartender Paradox View Post
Code you find online is almost always wrong (as in this case) and will cause you more headaches then writing it yourself. Especially with something this small.


Yes. Also you aren't using 2D arrays, they are declared as array[x][y].
I know. I just really didn't know where to start, so I looked at 7 different examples to get the idea. If it keeps giving me trouble, I'll just scrap it early.

P.S: I made it
Code:
int rows(100)
and now it works fine. I just have to learn how to make a 1D array int oa 2D array and then how to plot. Any help is appreciated.

P.P.S: Should I declare them as 2D arrays or is there a way to convert them? I imagine that would require reading them into new arrays, which is counter productive. I think I answered my question(?) I'll look into making 'em 2D.
post #7 of 13
Quote:
Originally Posted by MeeMoo220 View Post
I just have to learn how to make a 1D array int oa 2D array and then how to plot. Any help is appreciated.
Starting with a 2D array will be the easiest (I posted above how to do it). When you plot, probably the easiest way would be to print it out. Iterate (some sort of loop) first over the columns, then the rows. As you are going down each row, print the value. When you get to the end of a row, print a newline.
½
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD A64 3500+ Winchester DFI nF4 SLi-DR EVGA 7800GT OCZ 4000VX 
Hard DriveOptical DriveOSMonitor
Maxtor 300Gb 16Mb Buffer Spinney one XP Pro SOYO LCD 
KeyboardPowerCaseMouse
Broken Somewhat OCZ PowerStream 520W None Old 
Mouse Pad
Pad? AHAAHAHAH 
  hide details  
Reply
½
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD A64 3500+ Winchester DFI nF4 SLi-DR EVGA 7800GT OCZ 4000VX 
Hard DriveOptical DriveOSMonitor
Maxtor 300Gb 16Mb Buffer Spinney one XP Pro SOYO LCD 
KeyboardPowerCaseMouse
Broken Somewhat OCZ PowerStream 520W None Old 
Mouse Pad
Pad? AHAAHAHAH 
  hide details  
Reply
post #8 of 13
Thread Starter 
Quote:
Originally Posted by The Bartender Paradox View Post
Starting with a 2D array will be the easiest (I posted above how to do it). When you plot, probably the easiest way would be to print it out. Iterate (some sort of loop) first over the columns, then the rows. As you are going down each row, print the value. When you get to the end of a row, print a newline.
Thanks for all the input TBP, I'm gonna try and fix the 2D array mapping first before I attempt to plot anything. I'll post here if I have any more questions and hopefully, you'll be up .
post #9 of 13
Thread Starter 
Quick question:

Code:
 void plot( double data[ ], int nPoints, int nRows );
//This is the function that generates the actual plot.
Does the command plot() posted above use the 3 parameters as follows:
1st parameter = array to get the data from?
2nd parameter = x axis?
3rd parameter = y axis?
post #10 of 13
Quote:
Originally Posted by MeeMoo220 View Post
Quick question:

Code:
 void plot( double data[ ], int nPoints, int nRows );
//This is the function that generates the actual plot.
Does the command plot() posted above use the 3 parameters as follows:
1st parameter = array to get the data from?
2nd parameter = x axis?
3rd parameter = y axis?
As you haven't posted the function body no one can say if it uses the three parameters, but that function declaration does accept 3 parameters that could be used in the fashion you listed.

Actually it looks a little different, it looks like you want to try and do it with a 1D array whereas those requirements look more like it wants a 2D array. For yours it looks like you iterate a single time over the whole 1D array, inserting a newline every time you get to a multiple of nPoints/nRows. The requirements look like they want something like this:
Code:
void plot( double data[][], int nColumns, int nRows );
Where nColums is the x axis and nRows is the y axis, and you print the array like I've listed in a previous post.
½
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD A64 3500+ Winchester DFI nF4 SLi-DR EVGA 7800GT OCZ 4000VX 
Hard DriveOptical DriveOSMonitor
Maxtor 300Gb 16Mb Buffer Spinney one XP Pro SOYO LCD 
KeyboardPowerCaseMouse
Broken Somewhat OCZ PowerStream 520W None Old 
Mouse Pad
Pad? AHAAHAHAH 
  hide details  
Reply
½
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD A64 3500+ Winchester DFI nF4 SLi-DR EVGA 7800GT OCZ 4000VX 
Hard DriveOptical DriveOSMonitor
Maxtor 300Gb 16Mb Buffer Spinney one XP Pro SOYO LCD 
KeyboardPowerCaseMouse
Broken Somewhat OCZ PowerStream 520W None Old 
Mouse Pad
Pad? AHAAHAHAH 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Last C++ help thread - Reading txt into arrays and plotting.