I don't understand how this code find the rectangle or square where the sum of all integers are maximum, specially how the adding and subtracting working inside the for loop
if this approach or algorithm has a name, plz let me know
the code is-
#include <iostream>
#include <limits.h>
using namespace std;
int main()
{
int n=3,a[3][3]={-1,1,2,0,3,4,0,-1,0};
int i,j,k,l;
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
if(i>0) a[i][j]+=a[i-1][j];
if(j>0) a[i][j]+=a[i][j-1];
if(i>0&&j>0) a[i][j]-=a[i-1][j-1];
}
}
int ans=INT_MIN;
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
for(k=i; k<n; k++)
{
for(l=j; l<n; l++)
{
int temp=a[k][l];
if(i>0) temp-=a[i-1][j];
if(j>0) temp-=a[i][j-1];
if(i>0 && j>0) temp+=a[i-1][j-1];
if(temp>ans) ans=temp;
}
}
}
}
cout<<ans;
return 0;
}
Thanks
Related
My first question about CPP in Stackoverflow. I hope i get an answer.
#include <fstream>
using namespace std;
ifstream fin("Matr.dat");
ofstream fout("out.dat");
int a[1000][1000];
int b[10000];
int n, m;
void read()
{
fin>>n>>m;
for(int i=1; i<n; i++)
{
for(int j=1; j<m; j++)
fin>>a[i][j];
}
}
int vect()
{
int c=1;
for(int i=2; i<=n; i++)
{
for(int j=2; j<=m; j++)
{
b[c]=a[j]+a[j+1];
c++;
}
}
return c;
}
int main()
{
read();
int c=vect();
for(int i=0; i<c; i++)
fout<<b[i]<<' ';
return 0;
}
I and a colleague are trying to add the values of 2 columns into a vector.
For the line `b[c]=a[j]+a[j+1]; i receive an error saying that i use 2 incompatible types together. It is not working...
Can someone please help me add the values of 2 columns into a vector?
In the code below, my aim is to multiply two matrices reflect[3][3] and mat[3][s] where s can be any value 0-10. Here the statement (A) and (B) is not getting printed, please tell me why??
#include<iostream>
# include<math.h>
#include<conio.h>
using namespace std;
int mat[10][10];
int result[3][10];
int reflect[3][3]= {1,0,5,0,1,5,0,0,1};
int i , j,k,s;
void multiply_matrix(int A[3][3], int B[3][10])
{
for(i=0; i<3; i++)
for( j=0; j<10; j++)
result[i][j] = 0;
for (i = 0; i < 3; i++)
{
for (j = 0; j < s; j++)
{
result[i][j] = 0;
for (k = 0; k < 3; k++)
{
result[i][j]=result[i][j]+(A[i][k]*B[k][j]) ;
}
cout<<result[i][j]<<" ";//------(1)
}
cout<<endl;
}
cout<<"Multiplication after matrix: "<<endl;
for(i=0; i<3; i++)
{
for(j=0; j<s; j++)
{
cout<<result[i][j]<<" ";//------(B)
}
cout<<endl;;
}
}
int main()
{
int i, j,s;
cout<<"Enter the sides of polygon :\n";
cin>>s;
cout<<"Enter the coordinates of polygon :\n";
cout<<"Enter x coordinates ";
for(i=0; i<s; i++)
cin>>mat[0][i];
cout<<"Enter y coordinates ";
for(i=0; i<s; i++)
cin>>mat[1][i];
cout<<"\n\n";
for(i=0; i<s; i++)
mat[2][i] = 1;
cout<<"MAt: "<<endl;
for(i=0; i<3; i++)
{
for(j=0; j<s; j++)
{
cout<<mat[i][j]<<" ";
}
cout<<endl;
}
multiply_matrix(reflect, mat);
cout<<"End"<<endl;
return 0;
}
I have attached a sample output image for reference:
Output of code
I am a newbie and have tried various things but I am just not able to figure my mistake. Thanks in advance for your help.
You have two s in your code. Globally:
int i , j,k,s;
And in main:
int i, j,s;
The one in main you assign a value read from user input, but the global one is 0 always. multiply_matrix uses the global one, hence this loops have zero iterations:
for (j = 0; j < s; j++)
{
result[i][j] = 0;
for (k = 0; k < 3; k++)
{
result[i][j]=result[i][j]+(A[i][k]*B[k][j]) ;
}
cout<<result[i][j]<<" ";//------(1)
}
and
for(j=0; j<s; j++)
{
cout<<result[i][j]<<" ";//------(B)
}
Can you help me fix a bad access error please?
Here is the code:
#include <iostream>
using namespace std;
int main() {
int t,tr=0;
cin>>t;
while (tr<t) {
int n;
cin>>n;
int distance=n;
int number;
number=n*n;
int spiral[n][n];
for (int i=0;i<n;i++) {
for (int j=0; j<n; j++) {
spiral[i][j]=0;
}
}
for (int i=0; i<n;) {
for (int j=0; j<n;) {
spiral[i][j]=number;
number=number-1;
//cout<<"ij"<<endl;
for (int k=0; k<distance; k++) {
i++;
spiral[i][j]=number;
number--;
//cout<<"k"<<endl;
}
}
}
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
cout<<spiral[i][j];
}
cout<<endl;
}
tr++;
}
return 0;
}
Bad access is on
spiral[i][j]=number;
Here is the link for the problem but this is not important at the moment. I tried nszmobies but it didn't work so I'm asking you.
This is c++.
Here is the problem.
It seems that you have errors in your loops.
Loop
for (int j=0; j<n;)
looks as it is infinite because j variable isn't changing. Moreover variable i in
spiral[i][j]=number;
in your program can be greater or equal to n.
I have a simple sorting program being compiled by Dev-C++ 4.9.8.0. I ran the program (yes this compiles) and it simply stops after displaying the line where the vector is displayed for the first time. Note - it does not freeze, it seems to just be taking a pause. In the code, the selection sort comes next so I assume that the error happens there, but there is no error message for me to even figure out what to do!
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <cmath>
#include <ctime>
using namespace std;
void bubbleSort (vector<int>& data)
{
if(data.size() <= 1)
return;
int flag=1;
int temp;
for(int i=1; (i<=data.size()) && flag; i++)
{
flag=0;
for(int j=0; (j<data.size()-1); j++)
{
if(data[j+1] > data[j])
{
temp = data[j];
data[j] = data[j+1];
data[j+1] = temp;
flag=1;
}
}
}
}
void selectionSort(vector<int>& data)
{
int min, temp, n=data.size();
for (int i=0; i<n; i++)
{
min = i;
for (int j=i+1; j<n; j++)
{
if (j<min)
{
temp=i;
i=min;
min=temp;
}
}
}
}
int main()
{
int n;
vector<int> data;
cout<<"Vector length?: "<<endl;
cin>>n;
srand(time(0));
for (int i=0; i<n; i++)
{
data.push_back(rand()%20+1);
}
cout<<"Vector: ";
for (int i=0; i<data.size(); i++)
{
cout<<data[i]<<", ";
}
selectionSort(data);
cout<<"Sorted Vector: ";
for (int i=0; i<data.size(); i++)
{
cout<<data[i]<<", ";
}
system("Pause");
return 0;
}
selectionSort() method has variable 'n' that is completely a random value that happens to be on the stack at that location. You haven't initialized it!
You have a nested loop, which is O(n^2). Say n is 1982734 or some such arbitrarily large number. You are simply looping over 1982734 * 1982734 times. EVENTUALLY it will complete. Why don't you print the value of 'n' inside selectionSort(). Just initialize it with the size of the vector.
As others commented this whole work is in progress.
i need help din adding the elements of the 2 column.
the out of the 2nd column should be 2(bcos 1+1). add the values of each cell(sum =2). how to add the elements of a column. the loop should move down the column and add the values
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
int main()
{
char text[6][6];
ifstream stream1("c:\\cpptestdata.txt");
if(!stream1)
{
cout<<"Cannot read file\n";
}
while(!stream1.eof())
{
for(int i=0; i<6; i++)
{
for(int j=0; j<6; j++)
{
stream1>>text[i][j];
}
}
}
//checking if it has been correctly inserted.
for(int i=0; i<6; i++)
{
for(int j=0; j<6; j++)
{
cout<<text[i][j]<<"\t";
}
cout<<"\n";
}
cout<<"first two rows:"<<endl;
int i,j;
for (i=0; i<2; i++){
for (j=0; j<6; j++){
std::cout<<text[i][j]<<"\t"<<' ';
}cout<<endl;
}
cout<<"find immediate neighbours of A:"<<endl;
char largest=text[1][1];
for(i=0; i<6; i++){
for(j=1; j<2; j++){
if(text[i][j]>largest)
cout<<text[i][0]<<"N"<<"\t";
else
cout<<"0";
}cout<<endl;
}
cout <<" finding k neighbours for A : "<<endl;
for (i=1; i<6; i++){
int max = text[1][1]-'0';
for(j = 1; j<2; j++){
if(max < (text[i][j]-'0')){
max = text[i][j]-'0';
cout<<max;
}
else
cout <<"xx";
}cout<<endl;
}
return 0;
}
In general, if you want to access column c you write something like this:
for (int i = 0; i < 6; ++i) {
// access column c with text[i][c]
}
The following code is written to add the elements of the second column of your array, assuming the following things from your code:
It is a 6x6 array.
Instead of integer array, you are using a character array. (I didn't understand the need for this).
cout << "sum of elements\n";
int sum = 0;
for(i=0;i<6;i++)
{
sum = sum + (text[i][2] - '0'); //Convert char to int and add
cout <<" SUM = "<<sum<<"\n";
}
If no special reasons, define the array as an int array itself. Then you could directly add the values.
for(i=0;i<6;i++)
{
sum = sum + text[i][2];
cout <<" SUM = "<<sum<<"\n";
}