J
Josh
Howdy
i was recently given a program to do. I have to create a 2d matrix
with pointers i have the whole idea down with pointers but there is a
problem with one of them i have the code written down at bottom so any
help will help. The problem is that after it allocates memory for the
array of pointers it seems like it loops an extra time, giving it one
more element. Thanks
int **ptr1, *matrixInfo;
int size;
cout << "Enter the number of columns.\n";
cin >> size;
ptr1 = new int*[size];
matrixInfo = new int[size+1];
matrixInfo[0] = size;
for(int i = 1; i < size; i++){ //creates matrixinfo
matrixInfo = ((((rand()%size)*2)%8)+2);
}
system("PAUSE");
for(int k = 0; k < (size+1); k++){ //displays
matrixinfo
cout << matrixInfo[k];
}
for (int s = 0; s <= size; s++)
{
ptr1 = new int[matrixInfo[s+1]]; //allocating size of
pointer
for (int w = 0; w <= matrixInfo[w+1]; w++){
ptr1[w] = rand()%size*2%8+2; //storing random values
cout << ptr1[w] <<endl;
}
cout << endl;
}
cout << endl;
for(int j = 1; j <= size ; j++){
for(int i = 0; i <= matrixInfo[j]; i++){ //display values of
ptr
cout <<ptr1[j] << endl;
}
cout << endl;
}
i was recently given a program to do. I have to create a 2d matrix
with pointers i have the whole idea down with pointers but there is a
problem with one of them i have the code written down at bottom so any
help will help. The problem is that after it allocates memory for the
array of pointers it seems like it loops an extra time, giving it one
more element. Thanks
int **ptr1, *matrixInfo;
int size;
cout << "Enter the number of columns.\n";
cin >> size;
ptr1 = new int*[size];
matrixInfo = new int[size+1];
matrixInfo[0] = size;
for(int i = 1; i < size; i++){ //creates matrixinfo
matrixInfo = ((((rand()%size)*2)%8)+2);
}
system("PAUSE");
for(int k = 0; k < (size+1); k++){ //displays
matrixinfo
cout << matrixInfo[k];
}
for (int s = 0; s <= size; s++)
{
ptr1
pointer
for (int w = 0; w <= matrixInfo[w+1]; w++){
ptr1
cout << ptr1
}
cout << endl;
}
cout << endl;
for(int j = 1; j <= size ; j++){
for(int i = 0; i <= matrixInfo[j]; i++){ //display values of
ptr
cout <<ptr1[j] << endl;
}
cout << endl;
}