Instructions
Objective
Write a program to implement structures in C++.
Requirements and Specifications
![program-to-implement-structures-in-C (1)](https://www.programminghomeworkhelp.com/uploads/images/program-to-implement-structures-in-C-(1).webp)
![program-to-implement-structures-in-C 1](https://www.programminghomeworkhelp.com/uploads/images/program-to-implement-structures-in-C-1.webp)
Source Code
PART 1
#include
#include
#include
// Hold mobile phone information
struct Phone
{
char os[255]; // Operating system like Android, iOS
char make[255]; // Google, Apple, Samsung, Huawei and the like
int ram; // Ram in GB
double size; // Size inches like 6.5, 5.0, 5.5, and the like
int battery; // Capacity of battery like 4000mah and the like
};
// Create a new phone and add it to the list of phones
void addPhone(struct Phone *phones, int capacity, int *numPhones)
{
// Stop if we ran out of space
if (*numPhones >= capacity)
{
printf("Error: We have reached the maximum number of phones.\n");
return;
}
// Create the phone
struct Phone phone;
printf("Enter the operating system (e.g. Android, iOS): ");
scanf("%s", phone.os);
printf("Enter the make (e.g. Google, Apple, Samsung): ");
scanf("%s", phone.make);
printf("Enter the RAM in GB: ");
scanf("%d", &phone.ram);
printf("Enter the screen size in inches (e.g. 6.5, 5.0, 5.2): ");
scanf("%lf", &phone.size);
printf("Enter battery capacity in mAh: ");
scanf("%d", &phone.battery);
// Add the new phone
phones[(*numPhones)++] = phone;
printf("Ok: Phone created.\n");
}
// Display the phone details
void printPhone(struct Phone *phone)
{
printf("OS: %s\n", phone->os);
printf("Make: %s\n", phone->make);
printf("RAM: %d GB\n", phone->ram);
printf("Screen Size: %.1lf inches\n", phone->size);
printf("Battery Capacity: %d mAh\n", phone->battery);
}
// Search the phones that matches a screen size
void searchPhonesByScreenSize(struct Phone *phones, int numPhones)
{
printf("Enter the screen size in inches (e.g. 6.5, 5.0, 5.2): ");
double size;
scanf("%lf", &size);
// Find all phones that matches the screen size
int numFound = 0;
for (int i = 0; i < numPhones; i++)
{
if (phones[i].size == size)
{
printPhone(&phones[i]);
printf("\n");
numFound++;
}
}
printf("%d phone(s) found.\n", numFound);
}
// Print all phones
void printPhones(struct Phone *phones, int numPhones)
{
for (int i = 0; i < numPhones; i++)
{
printPhone(&phones[i]);
printf("\n");
}
}
// Entry point of the program
int main()
{
printf("What is the maximum number of phones we can add? ");
int capacity;
scanf("%d", &capacity);
// Make sure the capacity is logical
if (capacity <= 0) {
printf("Invalid value.\n");
return 0;
}
// Initialize the array
struct Phone *phones = (struct Phone *) malloc(sizeof(struct Phone) * capacity);
int numPhones = 0;
// Display a menu
while (1)
{
printf("Menu:\n");
printf("1. Add a new phone\n");
printf("2. Search phones by screen size\n");
printf("3. Display all phones\n");
printf("0. Exit\n");
printf("Option: ");
int option;
scanf("%d", &option);
if (option == 1)
addPhone(phones, capacity, &numPhones);
else if (option == 2)
searchPhonesByScreenSize(phones, numPhones);
else if (option == 3)
printPhones(phones, numPhones);
else if (option == 0)
break;
printf("\n");
}
// Delete the array
free(phones);
return 0;
}
PART 2
#include
#include
// A cartesian coordinate stores x and y values
struct CartesianCoordinate
{
double x;
double y;
};
// A polar coordinate stores a length and angle
struct PolarCoordinate
{
double length;
double angle;
};
// A coordinate can store either a Cartesian or a Polar
union Coordinate
{
struct CartesianCoordinate cartesian;
struct PolarCoordinate polar;
};
// Convert cartesian to polar
union Coordinate convertCartesianToPolar(struct CartesianCoordinate cartesian)
{
struct PolarCoordinate polar;
polar.length = sqrt(pow(cartesian.x, 2) + pow(cartesian.y, 2));
polar.angle = atan(cartesian.y / cartesian.x);
polar.angle *= (180 / 3.14);
union Coordinate coordinate;
coordinate.polar = polar;
return coordinate;
}
// Convert polar to cartesian
union Coordinate convertPolarToCartesian(struct PolarCoordinate polar)
{
struct CartesianCoordinate cartesian;
cartesian.x = polar.length * cos(polar.angle * 3.14/180);
cartesian.y = polar.length * sin(polar.angle * 3.14/180);
union Coordinate coordinate;
coordinate.cartesian = cartesian;
return coordinate;
}
// Entry point of the program
int main()
{
// Let the user decide
printf("Enter C if you want to enter a cartesian coordinate or P if you want to enter a polar coodinate: ");
char option;
scanf("%c", &option);
if (option == 'C' || option == 'c')
{
// Create a cartesian coordinate and convert it to polar
struct CartesianCoordinate cartesian;
printf("Enter X: ");
scanf("%lf", &cartesian.x);
printf("Enter Y: ");
scanf("%lf", &cartesian.y);
// Convert
union Coordinate coordinate = convertCartesianToPolar(cartesian);
// Display the result
printf("Polar Coordinate: Length = %.2f, Angle = %.2f degrees.\n", coordinate.polar.length, coordinate.polar.angle);
}
else if (option == 'p' || option == 'P')
{
// Create a polar coordinate and convert it to cartesian
struct PolarCoordinate polar;
printf("Enter length: ");
scanf("%lf", &polar.length);
printf("Enter the angle in degrees: ");
scanf("%lf", &polar.angle);
// Convert
union Coordinate coordinate = convertPolarToCartesian(polar);
printf("Cartesian Cordinate: X = %.2f,, Y = %.2f.\n", coordinate.cartesian.x, coordinate.cartesian.y);
}
return 0;
}
Similar Samples
Check out our diverse programming homework samples to see the high-quality solutions we provide. These examples demonstrate our expertise in various programming languages and problem-solving skills. Use them to get a glimpse of how we can help you achieve your academic goals.
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++