Computer Programming for Engineering Applications

Download Report

Transcript 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
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)
7/18/2015
Attribute
Data type
name
string
diameter
long int or double
number of moons
int
orbit time
double
Rotation time
double
ECE 175
2
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
typedef struct {
char name[20]; // name of the planet
double diameter; // diameter of the planet in km
int moons; // number of moon
double orbit_time; // orbit around the sun in years
double rotation_time; // orbit around itself in years
} planet_t;
7/18/2015
ECE 175
3
Variable Declaration
We can now define variables of type planet_t, similar to defining integers,
floats, etc.
planet_t mars={"Mars", 18765, 3, 12.2, 2.3};
planet_t venus={"Venus", 234532, 27, 23.44, 4.8};
planet_t pl3;
We can initialize structures on declaration like any other variable
Must be careful to make sure that the data types match
7/18/2015
ECE 175
4
Structure Type Definition
Syntax:
typedef struct {
type_1 var;
type_2 var;
. . .
type_n var;
} struct_name;
Example:
7/18/2015
typedef struct { // complex number structure
double real_part,
imag_part;
} complex_t;
ECE 175
5
Hierarchy of Structures
A user defined structure containing other structures
typedef struct {
char solar[20]; // name of the solar system
double diameter; // diameter of the solar system in km
planet_t planets[9]; // array of planets
} solar_system;
In the above structure we defined an array of type planet_t
7/18/2015
ECE 175
6
Manipulating Individual components of structures
7/18/2015
ECE 175
7
Accessing Components within the Hierarchy
typedef struct {
char solar[20]; // name of the solar system
double diameter; // diameter of the solar system in km
planet_t planets[9]; // array of planets
} solar_system;
solar_system solar1;
printf("The diameter of the first planet of solar system %s is %.1f", solar1.solar,
solar1.planets[0].diameter);
7/18/2015
ECE 175
8
Scanning for Input
Returning a variable of type struct
planet_t scan_planet(void)
{
planet_t pl;
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);
return pl;
}
In the main: planet_t planet1;
planet1= scan_planet();
7/18/2015
ECE 175
9
Alternative Implementation of Scanning
Via pointers
void scan_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);
}
In the main: planet_t planet1;
scan_planet(&planet1);
7/18/2015
ECE 175
10
Snapshot at the memory
7/18/2015
ECE 175
11
The Indirect Component Selection Operator
Replacing the (*pointer) operator
void scan_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);
}
In the main: planet_t planet1;
scan_planet(&planet1);
7/18/2015
ECE 175
12
Printing the Attributes of a Structure
Writing a printing function
In the main: print_planet(current_planet);
7/18/2015
ECE 175
13
Alternative Printing Function
7/18/2015
ECE 175
14
Comparing Two Structures
Compare whether two structures are equal
7/18/2015
ECE 175
15
Example: Update Time based on Seconds Elapsed
#include<stdio.h>
typedef struct {
int second, minute, hour;
} time_t;
time_t new_time(time_t t, int
void print_time(time_t t);
elapsed_secs);
int main(void)
{
time_t updated_time, current_time={0,0,0};
updated_time=new_time(current_time, 1000);
print_time(updated_time);
return (0);
}
7/18/2015
ECE 175
16
The time update and print functions
time_t new_time(time_t t, int elapsed_secs)
{
int new_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);
}
void print_time(time_t t)
{
printf("The current time is %2d:%2d:%2d\n", t.hour, t.minute, t.second);
}
7/18/2015
ECE 175
17