Please Explain me this C code [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 5 years ago.
Improve this question
Can some one please help me on understanding this code especially the 'space' part. How is value of space in spite being 4 printing no space in the first row of the output of this code:
#include <stdio.h>
/* Inverted mirrored right triangle */
int main(int argc, char *argv[])
{
int i, j, space;
for (i = 0; i <= 5 ;i++) {
for (space = 5 - i; space <= 4; space++) {
printf(" ");
}
for (j = 5 - i; j >= 0; j--) {
printf("*");
}
printf("\n");
}
return 0;
}

concentrate on this piece of code
for (i = 0; i <= 5 ;i++) {
--> for (space = 5 - i; space <= 4; space++) {
printf(" ");
}
in first iteration of inner for loop space value is space: 5-0 = 5
as the i:0 and for we have condition space <= 4 and this not satisfy control does not
go in to the inner of for braces and not print any space (for first row)
you must know how the for(;;) works.
for (i=0 ; i<10 ; i++ ) {
printf("%d",i);
}
// end loop line
for (loop index initialize ; condition ; do after innerloop commands ) {
command1;
command2;
}
first of all i:0 and because i is less than 10 inner of loop executes
and print 0 on screen.
after printing i are incremented by one (i++ equals to i = i+1).
then i:1 , i still less than 10 and print i:1, wee see 1 on screen and so
on. until we have i:9 print 9 on screen then increase by one i:10
then last iteration come, as now i:10 and is not less than 10 the inner of
loop not execute, program control comes to after loop (end loop line).

Related

Nesting For loop in c , at i = 4 ; j will be 2 how does it satisfy the condition? [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 last year.
Improve this question
{
for (int i = 0; i <= 5; i++)
{
for (int j = 5; j >= i; j--)
{
printf("+");
}
printf("\n");
}
}
At i = 4; j will be 2 how does it satisfy the condition of j>=i?
You seem to be thinking that "j will be 2" because you see two "+" in output in that line.
But that only means that the inner loop is executed twice. Which is because (quoting Barmars comment):
When i == 4, the inner loop will only loop twice, with j == 5 and j == 4.
You analysed the detailed behaviour of your code based on output, which is good. But if something puzzles you then you either need more output on exactly the detail which puzzles you; e.g. by actually outputting the value in question for verifying your assumptions. Or you could use a debugger.
For example. I changed your code in a way which probably makes things very obvious:
#include <stdio.h>
int main(void)
{
for (int i = 0; i <= 5; i++)
{
printf("%d: ", i);
for (int j = 5; j >= i; j--)
{
printf("%d", j);
}
printf("\n");
}
}
It gets you an output (e.g. here https://www.onlinegdb.com/online_c_compiler ) of:
0: 543210
1: 54321
2: 5432
3: 543
4: 54
5: 5
Where the line 4: 54 indicates two inner loop iterations, with values for j of 5 and 4, while i is 4.

Cs50 Mario: How can I improve this code? [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 5 years ago.
Improve this question
It took me all day to figure this out but even though I completed it I don't fully understand how it works and I feel there is a better or cleaner way of writing it.
Can some one explain how I can improve my code?
#include<cs50.h>
#include<stdio.h>
int main(void)
{
int rows, height, spaces, hashes;
do
{
printf("Height: ");
height = get_int();
}
while(height < 0 || height > 23);
for(rows = 0 ; rows < height; rows++)
{
for(spaces = height - 1; spaces > rows; spaces--)
{
printf(" ");
}
for(hashes = 0; hashes < spaces + 2; hashes++)
{
printf("#");
}
printf("\n");
}
return 0;
}
It is best if you first try to understand what the code is doing. Work it through on paper, starting with the input 1.
If you are keen you might like to explore the printf width and precision formatting, where each * in the format specifier is replaced by the corresponding function argument. But beware - printf is a complicated and detailed function. This also introduces the array.
char hatch[] = "##############################";
for(rows = 0; rows < height; rows++) {
printf("%*.*s\n", height + 1, rows + 2, hatch);
}
For the input of 1 your program prints
##
For the input of 5 it prints
##
###
####
#####
######
and so does this. The loop contains a single instruction.

Why are the wrong array values being printed? [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 6 years ago.
Improve this question
For some reason, the nested for loop I have created at the bottom seems to be printing out the wrong first value (Gives me 3, when it should be 8). Yet, when I simply do printf (at the bottom), I am given the right value. Not really sure what's wrong with my code.
#include <stdio.h>
int main(void)
{
int d;
printf("Please input dimensions: (between 3 and 9, inclusive): \n");
scanf("%i", &d);
int array[d][d];
int k = 1;
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
array[i][j] = (d * d) - k; //d^2 doesn't work to square a function
k++;
}
}
for (int z = 0; z < d; z++)
{
for (int y = 0; y < d; y++)
{
printf("%i\n", array[z][y]);
}
}
printf("%i\n", array[0][0]);
printf("%i\n", array[0][1]);
}
Edit: Sorry guys, the top value that was being printed was my own input. I was simply thinking it was the first value being printed.
Are you sure you aren't looking at your own input? I cut and paste and see:
Please input dimensions: (between 3 and 9, inclusive):
3
8
7
6
5
...
The 3 is actually what I typed and is echoed.

Continue n iterations [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 6 years ago.
Improve this question
I'd like to use continue statement (parametrized) n times:
int n = 7;
while(running()) {
commandA;
if(something) continue; // <- not once, but n times
commandB;
...
}
I'd like to use something like for(int i=0; i<n; ++i) continue; but the continue should be applied to the outer (while) loop. I'd like to skip n passes of the while loop.
The purpose is to always execute commandA, but skip n times commandB if running() condition is satisfied.
Is it possible to code that in C?
You could use an extra variable, like this, if I understood correctly what you are trying to achieve:
#include <stdio.h>
int main(void) {
int max_skip = 7;
int i = 0;
int something;
while(i < 10) {
something = i % 2;
if(something && max_skip-- >= 0)
continue;
++i;
}
return 0;
}
You short circuiting will come into play (as I explained here), which will protect max_skip from decreasing.
One way would be:
int n = 7;
while(running()) {
commandA;
if (n) { --n; continue; }
commandB;
...
}
continue can only be used to jump to the next iteration of the loop it appears in. You can't parametrize it, and you can't make it apply to any other loop. You will have to rewrite your loop.

For loop enhanced but with ambiguities [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 8 years ago.
Improve this question
All I want to know is whether this statement is feasible or not
for(j = 2; (j <= i) && flag; j++)
flag is initialized to i before this loop. I have not seen any such thing before.
The general for loop condition is like this:-
for(initialization ; condition; increment)
So what you are doing is correct.
Breaking down your for loop means:-
for(j=2;(j<=i)&& flag ;j++)
initialization is j=2;
condition is (j<=i)&& flag ;
increment is j++
One example:-
int main(int argc, const char * argv[])
{
int sum = 0;
int j = 100;
for(int i = 1; i<=100/2 && j>100/2; i++){
sum += i+j;
j--;
}
return sum;
}
Second Example with flag:
Remember bubble sort, In bubble sort we need two nested loops, outer loop runs for number of passes and inner loop do swapping task for each pair a[i], a[i + 1]. To save execution we can make use of some flag variable. If in some pass no swapping done this means no need to execute next pass and sorting completeed, read: Optimizing bubble sort:
Now code for this:
FLAG = 1;
for(i = 0; FLAG && (i < n - 1); i++){//If flag = ), break outer loop sorting done
FLAG = 0; // set flag = 0
for(j = 0; j < n - 1 - i; j++){
if(arr[j] > arr[j + 1]){
swap(arr[j], arr[j + 1]);
FLAG = 1; // if any swapping need, then check in next round
}
}
}
Notice outer loop condition FLAG && (i < n - 1), I think this is what you wants. Hope this help!

Resources