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;
}
Related
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.
What's wrong with my code
I Have to add all the edge values of the 2D Array.
The Function is not returning what I want. Its returning garbage value.
Program Req Output : Add all the edges value of the 2D Array and return its answer.
You are not allowed to use Vector etc.
Here is my Code :
#include <iostream>
using namespace std;
const int rows = 3;
const int cols = 4;
void AddEdges (int arr[rows][cols])
{
int sum = 0;
int totalOfEdges = 0;
int i,j;
int k = 0;
int l = 0;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= cols; j++)
{
if (i == 1 || i == rows || j == 1 || j == cols)
{
sum = arr[k][l];
totalOfEdges = sum + totalOfEdges;
}
l++;
}
k++;
}
cout << "Total of Edges = " << totalOfEdges << endl;
}
int main()
{
int arr[3][4] = {0};
cout << "Enter Elements for Array = \n";
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cin >> arr[i][j];
}
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
AddEdges(arr);
}
My Output :
Leaving the Correct Code in Case Any one need it .
#include <iostream>
using namespace std;
const int rows = 3;
const int cols = 4;
void AddEdges (int arr[rows][cols])
{
int sum = 0;
int totalOfEdges = 0;
int i,j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
if (i == 0 || i == (rows-1) || j == 0 || j == (cols-1))
{
sum = arr[i][j];
totalOfEdges = sum + totalOfEdges;
}
}
}
cout << "Total of Edges = " << totalOfEdges << endl;
}
int main()
{
int arr[3][4] = {0};
cout << "Enter Elements for Array = \n";
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cin >> arr[i][j];
}
}
cout << "--------" << endl;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
AddEdges(arr);
}
I have a two-dimensional array A. I need
the elements of the array are written to the elements of the dynamic array elements record the calculated sum of the row of the array A. The dynamic array elements are displayed using pointers.
const unsigned row = 3;
const unsigned col = 4;
int A[row][col];
std::cout << "Input A:\n";
for (int index = 0; index < row; ++index)
{
for (int j = 0; j < col; ++j)
{
std::cout << "A[" << index << "][" << j << "]=";
std::cin >> A[index][j];
}
}
unsigned* array = new unsigned[row];
for (int index = 0; index < row; ++index)
{
for (int j = 0; j < col; ++j)
{
array[index] += A[index][j];
}
}
for (unsigned* index = array; *index; ++index)
{
std::cout << *index << "\n";
}
But alas, it does not work properly. Help me figure it out.
UPD:
unsigned* end = array + row + 1;
std::cout << "Array:\n";
for (unsigned *ptr = array, index = 1; ptr <= end; ++ptr, ++index)
{
std::cout << *ptr << "\t";
}
You never initialized the array so this line has undefined behavior:
array[index] += A[index][j];
You can simply fix if with:
for (int index = 0; index < row; ++index) {
array[index] = 0;
}
Or:
for (int index = 0; index < row; ++index) {
unsigned sum = 0;
for (int j = 0; j < col; ++j) {
sum += A[index][j];
}
array[index] = sum;
}
And the last output can be changed too. Your version reads out of bounds so yet another undefined behavior.
for (int index = 0; index < row; ++index) {
std::cout << array[index] << "\n";
}
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)
I made Matrix Multiplication program in c++ using dynamic Multidimensional arrays.
problem is when i enter test values
matrix A = row1{ 1 } , row2 { 2 } matrix B = row1 {1 ,2 , 3} , it stops working at the loop where user
inputs values of first array , i found it using debugging. but program works fine when i enter
matrix A = row1{ 1 , 2 } , row2 { 3 , 4 } matrix B = row1 {5 , 6 } , row2 {7 , 8}
i want this program to be a general program that can multiply all matrixs
#include <iostream>
using namespace std;
class Lab_02
{
public:
void Product(){
int a1Rows, a1Columns;
int a2Rows, a2Columns;
cout << "Plz Enter the no. of rows for Array 1 :";
cin >> a1Rows;
cout << "Plz Enter the no. of columns for Array 1 :";
cin >> a1Columns;
cout << "Plz Enter the no. of rows for Array 2 :";
cin >> a2Rows;
cout << "Plz Enter the no. of columns for Array 2 :";
cin >> a2Columns;
int **dynamicArray = 0;
int **dynamicArray2 = 0;
int **dynamicArray3 = 0;
cout << endl;
for (int i = 0; i < a1Rows; i++)
{
dynamicArray3 = new int *[a1Rows];
}
for (int i = 0; i < a2Columns; i++)
{
dynamicArray3[i] = new int[a2Columns];
}
// memory allocated for elements of rows.
for (int i = 0; i < a1Rows; i++)
{
dynamicArray = new int *[a1Rows];
}
// memory allocated for elements of each column.
for (int i = 0; i < a1Columns; i++)
{
dynamicArray[i] = new int[a1Columns];
}
// memory allocated for elements of rows.
for (int i = 0; i < a2Rows; i++)
{
dynamicArray2 = new int *[a2Rows];
}
// memory allocated for elements of each column.
for (int i = 0; i < a2Columns; i++)
{
dynamicArray2[i] = new int[a2Columns];
}
cout << "enter the values or array 1 \n";
for (int i = 0; i < a1Rows; i++)
{
for (int j = 0; j < a1Columns; j++)
{
cout << "array[" << i << "][" << j << "]\t";
cin >> dynamicArray[i][j];
}
}
cout << "enter the values or array 2 :\n";
for (int i = 0; i < a2Rows; i++)
{
for (int j = 0; j < a2Columns; j++)
{
cout << "array[" << i << "][" << j << "]\t";
cin >> dynamicArray2[i][j];
}
}
int sum;
for (int i = 0; i < a1Rows; i++)
{
for (int j = 0; j < a1Columns ; j++)
{
sum = 0;
for (int k = 0; k < a2Columns ; k++)
{
sum = sum + (dynamicArray[i][k] * dynamicArray2[k][j]);
}
dynamicArray3[i][j] = sum;
}
}
cout <<"Result" << endl << endl;
for (int i = 0; i < a1Rows; i++)
{
for (int j = 0; j < a2Columns; j++)
{
cout << dynamicArray3[i][j] << "\t";
}
cout << endl;
}
}
};
void main(void)
{
Lab_02 object;
object.Product();
}
Your memory allocations for the matricies are the issue.
Change them to something like the following
// memory allocated for elements of rows.
dynamicArray = new int *[a1Rows];
// memory allocated for elements of each column.
for (int i = 0; i < a1Rows; i++)
{
dynamicArray[i] = new int[a1Columns];
}
You need to allocate one array of array for the rows and then you need to loop over the rows and allocate the columns.
The issue with the code is that you are not supposed to allocate the "rows" in a loop. All you need is one allocation for the rows, and then loop to allocate the data for each row.
So for example, instead of this:
for (int i = 0; i < a1Rows; i++)
{
dynamicArray = new int *[a1Rows];
}
// memory allocated for elements of each column.
for (int i = 0; i < a1Columns; i++)
{
dynamicArray[i] = new int[a1Columns];
}
The correct way would be this:
dynamicArray = new int *[a1Rows];
for (int i = 0; i < a1Columns; i++)
{
dynamicArray[i] = new int[a1Columns];
}
You made the same mistake for each of the loops.
Also, some points:
You failed to deallocate the memory that was allocated.
If you used std::vector, then things become much easier.
You need to check if the matrices are multiplyable before performing the sum loop. By multiplyable meaning that the number of columns and rows of matrix A and B satisfy the requirements for multiplication of A and B.
for (int i = 0; i < a1Rows; i++)
{
for (int j = 0; j < a1Columns; j++)
{
sum = 0;
for (int k = 0; k < a2Columns; k++)
sum = sum + (dynamicArray[i][k] * dynamicArray2[k][j]);
dynamicArray3[i][j] = sum;
}
}
This loop will go haywire if the dynamicArray1 and dynamicArray2 do not have the requisite number of columns and rows before multiplying.
First, the following test should be done before multiplying:
if (a1Columns != a2Rows)
return;
Second, your k loop is wrong. It should be this:
for (int k = 0; k < a2Rows; k++)
sum = sum + (dynamicArray[i][k] * dynamicArray2[k][j]);
dynamicArray3[i][j] = sum;