Accessing array of classes - arrays

I am trying to create a 1d array that contains classes as its type. The following code is what have so far:
public class dog{
int x;
int y;
int health;
}
Dog[] dog_properties = new Dog[4];
for(int i = 0; i < 4; i++){
Dog d = new Dog();
d.x = 2;
d.y = 3;
d.health = 10;
dog_properties[i] = d;
}
How do i access each property of dog from the array once it has been stored there? By this i mean i if want to iterate over the array later on how do i access d.x?

for(int i = 0; i < 4; i++){
int x = dog_properties[i].x;
}

for (int i = 0; i < 4; i++)
{
Dog d = dog_properties[i];
int x = d.x;
}

If the properties are public you can do:
for(int i; i < 4; i++){
int x = dog_properties[i].x;
}

Related

How can I create a generic function to sort an array of structures?

I'm working on this program for my University exam. I need to sort passenger array with a sorting algorithm (I choose bubblesort due to it's simplicity).
I need to create a generic function and pass as formal parameters:
-a list of objects i want to sort;
-a sort criterion.
So I think that I'll have to create only 1 Increasing sorting function and 1 decreasing sorting function and pass them the parameters to sort by.
I already tried to pass char *file_name to function, but I think that I'm wrong.
int passengersIncreasingBubbleSort_Birthyear(passengers test[], int x) {
int i = 0, j = 0, min_idx, flag = 0;
passengers temp;
for (i = 0; i < x; i++) {
min_idx = i;
for (j = i + 1; j < x; j++) {
if (test[j].birth_date.year < test[min_idx].birth_date.year) {
min_idx = j;
}
}
temp = test[min_idx];
test[min_idx] = test[i];
test[i] = temp;
flag = 1;
}
return flag;
}
I tried this:
int passengersIncreasingBubbleSort_Birthyear(passengers test[], int x, char *value1, char *value2) {
int i = 0, j = 0, min_idx, flag = 0;
passengers temp;
for (i = 0; i < x; i++) {
min_idx = i;
for (j = i + 1; j < x; j++) {
if (value1 < value2) {
min_idx = j;
}
}
temp = test[min_idx];
test[min_idx] = test[i];
test[i] = temp;
flag = 1;
}
return flag;
}
But it doesn't work as expected.
Ok, I achieved it.
int cmpfunction_Increasing_Birthdate (const void * a, const void * b)
{
passengers *passengerA = (passengers *)a;
passengers *passengerB = (passengers *)b;
return ( passengerA->signup_date.year - passengerB->signup_date.year );
}
and this is my call:
qsort(array, x, sizeof(passengers), cmpfunction_Increasing_Birthdate);
Now the question is, how can I also compare both year, month and day? Could I do it in the same compare function?

I am getting an error while using bubble sort in array of structures not able to find it

I am having a structure
typedef struct ratings {
int userId;
int movieId;
int rating;
}Ratings;
I am using bubble sort to sort as per my choice
Ratings rect[64], temp;
n = 64;
for (i = 0; i < n-1; i++)
{
for (j = 0; j < (n - i -1); j++)
{
if (REC1[j].movieId >= REC1[j+1].movieId)
{
temp = REC1[j];
REC1[j] = REC1[j + 1];
REC1[j + 1] = temp;
}
}
}
for (i = 0; i < n-1; i++)
{
for (j = 0; j < (n - i -1); j++)
{
if(REC1[j].movieId == REC1[j+1].movieId )
{
if (REC1[j].userId >= REC1[j+1].userId)
{
temp = REC1[j];
REC1[j] = REC1[j + 1];
REC1[j + 1] = temp;
}
}
}
}
i used the above logic to get my output and succeeded. Can it be reduced to less???? Please try it
my inputs are
1,1,9
1,2,0
1,3,2
2,1,2
2,2,10
2,3,10
3,1,7
3,2,1
3,3,9
i want the output as
1,1,9
2,1,2
3,1,7
1,2,0
2,2,10
3,2,1
1,3,2
2,3,10
3,3,9
for this i am using my above logic of sorting still not getting the result
This is the output i am getting but i need a the above output
You have already declared Ratings as a type of struct ratings.
Hence you should declare a array of structure like below
Ratings myratings[100], temp;
if (myratings[i].movieId > myratings[j].movieId)
{
temp = myratings[j];/* it might not work if you are not compiling with a C++ compiler better use memcpy function if you are using a C compiler */
myratings[j] = myratings[j + 1];
myratings[j + 1] = temp;
}
Please correct your bubble short algorithm like below:-
for (i = 0; i < n-1; i++)
{
for (j = 0; j < (n - i -1); j++)
{
if (rect[j].movieId > rect[j+1].movieId)
{
temp = rect[j];
rect[j] = rect[j + 1];
rect[j + 1] = temp;
}
}
}
if you want to swap value only not the object it should be like
int t;
//This is bubble sort for middle row
for (i = 0; i < n-1; i++)
{
for (j = 0; j < (n - i -1); j++)
{
if (rect[j].movieId > rect[j+1].movieId)
{
t = rect[j].movieId;
rect[j].movieId = rect[j+1].movieId;
rect[j+1].movieId = t;
}
}
}
//This is bubble sort for 1st row
for (i = 0; i < n-1; i++)
{
for (j = 0; j < (n - i -1); j++)
{
if (rect[j].userId > rect[j+1].userId)
{
t = rect[j].userId;
rect[j].userId = rect[j+1].userId;
rect[j+1].userId = t;
}
}
}
//This is bubble sort for 3rd row
for (i = 0; i < n-1; i++)
{
for (j = 0; j < (n - i -1); j++)
{
if (rect[j].rating > rect[j+1].rating)
{
t = rect[j].rating;
rect[j].rating = rect[j+1].rating;
rect[j+1].rating = t;
}
}
}
you have to add all the above three while loop in your code
The typedef keyword in
typedef struct ratings {
int userId;
int movieId;
int rating;
}Ratings, temp;
defines Ratings and temp as aliases for the type struct ratings, not as objects of the type struct ratings. What you want to do is something like
struct ratings {
int userId;
int movieId;
int rating;
};
struct ratings Ratings[N], temp; // where N is the size of the array you want.
or
typedef struct ratings {
int userId;
int movieId;
int rating;
}Ratings;
Ratings myRatings[N], temp;
I prefer the first form, myself. I'd rather not hide a struct type behind a typedef if the user of the type has to be aware of its struct-ness (i.e., having to access individual members).

Array values changing without reason

This is my code for Project Euler: Problem 11
int main(int argc, char** argv) {
char stevila [1600] = "08022297381500400075040507785212507791084949994017811857608717409843694804566200814931735579142993714067538830034913366552709523046011426924685601325671370236912231167151676389419236542240402866331380244732609903450244753353783684203517125032988128642367102638406759547066183864706726206802621220956394396308409166499421245558056673992697177878968314883489637221362309750076442045351400613397343133957817532822753167159403800462161409535692163905429635314755588824001754243629855786560048357189070544443744602158515417581980816805944769287392138652177704895540045208839735991607975732162626793327986688366887576220720346336746551232639353690442167338253911249472180846293240627636206936417230238834629969826759857404361620733529783190017431497148868116235705540170547183515469169233486143520189196748";
int stevilaGrid [20][20];
int stevilaRacunanje[4][4];
int stevecPoStevilih = 0;
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
stevilaGrid[i][j] = (stevila[stevecPoStevilih] - 48)*10 + stevila[stevecPoStevilih + 1] - 48;
stevecPoStevilih += 2;
}
}
int rezultat [10];
int najvecji = 0;
int trenutni;
int temp = 0;
for (int i = 0; i < 17; i++) {
for (int j = 0; j < 17; j++) {
//problems start here
for (int k = 0; k < 5; k++) {
for (int l = 0; l < 5; l++) {
temp = stevilaGrid[i + k][j + l];
stevilaRacunanje[k][l] = temp;
}
}
for (int k = 0; k < 5; k++) {
rezultat[k] = stevilaRacunanje[k][0] * stevilaRacunanje[k][1] * stevilaRacunanje[k][2] * stevilaRacunanje[k][3];
rezultat[k+4] = stevilaRacunanje[0][k] * stevilaRacunanje[1][k] * stevilaRacunanje[2][k] * stevilaRacunanje[3][k];
}
rezultat[8] = stevilaRacunanje[0][0] * stevilaRacunanje[1][1] * stevilaRacunanje[2][2] * stevilaRacunanje[3][3];
rezultat[9] = stevilaRacunanje[0][3] * stevilaRacunanje[1][2] * stevilaRacunanje[2][1] * stevilaRacunanje[3][0];
for (int k = 0; k < 10; k++) {
trenutni = rezultat[k];
if(trenutni > najvecji){
najvecji = trenutni;
}
}
}
}
printf("Najvecji zmnozek: %d", najvecji);
return (EXIT_SUCCESS);
}
First I convert the string of numbers into a 2D int array.
Then I try to divide the grid into smaller 4x4 squares with which I can work more easily. That is where the problems start (as marked in the code).
At the very beginning (*i=0,j=0;k=4,j=0*) something strange starts to happen. The values in *stevilaGrid[][]* start to change randomly and seemingly without a reason.
Can somebody please explain this to me. I have tested this behavior on Windows with Cygwin 64bit and Ubuntu with GCC 64bit.
[i + k][j + l];
When i==16 and k==4 or j==16 and j==4 you'll be hitting element [20]
Your array only goes 0...19

Why is my 3d array reversed?

I have a 3d array of ints stored in a struct which represents a tetris block:
typedef struct
{
int orientations[4][4][4];
dimension dimensions[4];
int i;
int x;
int y;
}block;
orientations is filled with every possible position for the block and dimension is a struct that provides information for collision checking:
typedef struct
{
int left, right, bottom;
}dimension;
each orientation and dimension should be linked by the block's i value. For some reason orientations (but not dimensions) seems to be reversed. Does anybody know why this is?
here is how I assign values to dimensions:
block* shape = malloc(sizeof(block));
shape->dimensions[0].left = 0;
shape->dimensions[0].right = 3;
shape->dimensions[0].bottom = 1;
shape->dimensions[1].left = 2;
shape->dimensions[1].right = 2;
shape->dimensions[1].bottom = 3;
shape->dimensions[2].left = 0;
shape->dimensions[2].right = 3;
shape->dimensions[2].bottom = 2;
shape->dimensions[3].left = 1;
shape->dimensions[3].right = 1;
shape->dimensions[3].bottom = 3;
and orientations:
int first[4][4] = {{0,0,0,0}, {2,2,2,2}, {0,0,0,0}, {0,0,0,0}};
int second[4][4] = {{0,0,2,0},{0,0,2,0},{0,0,2,0},{0,0,2,0}};
int third[4][4] = {{0,0,0,0},{0,0,0,0},{2,2,2,2},{0,0,0,0}};
int fourth[4][4] = {{0,2,0,0},{0,2,0,0},{0,2,0,0},{0,2,0,0}};
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
shape->orientations[0][i][j] = first[i][j];
}
}
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
shape->orientations[1][i][j] = second[i][j];
}
}
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
shape->orientations[2][i][j] = third[i][j];
}
}
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
shape->orientations[3][i][j] = fourth[i][j];
}
}
here is how I'm accessing the array:
shape->orientations[current->i][i][j]
when I attempt to access shape->orientations[3] it returns the values I set to shape->orientations[0] earlier. Any help would be greatly appreciated, thanks in advance.
The reason is that your array is indexed [orientation][row][column], but you are interpreting it as [orientation][x][y] when you should be interpreting it as [orientation][y][x]. So you are getting a pattern with the x and y swapped, which appears as if it has been rotated to a different orientation (actually it's equivalent to a rotation and a flip).

How to "combine" two int arrays?

Hello i have a little problem trying to "mix" two string arrays, i have searched about it but i have only found how to merge them or concatenate them, but thats not what i need.
i have two int arrays like this:
int no_items = 5;
int parent1[no_items], parent2[no_items];
if the arrays contains for example:
parent1[0] = 1;
parent1[1] = 2;
parent1[2] = 3;
parent1[3] = 4;
parent1[4] = 5;
and:
parent2[0] = 5;
parent2[1] = 1;
parent2[2] = 2;
parent2[3] = 3;
parent2[4] = 4;
given a "cross" point, for example 2:
parent1 should have his 2 first elements and the rest of parent2, and parent2 should have his first 2 elements and the rest of parent1. So the result should be:
parent1: 1,2 | 5,3,4
parent2: 5,1 | 2,3,4
where "|" is the break point index and the rest of elements should not be repeated.
How can i get this kind of mixing two int arrays? Thanks you!
at the moment i have this:
for(i = 0; i < cross_point; i++)
{
sprintf(buffer, "%d,", parent1[i]);
strcat(line1, buffer);
}
for(i = 0; i < cross_point; i++)
{
sprintf(buffer, "%d,", parent2[i]);
strcat(line2, buffer);
}
but i donĀ“t know how to go further than the cross point.
int *find(int *begin, int *end, int value)
{
int *p = begin;
for ( ; p != end; ++p)
if (*p == value) break;
return p;
}
int i, j;
int output1[no_items] = {0};
int output2[no_items] = {0};
int crosspoint = 3;
memcpy(output1, parent1, crosspoint * sizeof(int));
for (i = crosspoint, j = 0; i < no_items; ++i)
{
while (find(output1, output1+i, parent2[j]) != output1+i) ++j;
output1[i] = parent2[j];
}
memcpy(output2, parent2, crosspoint * sizeof(int));
for (i = crosspoint, j = 0; i < no_items; ++i)
{
while (find(output2, output2+i, parent1[j]) != output2+i) ++j;
output2[i] = parent1[j];
}
memcpy(parent1, output1, sizeof(parent1));
memcpy(parent2, output2, sizeof(parent2));
Demo: http://ideone.com/xUt6nQ
something like this should do it if you aren't concerned about creating temporaries:
int no_items = 5;
int output1[no_items];
int output2[no_items];
for (int i = 0; i < no_items; i++){
if(i < crosspoint){
output1[i] = parent1[i];
output2[i] = parent2[i];
}else{
output1[i] = parent2[i];
output2[i] = parent1[i];
}
}
If you are concerned about temporaries you need to swap the values, the logic should be fairly similar to above.
int no_items = 5;
int temp = 0;
for (int i = 0; i < no_items; i++){
if(i < crosspoint){
/* don't need to do anything here */
}else{
temp = parent1[i];
parent1[i] = parent2[i];
parent2[i] = temp;
}
}

Resources