At the moment I am creating a program where the user is asked to place the queens for the 8 Queens Problem. Right now I have nearly created the program, but I'm stuck on how to make the program check diagonally.
This is the (unfinished)code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int check_r_c(int**chess,int*p,int N,int M)
{
int times=0;
for(int i=0; i<N; i++)
{
times=0;
for(int j=0; j<M; j++)
{
if(chess[i][j] == 1)
times++;
}
if( times != 1)
{
*p=1;
return 1;
}
}
for(int j=0; j<M; j++)
{
times=0;
for(int i=0; i<N; i++)
{
if(chess[i][j] == 1)
times++;
}
if( times != 1)
{
*p=1;
return 1;
}
}
*p=0;
return 0;
}
int main()
{
int N,M;
printf("Give the number of rows: \n");
scanf("%d",&N);
printf("Give the number of columns: \n");
scanf("%d",&M);
int**chess = malloc(N*sizeof(int));
int i,j,ch;
int*ptr;
ptr=&ch;
if(chess==NULL)
{
printf("\nMemory cannot be allocated\n");
return 1;
}
for(i=0; i<N; i++)
{
if((chess[i] = malloc(M*sizeof(int)))==NULL)
{
printf("\nMemory cannot be allocated\n");
return 1;
}
}
for(int i=0; i<N; i++)
for(int j=0; j<M; j++)
chess[i][j]= 0;
for(int k=0; k<N; k++)
{
printf("Give the position of the %d queen\n",k+1);
scanf("%d",&i);
scanf("%d",&j);
if(chess[i][j] == 1)
{
printf("You cant put 2 queens in the same place!!!\n" );
return 0;
}
chess[i][j] = 1;
}
check_r_c(chess,ptr,N,M);
if(ch == 0)
printf("Solution is correct!");
else
printf("Solution is incorrect!");
for(int i=0; i<N; i++)
free(chess[i]);
free(chess);
}
The logic for checking diagonal for a queen placed is (p,q) is to check for all the places located at (p+i,q+i) for all i, where both p+i and q+i are within the board. For negative side, use (p-i,q-i).
Related
Everythings right except the output of the last part, I'm supposed to get this output https://i.stack.imgur.com/PRJMT.png but instead it got this https://i.stack.imgur.com/iZflq.png
Also heres my code:
#include <stdio.h>
int main() {
int n;
printf("Enter the number of goal-scorers: ");
scanf("%d", &n);
int ar[n];
for(int i=0; i<n; i++){
printf("Score of player #%d: ", i+1);
scanf("%d", &ar[i]);
}
for(int i=0; i<n; i++){
for(int j=i+1; j<n; j++){
if(ar[i] <ar[j]){
int temp = ar[j];
ar[j] = ar[i];
ar[i]; temp;
}
}
}
printf("\nHighest to lowest:\n");
for(int i=0; i<n; i++)
printf("Player #%d: %d\n",i+1, ar[i]);
return;
}
Found it!
ar[i]; temp;
But you want ar[i] = temp;
You enabled compiler warnings. What I don't get is why the warning for "statement has no effect" is not showing up.
The possible solution may be like :
#include <stdio.h>
struct player {
int no;
int score;
};
int main() {
int n;
printf("Enter the number of goal-scorers: ");
fflush(stdout);
scanf("%d", &n);
if (n <= 0)
return 0 ;
struct player players[n];
for (int i = 0 ; i < n ; i++) {
players[i].no = 1 + i;
printf("Score of player #%d: ", players[i].no);
fflush(stdout);
scanf("%d", &players[i].score);
}
for (int i = 0 ; i < n ; i++) {
for (int j = i + 1 ; j < n ; j++) {
if (players[i].score < players[j].score) {
struct player temp = players[j];
players[j] = players[i];
players[i] = temp;
}
}
}
printf("\nHighest to lowest:\n");
for (int i = 0 ; i < n ; i++)
printf("Player #%d: %d\n", players[i].no, players[i].score);
return 0;
}
I have output in image, but code is not proper
i want code for the given output
#include <stdio.h>
int main() {
int i, space, rows, k = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i, k = 0) {
for (space = 1; space <= rows - i; ++space) {
printf(" ");
}
while (k != 2 * i - 1) {
printf("* ");
++k;
}
printf("\n");
}
return 0;
}
The question was a little difficult to understand, I'm assuming you're asking how to get the output as described in the image. This code does that:
#include <stdio.h>
int main() {
int rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (int i=0; i<rows; ++i) {
for(int j=0; j<rows*2-1; ++j) {
if (j <= i || j >= rows*2-2-i) {
printf("* ");
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
I have tried to remove the duplicate elements from an array
using c language, here I'am using 3 for loop to remove the duplicate elements.
How can this optimize the code?
any other simple method to remove the duplicate elements?
Thanks.
#include <stdio.h>
#define MAX_SIZE 100 // Maximum size of the array
int main()
{
int arr[MAX_SIZE];
int size;
int i, j, k;
printf("Enter size of the array : ");
scanf("%d", &size);
printf("Enter elements in array : ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}
for(i=0; i<size; i++)
{
for(j=i+1; j<size; j++)
{
if(arr[i] == arr[j])
{
for(k=j; k<size; k++)
{
arr[k] = arr[k + 1];
}
size--;
j--;
}
}
}
printf("\nArray elements after deleting duplicates : ");
for(i=0; i<size; i++)
{
printf("%d\t", arr[i]);
}
return 0;
}
Can someone help me to figure out why my code is unable to accurately find the duplicate of elements?
#include <stdio.h>
int main() {
int array[10];
int count = 0;
printf("Enter a maximum of 10 values to store in an array: ");
for (int i = 0; i < 10; i++) {
scanf_s("%d", &array[i]);
}
for (int i = 0; i < 10; i++) {
for (int j = i + 1; j < 10; j++) {
if (array[i] == array[j]) {
count++;
break;
}
}
}
printf("The duplicates are : %d ", count);
}
I'm a beginner at this language so any advice and suggestions to help me solve this exercise will be much appreciated.
First of all the first loop runs 10 times even if the user enters less numbers. You can fix that by doing:
for (int i = 0; scanf_s("%d", &array[i]) == 1 && i < 10; i++);
Then the logic of the other two loops is wrong. I initially got wrong what you meant. I thought you wanted to know how many times a number is duplicated. So I wrote the wrong program and then modified it for your purposes. Here is your program:
#include <stdio.h>
int main() {
int n[10];
int dupes[5], d = 0;
int flag = 1, omg;
for ( omg = 0; scanf("%d", &n[omg]) == 1 && omg < 10; omg++);
for (int i = 0; i < omg; i++) {
for (int j = i+1; j < 10; j++) {
if( n[i] == n[j] ) {
if( d > 0 ) {
for(int k = 0; k < d; k++) {
if( n[i] == dupes[k] ) {
flag = 0;
break;
}
}
}
if( flag ) {
dupes[d] = n[i];
++d;
break;
}
else {
flag = 1;
break;
}
} // end outer if
}
}
printf("There are %d numbers that have at least one dupe\n", d);
return 0;
}
I named a variable omg out of desperation, writing this program was a nightmare. (Because it came from the ashes of a previous program)
Your code correctly determines the number of duplicate entries in the array.
If instead you want to determine the number of duplicated values, you must modify the algorithm:
#include <stdio.h>
int main() {
int array[10] = { 0 };
int count = 0;
printf("Enter a maximum of 10 values to store in an array: ");
for (int i = 0; i < 10; i++) {
scanf_s("%d", &array[i]);
}
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (array[i] == array[j]) {
if (i < j)
count++;
if (i != j)
break;
}
}
}
printf("There are %d duplicate values\n", count);
return 0;
}
I use a structure 'Number' which contains the number and its duplicate, then I fill the array and I put it in ascending order then I calculate the number of duplicate of each number and I fill in the strecture like this :
my code:
#include <stdio.h>
#define size 10
typedef struct Number
{
int number;
int duplicate;
}Number;
int main()
{
int array[size];
Number array2[size];
int count = 0;
printf("Enter a maximum of 10 values to store in an array: ");
for (int i = 0; i < size; i++)
{
scanf("%d", &array[i]);
}
int temp=size;
int temppppp=0;
for(int i=0;i<size;i++)
{
for(int j=i+1;j<size;j++)
{
if(array[i]>array[j])
{
temppppp=array[i];
array[i]=array[j];
array[j]=temppppp;
}
}
}
printf("\n\n");
for (int i = 0; i < size; i++)
{
printf("[%d]",array[i]);
}
printf("\n\n");
int i=0;
int j=0;
while(i<size)
{count=1;
while(i<(size-1)&&array[i]==array[i+1])
{
count++;
i++;
}
if(count>=2)
{
array2[j].number=array[i-1];
array2[j].duplicate=count;
j++;
}
i++;
}
int p=0;
while(p<j)
{
printf("\n[%d] has duplicated %d times !\n",array2[p].number,array2[p].duplicate);
p=p+1;
}
printf("\n\n");
printf("\nThere are %d duplicate values\n", j);
}
Assume the matrices to be of the form,
The required output on stdout using C language should be
My code:
#include<stdio.h>
int main(void)
{
int size, i=0, j=0;
printf("Enter the size of the array:");
scanf("%d",&size);
int A[size][size], B[size][size], C[size][size];
//Entering the values in A[][]
printf("enter the elements of Array A:\n");
for(int i=0; i<size; i++)
for(int j=0; j<size; j++)
scanf("%d",&A[i][j]);
//Entering the values in B[][]
printf("enter the elements of Array B:\n");
for(int i=0; i<size; i++)
for(int j=0; j<size; j++)
scanf("%d",&B[i][j]);
// Calculating C[][]=A[][]+B[][]
for(int i=0; i<size; i++)
for(int j=0; j<size; j++)
C[i][j]=A[i][j]+B[i][j];
i=0;
j=0;
while(i<size)
{
if( i==size/2)
printf("%*c%*c\n\n",6*size+1,'+',13*(size-1),'=');
while(j<size)
printf("%d\t",A[i][j++]);
j=0;
while(j<size)
printf("%d\t",B[i][j++]);
j=0;
while(j<size)
printf("%d\t",C[i][j++]);
j=0;
printf("\n\n");
i++;
}
}
But this code only gives the desired output when size=2.
But I need a solution which works for all values of size entered by the user.
you use a tab (8 columns) to go to the next column, so replace
printf("%*c%*c\n\n",6*size+1,'+',13*(size-1),'=');
by
printf("%*c%*c\n\n",8*size-4,'+',8*size,'=');
example :
of course that supposes all numbers need up to 7 columns
If you want to always have 1 line between each line with numbers modify the while to have :
while(i<size)
{
if( i==size/2)
printf("%*c%*c\n",8*size-4,'+',8*size,'=');
else
putchar('\n');
while(j<size)
printf("%d\t",A[i][j++]);
j=0;
while(j<size)
printf("%d\t",B[i][j++]);
j=0;
while(j<size)
printf("%d\t",C[i][j++]);
j=0;
putchar('\n');
i++;
}
producing :
Out of that I encourage you to check scanf always return 1 else you do not detect an invalid input
This code will solve my question,
int size_odd_or_even=size%2;
int check=1;
while(i<size)
{
if( size_odd_or_even==0 && i==size/2 && check==1)
{
printf("\n%*c%*c\n",8*size-4,'+',8*size,'=');
check=0;
continue;
}
if( size_odd_or_even==1 && i== (int)(size/2) )
{
while( j<size-1)
printf("%d\t",A[i][j++]);
printf("%d + ",A[i][j]);
}
else
{
while( j<size)
printf("%d\t",A[i][j++]);
}
j=0;
if( size_odd_or_even==1 && i== (int)(size/2))
{
while( j<size-1)
printf("%d\t",B[i][j++]);
printf("%d = ",B[i][j]);
}
else
{
while(j<size)
printf("%d\t",B[i][j++]);
}
j=0;
while(j<size)
printf("%d\t",Sum[i][j++]);
j=0;
if( size_odd_or_even==0 && !(i==size/2-1) )
printf("\n\n");
else if(size_odd_or_even==1)
printf("\n\n");
i++;
}
This code gives the outputs for size=4 and size=5 respectively as,
If this code can be optimised then please do tell.