abs function is not giving right answer in c++? [closed] - c++

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]);

Related

Why I'm getting WA(Wrong Answer) in CMPRSS problem of codechef? [closed]

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
I'm getting WA(Wrong Answer) in "Compress the List" problem Code(CMPRSS) of CodeChef, here is the link of the problem: https://www.codechef.com/problems/CMPRSS
I've checked the sample output given in the problem and also some self made test cases and It's working properly.I'm not getting what's wrong in my code
here is my approach:
#include <bits/stdc++.h>
using namespace std;
vector<int> number;
int main() {
int t;
cin>>t;
while(t--){
int n;
cin>>n;
for(int i=0;i<n;i++){
int temp;
cin>>temp;
number.push_back(temp);
}
int i;
for(i=0;i<n-1;i++){
int count = 0;
int temp = i;
while (number[temp+1]-number[temp]==1){
temp++;
count++;
}
if(count>1){
if(i+count==n-1){
cout<<number[i]<<"..."<<number[i+count]<<"\n";
} else{
cout<<number[i]<<"..."<<number[i+count]<<",";
}
i = i + count;
}
else{
cout<<number[i]<<",";
}
}
if(i!=n){
cout<<number[n-1]<<"\n";
}
number.clear();
}
return 0;
}
You forgot to check if number[temp+1] exists in the part
while (number[temp+1]-number[temp]==1){
temp++;
count++;
}
Therefore, it may read beyond the array and produce wrong output.
Try this case:
2
5
1 2 3 4 5
3
1 2 3
The part should be like this:
while (temp+1 < static_cast<int>(number.size()) && number[temp+1]-number[temp]==1){
temp++;
count++;
}
Your logic is partially correct because you forgot to check the upper limit index of vector: Please check the missing code in the bold letter or between ** code **:
#include <bits/stdc++.h>
using namespace std;
vector number;
int main() {
int t;
cin>>t;
while(t--){
int n;
cin>>n;
for(int i=0;i<n;i++){
int temp;
cin>>temp;
number.push_back(temp);
}
int i;
for(i=0;i<n-1;i++){
int count = 0;
int temp = i;
while (**temp+1<n &&** number[temp+1]-number[temp]==1){
temp++;
count++;
}
if(count>1){
if(i+count==n-1){
cout<<number[i]<<"..."<<number[i+count]<<"\n";
} else{
cout<<number[i]<<"..."<<number[i+count]<<",";
}
i = i + count;
}
else{
cout<<number[i]<<",";
}
}
if(i!=n){
cout<<number[n-1]<<"\n";
}
number.clear();
}
return 0;
}
Check my solution.

Skipped condition and I don't know why [closed]

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 6 years ago.
Improve this question
I was debugging this, and debugger skipped the last 'if' even 'sum' was equal to 'n' and jump straight to 'else', I don't know why. Please help.
P/s: Can I use dynamic array to increase the mobility of my program?
#include <iostream>
#include <math.h>
using namespace std;
int exponent_of_10(); // set position for digits
int exponent_of_10(int a, int b){
for(int j = b; j>0;j--)
{
a *= 10;
}
return a;
}
main() //check if the number was palindromic
{
int n;
int a[6]={0,0,0,0,0,0};
int i = 0;
int temp;
int S;
cout<< "Input n (maximum of 6 digits): ";
cin>> n;
do
{
if(n<1)
{break;}
temp=n%10;
a[i]=temp;
n=(n-temp)/10;
i++;
}
while (n!=0);
int sum = 0;
for(int j=0; j<=5; j++)
{
exponent_of_10(a[j],j);
S = exponent_of_10(a[j],j);
if (S==0)
{break;}
sum +=S;
}
if(sum==n)
{
cout<< "Congratz, this is PALIDROMIC NUMBER !!";
}
else
cout<< "Sad life, this is NOT palidromic number";
return 0;
}
When the code exits the do ... while() loop, n is 0. For the test in the if to be meaningful, the code should save the original value of n somewhere and compare sum to that original value.

Why does this matrix multiplication code not work [closed]

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;
}

Program To Test whether or not a 4x4 matrix is a magic square using functions in C++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Here is what I have. I have it outputting most of the sums just so i can check there values. I think the problem is with the value of the elements in the array storing the column sums.
I would greatly appreciate any feedback.
#include <iostream>
#include <cmath>
using namespace std;
void fillMatrix(int matrix[][4],const int SIZE);
int rowSum(int matrix[][4],const int SIZE,int row[]);
int columnSum(int matrix[][4], const int SIZE, int column[]);
bool isMagic(int matrix[][4], const int SIZE,int row[],int column[]);
int main()
{
const int SIZE=4;
int matrix[SIZE][SIZE];
int row[4],column[4];//arrays to be filled with row and column sums.
char response=0;
cout<<"This program determines whether or not a 4x4 square matrix is a magic square.\n";
do
{
fillMatrix(matrix,SIZE);
rowSum(matrix,SIZE,row);
columnSum(matrix,SIZE,row);
if(isMagic(matrix,SIZE,row,column))
cout<<"This is a magic square.\n\n";
else {
cout<<"This is not a magic square.\n\n";
}
cout<<"To end this program, enter q. To check another matrix, enter any other letter.\n";
cin>>response;
}while(response!='q'&&response!='Q');
return 0;
}
void fillMatrix(int matrix[][4],const int SIZE)
{
for(int i=0;i<4;i++)
{
cout<<"Enter four values for row "<<i+1<<".\n";
for(int j=0;j<4;j++)
{
cin>>matrix[i][j];
}
}
}
int rowSum(int matrix[][4],const int SIZE,int row[4])
{
int i=0;
int rowsum=0;
for(i=0;i<4;i++)
{
rowsum=0;
for(int j=0;j<4;j++)
{
rowsum+=matrix[i][j];
}
row[i]=rowsum;
cout<<row[i]<<endl;
}
return row[i];
}
int columnSum(int matrix[][4], const int SIZE, int column[4])
{
int j=0;
int columnsum=0;
for(j=0;j<4;j++)
{
columnsum=0;
for(int i=0;i<4;i++)
{
columnsum+=matrix[i][j];
}
column[j]=columnsum;
cout<<column[j]<<endl;
}
return column[j];
}
bool isMagic(int matrix[][4], const int SIZE,int row[4],int column[4])
{
int rightdiagonalsum=0, leftdiagonalsum=0, check;
for(int i=0;i<4;i++)
{
rightdiagonalsum+=matrix[i][i];
}
cout<<rightdiagonalsum<<endl;
for(int i=0;i<4;i++)
{
leftdiagonalsum+=matrix[i][3-i];
}
cout<<leftdiagonalsum<<endl;
for(int i=1;i<4;i++)
{
if (row[i]==row[i-1])
{
check=row[i];
}
else {
return false;
}
}
for(int j=0;j<4;j++)
{
if (column[j]!=check)
{
cout<<column[j]<<"*****";//For some reason, the value of column[j] is 0.
return false;
}
}
if (rightdiagonalsum!=check||leftdiagonalsum!=check)
{
return false;
}
return true;
}
This code:
rowSum(matrix,SIZE,row);
columnSum(matrix,SIZE,row);
should be:
rowSum(matrix,SIZE,row);
columnSum(matrix,SIZE,column);
So the column array in your code has zero values for the rather mundane reason that you never initialised it.
What's more you are accessing beyond the end of the arrays here:
return row[i];
and here:
return column[j];
At both of these points i and j have values 4. You would have avoided such a mistake had you declared the loop variables inside the for statement. That would have limited the scope of these variables.
To fix the problem, return rowsum and columnsum respectively.
I do wonder what purpose the various SIZE declarations serve since your hard code a value of 4 all over the place.

my sorting code has an error [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an error that the bsort(array[],a] has a 69 C:\Users\heide\Desktop\Exam.cpp expected primary-expression before ']' token
also the search(array[],a,b)
Can u please help me to finish this? Here is the code:
#include<iostream>
using namespace std;
struct Sort{
void search(int array[],int x,int y)
{
for(int i=0;i<=x;i++)
{
if(array[i]==y)
{
cout<<"The number you enter appeared on array number "<<i<<endl;
}
else
{
cout<<"ERROR"<<endl;
}
}
}
void swap(int *x,int *y)
{
int i=0;
i=*x;
*x=*y;
*y=i;
}
void bsort(int array[],int x)
{
for(int a=0;a<x-1;a++)
{
for(int b=0;b<x-1;b++)
{
if(array[a]> array[b])
{
swap(&array[a],&array[b]);
for(int i=0;i<=x;i++)
{
cout<<array[i]<<"\t";
}
cout<<endl;
}
}
}
}
};
int main()
{
Sort sort;
int a;
int b;
cout<<"Enter the number of elements in the array "<<endl;
cin>>a;
int array[a];
for(int i=0; i<=a;i++)
{
cout<<"Enter Number for Element "<<i<<": "<<endl;
cin>>array[i];
}
cout<<"The List Before Sorting"<<endl;
for(int i=0;i<=a;i++)
{
cout<<array[i]<<"\t"<<endl;
}
cout<<"Elements Will is Sorting....."<<endl;
sort.bsort(array[],a);
cout<<"Enter The Number Needed to be Searched "<<endl;
cin>>b;
sort.search(array[],a,b);
system("pause");
return 0;
}
Remove the square braces [] while invoking the bsort function. It should be
sort.bsort(array,a);