Reputation: 19
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <locale.h>
int main()
{
setlocale(LC_ALL, "Rus");
float arr[25];
int i;
double x;
int a, b, c, e, f;
double y = a * pow(x, 2) + b * x + c;
printf("a: ");
scanf_s("%d", &a);
printf("b: ");
scanf_s("%d", &b);
printf("c: ");
scanf_s("%d", &c);
printf("e: ");
scanf_s("%d", &e);
printf("f: ");
scanf_s("%d", &f);
double interval = (f - e) / 25.0 ;
for (int i = 0, double x = e; i < 25; i++, x += interval)
{
printf("%f", y);
x++;
}
system("pause");
}
I get [Error] expected identifier or '(' before 'double'. How can i fix it? It doesnt seem like i really need to change something in
for (int i = 0, double x = e; i < 25; i++, x += interval)
or maybe im wrong and dont know how to write multiple conditions.
Upvotes: 0
Views: 629
Reputation: 3460
You could embedded them into a struct. I do not recommend it because IMO it is not a good coding practice as it is not easy to understand (at first sight)…
typedef struct {int i; double x;} S;
for (S yourStruct = {0,e}; yourStruct.i < 25 ; yourStruct.i++, yourStruct.x += interval)
{
printf("%f", y);
yourStruct.x++;
}
Upvotes: 1
Reputation: 85757
Yeah, you can't do that.
By the way, those are declarations, not conditions. Only the middle part of a for
loop is a condition.
You can declare multiple variables in the first part of a for
loop, but only if they have the same base type (e.g. int
):
for (int x = 1, y = 2, z = 3; ...; ...)
The workaround in your case is to declare at least one of the variables outside of the loop:
{ // this outer block limits the scope of x
double x = e;
for (int i = 0; i < 25; i++, x += interval)
{
printf("%f", y);
x++;
}
}
That said, your code doesn't really make sense. Your loop doesn't use x
, so there's no point in setting it. On the other hand, the value you're printing 25 times (y
) doesn't change in the loop. It's set at the top of your main function, computed from a different x
variable that is uninitialized.
You should move the declaration and initialization of y
into the loop and delete the outer x
. See also https://stackoverflow.com/a/53238897/1848654.
Upvotes: 2
Reputation: 25518
It is not really a matter of the for loop:
void f(void)
{
int x, y; // legal - and you can do the same in a for loop's initialization section
int z, double d; // can't do that either, even outside for loop...
};
All variables that you declare in a single expression need to have the same (base!) type, be it within for loop, function body or global. 'Base type': Well, because you legally can do stuff like int x, *p;
, with x
and p
being of different type, but base/underlying type in both cases is int
.
Upvotes: 0
Reputation: 97918
You can't define variables with multiple types with the comma:
for(int i = 0, double x...
Instead:
x = e;
for (int i = 0; i<...
and the x
is already defined above.
Upvotes: 1