2
I wrote an algorithm in C to write on the screen ordered pairs of a function, with input of x integer numbers.
int main(int argc, char *argv[])
{
int x,i;
x = -1;
do{
system("cls");
scanf("%i",&x);
if(x>1000 || x <=0){
printf("Entrada Invalida.");
getch();
}
}while(x>1000 || x<=0);
//scanf("%i",&x);
float y[x];
for(i=0;i<=x;i++){
y[i]= pow(i,3) + pow(i,2) +i;
printf("(%i,%.2f)\n",i,y[i]);
}
system("PAUSE");
return 0;
}
I’ve put restrictions on entry, but the restrictions are just numerical. It turns out, with the previous code, if I type "Why" instead of a number, I get the expected result: "Invalid entry". But with the following code:
int main(int argc, char *argv[])
{
int x,i;
x = -1;
//scanf("%i",&x);
float y[x];
for(i=0;i<=x;i++){
y[i]= pow(i,3) + pow(i,2) +i;
printf("(%i,%.2f)\n",i,y[i]);
}
system("PAUSE");
return 0;
}
I get the following:
Why does this happen? Why is the constraint, even if it is only numerical, the result is "Invalid entry"? Why when there is no constraint the program calculates values, even though the input is string("Why")?
Output of a program that reads an integer number and writes it as float: {Why --> 2686792.000000}. When typing "Why" it prints that number.
– Gustavo Viana
the first part of his answer clarifies the fact that the condition is respected. If "Why" is equivalent to 26786792 then it is greater than 1000. I tried to vote for your answer , but I only have 6 points on the site.
– Gustavo Viana