Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'am trying to write a program which can calculate determinant of 2x2 matrix entered by user.
My code is as follows -
//Program to calculate determinant of matrices
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int arr[1][1];
int i,j,p,q,v,b,c,k,determinant;
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
p=i;
q=j;
cout<<"Enter element :- ";
cin>>arr[p][q];
cout<<"You are in "<<i<<j<<endl;
}
}
v=arr[1][1];
b=arr[1][2];
c=arr[2][1];
k=arr[2][2];
cout<<"Entered elements are = "<<v<<" "<<b<<" "<<" "<<c<<" "<<k<<endl;
determinant= ((v*k)-(c*b));
cout<<"Determinant of given matrix is = "<<determinant;
getch();
}
And this is the error I'am facing with this program -
Enter element :- 4
You are in 11
Enter element :- 9
You are in 12
Enter element :- 8
You are in 21
Enter element :- 7
You are in 22
Entered elements are = 4 8 8 7
Determinant of given matrix is = -36
At the position arr[1][2] I have entered '9' as the element, but instead it is printing the element assigned to position arr[2][1].
So, if anybody can help me resolve this error then I will be very much grateful to you.
The int arr[1][1] does not declare an matrix of two elements, it declares a 1x1 matrix.
Declare it instead as
int arr[2][2];
That being said, indices in C++ start at 0 so
v=arr[1][1];
b=arr[1][2];
c=arr[2][1];
k=arr[2][2];
should be
v=arr[0][0];
b=arr[0][1];
c=arr[1][0];
k=arr[1][1];
same goes for your for-loops
Array starts from index 0. The size of array should be a[2][2] since it is a 2x2 matrix.
Modify the loops accordingly for the array elements.
#include<iostream>
using namespace std;
int main()
{
int arr[2][2];
int i,j,p,q,v,b,c,k,determinant;
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
p=i;
q=j;
cout<<"Enter element :- ";
cin>>arr[p][q];
cout<<"You are in "<<i+1<<j+1<<endl;
}
}
v=arr[0][0];
b=arr[0][1];
c=arr[1][0];
k=arr[1][1];
cout<<"Entered elements are = "<<v<<" "<<b<<" "<<" "<<c<<" "<<k<<endl;
determinant= ((v*k)-(c*b));
cout<<"Determinant of given matrix is = "<<determinant;
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am trying to do dfs traversal using recursive call, My Graph, and vector<int>visited are global variables. num_v variable corresponds to number of vertices,num_e corresponds to number of edges I tried calling two DFS calls in main, but my output matches to the correct DFS order for the first case. and it gives incorrect out for the other, kindly help where I'm getting wrong in the implementation of it.
sample input: first line is a number of vertices and number of edges. next, each line is directed nodes between two-point.
5 5
1 2
2 4
4 5
5 3
1 3
expected output:
1 2 4 5 3
2 4 5 3
my wrong output
1 2 4 5 3
2
code:
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
int mx=1e5;
//int num_v;
vector<vector<int>>Graph(mx);
vector<int>visited;
void dfs(int v,vector<int>&vec){
visited[v]=1;
vec.push_back(v);
for(int neigh:Graph[v]){
if(visited[neigh]!=1){
dfs(neigh,vec);
}
}
}
int main(){
int num_v;int num_e;
cin>>num_v>>num_e;
visited.resize(num_v + 1);
for(int i=0;i<num_e;i++){
int u,v;
cin>>u>>v;
Graph[u].push_back(v);
}
vector<int>vec;
dfs(1,vec);
for(auto x:vec){
cout<<x<<" ";
}
visited.clear();
cout<<endl;
vector<int>tt;
dfs(2,tt);
for(auto y:tt){
cout<<y<<" ";
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I want to obtain the absolute difference of the sum of left and right diagonal of the given 2d array.
I have written the following function-
int diagonalDifference(vector<vector<int>> arr) {
int n=arr.size();
int summ1=0,summ2=0,result=0;
for(int i=0;i<n;i++)
{ for(int j=0;j<n;j++)
{
if(i==j)
{ summ1=summ1+arr[i][j];}
else if((i+j)==(n-1))
{ summ2=summ2+arr[i][j];}
}
}
result=abs(summ1-summ2);
return result;
}
input array
11 2 4
4 5 6
10 8 -12
Explanation- summ1=11+5+(-12)
summ2=4++5+10 result=|4-19|=|-15|=15
expected output: 15
The output I am getting is 10
Here is a solution in O(n) complexity. reference
for (int i = 0; i < n; i++)
{
summ1 += arr[i][i];
summ2 += arr[i][n-i-1];
}
The else-if should be an if by itself.
The else-if prevents the 5 from being added to summ2. Which means the final calculation is: (11+5-12)-(4+10) = -10. Whose absolute value is 10.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have to find all prime numbers between two given numbers(given in ascending order i.e small, large) I made logic such that my program starts from the given least number till the given most numbers and find factors for each number in between, if factors count are 2 i.e 1 and itself(which is a condition for a prime number), hence it is printed as prime. However I am unable to print my desired output.. can't track why(P.S I am 19 years old newbie in Programming)
#include <iostream>
using namespace std;
int main(){
int start,end;
cin>>start,end;
for(int i=start+1;i<end;++i){
int count;
for(int j=1;j<=i;++j){
if(i%j==0 || i/2==0)count++;
}
if(count==2) cout<<i<<endl;
}
return 0;
}
Input: 1 10
Expected Output:
2
3
5
7
9
Output: (nothing)
Your program has several issues.
cin>>start,end; is not going to read in 2 numbers. You need cin >> start >> end;
You are not initializing count to anything, so you invoke undefined behavior when you do count++. You need to do int count = 0;
Also, when checking if n is prime, you don't need to check for divisibility by 1 or n since this is always true.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm really fresh to C++, and I faced difficulties with the next question :
Write a program that asks the user to enter a number : n
THEN calculate f(n).
Notice that :
f(0)=3 and f(n+1) = 3/4 * f(n) + 4 ?
For example :
f(1)= 6.25
f(2)= 8.69
f(3)= 10.52
f(4)= 11.89
f(5)= 12.92
====================================================
So, how can I solve this?
thanks for all..
I try this code depending on Mr. paxdiablo answer :
#include<iostream>
using namespace std;
int main()
{
double x=0.0;
cout<<" enter an integer N:";
cin>> x;
double f1(double x)
{
if x==0.0
return 3;
return 3 / 4 * f1 (x-1) + 4;
}
return 0;
}
But the program never runs!
================================================
The Correct Solution is :
#include<iostream>
#include<iomanip> //To enable "setprecision" tool
using namespace std;
double f(int x){
if (x==0)
{return 3;}
return (3.0/4.0) * f(x-1) + 4.0; //we add zeros to get "double" results
}
int main()
{
int n=0;
cout<<" Please, enter an integer :";
cin>> n;
cout<<fixed<<setprecision(2)<<f(n); //"setprecision" used to get only two digits after the point
return 0;
}
BIG thanks to everyone gave me a hand and special thank to Mr. paxdiablo.
This sounds like a job for ... Recursion Man!
Simply define a recursive function that returns 3 when n is zero or calls itself with a reduced problem.
Pseudo-code would be something like
def f(x):
if x == 0:
return 3
return 3 / 4 * f(n-1) + 4
You can use std::cin >> dv to get a value from the user and that function (recoded in C++) to do the grunt work. Then std::cout << result to output the result. Just remember to use doubles rather than ints, including constants like 3.0.
You have to define the recursive function f, then read the input from the user, then apply the function to the input you've read (probably as an integer?), and print the output.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
for the question: http://www.codechef.com/FEB14/problems/TWODOGS
i wrote the following program but on the inline judge codechef i am getting sigsegv error. I want to know specifically which instruction is creating this error in my program so that i try and remove that.
here is the code:
#include<iostream>
using namespace std;
int main()
{
int n,k;
cin>>n>>k;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
int arr[n+1];
for(int i=1;i<n+1;i++)
arr[i]=n+1;
for(int i=0;i<n;i++)
{
int p=min(i+1,n-i);
arr[a[i]]=min(arr[a[i]],p);
}
int time=n+1;
for(int i=0;i<n;i++)
{
int p=a[i];
if(p!=k-p&&p<k)
time=min(time,max(arr[p],arr[k-p]));
}
if(time==n+1)
cout<<"-1";
else
cout<<time;
}
Given sample input 2:
Sample Input 2:
5 5
2 4 9 2 5
You cannot do this
arr[a[i]]=min(arr[a[i]],p);
because when i == 2, arr[a[i]] will be evaluated as arr[9], which is out of bounds and results modification of memory which you do not own, that is, undefined behavior. This is where your program is failing.
You'd have an easier time if your identifiers were more meaningful. That said:
cin>>a[i];
....
arr[a[i]]=min(arr[a[i]],p);
You are indexing arr[] by user input. If you are getting a segfault, it's best to put in asserts for these situations in case the assumption is invalid, something like:
assert(a[i] < n-1);
Next:
time=min(time,max(arr[p],arr[k-p]));
again, indexing array based on user input. Add another assert to test that arr[p] and arr[k-p] are valid.
Finally, for this line:
if(p!=k-p&&p<k)
Did you mean:
if(p!=k-p&&p<n)
a is allocated based on n, not k.
If you read the question carefully ,
the element in a[i] <= 10 ^ 6 .
You have declared an array arr of size n+1 .
Constraint on n : n <= 500000
Also you are trying to access arr[ a[i] ] which may go beyond the limit of array arr.
a[i] may be greater than 5 * 10^5 .
This is leading to segmentation fault error.