Computer Programming For Engineering Applications
Computer Programming For Engineering Applications
Engineering Applications
ECE 175
Intro to Programming
02/13/2021 ECE 175 1
Lecture Set Overview
• Array in C
array_name[array_subscript]
#define NUM 8
int x[NUM];
double score[NUM];
Example:
int x[8] = {10, 5, 2, -3, 25, 1001, -1,
23}
x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7]
10 5 2 -3 25 1001 -1 23
6
02/13/2021 ECE 175
Exercise 1 (3 mins):
1) For the declaration: int number[20];
2) Declare
- an array to store the square root of the integers from 0 to 10
- an array to store the cube of the square of the integers from 2 to 15.
Example:
int scores[]={0, 0, 0, 0, 0};
char vowels[]={'a', 'e', 'i', 'o', 'u', 'y'};
Notice that the size of an array that is fully initialized can be omitted
since the size can be deduced from the initialization list.
_____________________________
}
printf("ans is %d\n", sum); //Q2: what get printed?
return 0;
}
02/13/2021 ECE 175 11
Exercise 4 (8 mins): Work as a group at your table, write a program(on whiteboard) that
lets a user enter 8 numbers (stored in an array x) and then finds 1) the maximum value and
its location and 2) average. Your code MUST use a loop structure.
#include <stdio.h>
#define MAX_SIZE 8
int main(void)
{
int x[MAX_SIZE];
int i;
27 variables!
Second way:
int counter[27];
a b c d e f g h i j k l m n o p q r s t u v w x y z other
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
02/13/2021 ECE 175 14
(10 mins) Write (modify/add to the code below) a program to count and display the
occurrence frequency of every letter (both lowercase and UPPERcase) in a text file
“statement.txt”
#include <stdio.h>
int main(void) {
FILE *inp = fopen("statement.txt", "r");
char alp;
if (inp == NULL) {
printf("file not found");
}
else {
while (fscanf(inp, "%c", &alp) != EOF) {
printf("%c", alp);
}
fclose(inp);
}
return 0;
}
printf("%.1lf\n", *(ptr+2));
printf("%.1lf\n", *ptr+2);
ptr = &a[2];
for (i = 0; i < 3; i++) {
printf("%.1lf ", *(ptr + i));
}
return 0;
}
=> We can also use array as arguments to user-defined functions. In other words,
we can pass individual array elements to a function just like other variables.
function call
int id[10];
print_int_array(id, 10);
21
Example: Arrays and Functions
Calling the init and print_ar function
#include<stdio.h>
void init(float x[], int size)
{ // initialization of an array to zeros
int main(void) int i;
{ for (i=0; i<size; ++i)
int i, totalChars=0; x[i]=0;
float counters[27]; }
char letter;
int main(void){
int max1,max2, arr[]={-10, 45, 15,-12, 30, 28, -1, 27, 38};
twoMax(arr, SIZE, &max1, &max2);
simple(x); simple(&x[0]);
for(i=0;i<5;i++)
printf("%d ", x[i]);
printf("\n");
return 0;
}
Program analysis
Input: A file containing integer product codes. Product code cannot be zero
Output: The unique product codes (up to 50) – if a code is a duplicate, it is not stored.
Input file:
4564 3456 3234 4564 3467 1234 3456 3210 …
Output:
4564 3456 3234 3467 1234 3210 …
int main(void){
// declare and initialize relevant variables
// initialize array
int main(void) }
{
// initialize relevant variables void print(int x[], int size){
// array printing (only elements != 0)
// initialize array
} return flag;
}
02/13/2021 ECE 175 30
Activity 2 (12 mins): Write a C program
that stores up to 50 unique product codes
into an array by separating into two groups
at your table and do the following