Thread: Why is this happening?

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    Why is this happening?

    Code:
    #include <iostream>
    
    int main()
    {
        int lines;
        int columns;
        
        std::cout << "lines: ";
        std::cin >> lines;
        
        std::cout << "columns: ";
        std::cin >> columns;
        
        int *matrix1 = new int[lines * columns]; 
        
        for (int i = 0; i < lines; i++)
            for (int j = 0; j < columns; j++) {
                std::cout << "matrix1[" << i << "][" << j << "]: ";
                std::cin >> matrix1[i * j + j];
            }
        
        for (int i = 0; i < lines; i++)
            for (int j = 0; j < columns; j++)
                std::cout << matrix1[i * j + j] << std::endl;
        
        delete [] matrix1;
    }
    If I type
    1
    2
    3
    4
    5
    6
    7
    8
    9

    I get
    7
    2
    5
    7
    5
    6
    7
    8
    9


  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by caduardo21
    Code:
    matrix1[i * j + j]
    Don't you mean
    Code:
    matrix1[i * columns + j]
    in both cases?

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Rashakil Fol
    That name looks familiar. Daniweb?
    My best code is written with the delete key.

  4. #4
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Yes.

    Well isn't this a contentless post.

  5. #5
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    here is another contentless post
    in my hometown, we mean contentless post by saying "pour water"

  6. #6
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    hmm....
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is happening here?
    By kermit in forum C Programming
    Replies: 4
    Last Post: 02-01-2009, 11:59 AM
  2. Explain me what is happening
    By capvirgo in forum C Programming
    Replies: 2
    Last Post: 02-18-2008, 08:26 AM
  3. Matrix Multiplication - Weird things happening
    By spoon_ in forum C Programming
    Replies: 4
    Last Post: 09-02-2005, 07:53 PM
  4. Weird things happening!!!!!!
    By korbitz in forum Windows Programming
    Replies: 4
    Last Post: 03-22-2004, 06:31 AM
  5. Don't know why this is happening
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 03-27-2002, 06:30 PM