Printing patterns using arrays in c - c

The problem is to print this pattern:
5555555555
5444444445
5433333345
5432222345
5432112345
5432112345
5432222345
5433333345
5444444445
5555555555
This is my code:
#include<stdio.h>
int main()
{
int i,k,j,n,p;
printf("enter the no : ");
scanf("%d",&n);
p = n;
k = 0;
int a[2*n][2*n];
while (p>=1)
{
for(i=0+k;i<2*n-k;i++)
{
for(j=0+k;j<2*n-k;j++)
{
if(i == 2*n-k||i == k||j == k||j == 2*n-k)
{
a[i][j]=p;
}
else
{
a[i][j]= 8;
}
}
printf("\n");
}
k++,p--;
}
for(i=0;i<2*n;i++)
{
for(j=0;j<2*n;j++)
{
printf("%d",a[i][j]);
}
printf("\n");
}
return 0;
}
The result I get is:
5555555555
5444444448
5433333388
5432222888
5432118888
5432188888
5432888888
5438888888
5488888888
5888888888

Your code is overly complex. You should utilize the symmetry. Replace you while-loop with this code.
// Because of symmetry, both i and j can loop to n instead of 2*n
for(i=0; i<n; i++) {
p = n;
for(j=0; j<n; j++) {
// Assign four cells at once due to both horizontal and vertical symmetry
a[i][j] = a[i][2*n-j-1] = a[2*n-i-1][j] = a[2*n-i-1][2*n-j-1] = p;
if(j<i)
p--;
}
}

Related

Havel-Hakimi Algorithm in C

I'm trying to write havel-hakimi theorem in C. But I have a problem with the while loop. The program doesn't sort array again in the while loop and that's why output prints the wrong answer. Could show me what's my fault please?
# include <stdio.h>
int main(){
int j,i,vertex_number,temp1,temp2,a=0,b=0;
printf("Vertex Number:");
scanf("%d",&vertex_number);
int graph[vertex_number];
for(i=0;i<vertex_number;i++){
scanf("%d",&graph[i]);
}
while(1){
//SORTING ARRAY
for(i=0;i<vertex_number;i++){
for(j=i+1;j<vertex_number;j++){
if(graph[i]<graph[j]){
temp1=graph[i];
graph[i]=graph[j];
graph[j]=temp1;
}
}
}
//IF ALL VERTEX DEGREES EQUAL 0 GRAPH EXIST
for(i=0;i<vertex_number;i++){
if(graph[i]==0){
a++;
}
}
if(a==vertex_number){
printf(" graph exist.");
return 0;
}
//NEGATIVE VERTEX DEGREE NOT EXIST
for(i=0;i<vertex_number;i++){
if(graph[i]<0){
b++;
}
}
if(b>0){
printf("graph not exist.");
return 0;
}
temp2=graph[0];
for(i=0;i<temp2;i++){
graph[i]=graph[i+1];
}
vertex_number--;
for(i=0;i<temp2;i++){
graph[i]-=1;
}
printf("-------------\n");
for(i=0;i<vertex_number;i++){
printf("%d\n",graph[i]);
}
}
}
Your have 2 issues, the exist if graph[i]-=1; is negative and the removing loop doing should be done for vertex_number and not temp2
# include <stdio.h>
int main(void) {
int i, j, vertex_number, temp1, temp2;
printf("Vertex Number:");
scanf("%d", &vertex_number);
int graph[vertex_number];
for (i = 0; i < vertex_number; i++){
scanf("%d", &graph[i]);
}
while (1) {
//SORTING ARRAY
for (i = 0; i < vertex_number; i++) {
for (j = i+1; j < vertex_number; j++) {
if (graph[i] < graph[j]) {
temp1 = graph[i];
graph[i] = graph[j];
graph[j] = temp1;
}
}
}
//IF ALL VERTEX DEGREES EQUAL 0 GRAPH EXIST
if (graph[0] == 0) {
printf(" graph exist.");
return 0;
}
//NEGATIVE VERTEX DEGREE NOT EXIST
for (i = 0; i < vertex_number; i++) {
if (graph[i] < 0){
printf("graph not exist.");
return 0;
}
}
temp2 = graph[0];
vertex_number--;
for (i = 0; i < vertex_number; i++) { // HERE was your issue
graph[i] = graph[i + 1];
}
for (i = 0; i < temp2; i++) {
graph[i]-=1;
if (graph[i] < 0) {
printf("graph not exist.");
return 0;
}
}
printf("-------------\n");
for (i = 0; i < vertex_number; i++) {
printf("%d\n",graph[i]);
}
}
}
You don't need a, if the first is null all the other are null or negative
You don't need b neither just stop on first occurence
Not an answer, but a cleaned-up version that works follows.
The key is that it literally removes the first element from the array by advancing the pointer and reducing the size of the array. That way, we're always working with elements 0..s-1 or 0..n-1.
// Destroys the contents of the provided array.
int havel_hakimi(unsigned *degrees, size_t n) {
while (1) {
// Yuck
for (size_t i=0; i<n; ++i) {
for (size_t j=i+1; j<n; ++j) {
if (degrees[i] < degrees[j]) {
int temp = degrees[i];
degrees[i] = degrees[j];
degrees[j] = temp;
}
}
}
if (degrees[0] == 0)
return 1; // Has a simple graph.
// Remove first element.
unsigned s = degrees[0];
++degrees;
--n;
if (s > n)
return 0; // Invalid input!
if (degrees[s-1] == 0)
return 0; // Doesn't have a simple graph.
for (size_t i=s; i--; )
--degrees[i];
}
}
Tested using the following:
#include <stdio.h>
int main(void) {
{
unsigned degrees[] = { 6, 3, 3, 3, 3, 2, 2, 2, 2, 1, 1 };
printf("%d\n", havel_hakimi(degrees, sizeof(degrees)/sizeof(degrees[0]))); // 1
}
{
unsigned degrees[] = { 6, 5, 5, 4, 3, 2, 1 };
printf("%d\n", havel_hakimi(degrees, sizeof(degrees)/sizeof(degrees[0]))); // 0
}
return 0;
}

scanf() taking more inputs than expected [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I was attempting a problem to check symmetry in logos (link : https://www.hackerearth.com/practice/data-structures/arrays/multi-dimensional/practice-problems/algorithm/roy-and-symmetric-logos-1/description/). This is the function accepting input of the array called logo[][]:
void take_Logo(int logo[N_Max][N_Max], int N) //N is number of rows and columns
{
int i = 0, j = 0;
char in[80];
for(i = 0; i<N; i++)
{
for(j = 0; j<N; j++)
{
//gets(in); //error in input here
scanf("%s", in);
logo[i][j] = in[j] - '0';
}
}
}
The trouble is that when I run the code, for N = 2, it takes 4 inputs:
2
01
01
01
01
How do I correct it?
I am entering the 01's as a binary string.
Here is the complete code:
#include<stdio.h>
#define N_Max 32
int check_Symm(int logo[N_Max][N_Max], int N);
void take_Logo(int logo[N_Max][N_Max], int N);
int main(void)
{
int i = 0, T, N, logo[N_Max][N_Max];
scanf("%d", &T);
i = T;
while(i > 0)
{
--i;
scanf("%d", &N);
take_Logo(logo,N);
//check_Symm(logo, N) == 0 ? printf("NO\n") : printf("YES\n");
for(int k = 0; k<N; k++)
{
for(int l = 0; l<N; l++)
{
printf("%d ", logo[k][l]);
}
printf("\n");
}
}
}
void take_Logo(int logo[N_Max][N_Max], int N)
{
int i = 0, j = 0;
char in[80];
for(i = 0; i<N; i++)
{
for(j = 0; j<N; j++)
{
//gets(in); //error in input here
scanf("%s", in);
logo[i][j] = in[j] - '0';
}
}
}
int check_Symm(int logo[N_Max][N_Max], int N)
{
int test = 1;
if(N % 2 == 0)
{
for(int i = 0; i < N/2; i++)
{
for(int j = 0; j<N; j++)
{
if(logo[i][j] == logo[N-1-i][j])
continue;
else
{
test = 0;
return test;
}
}
}
for(int i = 0; i < N/2; i++)
{
for(int j = 0; j<N; j++)
{
if(logo[j][i] == logo[j][N-1-i])
continue;
else
{
test = 0;
return test;
}
}
}
}
else
{
for(int i = 0; i < N/2; i++)
{
for(int j = 0; j<N; j++)
{
if(logo[i][j] == logo[N-1-i][j])
continue;
else
{
test = 0;
return test;
}
}
}
for(int i = 0; i < N/2; i++)
{
for(int j = 0; j<N; j++)
{
if(logo[j][i] == logo[j][N-1-i])
continue;
else
{
test = 0;
return test;
}
}
}
for(int i=0; i < N/2 ; i++)
{
if(logo[N/2][i] == logo[N/2][N-i-1])
continue;
else
{
test = 0;
return test;
}
}
for(int i=0; i < N/2 ; i++)
{
if(logo[i][N/2] == logo[N-i-1][N/2])
continue;
else
{
test = 0;
return test;
}
}
}
return test;
}
The trouble is that when I run the code, for N = 2, it takes 4 inputs:
This is expected for N == 2:
// Do loop twice
for(i = 0; i<N; i++) {
// Do loop twice
for(j = 0; j<N; j++) {
// Read 2 * 2 times
scanf("%s", in);
logo[i][j] = in[j] - '0';
}
}

How can I make a program to check if the array is in descending , ascending order or none of them?

This code only lets user fill the array:
It is supposed to check whether is it in descending or ascending order, or maybe none of them, without using any sorting function. But it doesn't work properly,as it doesn't sort when it is neither ascending, nor descending
#include <stdio.h>
int main ()
{
int array[10];
int i;
int c;
int d;
printf("Enter the element of array:\n");
for(i=0; i<10; i++)
{
scanf("%d",&array[i]);
}
for(i=0; i<10; i++)
{
printf("%d\n",array[i]);
}
for(i=0; i<9; i++)
{
if(array[i]<array[i+1])
{
c=1;
}
else if(array[i]>array[i+1])
{
d=1;
}
}
if(c==1)
{
printf("ASCENDING");
}
else if(d==1)
{
printf("DESCENDING");
}
else
{
printf("NONE");
}
return 0;
}
#include <stdio.h>
#define ASND 0
#define DSND 1
int main ()
{
int a[10];
int i = 0;
int order;
printf("Enter the element of array:\n");
for(i=0; i<10; i++) {
scanf("%d",&a[i]);
}
for(i=0; i<10; i++) {
printf("%d\n",a[i]);
}
for(i=1;i<10;i++){
if(a[i-1] < a[i]) {
order = ASND;
break;
}
if(a[i-1] > a[i]) {
order = DSND;
break;
}
}
if(i==10) {
printf("all elements are same\n");
return 0;
}
if(order == ASND) {
for(i=1;i<10;i++) {
if(a[i-1] > a[i]) {
printf("no order\n");
return 0;
}
}
printf("ascending order\n");
return 0;
}
for(i=1;i<10;i++) {
if(a[i-1] < a[i]) {
printf("no order\n");
return 0;
}
}
printf("descending order\n");
return 0;
}
This one is quite simple and very readable. I think it can do the job well! Only in the need of one loop to check it, summing up a counter for ascending or descending, and checking after!
#include <stdio.h>
int main ()
{
int a[10];
int i = 0;
int order;
printf("Enter the element of array:\n");
for(i=0; i<10; i++) {
scanf("%d",&a[i]);
}
for(i=0; i<10; i++) {
printf("%d\n",a[i]);
}
int ascendingCount=0;
int descendingCount=0;
int num1=a[0];
for(int i=1;i<10;i++){
if(a[i]>=num1){
num1=a[i];
ascendingCount++;
}
else if(a[i]<=num1){
num1=a[i];
descendingCount++;
}
}
if(ascendingCount==9) printf("it is ascendingCount");
else if(descendingCount==9) printf("it is descending");
else printf("its nothing");
return 0;
}
When you have broken the ascending or descending rule you will need to break out of the loop so your not resetting the ascending or descending rule variables you have used.
#include <stdio.h>
int main ()
{
int array[10];
int i = 0;
int c = 0;
int d = 0;
printf("Enter the element of array:\n");
for(i=0; i<10; i++)
{
scanf("%d",&array[i]);
}
for(i=0; i<10; i++)
{
printf("%d\n",array[i]);
}
for(i=0; i<9; i++)
{
if(array[i]<array[i+1])
{
c=1;
}
else if(array[i]>array[i+1])
{
d=1;
}
// Run of same value
else if (d==0 && c==0)
{
continue;
}
// Can't be both ascending and descending
if (d == 1 && c == 1)
{
d = 0;
c = 0;
break;
}
}
if(c==1)
{
printf("ASCENDING");
}
else if(d==1)
{
printf("DESCENDING");
}
else
{
printf("NONE");
}
return 0;
}

cannot print the matrix on the console

Using the following code I cannot print any of the matrix A, B or C.
As a testing purpose even not an int is printed.
#include"stdio.h"
int main()
{
#define row 3
#define col 3
int A[row][col]={ {1,2,3},{2,3,4},{3,4,5}};
int B[row][col]={ {1,0,0},{0,1,0},{0,0,1}};
int i,j;
int C[row][col]={ {0,0,0},{0,0,0},{0,0,0}};
int temp=2;
for (i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
C[i][j]=A[i][j]*B[j][i]+C[i][j];
}
printf("%d \n ", C[i][j]);
getchar();
}
}
The problem in your code is here:
for (i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
C[i][j]=A[i][j]*B[j][i]+C[i][j];
}
// j contains 3 here therefore you acess the C array out of bounds
printf("%d \n ", C[i][j]); //<<<<<<<<<<<<<<
getchar();
}
You probably want this:
...
// Multiplication of A and B
for (i = 0; i<3; i++)
{
for (j = 0; j<3; j++)
{
C[i][j] = A[i][j] * B[j][i] + C[i][j];
}
}
// Display C
for (i = 0; i<3; i++)
{
for (j = 0; j<3; j++)
{
printf ("%d ", C[i][j]);
}
printf("\n");
}
getchar();
...
Even better: write a Display3x3Matrix and use it:
void Display3x3Matrix(int m[3][3])
{
for (int i = 0; i<3; i++)
{
for (int j = 0; j<3; j++)
{
printf("%d ", m[i][j]);
}
printf("\n");
}
}
...
printf("A\n");
Display3x3Matrix(A);
printf("\nB\n");
Display3x3Matrix(B);
printf("\nC\n");
Display3x3Matrix(C);
This works for me (printing the "A" Matrix):
#define row 3
#define col 3
int A[row][col] = { { 1,2,3 },{ 2,3,4 },{ 3,4,5 } };
int rows, columns;
for (rows = 0; rows<3; rows++)
{
for (int columns = 0; columns<3; columns++)
{
printf("%d ", A[rows][columns]);
}
printf("\n");
}
Change the for loop as follows
for (i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
C[i][j]=A[i][j]*B[j][i]+C[i][j];
printf("%d", C[i][j]);
}
printf("\n");
}
Demo

Array rotation in C

I am trying to rotate an array that looks like this:
a a a a
a b a a
b b b a
a a a a
I am supposed to rotate it 5 times for 90 degrees. It is supposed to be done in C.
I appreciate every help because I am just a beginner and am stuck on this.
Thanks in advance.
#include <stdio.h>
int main()
{
char array_1[4][4] = { {'-','-','-','-'},
{'-','o','-','-'},
{'o','o','o','-'},
{'-','-','-','-'}};
char array_2[4][4] = { {'-','-','-','-'},
{'-','o','o','-'},
{'o','o','-','-'},
{'-','-','-','-'}};
char array_3[4][4] = { {'-','-','-','-'},
{'-','o','-','-'},
{'-','o','-','-'},
{'-','o','o','-'}};
char array_4[4][4] = { {'-','-','o','-'},
{'-','-','o','-'},
{'-','-','o','-'},
{'-','-','o','-'}};
int counter = 0;
int counter_1 = 0;
for(counter = 0; counter < 4; counter++)
{
for(counter_1 = 0; counter_1 < 4; counter_1++)
{
printf("%c ",array_1[counter][counter_1]);
}
printf(" ");
for(counter_1 = 0; counter_1 < 4; counter_1++)
{
printf("%c ",array_2[counter][counter_1]);
}
printf(" ");
for(counter_1 = 0; counter_1 < 4; counter_1++)
{
printf("%c ",array_3[counter][counter_1]);
}
printf(" ");
for(counter_1 = 0; counter_1 < 4; counter_1++)
{
printf("%c ",array_4[counter][counter_1]);
}
printf(" ");
printf("\n");
}
printf("\n");
for(counter= 0; counter < 4; counter++)
{
for(counter_1 = 3; counter_1 >= 0; counter_1--)
{
printf("%c ",array_1[counter_1][counter]);
}
printf(" ");
for(counter_1 = 3; counter_1 >= 0; counter_1--)
{
printf("%c ",array_2[counter_1][counter]);
}
printf(" ");
for(counter_1 = 3; counter_1 >= 0; counter_1--)
{
printf("%c ",array_3[counter_1][counter]);
}
printf(" ");
for(counter_1 = 3; counter_1 >= 0; counter_1--)
{
printf("%c ",array_4[counter_1][counter]);
}
printf(" ");
printf("\n");
}
printf("\n");
like this:
#include <stdio.h>
typedef struct point { int x, y; } Point;
void rotate(int n, char array[n][n]){
//rotate right 90 degrees
if(n == 1) return ;
int times = n / 2;
for(int i = 0; i < times; ++i){
Point base = { i, i };
for(int j = 0; j < n - 1; ++j){
Point transition[4] = { {j, n-1}, {n-1,n-1-j},{n-1-j,0},{0,j} };
char curr = array[base.x][base.y+j];//base + {0,j}
for(int k = 0; k < 4; ++k){
char temp = array[base.x + transition[k].x][base.y + transition[k].y];
array[base.x + transition[k].x][base.y + transition[k].y] = curr;
curr = temp;
}
}
n -= 2;
}
}
void display(int n, char array[n][n]){
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
if(j)
putchar(' ');
putchar(array[i][j]);
}
putchar('\n');
}
putchar('\n');
}
int main(void){
//demo
char array4[4][4] = {
{'1','2','3','4'},
{'5','6','7','8'},
{'9','A','B','C'},
{'D','E','F','0'}
};
display(4, array4);
int n = 4;
while(n--){
rotate(4, array4);
display(4, array4);
}
char array5[5][5] = {
{'A','B','C','D','E'},
{'F','G','H','I','J'},
{'K','L','M','N','O'},
{'P','Q','R','S','T'},
{'U','V','W','X','Y'}
};
display(5, array5);
n = 4;
while(n--){
rotate(5, array5);
display(5, array5);
}
}

Resources