Computer Programming For Engineering Applications
Computer Programming For Engineering Applications
Engineering Applications
ECE 175
Intro to Programming
Lecture Set Overview
Switch statements
false “Acidic”
“Very
Acidic”
Pseudocode:
Begin
Initialize x
print: Enter a positive number
read x
if x > 0
compute square root of x
print: The square root of x is sqrt(x)
else
print: error message
End
#include <stdio.h>
#include <math.h> // declaration of the math library
int main(void)
{
float num, root; /* declaration of two float variables */
End
a) Check whether the distance between (x1,y1) and (x2,y2) is less than 10
Note: assuming that there are values in all these variables by initialization or a user will enter them.
double x1, y1, x2, y2;
b) To check whether the volume of sphere is greater than the volume of a cube
Note: assuming that there are values in all these variables by initialization or a user will enter them.
double r_sphere, side_cube;
Examples
}
}
b) x is equal to 1 or -1
int main(void)
{
char ltr; // declaration of a char variable
return(0);
}
02/13/2021 ECE 175 20
Operator Precedence
Note: Parentheses ( ) are executed first from left to right
Operator Highest
! + - & (unary operators)
* / %
+ -
< <= >= >
== !=
&&
|| Lowest
End
02/13/2021 ECE 175 22
Activity: Step 2 (C program)
Printing multiples of 5 and 7
#include <stdio.h>
// Printing multiples of 5 and 7
int main(void)
{
int i; // declaration of an integer variable
printf("Guess a multiple of 5 and 7:");
scanf("%d", &i); // awaiting for an integer from the user
return(0);
}
if (condition)
{
statements;
}
else if (condition)
{
statements;
}
else if (condition)
{
statements;
}
...
else
{
statements
}
What get printed if noise_dB = 70? What get printed if noise_dB = 70?
Enter a character: m
mM
Enter a character: ^
^
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
int main(void){
char letter;
printf("Enter a character: ");
scanf("%c", &letter);
return(0);
}
false true
pH>7
true false
pH=7 pH<12
false “Acidic”
“Very
Acidic”
if (pH > 7)
{
if (pH < 12)
printf("alkaline\n");
else // pH >= 12
printf("very alkaline\n");
}
else // pH < or = 7
{
if (pH == 7)
printf("neutral\n");
else
{
if (pH > 2)
printf("acidic\n");
else // if pH is less or equal to 2
printf("very acidic\n");
}
}
return 0;
}
02/13/2021 ECE 175 32
#include <stdio.h>
int main(void)
{
int pH; // integer variable
printf("Give the acidity of the solution:");
scanf("%d",&pH); // reading the acidity to pH variable
int main(void)
{
int pH; // integer variable
printf("Give the acidity of the solution:");
scanf("%d", &pH); // reading the acidity to pH variable
return(0);
}
break; <- required in switch, otherwise the default case will be executed as well
02/13/2021 ECE 175 35
#include<stdio.h>
int main(void){ switch Block Command
int watts;
printf("Enter the bulb wattage (in Watts):");
scanf("%d", &watts); // user input stored in watts
switch (watts)
{
case 15: // if watts = 15
printf("The bulb's brightness is 125 lumens\n");
break; // terminate
case 25: // if watts = 25
printf("The bulb's brightness is 215 lumens\n");
break; // terminate
case 40: // if watts = 40
printf("The bulb's brightness is 500 lumens\n"); // print 500
break; // terminate
case 60: // if watts = 60
printf("The bulb's brightness is 880 lumens\n"); // print 880
break; // terminate
case 75: // if watts = 75
printf("The bulb's brightness is 1000 lumens\n"); // print 1000
break; // terminate
case 100: // if watts = 100
printf("The bulb's brightness is 1675 lumens\n"); // print 1675
break; // terminate
default: // default case if the user inputs a value outside the table
printf("The bulb's brightness is unknown\n"); // unknown brightness
}
return(0);
02/13/2021 } ECE 175 36
One way equivalence of if-else-if with case
int main(void)
{
int watts;
return(0);
}
02/13/2021 ECE 175 38
Difference of switch with if-else
The if statement is more general than switch statement
Programming practice
Use switch statements when label contains a reasonable number of
integer( or char) values