This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I'm trying to print out an array of 100 lf values into columns of ten. I was planning on inserting a new line whenever I hit the tenth data value in each line, but I'm having trouble with my if statement. "Left operand must be 1-value" is the error I keep running into.
Here's the code as is:
for (x=0; x<100; x++)
{
if (x % 10 = 0)
{
printf("\n");
}
printf("|%-6.2lf|", i[x]);
}
Is there a cleaner way to do this?
The problem is this line:
if (x % 10 = 0)
That should be the double-equals sign:
if (x % 10 == 0)
A common typo.
Related
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I'm trying to covert an int to char. Is there any way to do that?
For example:
{
int i;
char d;
i = 55;
d = i;
printf("%c\n", d);
}
How do I make d = 55?
If you want to put the number 55 into a string, use sprintf
Indeed your example can do what you want.
If you really want to place safe, you may:
d = (char) i;
Try this code segment:
printf("%d\n", d);
char are presented in the memory as binary format wich is equivalent to a number and this number is called a code ascii. when you print the code ascii with "%c" Then it will print the charchter equivalent to this code ascii
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
int main() {
int i;
int four_ints[4];
char* c;
for(i=0; i<4; i++) four_ints[i] = 18;
c = (char*)four_ints;
for(i=0; i<4; i++) c[i] = 24;
printf("%x\n", four_ints[2]);
}
So if I print like that it will simply print 12.
However if I change it to printf("%x\n", four_ints[11])
It suddenly prints 28ac90
Why would it do that?
In the second statement printf("%x\n", four_ints[11]) you access a position of the array that was not reserved for your program (int four_ints[4]). That is you have no guarantees of what is stored on a not reserved portion of memory.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I'm trying to initialise all elements of a 3D array "A". The array consists of 2000x100x4 integer elements of the 3D array and is stored in row-major order. Each index at position [i,j,k] in "A" must be initialised with the value i*i*i + j*j*j.
How can I do this using for loops? Any suggestions? Thanks.
for(i=0;i<2000;i++)
for(j=0;j<100;j++)
for(k=0;k<4;k++)
A[i][j][k]= (i*i*i) + (j*j*j);
I hope I understood your question correctly. Or were you looking for something else?
It's not something hard to do:
int A[2000][100][4];
int i,j,k;
for (i=0;<2000;i++)
{
for (j=0;j<100;j++)
{
for (k=0;k<4;k++)
{
A[i][j][k] = i*i*i + j*j*j;
}
}
}
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Have to write a function return a square of number from 50 to 100, without using a seed and take no parameters.
I know how to generate a random number in a fixed range (for example 0 to 99):
int n;
n=rand(100); /* n is random number in range of 0 - 99 */
EDIT:
By the way, I used rand() and every time I run it give me same result. I know it because of my compiler, is there anyways to change it (remember not using a seed)
Any idea is much appreciated!
EDIT 2: OK I figured out how to fulfill all requirement. Thank you all!
Massimiliano is the first one who give me correct IDEA how to do it.
This is definitely more of a math question than a programming question, but the answer is in the question:
int n = random(51)+50;
return n*n;
Chose a random number in the range of 50 to 100. Then return its square.
int func(){
int r = random(51); //to choose a random number from 0 to 50.
r = r + 50; //to scale it to fit form 50 to 100
return r*r;
}
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have the following code:
void doPlayerMove(void)
{
bool moved = false;
while (!moved)
{
printf("\nWhere is the piece you want to move?(rc):");
int r = getchar() - '0';// gets the row number
int c = getchar() - '0';// gets the column number
printf("%d:%d", r, c);// prints the chosen row/column
clearInput();
printf("\nWhere is the space you want to move to?(rc):");
int r2 = getchar() - '0';
int c2 = getchar() - '0';
printf("%d:%d", r2, c2);
...
}
}
void clearInput(void)
{
while(getchar() != '\n');
}
this is the output i get:
Where is the piece you want to move?(rc):51
5:1
Where is the space you want to move to?(rc):40
4:00
Whats up with the extra 0? Does anyone see where the problem is?
As the OP says in a comment:
Problem solved, it was some output from some function i was calling in
the ... sorry for the false alarm!