Please explain the code? - c

int main()
{
int N, K, i, j, k, x, final, cur, A[22];
for(i=!!scanf("%d %d",&N,&K), printf("%d\n",(final=N*N)-N);i<=N;A[i++]=i);
for(i=(cur=N)-1; i>=1; i--)
for(j=1; j<=i; printf("%d %d min\n%d %d max\n",A[j],A[j+1],A[j],A[j+1]),A[j]=++cur, A[j+1]=++cur, j++);
for(printf("%d",final-1+(cur=final)*0+(x=2)*0); cur>N; printf(" %d",cur), cur-=x, x+=2);
return 0;
}
Please explain the use of 2 exclamation marks in the first "for" statement.

I will just explain first for loops, last 3 for loops is easy to understand.
Step by step explanation.
Step 1:
for(i=!!scanf("%d %d",&N,&K), printf("%d\n",(final=N*N)-N);i<=N;A[i++]=i);
Here, scanf("%d %d",&N,&K) returns 2 (number of integer read successfully.)
Step 2: single negation, !2 = 0, now negate this 0, !0 = 1. So, i = !!2 = 1
Step 3: Suppose your input was 3 5 [N=3, K=5]. so output of printf("%d\n",(final=N*N)-N) will be final = (3*3)-3 = 6
Step 4: Checking condition: i<=N means, is 1<=N? If yes then this loop continues until the condition becomes false. in the process as A[i++] = i, first the index i of A is set then i increases by 1, then the value of A[i] is set to i. So finally you get the following array:
A[1] = 2;
A[2] = 3;
A[3] = 4;

Step 3: Suppose your input was 3 5 [N=3, K=5]. so output of printf("%d\n",(final=N*N)-N) will be (final = (3*3)-3) = 6(output) ,final =6.

Related

How to make a series that adds up by 5 every 5 counts using for loop?

I'm kind of new in C programming and I'm trying to make a program that prints the nth term of a series and every 5 counts, it adds up by 5.
Example: 1, 2, 3, 4, 9, 10, 11, 12, 17, 18, 19, 20, 25......
Here is my code
int num,digit,count = 1;
printf("Enter n: ");
scanf("%d", &num);
for(int i=1; i<=num; i++){
count++;
if(count > 5){
count = 0;
i+=4;
}
printf("%d ",i);
}
My code doesn't get to the specific nth term that I'm asking for. For example, I've inputted 10 and it only shows up until the 6th term
The thing to do is get clear in your head what you want to do and how you are going to do it. Rather than tinkering with code, break things down into simple parts and make them clear.
#include <stdio.h>
int main(void)
{
int num = 10;
// Method 1: Spell everything out.
// i counts number of terms printed.
// j counts number of terms since last multiple of four terms.
// k is current term.
for (
int i = 0, j = 0, k = 1; // Initialize all counters.
i < num; // Continue until num terms are printed.
++i) // Update count.
{
printf("%d ", k); // Print current term.
++j; // Increment four-beat count.
if (4 <= j)
{
// Every fourth term, reset j and increment the term by 5.
j = 0;
k += 5;
}
else
// Otherwise, increment the term by 1.
k += 1;
}
printf("\n");
// Method 2: Use one counter and calculate term.
// Iterate i to print num terms.
for (int i = 0; i < num; ++i)
/* Break the loop count into two parts: the number of groups of 4
(i/4) and a subcount within each group (i%4). Looking at the
starts of each group (1, 9, 17, 25...), we see each is eight
greater than the previous one. So we multiply the group number by
8 to get the right offsets for them. Within each group, the term
increments by 1, so we use i%4 directly (effectively multiplied by
1). Then (i/4)*8 + i%4 would start us at 0 for i=0, but we want to
start at 1, so we add 1.
*/
printf("%d ", (i/4)*8 + i%4 + 1);
printf("\n");
}
You shall not change the variable i within the body of the for loop.
You need to introduce one more variable that will store the current outputted number.
Here is a demonstration program.
#include <stdio.h>
int main(void)
{
unsigned int n = 0;
printf( "Enter n: " );
scanf( "%u", &n );
for ( unsigned int i = 0, value = 0, count = 1; i < n; i++ )
{
if ( count == 5 )
{
value += 5;
count = 1;
}
else
{
++value;
}
printf( "%u ", value );
++count;
}
}
The program output is
Enter n: 13
1 2 3 4 9 10 11 12 17 18 19 20 25
Here's another take that I think is a bit less complicated, with explanations in the comments:
#include <stdio.h>
int main(void)
{
int num, count = 1;
num = 20;
// if you look closely, you actually have an initial condition before the
// main pattern starts. Once the pattern starts, its actually every _fourth_
// term that gets added by 5. You'll make things easier on yourself if you
// print out this initial condition, then handle the pattern in the loop.
// If you really want to be correct, you can wrap this in a if (num > 0) check
printf("%d ", count++);
// start at 1, because we already printed the first item
for(int i=1; i<num; i++, count++)
{
// now we can focus on every fourth term
if (i % 4 == 0)
{
// if it's the fourth one, add 5 to the previous value
// Of course this simplifies to count += 4
count = (count-1) + 5;
}
printf("%d ",count);
}
}
Demonstration

How do I fix the problem with the incorrect array value in for loop using C language? [duplicate]

This question already has answers here:
How dangerous is it to access an array out of bounds?
(12 answers)
Closed 2 years ago.
I am trying to understand how array boundaries work in C, so tried the following code:
#include <stdio.h>
int main(){
{
int i, s[4], t[4], u=0;
// fisrt loop
for (i=0; i<=4; i++)
{
s[i] =i;
t[i] =i;
printf("s = %d\n", s[i]);
printf("t = %d\n", t[i]);
}
// second loop
printf("s:t\n");
for (i=0; i<=4; i++)
printf("%d:%d\n", s[i], t[i]);
printf("u = %d\n", u);
}
return 0;
}
The output is as follows :
fisrt loop
s = 0
t = 0
s = 1
t = 1
s = 2
t = 2
s = 3
t = 3
s = 4
t = 4
second loop
s:t
4:0
1:1
2:2
3:3
4:4
u = 0
I am expecting both loops to print 5 elements from 0 to 4. As you can see, the first for loop's output looks ok, but in the second loop the value of s[0]
is wrong and for some reason turned to 4.
I am assuming that this is happening because of some problem in the array boundary, but I am not sure, please correct me if I am wrong. Is there any way to fix this, and make s[0] correct value? Cheers.
for (i=0; i <4; i++)
in your code you access 5 elements of the array and your array has only 4 elements. It ans Undefined Behaviour.
Second loop the same error.
There is a problem with the size of the array, you need to modify your code of that line to given code:
int i, s[4], t[4], u=0;
to
int i, s[5], t[5], u=0;
Delete the equals in the for loops:
for (i = 0; i < 4; i++)
The length of the arrays is 4. From 0 to 4 (included) there are 5 positions.
When you declare int s[4]; you create an arrays of lenght 4: s[0], s[1], s[2], s[3]. You can't access s[4].
This is because the position starts to 0.
If you want to have 5 elements for both arrays:
Change the declarations of s and t to s[5] and t[5]. Only at the declaration the 5 means 5 elements. Only at indexing and addressing a certain element, counting starts at 0, not 1.
If you want to have the arrays to be consisted of 4 elements:
for (i = 0; i <= 4; i++)
i <= 4 - With this condition and the initialization i = 0 you access memory beyond the bounds of the array at the last iteration, which invokes undefined behavior.
When i == 4, s[i] and t[i] is an out of bounds access as s and t are both arrays of 4 int, not 5.
It needs to be i < 4 or i <= 3 for the arrays of 4 int or (if you want to) or change the initialization to i = 1.

Finding the correct order from a set of dependencies

so here's my problem:
Input:
N, the total number of steps in the first line. Each step is implicitly numbered from 0 to N-1. A number K in the second line. Then the next K lines contain 2 integers each. Each such line represents a dependency: If "X Y" is in a line, then step X must necessarily be performed before step Y.
Output:
One line containing the steps in the correct order. If multiple such correct orders are possible, then print the one in which the smaller numbers come as early as possible.
Example:
3
2
1 0
0 3
1 0 3
Here's my code so far. I dont know how to compare the elements after sorting them so as to obtain the required format. Any leads would be appreciated .. thanks.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(){
int n,k,i,j,temp,a=2;
scanf("%d",&n);
scanf("%d",&k);
int arr[k][2];
int ans[n];
for(i=0;i<k;i++)
scanf("%d %d",&arr[i][0],&arr[i][1]);
for(i=0;i<k;i++){
for(j=i+1;j<k;j++){
if(arr[i][0]>arr[j][0]){
temp = arr[i][0];
arr[i][0] = arr[j][0];
arr[j][0] = temp;
temp = arr[i][1];
arr[i][1] = arr[j][1];
arr[j][1] = temp;
}
}
}
/*
for(i=0;i<k;i++)
printf("%d %d\n",arr[i][0],arr[i][1]);
*/
ans[0] = arr[0][0];
ans[1] = arr[0][1];
/*done[n];
for(i=0;i<n;i++)
done[i] = 0;
done[arr[0][0]] = 1;
done[arr[0][1]] = 1;
for(i=1;i<k;i++){
if(done[arr[i][0]] == 0 || done[arr[i]])
}
*/
for(i=0;i<n;i++)
printf("%d ",ans[i]);
printf("\n\n");
return 0;
}

print 2 numbers and skip 2 numbers in an array in C

I'm having a bit of difficulty trying to figure out a way to print some numbers in an array. I have an array[0,1,2,3,4,5,6] and I would like print the numbers 0,1,4,5. Is it possible to create a loop that can read the first two numbers skipping the next two and reading the following two numbers.
You can simply use modulo operation on current index to check if this number belongs to "print 2" or "skip 2":
int a[17];
int length = sizeof(a) / sizeof(a[0]);
for (int i = 0; i < length; i++)
{
if (i % 4 < 2)
printf("%d ", a[i]);
}
So, for i equal to 0 and 1, it will output value. For i == 2 and i == 3, the condition will result to false. Next, it will take 4, 4 % 4 is 0, and it will repeat it every 4 steps.
Pseudocode:
arr = [0,1,2,3,4,5,6];
skip = 2;
print = true;
while(i < arr.length){
for(j = 0; j < skip; ++j){
if(print){
output arr[i];
}
//increment array counter
i++;
//toggle print bool
print = !print
}
}
Just change the value of skip to set the interval, and set print = false if you'd like it to skip the first skip entries

nested loop comparing ints in an array

this is what I have. it is skipping over the zero. how can I fix that? I'm trying to count the number of times the numbers are duplicated.
void hit_rate(int a, int cmset[])
{
int i, j, k=0;
for(i=0;i<a;i++){
for(j=i;j<a;j++){
if((cmset[i] == cmset[j])){
k++;
}
}
printf("%d\n",k);
k=0;
}
}
cmset k **now** prints
4 2
6 1
0 3
0 2
0 1
1 1
2 1
4 1
While counting duplicates,
e.g. arr[5] = {1, 2, 2, 3, 3};
start with
i = 0; // first loop
j = i; //2nd loop
comapre arr[i] == arr[j]; //condition
By this what happens if you have tested arr[0] with all i = 1..4;
in next iteration, you have need not to check a[1] with arr[0], because it's already done (or checked).
increase the counter(when duplication matches). once it ends the end of the array reset counter. and print it.
I hope it helps. Still confused then I will provide you sample code.
Should be for(j=0;j<a;j++)

Resources