MAX_value in 2d array for each column - c++

What is wrong with that code?
Sorry, if here bad code.
I'm need to find max value for all column in 2d dimensional of float values, then find them sum.
First of all I write values for Array, then display them at screen and tried to find max values for each column.
Photo: didn't working and correctly work
___________________
[5.7 ; 4.2 ; 5.8;]
[654.87; 5.86; 3.76;] - Work correctly
[8.54; 7.54; 8.4;]
------------------
Max value of 1 column = A[2,1] = 654.87;
Max value of 2 column = A[3,2] = 7.54;
Max value of 3 column = A[3,3] = 8.4;
___________________
[4.6 ; 2.65 ; 76.3;]
[65.64; 7.32; 76.2;] - Work not correctly
[654.8; 1.6; 5.7;]
------------------
Max value of 1 column = A[3,1] = 654.8;
Max value of 2 column = A[3,2] = 7.32;
Max value of 3 column = A[3,3] = 5.7;
#include<iostream>
#include<conio.h>
int main(void)
{
system("cls");
int N;
int suma = 0;
A:
std::cout<<"Write array size N x N : ";
std::cin>>N;
if(N>10 || N<=1)
{
system("cls");
std::cout<<"N must be <= 10 and > 1;"<<std::endl;
goto A;
}
float **A = new float *[N];
for(int i = 0; i < N; i++)
{
A[i] = new float [N];
for(int j = 0; j < N; j++)
{
std::cout<<"A["<<i+1<<"][";
std::cout<<j+1<<"] = ";
std::cin>>(*(*(A+i)+j));
}
}
system("cls");
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
if(j<N-1)
{
std::cout<<"A["<<i+1<<"]["<<j+1<<"] = "<<*(*(A+i)+j);
std::cout<<"; ";
}
else
{
std::cout<<"A["<<i+1<<"]["<<j+1<<"] = "<<*(*(A+i)+j);
std::cout<<";"<<std::endl;
}
}
}
float *max = new float [N];
std::cout<<"------------------------------------";
std::cout<<std::endl;
for(int i = 0; i < N - (N - 1); i++)
{
for(int j = 0; j < N; j++)
{
*(max+j) = *(*(A+i)+j);
}
}
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
if( (*(max+i)) < (*(*(A+j))+i) &&
(*(max+i)) != ((*(*(A+j)))+i) )
{
*(max+i) = *(*(A+j)+i);
}
}
}
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
if( (*(*(A+j)+i)) == (*(max+i)) )
{
std::cout<<"Max value of "<<i+1;
std::cout<<" column = ";
std::cout<<"A["<<j+1<<"]["<<i+1;
std::cout<<"] = "<<(*(max+i));
std::cout<<std::endl;
}
}
suma+=(*(max+i));
}
std::cout<<"Sum of largest value = "<<suma;
_getch();
}

Oh... sorry just a mistake in if() by which i losen more than 3.5 hour...
if( (*(max+i)) < (*(*(A+j))+i)
&&
(*(max+i)) != ((*(*(A+j)))+i) )
{...}
I changed it to:
if( (*(max + i)) < (*(*(A + j) + i))
&&
(*(max + i)) != (*(*(A + j) + i))
)
{...}
After that change all work properly

Related

How do I find errors in a recursive function?

I'm trying to find the smallest of the biggest sum of each column of every possible permutations of a given 2D array NxN, where the values in each row can shift towards the left. For example, the array
4 6
3 7
would have 4 possibles permutations:
4 6 6 4 4 6 6 4
3 7 3 7 7 3 7 3
The biggest sum of each permutation is respectively, 13, 11, 11, 13. Thus the smallest of the biggest sums is 11. I have written a recursive function that should work, but for some reason, it only works for arrays that are smaller than 6x6... I'm new at programming, and just recently learned about recursion, any help or counsel on how to think recursively and to debug code would be greatly appreciated...
For the array 4x4
7410 1371 2665 3195
4775 4130 6499 3414
300 2092 4009 7638
5351 210 7225 7207
The answer is 18349, and my code gives me the correct answer.
However, for the array 6x6
5219 842 7793 2098 5109 2621
1372 3253 3804 5652 810 1620
4894 6792 1784 4335 4772 6656
3203 1070 4716 5335 1157 6855
5529 2767 2205 408 7516 7454
375 7036 2597 5288 937 2893
The answer should be 23733, but I've got 24176. How is this possible?
Here's my code:
#include <iostream>
using namespace std;
#define MAX_N 1000
int n, matrix[MAX_N][MAX_N], shift[MAX_N] = {0}, minSum = 100000000;
void possibTree(int position){
//Base case
if(position == n){
for (int i = 0; i < n; i++) {
// Temporary array to store the values in the row that just shifted towards the left
int temp[MAX_N] = {0};
for (int j = 0; j < n; j++) {
if(j - shift[i] < 0)
temp[n+(j-shift[i])] = matrix[i][j];
else
temp[j-shift[i]] = matrix[i][j];
}
for (int k = 0; k < n; k++)
matrix[i][k] = temp[k];
}
int max = 0;
for (int i = 0; i < n; i++) {
int temp = 0;
for (int j = 0; j < n; j++) {
temp += matrix[j][i];
}
if(temp > max)
max = temp;
}
if(minSum > max)
minSum = max;
return;
}
for (int i = 0; i < n; i++) {
shift[position] = i;
possibTree(position+1);
}
return;
}
int main() {
while(cin >> n){
memset(matrix, 0, sizeof(matrix));
memset(shift, 0, sizeof(shift));
if(n == -1) // The user enters "-1" to end the loop and terminate the program.
return 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> matrix[i][j];
}
}
possibTree(0);
cout << minSum << endl;
minSum = 100000000;
}
return 0;
}
Ok I believe I understand my mistake, I have to reset the matrix to its original state at the end of each base case, when the matrices are small, the code is still capable of finding all the possible biggest sums, but when the matrices got bigger, some of the possibilities weren't generated. Here's my code:
#include <iostream>
using namespace std;
#define MAX_N 1000
int n, matrix[MAX_N][MAX_N], OrigMatrix[MAX_N][MAX_N], shift[MAX_N] = {0}, minSum = 100000000;
void possibTree(int position){
//Base case
if(position == n){
for (int i = 0; i < n; i++) {
// Temporary array to store the values in the row that just shifted towards the left
int temp[MAX_N] = {0};
for (int j = 0; j < n; j++) {
if(j - shift[i] < 0)
temp[n+(j-shift[i])] = matrix[i][j];
else
temp[j-shift[i]] = matrix[i][j];
}
for (int k = 0; k < n; k++)
matrix[i][k] = temp[k];
}
int max = 0;
for (int i = 0; i < n; i++) {
int temp = 0;
for (int j = 0; j < n; j++) {
temp += matrix[j][i];
}
if(temp > max)
max = temp;
}
if(minSum > max)
minSum = max;
//EDITS
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = OrigMatrix[i][j];
}
}
return;
}
for (int i = 0; i < n; i++) {
shift[position] = i;
possibTree(position+1);
}
return;
}
int main() {
while(cin >> n){
memset(matrix, 0, sizeof(matrix));
memset(shift, 0, sizeof(shift));
if(n == -1) // The user enters "-1" to end the loop and terminate the program.
return 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> matrix[i][j];
}
}
//EDITS
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
OrigMatrix[i][j] = matrix[i][j];
}
}
possibTree(0);
cout << minSum << endl;
minSum = 100000000;
}
return 0;
}

How do i raise the user input matrix to the nth power using pointers, now i am only able to calculate to the power of 2

I am trying to raise the user input M X M matrix by nth power, but i am only able to raise the matrix to the power of 2. if i input any other number higher than 2 it only gives the matrix raise to the power of 2. How do i do it using pointers ?
int main() {
int M = 0;
int N = 0;
printf("Enter the size of R: ");
scanf("%d",&M);
printf("Enter the value of power N: ");
scanf("%d",&N);
int R[M][M]; //maximum dimension = 5
int result[M][M];
int newR[M][M];
if (M < 1 || M > 5)
{
printf("Error: Size of M must stay between 1 to 5.\n");
}
else
{
for (int i = 0; i < M; i++){
for (int j = 0; j < M; j++){
printf("Enter data in R(%d,%d): ",i+1,j+1);
scanf("%d",&R[i][j]);
}
}
for (int i = 0; i < M; i++){
for (int j = 0; j < M; j++){
printf("R(%d,%d) = %d\n",i+1,j+1,newR[i][j]);
}
}
}
int sum = 0;
for (int i = 0; i < N; i++)
{
for ( int row = 0 ; row < M ; row++ )
{
for (int col = 0 ; col < M ; col++ )
{
for (int count = 0 ; count < M ; count++ )
{
sum += R[row][count]*R[count][col];
}
result[row][col] = sum;
printf("%d\n",result[row][col]);
sum = 0;
}
}
for (int row = 0; row < M; row++)
{
for (int col = 0 ; col < M ; col++ )
{
newR[row][col] = result[row][col];
// R[row][col] = 0;
}
}
}
for (int i = 0; i < M; i++){
for (int j = 0; j < M; j++){
printf("newR(%d,%d) = %d\n",i+1,j+1,newR[i][j]);
}
}
return 0;
}```

How do you find the location of a number in a sequence?

Say that I have a sequence:
int seq[4][4];
Then, lets say seq[1][2]=8;
No other values of the sequence yields 8.
If I want to find the values of a sequence and print out which one it is, (e.g. 1,2 and make x=1 and y=2) how can I do that? What
int x,j;
for (int i = 0; i < 4; i++) // looping through row
{
for(int j = 0; j < 4; j++) //looping through column
{
if (seq[i][j] == 8) //if value matches
{
x = i; y = j; //set value
i = 4; //set i to 4 to exit outer for loop
break; //exit inner for loop
}
}
}
int numberBeingSearchedFor = *Any Value Here*;
int array[*numRows*][*numColumns*];
int firstOccuranceRow = -1, firstOccuranceColumn = -1;
for(int i = 0; i < numRows; ++i)
{
for(int j = 0; j < numColumns; ++j)
{
if(array[i][j] == numberBeingSearchedFor)
{
firstOccuranceRow = i;
firstOccuranceColumn = j;
i = numRows; //Credit to other answer, I've never seen that :) It's cool
break;
}
}
}
if(firstOccuranceRow == -1 || firstOccuranceColumn == -1)
{
//Item was not in the array
}

dijkstra's algorithm using matrix in c++. what's wrong in this code?

#include <iostream>
int n, m, v1, v2, weight;
cin >> n >> m;
int** graph = new int*[n];
int* distance = new int[n];
int* s = new int[n];
for (int i = 0; i < n; ++i)
graph[i] = new int[n];
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
graph[i][j] = INT_MAX;
for (int i = 0; i < m; ++i)
{
cin >> v1 >> v2 >> weight;
graph[v1][v2] = weight;
graph[v2][v1] = weight;
}
for (int i = 0; i < n; ++i)
distance[i] = INT_MAX;
for (int i = 0; i < n; ++i)
distance[i] = graph[0][i];
for (int i = 0; i < n; ++i)
s[i] = 0;
distance[0] = 0;
int min = INT_MAX;
int vertex = 0;
for (int j = 0; j < n-1; ++j){
min = INT_MAX;
for (int i = 0; i < n; ++i)
if (s[i] == 0 && min >= distance[i])
{
vertex = i;
min = distance[i];
}
s[vertex] = 1;
cout << vertex << " ";
for (int i = 0; i < n; ++i)
if (distance[i]>distance[vertex] + graph[vertex][i])
distance[i] = distance[vertex] + graph[vertex][i];
}
for (int i = 0; i < n; ++i)
cout << distance[i] << " ";
cout << endl;
return 0;
}
Hi. I'm making Dijkstra's algorithm using two-dimentional matrix..
but this code doesn't work. and i don't know why! Can you fix my problem??
i want to make output all distance of graph. but output is looks like array point garbage value like -2345...
Can you help me??
There are some problems in this loop:
for (int i = 0; i < n; ++i)
if (distance[i]>distance[vertex] + graph[vertex][i])
distance[i] = distance[vertex] + graph[vertex][i];
should change to the blew code:
for (int i = 0; i < n; ++i)
if (s[i] == 0 && graph[vertex][i] != INT_MAX && distance[i]>distance[vertex] + graph[vertex][i])
distance[i] = distance[vertex] + graph[vertex][i];
because if the graph[vertex][i] == INT_MAX, the sum of distance[vertex] + graph[vertex][i] is overflow. Another problem is that the vertex i should not be marked before.

Find a subarray of m*m (2<=m<n) having largest sum; out of an n*n int array(having +ve, -ve, 0s)

I have written a solution for the above problem but can someone please suggest an optimized way.
I have traversed through the array for count(2 to n) where count is finding subarrays of size count*count.
int n = 5; //Size of array, you may take a dynamic array as well
int a[5][5] = {{1,2,3,4,5},{2,4,7,-2,1},{4,3,9,9,1},{5,2,6,8,0},{5,4,3,2,1}};
int max = 0;
int **tempStore, size;
for(int count = 2; count < n; count++)
{
for(int i = 0; i <= (n-count); i++)
{
for(int j = 0; j <= (n-count); j++)
{
int **temp = new int*[count];
for(int i = 0; i < count; ++i) {
temp[i] = new int[count];
}
for(int k = 0; k < count; k++)
{
for(int l = 0; l <count; l++)
{
temp[k][l] = a[i+k][j+l];
}
}
//printing fetched array
int sum = 0;
for(int k = 0; k < count; k++)
{
for(int l = 0; l <count; l++)
{
sum += temp[k][l];
cout<<temp[k][l]<<" ";
}cout<<endl;
}cout<<"Sum = "<<sum<<endl;
if(sum > max)
{
max = sum;
size = count;
tempStore = new int*[count];
for(int i = 0; i < count; ++i) {
tempStore[i] = new int[count];
}
//Locking the max sum array
for(int k = 0; k < count; k++)
{
for(int l = 0; l <count; l++)
{
tempStore[k][l] = temp[k][l];
}
}
}
//printing finished
cout<<"------------------\n";
//Clear temp memory
for(int i = 0; i < size; ++i) {
delete[] temp[i];
}
delete[] temp;
}
}
}
cout<<"Max sum is = "<<max<<endl;
for(int k = 0; k < size; k++)
{
for(int l = 0; l <size; l++)
{
cout<<tempStore[k][l]<<" ";
}cout<<endl;
}cout<<"-------------------------";
//Clear tempStore memory
for(int i = 0; i < size; ++i) {
delete[] tempStore[i];
}
delete[] tempStore;
Example:
1 2 3 4 5
2 4 7 -2 1
4 3 9 9 1
5 2 6 8 0
5 4 3 2 1
Output:
Max sum is = 71
2 4 7 -2
4 3 9 9
5 2 6 8
5 4 3 2
This is a problem best solved using Dynamic Programming (DP) or memoization.
Assuming n is significantly large, you will find that recalculating the sum of every possible combination of matrix will take too long, therefore if you could reuse previous calculations that would make everything much faster.
The idea is to start with the smaller matrices and calculate sum of the larger one reusing the precalculated value of the smaller ones.
long long *sub_solutions = new long long[n*n*m];
#define at(r,c,i) sub_solutions[((i)*n + (r))*n + (c)]
// Winner:
unsigned int w_row = 0, w_col = 0, w_size = 0;
// Fill first layer:
for ( int row = 0; row < n; row++) {
for (int col = 0; col < n; col++) {
at(r, c, 0) = data[r][c];
if (data[r][c] > data[w_row][w_col]) {
w_row = r;
w_col = c;
}
}
}
// Fill remaining layers.
for ( int size = 1; size < m; size++) {
for ( int row = 0; row < n-size; row++) {
for (int col = 0; col < n-size; col++) {
long long sum = data[row+size][col+size];
for (int i = 0; i < size; i++) {
sum += data[row+size][col+i];
sum += data[row+i][col+size];
}
sum += at(row, col, size-1); // Reuse previous solution.
at(row, col, size) = sum;
if (sum > at(w_row, w_col, w_size)) { // Could optimize this part if you only need the sum.
w_row = row;
w_col = col;
w_size = size;
}
}
}
}
// The largest sum is of the sub_matrix starting a w_row, w_col, and has dimensions w_size+1.
long long largest = at(w_row, w_col, w_size);
delete [] sub_solutions;
This algorithm has complexity: O(n*n*m*m) or more precisely: 0.5*n*(n-1)*m*(m-1). (Now I haven't tested this so please let me know if there are any bugs.)
Try this one (using naive approach, will be easier to get the idea):
#include <iostream>
#include<vector>
using namespace std;
int main( )
{
int n = 5; //Size of array, you may take a dynamic array as well
int a[5][5] =
{{2,1,8,9,0},{2,4,7,-2,1},{5,4,3,2,1},{3,4,9,9,2},{5,2,6,8,0}};
int sum, partsum;
int i, j, k, m;
sum = -999999; // presume minimum part sum
for (i = 0; i < n; i++) {
partsum = 0;
m = sizeof(a[i])/sizeof(int);
for (j = 0; j < m; j++) {
partsum += a[i][j];
}
if (partsum > sum) {
k = i;
sum = partsum;
}
}
// print subarray having largest sum
m = sizeof(a[k])/sizeof(int); // m needs to be recomputed
for (j = 0; j < m - 1; j++) {
cout << a[k][j] << ", ";
}
cout << a[k][m - 1] <<"\nmax part sum = " << sum << endl;
return 0;
}
With a cumulative sum, you may compute partial sum in constant time
std::vector<std::vector<int>>
compute_cumulative(const std::vector<std::vector<int>>& m)
{
std::vector<std::vector<int>> res(m.size() + 1, std::vector<int>(m.size() + 1));
for (std::size_t i = 0; i != m.size(); ++i) {
for (std::size_t j = 0; j != m.size(); ++j) {
res[i + 1][j + 1] = m[i][j] - res[i][j]
+ res[i + 1][j] + res[i][j + 1];
}
}
return res;
}
int compute_partial_sum(const std::vector<std::vector<int>>& cumulative, std::size_t i, std::size_t j, std::size_t size)
{
return cumulative[i][j] + cumulative[i + size][j + size]
- cumulative[i][j + size] - cumulative[i + size][j];
}
live example