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 6 years ago.
Improve this question
#include<stdio.h>
#include<conio.h>
int main()
{
int ar1[3][3] = {{1,0,0},{0,1,0},{0,0,1}};
int ar2[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int ar3[3][3];
int i,j,k;
for(i=0;i<3;i++)
{
ar3[i][j] = 0;
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
ar3[i][j] = ar3[i][j]+(ar1[i][k]*ar2[k][j]);
}
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++);
printf("%d\t",ar3[i][j]);
}
getch();
return 0;
}
When I compile the code in Dev C++ it does not give any error but fails to run and the application stops working. Whats wrong with it?
On the line
ar3[i][j] = 0;
j is still garbage so you get undefined behavior, which may lead to different kinds of faults - erroneous results, segfaults, on some rare cases it may even work as expected.
Move that line inside the second loop.
you should initialize arr3[i][j] to 0 as int arr3[i][j]={0};. Also while displaying the multiplication matrix you closed the second loop with j. Don't close that otherwise you won't get desired output. I corrected it. Hope this one solves your problem.
#include<stdio.h>
#include<conio.h>
int main()
{
int ar1[3][3] = {{1,0,0},{0,1,0},{0,0,1}};
int ar2[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int ar3[3][3] = {0}; // here goes initialization
int i,j,k;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
ar3[i][j] = ar3[i][j]+(ar1[i][k]*ar2[k][j]);
}
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",ar3[i][j]);
}
printf("\n");
}
getch();
return 0;
}
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 2 years ago.
Improve this question
Here is the problem: https://codeforces.com/contest/1256/problem/D .I'm getting runtime error("out of bound") in test case 15 here is my submission: https://codeforces.com/contest/1256/submission/84865113
Where am I doing wrong?
my approach:
#include <iostream>
using namespace std;
int main() {
int t;
cin>>t;
while (t--){
int n,k;
cin>>n>>k;
string num;
cin>>num;
int start = 0;
for(int i=0;i<n;i++){
if(num[i]=='0' && k>0){
if((k-abs(i-start))>=0){
swap(num[start],num[i]);
k = k - abs(i-start);
start++;
}
else{
swap(num[i],num[i-k]);
k = 0;
}
}
}
cout<<num<<"\n";
}
return 0;
}
the range of k is out of int. You should use long long.
9428683473 is out of 2147483647(max value for type int), so you should not use int. And the var i and start should change too.
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 4 years ago.
Improve this question
I am working on a complete pivoting method for Gaussian elimination. The only problem is with abs() function.
In code, the abs function is not giving right value 2nd time. It should give -38 while it is giving 4.4.
If anyone knows, please do tell me the error. Details: in the first loop i finds the maximum value and then divide the whole row by that value and then removes that row from all rows.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
cout.precision(3);
float a[4][5]={{4,-4,-3,7,1.3},{8,-3,-8,17,6.6,},{12,-12,-16,29,-2.1},{-8,33,-25,36,10.4}},temp[5];
float max=0;
int c,cl,no=4;
for(int ku=no;ku>=0;ku--)
{
for(int r=0;r<ku;r++)
{
for(int i=0;i<ku;i++)
{
for(int j=0;j<4;j++)
{
if(abs(a[i][j])>max)
{
max=a[i][j];
c=i;
cl=j;
}
}
}
cout<<"****max:"<<max<<"*****"<<endl;
for(int k=0;k<5;k++)
{
if(r==c)
{
a[r][k]=a[r][k]/max;
}
}
}
for(int r=0;r<ku;r++)
{
for(int p=0;p<5;p++)
{
if(r==c)
{
temp[p]=a[r][p];
}
else
{
a[r][p]=a[r][p]-(a[c][p]*a[r][cl]);
}
}
}
for(int i=0;i<ku;i++)
{
for(int pi=0;pi<5;pi++)
{
a[c][pi]=a[ku][pi];
a[ku][pi]=temp[pi];
}
}
for(int j=0;j<5;j++)
{
cout<<" temp:"<<temp[j]<<" ";
}
cout<<endl<<endl;
for(int i=0;i<ku-1;i++)
{
for(int j=0;j<5;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
}
}
Your condition is if(abs(a[i][j])>max), so it looks like you're comparing the magnitude. On the next line, you assign max=a[i][j], which will store a negative number. The next iteration will replace this.
What you want to store is
max = abs(a[i][j]);
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 6 years ago.
Improve this question
this program suppose to convert decimal to binary but somehow i screw it up
can some one point out the error for me?
thanks a lot
#include<conio.h>
#include<stdio.h>
int main(){
int a;
int b[20];
int q = 0;
printf("decimal : ");scanf("%d",&a);
while(a>0)) {
b[q]=a%2;
a=a/2;
q++;
}while(a>0);
printf("binary : ");
for (int i = q-1; i>=0;i--){
printf("%d",b[q]);
}
}
Corrected code is:
#include<conio.h>
#include<stdio.h>
int main(){
int a;
int b[20];
int q = 0;
printf("decimal : ");scanf("%d",&a);
while(a>0) {
b[q]=a%2;
a=a/2;
q++;
}
printf("binary : ");
for (int i = q-1; i>=0;i--){
printf("%d",b[i]);
}
}
You were printing b[q] instead of b[i]
There are some problems with your code:
you added an extra ")" of the first while;
the second 'while' is useless (the code is being repeated due to the first one)
you are not printing the elements you want (you should use var 'i'), what you are really printing is the value after the last 0/1 (because you are using 'q')
code should look like this:
#include <conio.h>
#include <stdio.h>
int main() {
int a;
int b[20];
int q = 0;
printf("decimal: ");
scanf("%d", &a);
while (a > 0) {
b[q] = a % 2;
a = a / 2;
q++;
}
printf("binary: ");
for (int i = q - 1; i >= 0; i--) {
printf("%d", b[i]);
}
}
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 8 years ago.
Improve this question
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector <int> v;
vector<int> :: iterator ip;
v.push_back(2);
for(int i=3;i<=32000;i+=2)
{
int top;
top = sqrt(i)+1;
int flag=0;
if(*ip>top) break;
if(i%*ip==0)
{
flag=1;
break;
}
if(flag==0)
{
v.push_back(i);
}
}
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
cout<<v.at(n)<<endl;
}
return 0;
}
can you please explain me why my code is getting runtime error.
is size of the vector is getting out of range ??
I dont know why i am getting wrong answer in this code although my code is perfectly fine.
You're dereferencing an uninitialized iterator, you want something more like:
vector<int>::iterator ip = v.begin();
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
please someone help me finding the error in this code. i've its python counterpart which is working fine, but when i code this in c++ , giving values(N) >=5 gives me an infinite loop.
[in this problem, you have to find the no. of trailing zeros in the factorial of a given number]
[here T is no. of test cases, N is the number, and z is output]
#include <iostream>
using namespace std;
int main(){
long int T,N,Z;
cin>>T;
for(int x=0;x<T;x++){
Z=0;
cin>>N;
for(int y=1;y<=N;y++){
if(y%5==0){
while(y/5!=0 && y%5==0){
Z+=1;
y/=5;
}
}
}
cout<<Z<<endl;
}
}
my python code is (which is working fine)
T=int(raw_input())
for x in range(1,T+1):
Z=0
N=int(raw_input())
for y in range(1,N+1):
if y%5==0:
while y/5!=0 and y%5==0:
Z=Z+1
y=y/5
print Z
change this:
for(int y=1;y<=N;y++){
int temp=y;
if(temp%5==0){
while(temp/5!=0 && temp%5==0){
Z+=1;
temp/=5;
}
}
}
your logic is not correct. Look at this, which i correctly submitted , hope it helps:-
#include<iostream>
using namespace std;
int tailing_zero(int n){
int ans=0;
int DIV=5;
while(n/DIV >0){
ans=ans+n/DIV;
DIV=DIV*5;
}
return ans;
}
int main(){
int run_count;
cin>>run_count;
int input[run_count];
for(int i=0;i<run_count;i++){
cin>>input[i];
}
for(int i=0;i<run_count;i++){
cout<<tailing_zero(input[i])<<endl;
}
return 0;
}