0

I'm new to programming and was finding transpose of a matrix. However, I want the input of the matrix from the user and by writing the following code, the complier doesn't take any input values and immediately stops. I looked into previous questions posted here about the same but found non useful.

#include<iostream>
using namespace std;
int main()
{
    int rows,val;
    int num[rows][rows];
    cin>> rows;
    for(int i=1; i<= rows; i++)
    {
        for(int j = 1; j <= rows; j++)
        {
            cin>> val;
            arr[i][j]= val;
        }
        cout<<endl;
    }
Zoe
  • 27,060
  • 21
  • 118
  • 148
Manya Garg
  • 13
  • 4
  • whats the size of this array `int num[rows][rows];` ? (you declare it **before** reading `rows` from the user) Also read this: [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – 463035818_is_not_an_ai Dec 08 '21 at 10:27
  • When you define the array `num`, what is the value of `rows`? – Some programmer dude Dec 08 '21 at 10:27
  • @463035818_is_not_a_number i found the error, thank you. i guess that's why it was not complying properly. – Manya Garg Dec 08 '21 at 10:30

1 Answers1

0
  1. You can't use variables in array length if they aren't defined as one of the comments mentioned.
  2. arr[i][j] inside your nested for loop isn't declared so that would also give an error, I guess you wanted to use num array which you declared. The rest is all looking good
Sugarcube
  • 73
  • 6