If I want to switch all the elements "1" to "3" ,and all the elements "3" to "1", then the input is 1 3.
Here's the code, how can I modify it?
#include <bits/stdc++.h>
using namespace std;
void swap1(int *A,int *B){
int temp=*A;
*A=*B;
*B=temp;
}
int main(){
int a,b;
int temp1,temp2;
cin>>a>>b;
int board[9][9]={
{3,0,2,0,0,5,6,9,0},
{0,4,0,0,9,6,0,3,0},
{0,5,0,0,0,8,0,0,0},
{1,9,0,0,8,0,7,0,3},
{0,0,0,0,0,0,0,0,0},
{5,0,7,0,3,0,0,6,1},
{0,0,0,8,0,0,0,2,0},
{0,8,0,9,6,0,0,7,0},
{0,6,5,7,0,0,3,0,9},
};
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
if(board[i][j]==a){
swap1(&a,&b);
}
}
}
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
cout<<board[i][j];
if(j<8)cout<<" ";
}
cout<<"\n";
}
return 0;
}
swap1(&a,&b);
Swaps the values of a and b, the integers you use to store the user input. If you want to modify the array elements you should rather have something like this inside your loop:
if (board[i][j] == a) board[i][j] = b;
else if (board[i][j] == b) board[i][j] = a;
(no need to swap)
Related
The code given below is a code for n*n matrix however on executing this code gives garbage values as output for n>=3.Kindly suggest me why?Also if possibly can somebody tell me how i can return 2D array from a function in c++.Thanks in advance.
#include<iostream>
using namespace std;
void rowcol(int[][10],int[][10],int,int);
int det(int arr[][10],int n)
{
int cofact[10][10];
if(n==2)
return (arr[0][0]*arr[1][1])-(arr[0][1]*arr[1][0]);
int val=0,flg=1;
for(int i=0;i<n;i++)
{
rowcol(arr,cofact,i,n);
val=val+(flg*det(cofact,n-1));
flg*=-1;
}
return val;
}
void rowcol(int arr[][10] ,int cofact[][10],int l,int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if((i!=l)&&(j!=l))
cofact[i][j]=arr[i][j];
}
}
}
int main()
{
int n,val;
int arr[10][10];
cout<<"Enter the size of matrix:";
cin>>n;
cout<<"\nEnter the array elements row by row";
for(int i=0;i<n;i++)
{
cout<<"\nEnter Row "<<i<<":";
for(int j=0;j<n;j++)
{
cin>>arr[i][j];
}
}
val=det(arr,n);
cout<<"\nThe determinant of given matrix is:"<<val;
return 0;
}
I want to determine the intersection of 2 bi-dimensional arrays.
Here is my code:
#include < iostream >
using namespace std;
void type(int mat[][10],int n)
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cin>>mat[i][j];
}
void inter(int a[][10], int n,int b[][10],int m)
{
int k1=0,p, x,ok,j, c[50],i;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
p=0; x=0;
while(p<m)
{
ok=0;
while(x<m)
{
if(a[i][j]==b[p][x]) ok=1;
if(ok) c[k1++]=a[i][j];
x++;
}
p++;
}
for(i=0;i<n;i++)
{
cout<<c[i]<<" ";
cout<<endl;}
}
}
}
} // !!! added in edit!!!
int main()
{
int n,m,mat[10][10],c[30],mat2[10][10];
cout<<"n= ";cin>>n;
type(mat,n);
cout<<"m= "; cin>>m;
type(mat2,m);
inter(mat,n,mat2,m);
return 0;
}
It doesn't show me the expected answer. It shoes me numbers like these : 1
4758024
1
4758024
Can somebody help me?
I have written a program to print the matrix in the spiral form, but it only works for 3*3. how to make it useful for all the dimensions.
#include<iostream.h>
#include<conio.h>
void main()
{
int i,j,k=1,l=0;
int n;
clrscr();
cout<<"Enter the number of row : ";
cin>>n;
int a[3][3];
cout<<"Matrix Form : "<<"\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=i*n+(j+1);
cout<<a[i][j]<<"\t";
}
cout<<"\n";
}
cout<<"Spiral form"<<"\n";
for(i=k;i<n;i++)
{
cout<<a[k][i]<<"\t";
}
k++;
for(i=k;i>=0;i--)
{
cout<<a[k][i]<<"\t";
}
k--;
for(i=k;i>=0;i--)
{
cout<<a[i][l]<<"\t";
}
for(i=k;i<n;i++)
{
cout<<a[l][i]<<"\t";
}
getch();
}
Assuming the code is working, you need to act on this row:
int a[3][3];
The easiest way is using C++ std::vector:
std::vector<std::vector<int> > a(n, std::vector<int>(n));
Remember to #include <vector> as well.
I tried to generate 3*3 spiral matrix using c by using increment and decrement of rows and columns in an array,then found out sum of main and opposite diagonals of the matrix.
My code is as follows
#include<stdio.h>
#define n 3
int main()
{
int m=n/2,i,j,a[10][10],c=1,sum=0,s=0;
for(i=m,j=m;j<n;j++)
{
a[i][j]=c;
c++;
}
j--;
i++;
while(j>=0)
{
a[i][j]=c;
c++;
j--;
}
i--;
j++;
a:
if(i>=0)
{
a[i][j]=c;
c++;
i--;
goto a;
}
i++;
j++;
b:
if(j<n)
{
a[i][j]=c;
c++;
j++;
goto b;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("\t%d",a[i][j]);
}printf("\n");
}
for (i = 0; i <n; i++)
{
sum = sum + a[i][i];
s=s+a[i][n-i-1];
}
printf("sum of diagonals is d1: %d d2: %d d1+d2: %d",sum,s,s+sum);
return 0;
}
output:
click here to view output
I know the logic how to merge two arrays but the problem is how to code.
This was my code n it is giving correct ans but my sir told me that do it again,please tell me what I have to add in this code,
#include<iostream>
using namespace std;
int mergeArrays(int array1[],int size1,int array2[],int size2);
int main()
{
const int size1=8;
const int size=12;
int arrayA[size1]={10,25,37,49,50,51,55,60};
int arrayB[size]={2,5,26,27,29,32,40,45,70,80,90,95};
mergeArrays(arrayA,size1,arrayB,size);
}
int mergeArrays(int array1[],int size1,int array2[],int size2)
{
int size3=size1+size2;
int *array3=new int[size3];
int k=0;
for(int i=0;i<size1;i++)
{
array3[k]=array1[i];
cout<<" "<<array3[k];
}
int j=0;
for(int i=size1;i<size2;i++)
{
array3[k]=array2[j];
}
for(int i=size1;i<size2;i++)
{
for(int j=0;j<size2;j++)
{
array3[i]=array2[j];
cout<<" "<<array3[i];
}
cout<<endl;
delete[]array3;
return array3[k++];
}
}
I had searched this in many places but could not corrected my code
I had written this code but it is not giving correct ans.
#include<iostream>
using namespace std;
int merge(int *a,int *b,int aSize,int bSize);
int main()
{
const int aSize={8};
const int bSize={12};
int arrayA[aSize]={10,25,37,49,50,51,55,60};
int arrayB[bSize]={2,5,26,27,29,32,40,45,70,80,90,95};
merge(arrayA,arrayB,aSize,bSize);
return 0;
}
int merge(int *a,int *b,int aSize ,int bSize)
{
int cSize=aSize+bSize;
int *c=new int[cSize];
int j=0,k=0;
int i=0;
while(i<=aSize&&j<=bSize )
{
if(a[aSize ]<=b[bSize])
{
c[k]=a[aSize];
k++;
i++;
}
else
{
c[k]=b[bSize];
k++;
j++;
}
}
for(int i=0;i<k;i++)
{
cout<<c[i]<<endl;
}
delete[]c;
return c[k++];
}
your sir request you do Merging two arrays in ascending order. so i think you should return a new array, fill with array1 and array2's element, and the elements should be ascending order. here is a implement.(suppose your input arraies is already in ascending order.)
#include <iostream>
using namespace std;
int mergeArrays(int array1[],int size1,int array2[],int size2, int outArray[]);
int main()
{
const int size1=8;
const int size=12;
int arrayA[size1]={10,25,37,49,50,51,55,60};
int arrayB[size]={2,5,26,27,29,32,40,45,70,80,90,95};
int outArray[size1+size];
int len = mergeArrays(arrayA,size1,arrayB,size, outArray);
cout <<" "<< len;
for (int i = 0; i< size1+size; ++i){
cout <<" " << outArray[i];
}
}
int mergeArrays(int array1[], int size1, int array2[], int size2, int outArray[])
{
int i=0, j=0, k=0;
int retSize = size1+size2;
while (k<retSize){
if (i==size1){// only left array2, copy it
for (; j<size2; ++j){
outArray[k++] = array2[j];
}
}else if (j == size2) { // only left array1, copy it
for (; i<size1; ++i){
outArray[k++] = array1[i];
}
}
else if (array1[i] > array2[j]){ // copy the min value to outArray
outArray[k++] = array2[j++];
}else{
outArray[k++] = array1[i++];
}
}
return k;
}
now, let's look at your first code:
int mergeArrays(int array1[],int size1,int array2[],int size2)
{
int size3=size1+size2;
int *array3=new int[size3];
int k=0;
for(int i=0;i<size1;i++)
{
array3[k]=array1[i]; // k is not changed, so you just assign array1's each value to array3[0]
cout<<" "<<array3[k];
}
int j=0;
// what's the purpose of this loop?
// and in loop, you don't use i, you just repeat set array3[0] = array2[0]!!
for(int i=size1;i<size2;i++)
{
array3[k]=array2[j];
}
for(int i=size1;i<size2;i++) // if array2's length bigger than array1's, will enter this loop.
{
for(int j=0;j<size2;j++)
{
array3[i]=array2[j]; // this just repeat assign array2's each value to array3[i]!!
cout<<" "<<array3[i];
}
cout<<endl;
delete[]array3;
return array3[k++]; // you delete array3, but at here you access it!! this will crash!
// also, in this for i loop, you have return, so it will only execute once.
}
// reach function end and no return if not enter for loop.
}
I haven't looked at your second code. I think you still need to do more study.
I am trying to implement a simple merge sort for even number of elements using the following code :
#include <iostream>
using namespace std;
void merge(int arr1[10],int len1,int arr2[10],int len2,int arr3[10],int len3)
{
int i,j,k;
while(i<len1&&j<len2&&k<len3)
{
if(arr1[i]<arr2[j])
{
arr3[k] = arr1[i];
i++;k++;
}
else if(arr1[i]>arr2[j])
{
arr3[k] = arr2[j];
k++;
j++;
}
}
}
void mergeSort(int a[10],int n)
{
int arr1[10],arr2[10];
for(int i=0;i<n/2;i++)
{
arr1[i] = a[i];
}
for(int i=(n/2+1);i<n;i++)
{
arr2[i] = a[i];
}
mergeSort(arr1,n/2);
mergeSort(arr2,n/2);
merge(arr1,n/2,arr2,n/2,a,n);
}
int main() {
int arr[10],n;
cout << "Enter number of elements\n";
cin >> n;
cout<<"Enter elements\n";
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
mergeSort(arr, n);
cout<<"Sorted array is"<<endl;
for(int i=0;i<n;i++)
{
cout<<arr[i]<<endl;
}
return 0;
}
But I am getting a EXC_BAD_ACESS error on the opening brace of the mergeSort() method. I am new to Xcode and not sure on how to fix this. How do I fix this ?
Thanks !
You have forgot to initialize the int variables i, j, k to 0 (zero) in the function definition for merge(...).
void merge(int arr1[10],int len1,int arr2[10],int len2,int arr3[10],int len3)
{
// int i,j,k; // Not initialized.
int i = j = k = 0;
while(i<len1&&j<len2&&k<len3)
{
...
...
}
After Edit:
void mergeSort(int a[10],int n)
{
int arr1[10],arr2[10];
for(int i=0;i<n/2;i++)
{
arr1[i] = a[i];
}
// Here you are skipping the middle index.
// Let's say array is of length 10, then arr1 contains elements on index from 0 to 4 (5 elements),
// whereas arr2 contains elements on index from 6 to 9 (4 elements).
// 5th index element is missing.
// for(int i=(n/2+1);i<n;i++)
for(int i=(n/2);i<n;i++)
{
arr2[i] = a[i];
}
mergeSort(arr1,n/2);
mergeSort(arr2,n/2);
merge(arr1,n/2,arr2,n/2,a,n);
}
Hope it helps!