Can array have a value of a struct with a couple members in a struct ?

Can array have a value of a struct with a couple members in a struct ?




1
2
3
4
5
6
  struct allCarsGarage 
{
   int carOwnerId; 
   int carId;

} 


Can I make an array as so :

1
2
3
4
5
6
7
8
const int maxCars = 10; 
  int main(){

   int carInGarage[maxCars];

  
  
} 


how can i make the array value of :

 
  carsInGarage[1] //to hold to values by using the struct ? 


and if that is possible how can you choose what value you want use


if

 
  carsInGarage[1]



had to values
1
2
3
   allCarsGarage CarsInfo;
   CarsInfo.carOwnerId = 1234;
   CarsInfo.carId = 4321;


What if I only wanted to use the cars carId for example

cout << "Your car Id is " << // how would I call it here ? ;





can i just say ?
1
2
3
   
  carsInGarage[1] = CarsInfo.carOwnerId;
  carsInGarage[1]= CarsInfo.carId;

Last edited on
Yes, Almost, Yes.

int carInGarage[maxCars];

You do realize this is an array of integers, hows an array of integers gonna access the struct? You wanna do this

allCarsGarage carInGarage[10]; And now you can do

1
2
3
4
5
carsInGarage[0] = CarsInfo.carOwnerId;
carsInGarage[0]= CarsInfo.carId;

carsInGarage[1] = CarsInfo.carOwnerId;
carsInGarage[1]= CarsInfo.carId;
That is really useful and i have tried it but i am having a problem still understanding

let say I used
 
allCarsGarage carInGarage[10];


and what if all cars had different carId and carOwnerId . i have this function that sorts things for you the prof gave us to use like so :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

 void selectionSort(int a[ ], int n) 
		{
		int i, k, indexOfNextSmallest, temp;
		for (i = 0; i < n - 1; i++)
		{
		indexOfNextSmallest = i;
		for (k=i+1; k<n; k++)
		if (a[k] < a[indexOfNextSmallest])
		indexOfNextSmallest = k;
		temp = a[i];
		a[i] = a[indexOfNextSmallest];
		a[indexOfNextSmallest] = temp;
		}
        }


And I wanted to use it to sort all of the cars carId ONLY
what do I put in the parameters when i call it?

I have tried this but I get an error

 
selectionSort( carInGarage.carId , max cars) ;






And I wanted to use it to sort all of the cars carId ONLY

You cant do that without operator overloading, which is something a bit advanced.
ยจ
The function your professor provided takes an arary of integers
void selectionSort(allCarsGarage a[], int n)

Meaning, the instructions are wrong or something. Because then you'd have to create a integer array in main.

int carInGarage[maxCars];

And in that case, you cant do

1
2
3
4
5
carsInGarage[0] = CarsInfo.carOwnerId;
carsInGarage[0]= CarsInfo.carId;

carsInGarage[1] = CarsInfo.carOwnerId;
carsInGarage[1]= CarsInfo.carId;


So no clue whats going on now.
Topic archived. No new replies allowed.