What is exactly dangling-else problem in c? [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
How can the code work like this?
Which if-else statements are linked with each other?
So why is the output like that "$$$$$"?
#include <stdio.h>
int main() {
int x = 11;
int y = 9;
if(x<10)
if(y>10)
puts("*****");
else
puts("#####");
puts("$$$$$");
return 0;
}

Save time. Use an auto formatter.
Hopefully then "why is the output like that "$$$$$"?" is self apparent.
#include <stdio.h>
int main() {
int x = 11;
int y = 9;
if (x < 10)
if (y > 10)
puts("*****");
else
puts("#####");
puts("$$$$$");
return 0;
}

Related

find max of some matrix elements in C [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
so I got a task at my Uni a week ago:
There is n*n matrix given which is a square matrix, but I have to find max of these elements on the picture, question is how to do it ?
Well, I won't just do your homework but here is some code that you can consider a hint to get you started.
#include <stdio.h>
void printRelevant(int n)
{
for(int r=0; r<n; ++r)
{
for(int c=0; c<n; ++c)
{
if (r < n/2 || c > r || c < n-r-1)
{
printf("-");
}
else
{
printf("X");
}
}
printf("\n");
}
}
int main(void) {
printRelevant(17);
return 0;
}
Output:
-----------------
-----------------
-----------------
-----------------
-----------------
-----------------
-----------------
-----------------
--------X--------
-------XXX-------
------XXXXX------
-----XXXXXXX-----
----XXXXXXXXX----
---XXXXXXXXXXX---
--XXXXXXXXXXXXX--
-XXXXXXXXXXXXXXX-
XXXXXXXXXXXXXXXXX

C code for finding 1(factorial)2(fac)+2(fac)3(fac)+...9(fac)10(fac) [closed]

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 5 years ago.
Improve this question
I am new to programming and have just started with array. This was my code for the problem
#include <stdio.h>
int main(){
int i=1,f=1,num,fac[10],sum=0;
for (num=1; num<=10; num++) {
for (i; i<=num; i++) {
f = f * i;
}
fac[num-1]=f;
}
for(i;i<=9;i++)
sum = sum + fac[i] * fac[i+1];
printf("The sum is %d",sum );
return 0;
}
Output it is giving is-The sum is 0
So what are the corrections to be made or any other code for the problem?
You need to reinitialize the variable i more often.
Instead of having a for loop that looks like for (i; i<=N; i++), initialize i and do for (i=0; i<=N; i++)

Print shape of numbers using arrays [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to print the shape of numbers using 2D arrays. What I am saying is just like this. Think I want print num 2.
111111
11
111111
11
111111
I already tried, starting like this, but I cant build it further.
#include <stdio.h>
int main(){
int Num[5][6]= {{111111},
{000011},
{111111},
{110000},
{111111}};
int i,j;
for(i=0;i<6;i++){
for(j=0;j<7;j++){
printf("%d",Num[i][j]);
}
}
return 0;
}
Instead of spaces i have included "0" in my code.
You probably want this:
#include <stdio.h>
int main() {
int Num[5][6] =
{
{ 1,1,1,1,1,1 },
{ 0,0,0,0,1,1 },
{ 1,1,1,1,1,1 },
{ 1,1,1,1,0,0 },
{ 1,1,1,1,1,1 }
};
int i, j;
for (i = 0; i<5; i++) {
for (j = 0; j<6; j++) {
if (Num[i][j] == 1)
printf("1");
else
printf(" ");
}
printf("\n");
}
return 0;
}
Disclaimer: there is still room for improvement. The proposed solution is as close as possible to the original code.

What code is faster? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
We have the following 2 snippets of code in c that do the same task.
CODE #1:
int b = 0;
for (int i = 0; i < len; i++)
{
if (x1 == x0[i])
{
if (y1 == y0[i])
{
b = 1;
break;
}
}
}
CODE #2:
int b = 0;
for (int i = 0; i < len; i++)
{
if (x1 == x0[i] && y1 == y0[i])
{
b = 1;
break;
}
}
What faster CODE #1 or CODE #2?
I really searched answer in the internet but did not find anything.
None!
They are both the same code.
They are written differently, but take the exact same instructions and comparisons to achieve the result, therefore, they are the same.
So, none of them is faster than the other.

How to print char value in int array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have an array board[3][3] of type int. I want to show 8 numbers and on the last place "_" must be shown. I don't know how to do that. Please help
If the 8 numbers you want print is the first 8, you can just use a loop to with printf to print it until the last number:
int j, k;
for (j = 0; j < 3; j++) {
for (k = 0; k < 3; k++) {
if (j == 2 && k == 2)
printf("_");
else
printf("%d ", board[j][k]);
}
printf("\n");
}

Resources