I am making a game using lite-C (exactly same syntax as C). and I cannot make this loop work.
It gives me an error at this line at compilation.
for(int i = 0; i < (cantenemigu * 3); i += 3)
I have an array with the information of where to create the enemies.
the array contains the x,y,z coordinates.
cantenemigu is the amount of enemies that there are in the array.
With this loop I would get the information of each enemy and create it.
[EDIT]
The answers didn't work. I added the ; acsidently while writing the post.
Maybe the problem is somewhere else;
Here is the hole part.
int cantenemigu = 3;
var posenemigu[] = {-900, 550, -10, -1100, 1600, -10, 70, 1680, 20};
void load_enemigunan()
{
for(int i = 0; i < (cantenemigu * 3); i += 3)
{
ent_create("targetr.mdl",vector(posenemigu[i],
posenemigu[i + 1],
posenemigu[i + 2]),NULL);
}
}
This is the code if I don't add the <br>
I solved it.
this worked.
int i
for(i = 0; i < 3*cantenemigu; i += 3)
{
ent_create("targetr.mdl",vector(posenemigu[i],
posenemigu[i + 1],
posenemigu[i + 2]),NULL);
}
In C# it doesn't have be declared before. I assumed it was also so in C. (or maybe it's a bug in the compiler).
for (int i = 0; i < (cantenemigu * 3); i += 3)
There should not be any ; after i += 3.
Try changing your code to this: (note what I've done is move the declaration of i outside the for loop.
int cantenemigu = 3;
var posenemigu[] = {-900, 550, -10, -1100, 1600, -10, 70, 1680, 20};
void load_enemigunan(){
int i;
for(i = 0; i < (cantenemigu * 3); i += 3){
ent_create("targetr.mdl",vector(posenemigu[i],
posenemigu[i + 1],
posenemigu[i + 2]),NULL);
}
}
Get rid of the 3rd ;.
for(int i = 0; i < (cantenemigu * 3); i += 3)
It looks like you are missing a closing parenthesis for your call to vector.
ent_create(
"targetr.mdl",
vector(
posenemigu[i],
posenemigu[i + 1],
posenemigu[i + 2],
NULL
);
for(int i = 0; i < (cantenemigu * 3); i += 3;)
What is the error? That last semicolon shouldn't be there.
What is the body of the loop?
What type of variable is cantenemigu? Can it be coerced to int?
Related
How to find a missing number from a list in Dart language
a=[1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12];
for(int num in a){
if(a.contains(num+1) == false && a.last != num){
return num+1
}
return null; // No missing value
}
Assuming you want to find 7 in the example you could do something like this. It checks if the next value is provided. It is also necessary to check for the last element because lastElement+1 will always not be included in the array.
Assuming the list is sorted you can use:
Iterable<int> findMissingInts(List<int> ints) sync* {
for (var i = 0; i < ints.length - 1; i++) {
for (var j = ints[i] + 1; j < ints[i + 1]; j++) {
yield j;
}
}
}
main() {
print(findMissingInts([1,2,4,5,8])); //(3, 6, 7)
}
Using sum formula n*(n+1)/2 its easy to figure out..
import "dart:math";
main(){
var a = [1,2,3,4,5,6,8,9,10];
int n = a.reduce(max);
var sum = n*(n+1)/2;
var sumOfArray = a.reduce((a,b) => a+b);
print(sum-sumOfArray);
}
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 2 years ago.
Improve this question
I can't figure out why is my program not working. Can someone from the experts help? :)`
I'm getting storage junk numbers in my console, my third array should be a mix of array aA[] and aB[], first goes the elements from array aA[] then from aB[] once at the time.
// 2te Vektor HÜ
// Bsp.: aA[7, 8, 9] aB[14, 15, 16]
// => aC[7, 14, 8, 15, 9, 16]
// returns size of C`int val = 0;
`````````````````````````````````
int Mischen(int aA[], int aB[], int aC[], int aLaenge) {
int val = 0;
val = aLaenge;
for (int i = 0; i < aLaenge; i++) {
int c = 0, x = 1;
c = c += 2;
x = x += 2;
aC[c] = aA[i], aC[x] = aB[i];
}
return val;
}
You simplify the loop. Remember, you need to copy the values from the source arrays to the alternate index in the destination array. To elaborate,
aA[0], aA[1], aA[2].. should go to aC[0], aC[2], aC[4]...
aB[0], aB[1], aB[2].. should go to aC[1], aC[3], aC[5]...and so on.
So the logic can be
Copy the value at aA[i] to aC[2*i].
Copy the value at aB[i] to aC[(2*i)+1].
So, change it to
for (int i = 0; i < aLaenge; i++) {
aC[2*i] = aA[i];
aC[(2*i)+1] = aB[i];
}
You can change the code like this way, you should defined the variable c and x outside the loop and every time increase them by two.
int Mischen(int aA[], int aB[], int aC[], int aLaenge) {
int val = 0;
val = aLaenge;
int c = 0, x = 1;
for (int i = 0; i < aLaenge; i++) {
//int c = 0, x = 1;
//c = c += 2;
//x = x += 2;
aC[c] = aA[i];
aC[x] = aB[i];
c += 2;
x += 2;
}
return val;
}
I'm working on a game modification for a third party game (a so called "mod"), and I want to add the player to an array.
I will explain the process of how it works. My code looks like this:
int numElements = 20;
int arrSize = numElements * 2 + 2;
Ped peds[arrSize];
peds[0] = numElements;
int countPeds = GET_PED_NEARBY_PEDS(PLAYER_PED_ID(), peds, -1);
The game function called GET_PED_NEARBY_PEDS will populate the peds array with IDs of the pedestrians walking around the PLAYER_PED_ID (our player).
My goal here is to also add our player to this array aswell. Right now, it only gathers the pedestrians around the player, and I want the player to be included in this array aswell.
I came to the conclusion that I should create a new array, and add the PLAYER_PED_ID to this new array, like so:
Ped newpeds[arrSize + 1];
for (int i = 0; i < arrSize; i++) newpeds[i] = peds[i];
newpeds[arrSize + 1] = PLAYER_PED_ID();
And then instead of using the peds array in my code, I will be using newpeds. But for some reason the modifications don't affect the player, but only the pedestrians around the player (like the first code example).
This is what my full code looks like:
int numElements = 20;
int arrSize = numElements * 2 + 2;
Ped peds[arrSize];
peds[0] = numElements;
int countPeds = GET_PED_NEARBY_PEDS(pedID, peds, -1);
Ped newpeds[arrSize + 1];
for (int i = 0; i < arrSize; i++) newpeds[i] = peds[i];
newpeds[arrSize + 1] = PLAYER_PED_ID();
for (int i = 0; i < countPeds; i++) {
Ped ped = newpeds[i * 2 + 2];
//...
}
How can I add the player to the array? Could it have something to do with the math? Any help is appreciated. :)
This may be because you are trying to add PLAYER_PED_ID() at wrong index.
for (int i = 0; i < arrSize; i++) newpeds[i] = peds[i];
newpeds[arrSize + 1] = PLAYER_PED_ID();
You should be adding PLAYER_PED_ID() at :
newpeds[arrSize] = PLAYER_PED_ID();
I've got a piece of code, it's purpose is to draw a background image on one of the game levels. For this purpose I create this structure.
typedef struct crate_t {
int x = 0;
int y = 0;
int h = 0;
int w = 0;
int type = BACKGROUND;
}crate;
Then in the main function I create a 2D array
crate **Crates = (crate**)malloc(sizeof(crate)*(SCREEN_WIDTH / GrassBlock->w));
for (int i = 0; i <= SCREEN_HEIGHT/GrassBlock->h; i++) {
Crates[i] = (crate*)malloc(sizeof(crate)*(SCREEN_HEIGHT / GrassBlock->h));
}
and I pass it to the function counter = DrawLevelBG(screen, GrassBlock, Border, Crates);. The problem is that the function causes error. "Access violation writing location." at Obstacles[i][j].x = x;
int DrawLevelBG(SDL_Surface *screen, SDL_Surface *sprite, SDL_Surface *border, crate **Obstacles) {
int x = 0;
int y = 0;
int i = 0;
int j = 0;
bool condition = 0;
while (y < SCREEN_HEIGHT + sprite->h) {
DrawSurface(screen, sprite, x + (sprite->w / 2), y + (sprite->h / 2));
if (x >= SCREEN_WIDTH - sprite->w || x == 0 || y == 0 || y >= SCREEN_HEIGHT - sprite->h) {
DrawSurface(screen, border, x + (sprite->w / 2), y + (sprite->h / 2));
Obstacles[i][j].x = x;
Obstacles[i][j].y = y;
Obstacles[i][j].h = border->h;
Obstacles[i][j].w = border->w;
Obstacles[i][j].type = WALL;
i++;
if (x >= SCREEN_WIDTH - sprite->w) {
y += sprite->h;
x = 0;
j++;
condition = 1;
}
}
if (!condition) {
x += sprite->w;
}
condition = 0;
}
return i;
}
I know that these ones are caused by pointers not pointing actually to anything but I can't understand what's wrong here. Any help would be greatly appreciated. Thanks.
EDIT
I've changed my memory allocation piece of code so it looks like that now:
crate **Crates = (crate**)malloc(sizeof(crate*)*(SCREEN_WIDTH / GrassBlock->w)*(SCREEN_HEIGHT / GrassBlock->h));
for (int i = 0; i <= SCREEN_WIDTH/GrassBlock->w; i++) {
Crates[i] = (crate*)malloc(sizeof(crate)*(SCREEN_HEIGHT / GrassBlock->h));
}
According to all your replies guys. Unfortunately this doesnt solve the problem. +Important info, the function DrawLevelBG causes ERROR on the first iteration of loop.
In the first allocation you create an array from pointers. So you need to allocate memory for pointers:
crate **Crates = (crate**)malloc(sizeof(crate*)*(SCREEN_WIDTH / GrassBlock->w));
Thanks for all the help guys. The problem was iterators, not only did I make my 2D array SCREEN_HEIGHT wide and SCREEN_WIDTH high which was the opposite of what I wanted but aswell the iteration in DrawLevelBG was wrong as pointed out. I had to swap my "i" and "j" and make some corrections, so thanks alot Some programmer dude for pointing that out. Thanks alot.
Here is the problem's address.
https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/
And here is my C code:
int removeDuplicates(int* nums, int numsSize) {
int* result;
int result_pointer = 0;
int allow_appearance = 2;
int appeared_number;
int i;
if (0 == numsSize) {
return 0;
}
result = (int*)malloc(numsSize * sizeof(int));
for (i = 0; i < numsSize; i++) {
appeared_number = 1;
result[result_pointer] = nums[i];
result_pointer += 1;
while (nums[i + 1] == nums[i]) {
i += 1;
if (appeared_number < allow_appearance) {
result[result_pointer] = nums[i];
result_pointer += 1;
appeared_number += 1;
}
}
}
for (i = 0; i < result_pointer; i++) {
nums[i] = result[i];
}
free(result);
return result_pointer;
}
Test data:
test case 1:[0, 0, 0, 0, 3] (The 158th case)
test case 2:[0,0,1,1,1,2,2,2,3,3,4] (The 163rd case)
Correct output:
for test case 1:[0,0,3]
for test case 2:[0,0,1,1,2,2,3,3,4]
when i click Run Code, everything is ok, and the code run correctly on my macbook pro, using gcc as the compiler and gdb as debugger.
However, when I click Submit Solution, it output [0,0,3,3] for test case 1.
And then I try to comment out the code "free(result);", it output [0,0,1,1,2,2,3,3,4,4] for test case 2. Test case 2 is behind the test case 1, which means that test case 1 was passed after commenting out the code "free(result);".
It seems that the code I comment out has no relation with the result, what's wrong with my code?
Thanks!
while (nums[i + 1] == nums[i]) { ... }
Consider what happens with arrays like [1, 2, 2, 2, 2, 2]. It'll happily read past the end.
You need to stop the loop when you reach the end:
while (i+1 < numsSize && nums[i + 1] == nums[i]) { ... }