I need to implement a 5x5 dynamic array where
every element in it is equal to the sum of its two indices. For example, the first element, at (0,0), has the value 0+0=0.
Here is my code:
# include<iostream>
using namespace std;
int main()
{
int size =5;
int *array=new int[size];
for (int i = 0; i < size; i++)
delete [] array;
return 0;
}
I need help to implement sum of index.
You need at first to implement a two-dimensional array.:)
Here you are.
#include <iostream>
int main()
{
const size_t N = 5;
int ( *array )[N] = new int[N][N];
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < N; j++ ) array[i][j] = i + j;
}
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < N; j++ ) std::cout << array[i][j] << ' ';
std::cout << std::endl;
}
delete [] array;
return 0;
}
And do not pay attention that the answer is down voted. There is nothing wrong with the answer. :)
Firstly, you should create a 2d-array, not just a array.
void foo() {
int **a = new int*[5];
for (int i = 0; i < 5; i++)
a[i] = new int[5];
}
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
a[i][j] = i + j;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++)
cout << a[i][j] << " ";
cout << endl;
}
for (int i = 0; i < 5; i++)
delete[] a[i];
delete[] a;
}
And, of course, don't forget to clear your memory)
Related
I want to delete column with max integer in 2d array, I do it in this way, but why is deleting the column and also row? Can I fix that and delete only column? The task was do it with delete command, but now I think it's impossible
#include <iostream>
using namespace std;
int main()
{
int row = 3, col = 3;
int** arr = new int* [row];
for(int i = 0; i < row; i++){
arr[i] = new int[col];
}
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++) {
cin >> arr[i][j];
}
}
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
cout << " ------------- " << endl;
int max = 0, index = 0;
for(int i =0; i < row; i++){
for(int j = 0; j < col; j++){
if(arr[i][j] > max){
max = arr[i][j];
index = i;
}
}
}
delete [] arr[index];
int** tmp = new int*[index - 1];
int tmpI = 0;
for(int i = 0; i < col; i++){
if(i != index){
tmp[tmpI++] = arr[i];
}
}
delete [] arr;
arr = tmp;
col = col - 1;
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
}
For starters the variable index is set to a row number
index = i;
Then this row is deleted
delete [] arr[index];
But you are going to remove a column instead of a row. So this code does not make a sense.
Also you are incorrectly searching the maximum element. If the user will enter all negative values then the maximum value will be equal to 0 though an element with such value is not present in the array.
In this declaration
int** tmp = new int*[index - 1];
you allocated an array with rows that one less than the number of rows in the original array. Moreover if index is equal to 0 then there is allocated a very large extent of memory.
This statement
delete [] arr;
produces a memory leak.
It seems what you need is something like the following
#include <iostream>
int main()
{
size_t row = 3, col = 3;
int **arr = new int * [row];
for ( size_t i = 0; i < row; i++ )
{
arr[i] = new int[col];
}
for ( size_t i = 0; i < row; i++ )
{
for ( size_t j = 0; j < col; j++ )
{
std::cin >> arr[i][j];
}
}
for ( size_t i = 0; i < row; i++ )
{
for ( size_t j = 0; j < col; j++ )
{
std::cout << arr[i][j] << " ";
}
std::cout << std::endl;
}
std::cout << "------------- " << std::endl;
size_t max_i = 0, max_j = 0;
for ( size_t i = 0; i < row; i++ )
{
for ( size_t j = 0; j < col; j++ )
{
if ( arr[max_i][max_j] < arr[i][j] )
{
max_i = i; max_j = j;
}
}
}
int **tmp = new int*[row];
for ( size_t i = 0; i < row; i++ )
{
tmp[i] = new int[col-1];
}
for ( size_t i = 0; i < row; i++ )
{
for ( size_t j = 0, k = 0; j < col; j++ )
{
if ( j != max_j ) tmp[i][k++] = arr[i][j];
}
}
for ( size_t i = 0; i < row; i++ )
{
delete [] arr[i];
}
delete [] arr;
arr = tmp;
--col;
for ( size_t i = 0; i < row; i++ )
{
for ( size_t j = 0; j < col; j++ )
{
std::cout << arr[i][j] << " ";
}
std::cout << std::endl;
}
for ( size_t i = 0; i < row; i++ )
{
delete [] arr[i];
}
delete [] arr;
return 0;
}
The program output might look like
1 2 3
6 5 4
7 9 8
-------------
1 3
6 4
7 8
Here's an alternate suggestion: You are storing the data in row x column format. If you change to column x row format, it becomes easier to delete a column. I also made a helper function to print the matrix and I combined the input loop and the loop that finds the max.
#include <iostream>
#include <iomanip>
#include <cstdlib> // for rand
#include <climits>
using namespace std;
void print_matrix(int** arr, int rows, int columns)
{
for(int row = 0; row < rows; row++){
for(int col = 0; col < columns; col++) {
cout << setw(2) << arr[col][row] << " ";
}
cout << "\n";
}
}
int main()
{
srand(time(nullptr)); // For testing
int rows = 3, columns = 3;
// Create the matrix
int** arr = new int *[columns];
for (int col = 0; col < columns; col++) {
arr[col] = new int[rows];
}
// Input values - finding max, too
int max_value = INT_MIN, max_value_column = -1;
for (int row = 0; row < rows; row++) {
for (int col = 0; col < columns; col++) {
//cin >> arr[col][row];
arr[col][row] = rand() % 50; // Using rand for testing.
if (arr[col][row] > max_value) {
max_value = arr[col][row];
max_value_column = col;
}
}
}
print_matrix(arr, rows, columns);
cout << " ------------- " << endl;
// Delete the column with max
delete [] arr[max_value_column];
columns--;
// Shift columns to the right of the deleted column left one
for (int col = max_value_column; col < columns; col++) {
arr[col] = arr[col + 1];
}
print_matrix(arr, rows, columns);
return 0;
}
The problem with my code is that it is not identifying my function, I am not sure if the function is incorrect or written with the wrong syntax. What I have tried is to create a new array for the location of the largest index but it doesn't seem to work.
#include <iostream>
#include <iomanip>
using namespace std;
void locateLargest(const double a[][4], int location[]);
const int ROW_SIZE = 3;
const int COLUMN_SIZE = 4;
int main(){
int location [ROW_SIZE][COLUMN_SIZE];
double matrix [ROW_SIZE][COLUMN_SIZE];
double input;
cout<<"Enter the array: "<< endl;
for (int i = 0; i < ROW_SIZE; i++){
for(int j = 0; j < COLUMN_SIZE; j++){
cin>>input;
matrix[i][j] = input;
}
}
for(int i = 0; i < ROW_SIZE; i++){
for(int j = 0; j < COLUMN_SIZE; j++){
cout<< setw(4)<<matrix[i][j]<< " ";
}
cout<< endl;
}
locateLargest(matrix, location)
}
You can keep track of the max value's indices while iterating through the matrix.
void max_idx(const double (&arr)[RS][CS]) {
double curr_max = arr[0][0];
size_t max_i = 0, max_j = 0;
for (size_t i = 0; i < RS; ++i) {
for (size_t j = 0; j < CS; ++j) {
if (curr_max < arr[i][j]) {
curr_max = arr[i][j];
max_i = i;
max_j = j;
}
}
}
cout << "Largest value is at (i=" << max_i << ", j=" << max_j << ")\n";
}
Demo
First of all, you have to make sure that your code is consistent : in the prototype of your locateLargest function, location is a one-dimensional array but in your main() function it is a two-dimensional one.
This is how I would write this :
#include <iostream>
#include <iomanip>
using namespace std;
void locateLargest(double** a, int* location);
const int ROW_SIZE = 3;
const int COLUMN_SIZE = 4;
int main()
{
int location [2];
double* matrix [ROW_SIZE];
for(int s= 0; s< ROW_SIZE; s++)
{
matrix[s]= new double[COLUMN_SIZE];
}
double input;
cout<<"Enter the array: "<< endl;
for (int i = 0; i < ROW_SIZE; i++)
{
for(int j = 0; j < COLUMN_SIZE; j++)
{
cin>>input;
matrix[i][j] = input;
}
}
for(int i = 0; i < ROW_SIZE; i++)
{
for(int j = 0; j < COLUMN_SIZE; j++)
{
cout<< setw(4)<<matrix[i][j]<< " ";
}
cout<< endl;
}
locateLargest(matrix, location);
}
void locateLargest(double** a, int* location)
{
int i, j;
double maxVal= a[0][0]; location[0]= location[1]= 0;
for(i = 0;i < ROW_SIZE; i++)
{
for(j = 0; j < COLUMN_SIZE; j++)
{
if(maxVal < a[i][j])
{
location[0] = i;
location[1]= j;
maxVal= a[i][j];
}
}
}
cout << "The location of the largest element is at ("<< location[0] << " , "<<
location[1] <<" ) . it is : "<< maxVal<<endl;
}
max represents the maximum value of your matrix's elements, you first set it to be equal to the first element and then compare it to each element of the matrix. Each time you find an element that is larger than max, you assign his value to max and his position to location and at the end of the iterations, you have the largest value and his location.
Hey I am beginner at C++ programming. I have made a program that is meant to add two 2D arrays together. However, The program outputs the values until the program crashes. Can someone help me to identify the problem?
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
int a[10][10], c[10][10], i, j;
for (i = 1; i <= 10; ++i)
{
for(j=0; j < 10; ++j)
{
a[i][j] = i * j;
}
}
// We are able to treat the individual columns as arrays
for (int i = 0; i < 10; ++i)
{
int *b = a[i];
for (int j = 0; j < 10; ++j)
{
cout << b[j] << " ";
}
cout << endl;
}
cout << "****" << endl;
// Declare a multidimensional array on the heap
int **b = new int*[10];
// need to allocate all members individually
for (int i = 0; i < 10; ++i)
{
b[i] = new int[10];
}
// Set the values of b
for (int i = 0; i < 10; ++i)
{
for (j = 0; j < 10; ++j)
{
b[i][j] = (i * 10) + j;
}
}
for (i = 0; i < 10; ++i)
{
for (j = 1; j <= 10; ++j)
{
c[i][j] = a[i][j] + b[i][j];
}
}
for (i = 0; i < 10; ++i)
{
for (j = 1; j <= 10; ++j)
{
cout << c[i][j] << endl;
}
}
// Delete the multidimensional array - have to delete each part
for (int i = 0; i < 10; ++i)
{
delete[] b[i];
}
delete[] b;
return 0;
}
I corrected your code.Now, It's working and program didn't crash. You can try it out.
#include<conio.h>
#include<iostream.h>
int main(int argc, char** argv)
{
int a[10][10], c[10][10], i, j;
for (i = 0; i <10; ++i)
{
for(j=0; j < 10; ++j)
{
a[i][j] = i * j;
}
}
//We are able to treat the individual columns as arrays;
for (i = 0; i < 10; ++i)
{
int *b = a[i];
for (int j = 0; j < 10; ++j)
{
cout << b[j] << " ";
}
cout << endl;
}
cout << "****" << endl;
//Declare a multidimensional array on the heap;
int **b = new int*[10];
//need to allocate all members individually
for (i = 0; i < 10; ++i)
{
b[i] = new int[10];
}
//Set the values of b
for ( i = 0; i < 10; ++i)
{
for (j = 0; j < 10; ++j)
{
b[i][j] = (i * 10) + j;
}
}
for (i = 0; i < 10; ++i)
{
for (j = 0; j <10; ++j)
{
c[i][j] = a[i][j] + b[i][j];
}
}
for (i = 0; i < 10; ++i)
{
for (j = 0;j < 10; ++j)
{
cout << c[i][j] << " ";
}
cout<<endl;
}
// Delete the multidimensional array - have to delete each part
for (i = 0; i < 10; ++i)
{
delete[] b[i];
}
delete[] b;
return 0;
}
I make a 3d dynamic array by this using this code
//layer = 2
//levelSize.x = 100
//levelSize.y = 100
level_array = new int**[layer];
for(int i = 0; i < layer; ++i)
{
level_array[i] = new int*[(int)levelSize.x];
for(int j = 0; j < levelSize.x; ++j)
level_array[i][j] = new int[(int)levelSize.y];
}
but when I want to delete it, the program crashes
for(int i = 0; i != levelSize.x; ++i)
{
for(int j = 0; j != levelSize.y; ++j)
{
delete[] level_array[i][j];
}
delete[] level_array[i];
}
delete[] level_array;
I don't know where is wrong in the code of deleting an array.
Please help me check the code, Thanks
You allocate memory for array with dimensions [layer][levelSize.x][levelSize.y], but while deleting you operate with it like with array with dimensions [levelSize.x][levelSize.y][somenting].
for(int i = 0; i != layer; ++i)
// ^^^^^ not levelSize.x
{
for(int j = 0; j != levelSize.x; ++j)
// ^^^^^^^^^^^ not levelSize.y
{
delete[] level_array[i][j];
}
delete[] level_array[i];
}
delete[] level_array;
Add this command to the end, otherwise the pointer will be at the top of the stack.
level_array = nullptr;
it's so important to make difference between deleting a single dim array and multi-dim allocated on the heap:
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int*** ptrInt = new int**[3];
for(int i(0); i < 3; i++)
ptrInt[i] = new int*[3];
for(int i = 0; i < 3; i++)
{
for(int j(0); j < 3; j++)
ptrInt[i][j] = new int[3];
}
for(int i = 0; i < 3; i++)
{
for(int j(0); j < 3; j++)
for(int k(0); k < 3; k++)
ptrInt[i][j][k] = k;
}
for(int i = 0; i < 3; i++)
{
for(int j(0); j < 3; j++)
for(int k(0); k < 3; k++)
cout << "ptrInt[" << i << "][" << j << "][" << k << "]: " << ptrInt[i][j][k] << endl;
}
// now freeing memory:
for(int i = 0; i < 3; i++)
{
for(int j(0); j < 3; j++)
delete[] ptrInt[i][j];
delete[] ptrInt[i];
}
delete[] ptrInt;
cout << endl << endl << endl;
return 0;
}
int main (void)
{
int** arr = new int*[4];
for (int i = 0; i < 4; i++) arr[i] = new int[4] {1, 0, 0, 1};
const int* p = &(arr[0][0]);
TFigure* test = new TFigure(arr, 4, 4);
test->resolve();
for (int i = 0; i < 4; i++) delete[] arr[i];
delete[] arr;
return 0;
}
where constructor declaration is
line 57:
TFigure(int **ia, int n, int m)
N = n;
M =m;
landscape = new int*[n];
puddles = new int*[n];
for (int i = 0; i < n; i++){
landscape[i] = new int[m];
puddles[i] = new int[n];
for (int j = 0; j < m; j++)
landscape[i][j] = *ia[i][j];
}
for (int i = 0; i < n; i++)
for (int j = 0; j < 0; j++)
if (i == 0 || i == N || j == 0 || j == M)
puddles[i][j] = 0;
else
puddles[i][j] = 1;
for (int i = 0; i < N; i++){
for (int j = 0; j < M; j++)
std::cout << puddles[i][j] << ' ';
std::cout << std::endl;
}
for (int i = 0; i < N; i++){
for (int j = 0; j < M; j++)
std::cout << landscape[i][j] << ' ';
std::cout << std::endl;
}
};
but I have an error
57:43: error: invalid type argument of unary «*» (have «int»)
I don't understand what causes this.
The problem is with this line:
landscape[i][j] = *ia[i][j];
ia[i][j] gives you an int which you then try to dereference. It seems like you really just want:
landscape[i][j] = ia[i][j];
I'm not sure if this was a mistake when copy and pasting or not, but your constructor definition is missing an opening {.
TFigure(int **ia, int n, int m) {
// Here ^