I seem to be getting a segmentation fault in my array. When I run it the the TF and TC appear at the top then the array. That's when the error occurs. I tried debugging it and when tf = 10 the error was occuring. Can someone help plz.

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int tf = 0;
    int r = 0;
    double array[r][1];

    cout << "TF     TC" << endl;

    while(tf <=100) {
    for(r=0; r < 20; r++) {
          array[r][0] = tf;
          array[r][1] = tf * (5/9) -32;  
          tf+=5;
          r++;
          }


    }                    
    for(int r=0;r<20;r++)  {
          cout << array[r][0] << setw(5) << array[r][1];
          }

    system("PAUSE");
    return 0;
}

double array[r][1]; <--- r is 0, so this is the same as writing:
double array[0][1];

You then go on to increment r:
for(int r=0;r<20;r++) {

and try to write out of array bounds:
array[r][0] = tf;

but the array only goes up to array[0], not array[1].

I don't know whats really heppening cause i'm new to this but everything i try doesnt seem to work. if you could explain with more detail that would be a great help thanks

An array has a size, you aren't properly defining your array size:

int r=0;
double array[r][1]; // this is a declaration

Change that to this:

int r=0; // int r(0); is also ok
double array[20][1]; // this allocates space for 40 doubles

Your while loop is redundant, just delete it.

double values[20][1]; // I renamed this because I think naming it array might be
// confusing you, sorry if that sounds condescending, it isn't meant to be.
// arrays are declared like this:
// class name[size];
// where class is any class/struct/primitive, whether it's "int" "double" or a user defined class
// the size is the maximum number of things that can appear in that array
// the last element is out of bounds, and arrays start from the 0th element

for( r=0; r<20; ++r ) { // this will count from 0 to 19, 20 is out of bounds
  values[r][0] = tf;
  values[r][1] = tf * (5/9) -32;
  tf+=5;
  // don't increment r here, you did it in the for loop already
}

Hope that helps!

i have to say that you guys have missed a big problem with this code. Or the compilers you guys are using are not good enough to compile your programs.

look at this :

double values[20][1]; 
for( r=0; r<20; ++r ) { // this will count from 0 to 19, 20 is out of bounds
  values[r][0] = tf;
  values[r][1] = tf * (5/9) -32;
  tf+=5;
  // don't increment r here, you did it in the for loop already
}

Even though the values array is a two dimensional array but you are still trying to access an element which is not defined. When you type:

int val[1];

That means val has only one element which is val[0].
you guys are trying to access val[1] hence you guys are getting out of bound error/exception.

Hope this helps...

Haha, I wasn't compiling it at all. That's basically a typo and is the same thing that I'm pointing out in the first place.... "It's out of bounds because you didn't allocate that much", which is what I said in my first post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.