#include <stdio.h>
#include <string.h>
/* Structure Definition */
typedef struct{
int flight_number;
char destination[50];
int available_seats;
}Flight;
/* Function Prototypes */
void input_flights(int n, Flight flights[n]);
int search_flights(int n, Flight flights[n], char destination[], int result[]);
void display(int count, int result[], Flight flights[]);
int main()
{
int n = 4;
Flight flights[n];
input_flights(n,flights);
char search_dest[50];
int result[n];
printf("\nEnter destination to search: ");
scanf("%s",search_dest);
int count=search_flights(n,flights,search_dest,result);
display(count,result,flights);
return 0;
}
/* Function to input flight details */
void input_flights(int n, Flight flights[n])
{
for(int i=0;i<n;i++){
printf("\nEnter details of flight %d\n",i+1 );
printf("Enter flight number: ");
scanf("%d",&flights[i].flight_number);
// write code
printf("Enter destination: ");
scanf("%s",flights[i].destination);
// write code
printf("Enter available seats: ");
scanf("%d",&flights[i].available_seats);
// write code
}
}
/* Function to search flights */
int search_flights(int n, Flight flights[n], char destination[], int result[])
{
int count = 0;
for(int i=0;i<n;i++){
if(strcmp(destination,flights[i].destination)==0){
result[count] = i;
count++;
}
}
// write condition using strcmp
// store index in result[count]
return count;
}
/* Function to display result */
void display(int count, int result[], Flight flights[])
{
if(count==0){
printf("No flight available to the given destination\n");
}
else{
printf("Flights available:\n");
for(int i=0;i<count;i++){
int idx = result[i];
printf("Flight Number: %d\n", flights[idx].flight_number);
}
}
}
&&&&&&&&&&&
#include <stdio.h>
/* Structure Definition */
typedef struct {
int length;
int width;
int area;
} Rectangle;
/* Function Prototypes */
void input_rectangles(int n, Rectangle rects[n]);
void calculate_areas(int n, Rectangle rects[n]);
int find_max_index(int n, Rectangle rects[n]);
void display_areas(int n, Rectangle rects[n]);
void display_max(Rectangle rects[], int max_index);
int main()
{
int n;
printf("Enter number of rectangles: ");
scanf("%d", &n);
if (n <= 0)
{
printf("Invalid number of rectangles\n");
return 0;
}
Rectangle rects[n];
input_rectangles(n, rects);
calculate_areas(n, rects);
display_areas(n, rects);
int max_index = find_max_index(n, rects);
display_max(rects, max_index);
return 0;
}
/* Function to input rectangle details */
void input_rectangles(int n, Rectangle rects[n])
{
for (int i = 0; i < n; i++) {
printf("Enter length of rectangle %d: ", i + 1);
scanf("%d", &rects[i].length);
printf("Enter width of rectangle %d: ", i + 1);
scanf("%d", &rects[i].width);
}
}
/* Function to calculate areas */
void calculate_areas(int n, Rectangle rects[n])
{
for (int i = 0; i < n; i++) {
rects[i].area = rects[i].length * rects[i].width;
}
}
/* Function to display all areas */
void display_areas(int n, Rectangle rects[n])
{
printf("\nAreas of rectangles:\n");
for (int i = 0; i < n; i++) {
printf("Rectangle %d area = %d\n", i + 1, rects[i].area);
}
}
/* Function to find index of rectangle with maximum area */
int find_max_index(int n, Rectangle rects[n])
{
int max_index = 0;
for (int i = 1; i < n; i++) {
if (rects[i].area > rects[max_index].area) {
max_index = i;
}
}
return max_index;
}
/* Function to display rectangle with maximum area */
void display_max(Rectangle rects[], int max_index)
{
printf("\nRectangle %d has the largest area = %d\n",
max_index + 1, rects[max_index].area);
}1 views