Code not running but compiles? [closed] - c

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.

Related

its rather basic I need help to understand c and my programm keeps relling me there's an error. it keeps saying error: expected ‘;’ before ‘)’ token [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 1 year ago.
Improve this question
Here is the code that i need help with
#include <stdio.h>
int
main ()
{
int x = 10, y = 20, z;
for (z = 0, z < y; z++)
{
if (z == x)
{
printf ("%d\n", z);
}
else
{
break;
}
}
In for loop you can't use ',' to separate declarations.
It should be for( z=0; z<y; z++)
and also if you use break; it will terminate in 1st iteration.
So correct program is
#include <stdio.h>
int main ()
{
int x = 10, y = 20, z;
for (z = 0; z < y; z++)
{
if (z == x)
{
printf ("%d\n", z);
}
else
{
}
}
}
There are two issues as I can see:
You have missing curly braces, to properly enclose the scopes.
The syntax of the for loop is not correct. Simply
for (z = 0, z < y; z++)
should be
for (z = 0; z < y; z++)
^^-----------------------the , is to be ;
That said, for a hosted environment, the usual signature of main() is int main(void), in case you want to ignore the command line arguments.

What is causing a segmentation fault in this function? [closed]

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 5 years ago.
Improve this question
So, as an exercise, I am developing a code that uses a recursive function in order to make a simple mathematical expressions calculator. The problem is, I am getting a segmentation fault (6) or (11) when I run it, but I have checked a hundred times and each function call seems to be accessing memory only from the variables in the function just above it on the stack, provided by the pointer *init. Where am I getting this wrong?
Code is as follows:
int solve(char *expression, int *init) {
int result;
int l = strlen(expression);
int i = *init;
//Inicializing result:
for (int n = 0; n <= l; n++) {
if ((expression[n]=='1')||(expression[n]=='2')||(expression[n]=='3')||(expression[n]=='4')||(expression[n]=='5')||
(expression[n]=='6')||(expression[n]=='7')||(expression[n]=='8')||(expression[n]=='9')) {
result = expression[n]-48;
break;
}
}
//Doing calculations:
int j = i;
for (j; j <= l; j++) {
if (expression[j] == '(') {
result = result + solve(expression, &j);
}
if (expression[j] == '+')
result = result + (expression[j+1]-48);
if (expression[j] == '-')
result = result - (expression[j+1]-48);
if (expression[j] == '*')
result = result * (expression[j+1]-48);
if (expression[j] == '/')
result = result / (expression[j+1]-48);
if (expression[j] == ')')
return result;
}
return result;
}
There is infinite recursion in the code
int solve(char *expression, int *init) {
/* ... */
int j = i;
for (j; j <= l; j++) {
if (expression[j] == '(') {
result = result + solve(expression, &j);
/* ... */
If it ever founds a '(' in expression, then the function is called over and over again with the same parameters, until the program stack is filled up. Then you get a segfault.
When using loops or recursion, you should always make sure that the construct eventually terminates, i.e. it gets demonstrably closer to the terminating condition on every iteration. It's trivial to see when doing a simple loop like for(i=0;i<x;i++), but considerably harder when the iteration and the terminating conditions are scattered over a recursive function.
It's not the only problem with your code, but arguably the most severe one.

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 inside a for loop with if else condition [closed]

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 7 years ago.
Improve this question
for (x = 0; x < 4; x++) {
for (int a = 22; a <= 62;) {
if (isCooked[x] == 1) {
gotoxy(a,3); printf("cooked");
gotoxy(a,4); printf("%-10s",food[userServings[x]]);
a += 12;
} else {
gotoxy(a,3); printf("!");
gotoxy(a,4); printf("%-10s",food[userServings[x]]);
a += 12;
}
}
}
input
output
May I ask whats wrong with loop above and conditions. I'm trying to print the name of the 4 vegetables that I have chosen. By using gotoxy I want to print them on the given coordinates on my loop.
In c you can't declare variable anywhere. You have declared int a in the inner for loop.
in your program x is not declared
Instead of a second loop, you should just compute the gotoxy coordinate:
for (x = 0; x < 4; x++) {
int a = 22 + x * 12;
if (isCooked[x] == 1) {
gotoxy(a, 3); printf("cooked");
gotoxy(a, 4); printf("%-10s", food[userServings[x]]);
} else {
gotoxy(a, 3); printf("! ");
gotoxy(a, 4); printf("%-10s", food[userServings[x]]);
}
}

my C code is too awkward [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Assume the character a,b,c,d,e represent the number 1 to 9, and they cannot be equal to
each other.
Question:
How many equals that can meet (ab * cde = adb * ce).
Example:
36 * 495 = 396 * 45.
Here is my code,and the result is right.However,i think my code is too awkward,especially in (if(a!=b&&a!=c&&a!=d&&a!=e&&b!=c&&b!=d&&b!=e&&c!=d&&c!=e&&d!=e&&c*d*e!=0))
I would appreciate it if someone could give me a better solution.
#include<stdio.h>
main(){
int a,b,c,d,e,m,n,i=0;
long f1,f2,f3,f4;
for(m=11;m<=99;m++){
a=m/10;
b=m%10;
if(a!=b&&a*b!=0)
{
for(n=101;n<=999;n++)
{
c=n/100;
d=n%100/10;
e=n%10;
if(a!=b&&a!=c&&a!=d&&a!=e&&b!=c&&b!=d&&b!=e&&c!=d&&c!=e&&d!=e&&c*d*e!=0)
{
f1=a*10+b;
f2=c*100+d*10+e;
f3=a*100+d*10+b;
f4=c*10+e;
if(f1*f2==f3*f4) i++;
printf("\n%d%d*%d%d%d*=%d%d%d*%d%d\n",a,b,c,d,e,a,d,b,c,e);
}
}
}
}
printf("%d\n",i);
return 0;
}
If you can, instead of
int a,b,c,d,e;
Try to use
int numbers[5];
And then to check if your numbers are all different, you can use for loops
doubleOccurence = FALSE; /* where FALSE = 0 */
for (i=0; i < 4; i++) {
for (j=i+1; j < 5; j++) {
doubleOccurence = doubleOccurence || (numbers[i] == numbers[j]);
}
}
It looks a bit clearer to me.
Unfortunately you can't really iterate through a list of variables you are better off with an array of numbers like Julien mentions in his answer.
int nums[5];
replace a with nums[0], b with nums[1], etc....
But then I would go one step further to tidying up your code and call a function that takes in the array to check uniqueness:
if(listIsUnique(nums, 5)) // yes hardcoded the 5, but that can be sorted
{
...
}
And then:
bool listIsUnique(int* nums, int len)
{
for (int i = 0; i < len; i++)
for (int j = i + 1; j < len; j++)
if (nums[i] == nums[j])
return false; // return false as soon as you find a match - slightly faster :)
return true; // if we get here its a unique list :)
}
Note: code is untested, there may be mistakes :o

Resources