Problems with 'int'

喜夏-厌秋 提交于 2019-12-08 02:41:44

问题


I'm new to programming, so I want to write a code that will let me input a 2 dimensional array (or a matrix in my case) and print it afterwards.

#include <iostream>
using namespace std;

void printArray( const int *array, int count ) 
    { 
       for ( int i = 0; i < count; i++ ) 
          cout << array[ i ] << " "; 

       cout << endl; 
    }

int main () {
int n;
cout<<"Please enter the length of your matrix : "<<endl;
cin>>n;
int * y=new int [n];
for (int w = 0; w <= n-1; w++ ) {

    y[w] = new int [n];
    cout<<"Insert the elements ";

            for (int z = 0; z <= n-1; z++)
            {
                cin >>y [w][z];
            }   
}

printArray(y, n);

}

However I get errors like "invalid conversion from 'int*' to 'int'" and "invalid types int[int] for array subscript". Can you please review my code and point my flaws?

Thanks


回答1:


You declared y as an int* which would only be 1-dimensional. You would need to declare y as int** for it to be 2-dimensional.

The reason your code does not compile is because int* y points to a single block of memory (that being an array of integers, in other words, a bunch of ints.). y[w] is one of those ints inside this array so y[w] = new int[n] fails to compile because you are trying to assign an int* to an int.

Changing y to an int** means that y can point to an array of int*s. Since each int* can point to an array of int, you will have a 2-dimensional array.

Example code for 10x10 matrix with int**:

int** iShouldUseStdVector = new int*[10]; // allocate 10 int* <--
for (int i = 0; i < 10; i++)
{
    iShouldUseStdVector[i] = new int[10]; // allocate 10 int <--
    for (int k = 0; k < 10; k++)
    {
        iShouldUseStdVector[i][k] = k;
    }
}

Example code for 10x10 matrix with std::vector:

std::vector<std::vector<int>> thisIsEasy;
for (int i = 0; i < 10; i++)
{
    thisIsEasy.push_back(std::vector<int>());
    for (int k = 0; k < 10; k++)
    {
        thisIsEasy[i].push_back(k);
    }
}

I would recommend using std::vector<std::vector<int>> y; instead since it handles the memory for you by conveniently growing as you want to add more elements and freeing the memory when its destructed.




回答2:


int * y=new int [n];

That is an array of length n. What you would need is:

int **y = new int*[n];
for (int i=0; i<n; i++)
    y[i] = new int[n];

....

//delete[] y[i] in a loop
delete[] y;

Since you are using C++, why not:

#include <vector>

...
std::vector<std::vector<int> > matrix;



回答3:


int * y=new int [n]; is a pointer to a dynamically-allocated array of int values;

y[w] = new int [n]; tries to assign a pointer into an element of the array.

As a learning exercise, messing around with raw arrays is a good idea. Once you know how to use them, you will know enough to stop using them (very much) and go with automatic storage types like std::vector.




回答4:


1) You want to declare y as a pointer to an array of int:

int ** y = new int* [n];

2) In printArray you are dealing with a matrix, not an array. To access the members of a matrix use two nested for loops like this:

for (int i = 0; i < nLines; i++) {
    for (int j = 0; i < nColumns; j++) {
        std::cout << matrix[i][j] << std::endl;
    }
}

3) You need to pass a pointer to an array of int, not a pointer to an int to the printArray method.

void printArray( const int **array, int count ) 

Find the working code, with the above fixes here: http://ideone.com/2h9dR




回答5:


First, read the other answers and then perhaps reread the pointer chapter in a good C++ book. Now, unless you need extreme speeds, use vector of vector of double. With C++11 (the newer C++ standard) it's really nice and readable, so I post that first:

#include <iostream>
#include <vector>

void printArray( std::vector< std::vector<double> > & v ) {
   for ( const auto & row : v ){
      for ( const auto & value : row ){
        std::cout << value << " ";
      }
      std::cout << std::endl;
   }
}

int main () {
   int n;
   std::cout<<"Please enter the length of your matrix : "<<std::endl;
   std::cin>>n;
   std::vector<std::vector<double>> y(n,std::vector<double>(n,0));
   for ( auto & row : y ){
      std::cout<<"Insert the elements of row :";
      for ( auto & value : row ){
        std::cin >> value;
      }   
   }
   printArray(y);   
}

For older C++ it's like this:

void printArray( std::vector< std::vector<double> > & v ) {
   for ( std::vector<std::vector<double> >::const_iterator it = v.begin(); it != v.end();it++){
      for ( std::vector<double>::const_iterator it2 = it->begin(); it2!= it->end();it2++) {
     std::cout << (*it2) << " ";
      }
      std::cout << std::endl;
   }
}

int main () {
   int n;
   std::cout<<"Please enter the length of your matrix : "<<std::endl;
   std::cin>>n;
   std::vector<std::vector<double> > y(n,std::vector<double>(n,0));
   for ( std::vector<std::vector<double> >::iterator it = y.begin(); it!= y.end();it++){
      std::cout<<"Insert the elements of row :";
      for ( std::vector<double>::iterator it2 = it->begin(); it2!= it->end();it2++) {
     std::cin >> (*it2);
      }   
   }
   printArray(y);   
}

Note that y(n,std::vector<double>(n,0)) means to make n vectors, each with n zeros. You can use also use y[1][2] to get and set values. If you use y.at(1).at(2) instead you get proper checking so that you get an exception if you read or write out of bounds.



来源:https://stackoverflow.com/questions/11725844/problems-with-int

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!