Thread: no error of compilation but my code makes codeblocks bug

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    54

    no error of compilation but my code makes codeblocks bug

    Hi everybody, something may be wrong in my code even if there are no errors of compilation. But i don't know why!
    Any help would be greatly appreciated.
    My code :
    Code:
    #include <iostream>#include <iomanip>
    #include <cstdlib>
    
    
    using namespace std;
    
    
    
    
    void initArray(float largeArray[6][6]){
        for (int x=0; x < 6; x++){
                for (int y=0; y<6; y++){
                      largeArray[x][y] = 0;
            }
        }
    
    
    
    
    }
    
    
    
    
    float printOutArray(float largeArray[6][6]){
        for (int x = 0; x < 6; x++){
    
    
    
    
                for (int y = 0; y < 6; y++){
                        cout << largeArray [x] [y]<<'\t';
                    }
               cout << endl;
            }
    return largeArray[6][6];
    }
    
    
    float tata (int degree_denominator, float largeArray[6][6] )
    {
    float array_of_coefficients[6];
    int c=0;
    int l=0;
    int x;
    for (x=degree_denominator ; x>=0 ; x--){
                                            if (l==2){l=0;c++;}
                                   cout << "what is the coefficient linked with degree" << x << "?" << endl;
                                   cin >> array_of_coefficients[x];
                                   largeArray[l][c] = array_of_coefficients[x];
                                   l++;
                                  }
    
    
     return largeArray[6][6];
    }
    
    
    
    
    float toto(float largeArray[6][6])
    {
    float finalArray[9][9] = {0};
    int u = 0;
    int v = 0;
    int line = 0;
    int i = 0;
    int r = line - 2;
    int s = line - 1;
    int f = i + 1;
    
    
    for (u = 0; u <= 1 ; u ++){
                     for (v = 0; v<=9; v++){
                                    finalArray[u][v] = largeArray[u][v];
                     }
      }
    
    
    for (line = 2; line <= 9 ; line ++){
            for (i = 0; i<=9; i++){
                                    finalArray[line][i] = ((finalArray[r][f]) - ( ( finalArray[s][f] ) * ( (finalArray[r][i])/ (finalArray[s][i])) ) ) ;
                                    }
                          }
    return finalArray[9][9];
    }
    
    
    
    
    
    
    float printOutArray2(float finalArray[9][9]){
        for (int x = 0; x <= 9; x++){
    
    
    
    
                for (int y = 0; y <= 9; y++){
                        cout << finalArray [x] [y]<<'\t';
                    }
               cout << endl;
            }
    return finalArray[9][9];
    }
    
    
    
    
    int main()
    {
        float finalArray [9][9];
        float largeArray [6][6];
        initArray(largeArray);
        printOutArray(largeArray);
        int degree_denominator;
        cout << "what is the degree of the denominator?" << endl;
        cin >> degree_denominator;
        tata(degree_denominator, largeArray);
        printOutArray(largeArray);
        toto(largeArray);
        cout << "final array:" << endl;
        printOutArray2(finalArray);
        system("PAUSE");
    
    
    }

  2. #2
    Registered User
    Join Date
    Feb 2013
    Posts
    54
    Codeblocks stops to work when i run this code, can someone tell me why?

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by funnydarkvador View Post
    Codeblocks stops to work when i run this code, can someone tell me why?
    My guess is it is because you keep trying to return memory that you do NOT own.

    This is one of the times you did it.
    Code:
    return largeArray[6][6];
    Another time.
    Code:
    return finalArray[9][9];
    Code:
    float finalArray[9][9] = {0};
    The above code declares an 9 by 9 array; this mean the last float is finalArray[8][8].
    The element finalArray[9][9] is NOT part of the array and when you use it; the computer could react in any way;
    This includes deleting all your software or crashing.

    Tim S.
    Last edited by stahta01; 03-18-2013 at 06:22 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Previous threads where you've been told that what you're doing is NOT THE RIGHT WAY TO RETURN AN ARRAY!!!!
    http://cboard.cprogramming.com/cplus...parameter.html
    number of columns and lines in my array
    error : variable-sized object 'largeArray2' may not be initialized
    http://cboard.cprogramming.com/cplus...ore-float.html

    Your indentation needs work (still!).

    Here, consider the following.
    Code:
    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    using namespace std;
    
    void initArray(float largeArray[6][6])
    {
      for (int x = 0; x < 6; x++) {
        for (int y = 0; y < 6; y++) {
          largeArray[x][y] = 0;
        }
      }
    }
    
    void printOutArray(float largeArray[6][6])
    {
      for (int x = 0; x < 6; x++) {
        for (int y = 0; y < 6; y++) {
          cout << largeArray[x][y] << '\t';
        }
        cout << endl;
      }
    //!! DONT DO THIS  return largeArray[6][6];
    }
    
    void tata(int degree_denominator, float largeArray[6][6])
    {
      float array_of_coefficients[6];
      int c = 0;
      int l = 0;
      int x;
      for (x = degree_denominator; x >= 0; x--) {
        if (l == 2) {
          l = 0;
          c++;
        }
        cout << "what is the coefficient linked with degree" << x << "?" << endl;
        cin >> array_of_coefficients[x];
        largeArray[l][c] = array_of_coefficients[x];
        l++;
      }
    //!! DONT DO THIS  return largeArray[6][6];
    }
    
    void toto(float largeArray[6][6], float finalArray[9][9] )
    {
    //!! is now a parameter  float finalArray[9][9] = { 0 };
      int u = 0;
      int v = 0;
      int line = 0;
      int i = 0;
      int r = line - 2;
      int s = line - 1;
      int f = i + 1;
    
      for (u = 0; u <= 1; u++) {
        for (v = 0; v <= 9; v++) {
          finalArray[u][v] = largeArray[u][v];
        }
      }
    
      for (line = 2; line < 9; line++) { //!! <, not <=
        for (i = 0; i < 9; i++) {        //!! <, not <=
          finalArray[line][i] =
              ((finalArray[r][f]) - ((finalArray[s][f]) * ((finalArray[r][i]) / (finalArray[s][i]))));
        }
      }
    //!! DONT DO THIS  return finalArray[9][9];
    }
    
    
    void printOutArray2(float finalArray[9][9])
    {
      for (int x = 0; x <= 9; x++) {
        for (int y = 0; y <= 9; y++) {
          cout << finalArray[x][y] << '\t';
        }
        cout << endl;
      }
    //!! DONT DO THIS  return finalArray[9][9];
    }
    
    int main()
    {
      float finalArray[9][9];
      float largeArray[6][6];
      initArray(largeArray);
      printOutArray(largeArray);
      int degree_denominator;
      cout << "what is the degree of the denominator?" << endl;
      cin >> degree_denominator;
      tata(degree_denominator, largeArray);
      printOutArray(largeArray);
      toto(largeArray,finalArray);  //!! extra parameter for the result
      cout << "final array:" << endl;
      printOutArray2(finalArray);
      system("PAUSE");
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using less malloc and free makes code _slower_
    By chlorine in forum C Programming
    Replies: 21
    Last Post: 08-12-2011, 09:03 AM
  2. error with scanf statement in codeblocks
    By narendrav in forum C Programming
    Replies: 1
    Last Post: 03-19-2011, 11:25 AM
  3. codeblocks compilation error, C Tutorial
    By boarder428 in forum C Programming
    Replies: 8
    Last Post: 09-26-2010, 03:00 PM
  4. error: was not declared in this scope compilation error
    By i_r_tomash in forum C Programming
    Replies: 2
    Last Post: 05-27-2010, 07:44 AM
  5. code not working in codeblocks?
    By jamort in forum C++ Programming
    Replies: 4
    Last Post: 11-09-2009, 09:26 PM