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);
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
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.
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 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.
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.
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 am creating a c++ array program in which I am trying to get input from the user but during insertion I want to prompt the user for duplicate values. I have used a while loop and for loop inside, but it doesn't work if the user enters a duplicate value. He will be asked to enter the value again at the particular index.
int size=0;
int k;
int index=0;
int temp=0;
int aray1[2];
char ch='y';
while(ch='y') {
for(k=0; k<=2; k++)
{
if(aray1[k]==temp) {
cout<<"please do not enter duplicates";
ch='y';
index--;
} else {
aray1[index]=temp;
index++
ch='n';
}
}
system("pause");
}
I would use an std::vector instead.
#include<iostream>
#include<vector>
#include<algorithm>
int main(){
using namespace std;
vector<int> v;
int size=2;
while(v.size()<size){
int i;
cin >> i;
vector<int>::iterator it = find(v.begin(), v.end(), i);
if(it==v.end()) // i is not in v so insert it to the end of the vector.
v.push_back(i);
else
cout << "Duplicate entered." << endl;
}
}
http://www.cplusplus.com/reference/algorithm/find/