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.
Related
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;
}
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
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++)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I've been trying to learn C by myself for the past two days and I can't seem to get this program to run. Sorry for this probably trivial question, just starting out and can't seem to find a quick answer.
#include <stdio.h>
void chopper() {
int z = 0;
while (z < 10) {
printf("They equal and this code works!");
z++;
}
}
int main() {
int x = 0;
int flag = 0;
if (flag == 1) {
chopper();
}
for (int x; x < 10; x++) {
printf("%d\n", x);
if (x == 10) {
flag == 1;
}
}
return 0;
}
You have multiple issues.
flag == 1; inside if is useless. Probably you meant and want flag = 1;.
In the for loop, x is uninitialized.
The outer scope x is unused.Note
What you want is to rewrite the for loop statement as
for (x; x < 10; x++)
or,
for (; x < 10; x++)
to make use of the outer x variable. As per the code shown, you don't need two separate variables anyway.
Note: To understand more about scope, please refer to this previous Q&A.
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
I'm new to C Algorithm and come to ask for some help.
I hope to check whether or not an element exists in a table, can anyone give me some good algorithm? What I do is a cycle and a flag, then quit the cycle and verify the flag. But it looks like stupid, so I guess there would be more efficient algorithm. My code is following:
int j=0;
u8_t next_header[]={0x11, 0x22};
for(i = 0; i < sizeof(next_header); ++i)
{
if (buf[6] != next_header[i])
continue;
else
++j;
}
if(j == 0)
{
// execution
}
else
{
// execution
}
pack it in a function, so you can jump out of the loop using return as soon as the element is found:
int search_for_elements(int element)
{
int i;
u8_t next_header[]={0x11, 0x22};
for(i = 0; i < sizeof(next_header); ++i)
{
if (element == next_header[i])
return 1; // found the element;
}
return 0; // :( no element found
}
Yes, if your table is a simple array looping is the way to go. That said, the usual pattern is to use a break statement instead of a continue.
int found = 0;
for(i=0=; i<the_array_length; i++){
if(array[i] == the_element_i_am_searching){
found = 1;
break;
}
}
Also, its more idiamatic to test booleans with if(i) instead of if(i != 0). (That said, if your variable is a number that is not always 0 or 1, like your j , I would keep the explicit comparison for clarity)