1 / 17

Computer Programming for Engineering Applications

Computer Programming for Engineering Applications . ECE 175. Intro to Programming . Dealing with Multiple-type Objects. Built-in C data types are limited to: integers, characters, floating point numbers Problem : Programmers have to deal with real-word objects

audreym
Download Presentation

Computer Programming for Engineering Applications

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Programming forEngineering Applications ECE 175 Intro to Programming

  2. Dealing with Multiple-type Objects Built-in C data types are limited to: integers, characters, floating point numbers Problem: Programmers have to deal with real-word objects E.g. A planet, an employee, a plane, a multi-modal sensor Each object has multiple attributes that must be stored and manipulated E.g. Planet name, diameter, number of moons, orbit time (around the sun), rotation time (time to revolve around its axis) ECE 175

  3. User-Defined Types Define new data types called structures that are aggregates of fundamental data types (or other defined structures) Allow us to efficiently handle real-world objects typedefstruct { char name[20]; // name of the planet double diameter; // diameter of the planet in km int moons; // number of moon doubleorbit_time; // orbit around the sun in years doublerotation_time; // orbit around itself in years } planet_t; ECE 175

  4. Variable Declaration We can now define variables of type planet_t, similar to defining integers, floats, etc. We can initialize structures on declaration like any other variable Must be careful to make sure that the data types match planet_t mars={"Mars", 18765, 3, 12.2, 2.3}; planet_t venus={"Venus", 234532, 27, 23.44, 4.8}; planet_t pl3; ECE 175

  5. Structure Type Definition Syntax: typedefstruct{ type_1var; type_2var; . . . type_nvar; } struct_name; Example: typedefstruct{ // complex number structure doublereal_part, imag_part; } complex_t; ECE 175

  6. Hierarchy of Structures A user-defined structure containing other structures In the above structure we defined an array of type planet_t typedefstruct { char galaxy[20]; // name of the galaxy double diameter; // diameter of the galaxy in km planet_t planets[9]; // number of planets } solar_system; ECE 175

  7. Manipulating Individual components of structures ECE 175

  8. Accessing Components within the Hierarchy typedefstruct { char galaxy[20]; // name of the planet double diameter; // diameter of the planet in km planet_t planets[9]; // number of moon } solar_system; solar_system galaxy1; printf("The diameter of the first planet of galaxy %s is %.1f", galaxy1.galaxy, galaxy1.planets[0].diameter); ECE 175

  9. Scanning for Input Returning a variable of type struct In the main: planet_t planet1; planet1= scan_planet(); planet_tscan_planet(void) { planet_tpl; printf("Enter the name of the planet:"); scanf("%s", pl.name); printf("Enter the diameter of the planet in Km:"); scanf("%lf", &pl.diameter); printf("Enter the number of moons:"); scanf("%d", &pl.moons); printf("Enter the orbit time in years:"); scanf("%lf", &pl.orbit_time); printf("Enter the rotation time in years:"); scanf("%lf", &pl.rotation_time); returnpl; } ECE 175

  10. Alternative Implementation of Scanning Via pointers In the main: planet_t planet1; scan_planet(&planet1); voidscan_planet(planet_t *plnp) { printf("Enter the name of the planet:"); scanf("%s", (*plnp).name); printf("Enter the diameter of the planet in Km:"); scanf("%lf", &(*plnp).diameter); printf("Enter the number of moons:"); scanf("%d", &(*plnp).moons); printf("Enter the orbit time in years:"); scanf("%lf", &(*plnp).orbit_time); printf("Enter the rotation time in years:"); scanf("%lf", &(*plnp).rotation_time); } ECE 175

  11. Snapshot at the memory ECE 175

  12. The Indirect Component Selection Operator Replacing the (*pointer) operator Function call: planet_t planet1; scan_planet(&planet1); voidscan_planet(planet_t *plnp) { printf("Enter the name of the planet:"); scanf("%s", plnp->name); printf("Enter the diameter of the planet in Km:"); scanf("%lf", &plnp->diameter); printf("Enter the number of moons:"); scanf("%d", &plnp->moons); printf("Enter the orbit time in years:"); scanf("%lf", &plnp->orbit_time); printf("Enter the rotation time in years:"); scanf("%lf", &plnp->rotation_time); } ECE 175

  13. Printing the Structure Attributes Function call: print_planet(current_planet); voidprint_planet(planet_tpl) // input: one planet structure { printf("%s\n", pl.name); printf(" Equatorial diameter: %.0f km\n", pl.diameter); printf(" Number of moons: %d\n", pl.moons); printf(" Time to complete one orbit of the sun: %.2f years\n", pl.orbit_time); printf(" Time to complete one rotation on axis: %.4f hours\n", pl.rotation_time); } ECE 175

  14. Printing the Structure Attributes - Pointers Function call: print_planet(&current_planet); voidprint_planet(planet_t*pl_pt) // pointer to a planet { printf("%s\n", pl_ptname); printf(" Equatorial diameter: %.0f km\n", pl_ptdiameter); printf(" Number of moons: %d\n", pl_ptmoons); printf(" Time to complete one orbit of the sun: %.2f years\n", pl_ptorbit_time); printf(" Time to complete one rotation on axis: %.4f hours\n", pl_ptrotation_time); } ECE 175

  15. Comparing Two Structures intplanet_equal(planet_t planet_1, planet_t planet_2) { return (strcmp(planet_1.name, planet_2.name) == 0 && planet_1.diameter == planet_2.diameter && planet_1.moons == planet_2.moons && planet_1.orbit_time == planet_2.orbit_time && planet_1.rotation_time == planet_2.rotation_time); } ECE 175

  16. Example: Update Time based on Seconds Elapsed #include<stdio.h> typedefstruct { int second, minute, hour; } time_t; time_tnew_time(time_t t, intelapsed_secs); voidprint_time(time_t t); int main(void) { time_tupdated_time, current_time={0,0,0}; updated_time=new_time(current_time, 1000); print_time(updated_time); return (0); } ECE 175

  17. The time update and print functions • time_tnew_time(time_t t, intelapsed_secs) • { • intnew_hr, new_min, new_sec; • new_sec = t.second + elapsed_secs; • t.second = new_sec % 60; • new_min = t.minute + new_sec / 60; • t.minute = new_min % 60; • new_hr = t.hour + new_min / 60; • t.hour = new_hr % 24; • return (t); • } • voidprint_time(time_t t) • { • printf("The current time is %2d:%2d:%2d\n", t.hour, t.minute, t.second); • } ECE 175

More Related