spiral matrix using c program - c++

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

Related

How to switch elements in a 9*9 matrix

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)

spiral matrix breaks on the last "for" loop and doesn't show anything

#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter the size of the array :";
cin>>n;
int A[n][n];
int y=n,k=1,p=0,i;
while(k<=n*n)
{
for(i=p;i < y;i++)
{
A[y-1][i]=k++;
}
for(i=y - 2;i > p;i--)
{
A[i][y-1]=k++;
}
for(i=y - 2;i > p;i--)
{
A[p][i]=k++;
}
for(i = p + 1;i < y; i++)
{
A[i][p]=k++;
}
p++;
y--;
}
if(!n%2)
{
A[(n+1)/2][(n+1)/2]=n*n;
}
for(i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<A[i][j]<<"\t";
}
cout<<endl;
}
return 0;
I need to do a spiral matrix the way like this > enter image description here.
It breaks on the last "for" cycle and just doesn't show anything;;; Still, it shows up if I'm replacing one of the loop's statements;; I would be grateful if you point me where's my mistake!
(this code is a modified one brought from here https://www.includehelp.com/cpp-programs/print-a-spiral-matrix.aspx)
There were simply some little mistakes on the bounds of the loops (the bounds of the spiral). Here is a slightly modified programme.
PS: Note that you should avoid to use VMA int A[n][n] which is C, not C++.
#include<iostream>
//using namespace std;
int main()
{
int n;
std::cout << "Enter the size of the array :";
std::cin >> n;
int A[n][n];
int y = n, k = 1,p = 0,i;
while(k<= n*n)
{
for(i=p;i < y;i++)
{
A[y-1][i]=k++;
}
for(i=y - 2;i >= p;i--)
{
A[i][y-1]=k++;
}
for(i = y - 2;i >= p;i--)
{
A[p][i]=k++;
}
for(i = p + 1;i < y-1; i++)
{
A[i][p]=k++;
}
p++;
y--;
}
if(!n%2)
{
A[(n+1)/2][(n+1)/2]=n*n;
}
for(i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
std::cout<<A[i][j]<<"\t";
}
std::cout << "\n";
}
return 0;
}

Intersection of 2 bidimensional arrays

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?

Sorting, how should I cout the round and pass?

Our professor told us to make our program cout the round and pass of the sorting algorithm.. What should I do then?
https://qph.is.quoracdn.net/main-qimg-21f7faf7e69c3317661f5f959008e031?convert_to_webp=true
Like this one ^
Bubble sort code
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for ( c = 0 ; c < n ; c++ )
printf("%d\n", array[c]);
return 0;
}
Is this right??? --->
#include<stdio.h>
int main()
{
int a[100],c[100],b,n,i,temp;
printf("\t\t\tBUBBLE SORT\n");
printf("Enter total numbers : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter %d number : ",i+1);
scanf("%d",&a[i]);
c[i]=a[i];
}
printf("\n");
for(i=0;i<n;i++)
{
for(int j=0;j<n-1;j++)
{
b=0;
if(a[j]<a[j+1])
{ b++;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
if(b!=0)
{
for(int k=0;k<n;k++)
{
printf("%d ",a[k]);
}
printf("\n");
}
}
}
printf("\nAssending order By Bubble sort\n\t");
for(i=0;i<n;i++)
{
printf("%d ",c[i]);
}
return 0;
}
This is just simple step by step bubble sort problem.
just make two functions as bubble_asc(), bubble_desc()
and call it from the main function().
int main() {
...
bubble_asc();
bubble_desc();
...
}
void bubble_asc() {
...
do step by step bubble sort with ascending order here.
for(~~~~~) {
for(~~~~~~) {
if(array[a] < array[b])
....
}
}
print arrays here..
}
void bubble_desc() {
...
do step by step bubble sort with descending order here.
for(~~~~~) {
for(~~~~~~) {
if(array[a] > array[b])
....
}
}
print arrays here..
}

Merge sort throwing index out of bounds exception in Xcode

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!