Programming Applications for Engineers
P a g e | 1
Programming Applications for Engineers
CS 159 (Summer 2 – 2020)
Weekly Assessment 3 – (15%)
Individual submission: Upload a clear version of your answers to
Moodle.
Student name:.. Student ID:
1. Name the .c file with your ID number. Mention your name, ID as comments at top of
code.
2. Each weekly assessment can be submitted twice within the deadline.
3. Strictly, the submission file type should be .c file.
4. Weekly Assessment submission deadline is 13/09/2020 @23:59. A delay in
assessment submission will result in the following grade deduction
Delay of 1 Day : 10% Deduction
Delay of 2 Days: 20% Deduction
Delay of 3 Days: 40% Deduction
Delay of 4 Days: 50% Deduction
Delay of 5 Days: 100% Deduction
5. In case of plagiarized submission, student will receive a notification email with the
below penalty applied starting the date of the email:
Resubmission on the first day (within 24 hours after notification): 20%
deduction
Resubmission on the second day after notification: 30% deduction
Resubmission on the third day after notification: 50% deduction
Failing to resubmit within three days after notification, the assessment will be
graded with zero
6. Please check Banner for your Gradebook and Attendance.
7. AUM rules and regulations apply.
P a g e | 2
Problem
Create a C program which calculates the evaluation points of participants in a competition. The
program should take the ratings given by the participants for each other and perform several
evaluations. The program design should use main and the below functions. Note that you need
to use at least one nested loop in the body of each function.
Consider the constant SIZE with value equal to 5 and a two-dimensional array of size: SIZE x SIZE
called rate_participant, that represents rating points among participants. The participant
ID is equal to the value of the index in the array rate_participant. For example, the first
row in the array (index = 0) represents the points given from participant with ID = 0 to all other
participants (ID=0, ID= 1, ID=2 and ID=3).
Part 1
1. Define a function O_Highest_6 that accepts a 2D array rate_participant and a 2D
array called high, which represents each rater ID and rated participant ID with the
highest rate. The function finds the highest rate each participant has given to all other
participants and stores the rater participant ID and rated participant ID in array high as
shown in the example below. The array high should have 2 columns to store the rater
ID and the rated ID. (20 points)
Participant 0 Participant 1 Participant 2 Participant 3 Participant 4
Participant 0 25 50 75 100 125
Participant 1 50 25 75 100 125
Participant 2 50 75 25 100 125
Participant 3 50 75 100 25 125
Participant 4 50 75 100 125 25
rate_participant
Rater ID Rated ID
0 4
1 4
2 4
3 4
4 3
high
Part 2
2. Define a function O_Sum_6 that accepts a 2D array rate_participant and a one-
dimensional array sum_rate of size equal to SIZE as formal parameters. The function
calculates the sum points each participant has received from all other participants and stores
them in sum_rate. (20 points)
P a g e | 3
3. Define a function O_Display_6 that accepts rate_participant and sum_rate as
formal parameters and displays all arrays elements as shown in the sample output below. It
also finds and displays who won the competition. (20 points)
4. Define a function O_Student_6 that displays your name, your CS 159 section number and your
ID on the screen as shown below. (20 points)
Figure 1. Students information.
5. Write a main function that performs the following: (20 points)
Define and initialize the following arrays: rate_participant, high and sum_rate
with 0.
Prompt the user to fill the array rate_participant with points entered by other
participants based on their performance: 50, 75, 100, 125 (from the Worst to the Best)
-Note that the participants cannot rate themselves. Their points should be set to 25 by
default. Also, they cannot rate two participants with the same point.
Display the sample output by calling the following functions: O_Sum_6, O_Display_6 and
O_Student_6
P a g e | 4
Sample Output: Course Title Programming Applications for Engineers
Course Code CS 159 Lab
Assignment Title
Arrays One Dimensional
P a g e | 2
Table of Contents
Scenario
Objective
Overview
Function Communication with Arrays
Exercise 1
Exercise 2
Exercise 3
Exercise 4
Page3
Page3
Page3
Page4
Page5
Page6
Page6
Page6
Page6
P a g e | 3
Arrays One Dimensional
Scenario:
In this lab you will be able to understand the concept and uses of arrays in C program.
Objectives:
To understand the basic concepts of arrays (Single Dimensional)
To be able to define and initialize arrays
To be able to pass arrays and elements to functions
To write programs that search arrays
Overview:
This lab will make you capable of dealing with arrays in C program by creating single dimensional
array, inserting and accessing elements from array.
P a g e | 4
Concept of Arrays
Array is used to store sequential collection of data of the same data type.
Example: storing a string that contains series of characters.
A specific element in an array is accessed by an index.
Single Dimensional Arrays
All arrays consist of contiguous memory locations. The lowest address corresponds to the first
element and the highest address to the last element.
Array Declaration
To declare an array in C, you need to specify the type of the elements and the number of
elements required by an array as follows:
type arrayName [ arraySize ];
Example: int mark [5]; //array name is mark, type is int and the size is 5
Array Initialization
You can initialize array in C either one by one element, or using a single statement.
int a [5] = {34,67,54,33,66}; // array is declared and initialized at same time
Accessing/Inserting array elements:
C uses an index to access elements from array. You can use any numeric constant to access
element of array by using index.
Index expression should be as below,
int x = a [4]; // variable x has the value of index 4 of array a
To process all elements in an array, you have to use for loop as below,
//Reading or inserting elements
for (i=0;i<10;i++) //if the size of array is 10
{
scanf(%d,&a[i]); //inserting elements into array a
P a g e | 5
}
//Accessing for printing
for (i=0;i<10;i++) //if the size of array is 10
{
printf(%d,a[i]); //accessing all array elements
}
Function communication with arrays:
//Passing whole array to a function
void fun(int fmark[ ]); //function declaration
int mark[10]; // array declaration
fun(mark); // function calling, and passing the whole array mark
void fun (int fmark [ ]) //function definition
{
}
//Passing data from array
void fun(int fmark[ ]); //function declaration
int mark[10]; // array declaration
fun(mark[3]); //function calling, and passing a single value (4th element) in the array mark
void fun (int fmark ) //function definition
{
}
P a g e | 6
Exercise 1: Write a C program that prompts the user to enter 10 numbers into a single
dimensional array, and find the largest number in the array.
Exercise 2: Write a function Equal in C to test if every element in array A is equal/not equal to
its corresponding element in array B. Size of both arrays is 5. Write a main function that
prompts the user to enter 5 numbers into each of two arrays A and B. Then, it calls the function
Equal to test and print if a[0] = b[0], a[1] = b[1] and so forth.
Exercise 3: Write a function FindSum in C to calculate and return the sum of all even
elements in an integer array of size 10. Write a main function that prompts the user to enter
10 numbers into the array. Then, it calls the function Findsum and prints the returned value.
Exercise 4: Write a function ChangeArray in C that takes a 1D integer array A of size 10, as
an argument. The function should search for all negative array elements, and replace them
with zero. The function should print the updated array. Write a main function that prompts
the user to enter 10 numbers into an array A. Then, it calls the function ChangeArray and pass
the array A as argument.
//Sample Output
Enter 5 numbers into array A: 2 3 5 7 9
Enter 5 numbers into array B: 2 3 8 7 4
A[0] is equal to B[0]
A[1] is equal to B[1]
A[2] is not equal to B[2]
A[3] is equal to B[3]
A[4] is not equal to B[4]
//Sample Output
Enter 10 numbers into array:
2 -3 -6 7 -8 1 6 -4 8 5
The updated array has the following elements:
2 0 0 7 0 1 6 0 8 5
Course Title Programming Applications for Engineers
Course Code CS 159 Lab
Assignment Title
Arrays Two Dimensional
P a g e | 2
Table of Contents
Scenario
Objective
Overview
Two Dimensional Arrays
Function Communication with Arrays
Exercise 1
Exercise 2
Exercise 3
Exercise 4
Page3
Page3
Page3
Page4
Page5
Page6
Page6
Page6
Page7
P a g e | 3
Arrays Two Dimensional
Scenario:
In this lab you will be able to understand the concept and uses of arrays in C program.
Objectives:
To understand the basic concepts of arrays (two dimensional)
To be able to define arrays
To be able to pass arrays and elements to functions
To write programs that search arrays
Overview:
This lab will make you capable of dealing with arrays in C program by creating single and two
dimensional array, inserting and accessing elements from array.
P a g e | 4
Concept of arrays:
Array is used to store sequential collection of data of the same data type.
Example: storing a string that contains series of characters.
Two dimensional arrays:
Data are stored in more than one dimension. For example, table with column value and row
value. In essence, it is a list of one-dimensional arrays.
Declaration & initialization of 2D array
A two-dimensional array can be viewed as a table which has x number of rows and y number of
columns. A 2-dimensional array a, which contains three rows and four columns can be shown as
below:
Example: int a [3] [4]; // a [rows] [columns]
Accessing / inserting elements into 2D array:
You have to use two for loops to work with both row value and column value as shown in below
example;
for (i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
scanf(%d,&a[ I ] [ j ]); // inserting numbers into the array
printf(%d, a[ i ] [ j ]); // printing array elements
}
}
Initialization of a 2 dimensional array:
Two dimensional arrays may be initialized by specifying bracketed values for each row. The
following code declares and initialize an array with 3 rows and 4 columns.
int a[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */
};
P a g e | 5
The nested braces, which indicate the intended row, are optional. The following initialization is
equivalent to previous example:
int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
Function Communication with Two-Dimensional Array:
//Passing a Row
void circle(int [ ]); // function declaration
Row = 5;
Col = 5;
int data [row][col]; // 2D array declaration
circle (data [row#]); //function calling and passing 1 row as argument
void circle(int x[ ]) //function definition
{
}
//Passing the whole array
void circle(int x[ ] [5] ); /*function declaration , should specify the size of columns where
size of rows is not necessary in the case of fixed size array. */
int data [5][5]; //array declaration
circle (data); //function calling and passing the array as argument
void circle( int x[ ] [5] ) //function definition
{
}
P a g e | 6
Exercise 1: Write a function that copies a one-dimensional array of 15 numbers into a two-
dimensional array of 3 rows and 5 columns. The function should print the two dimensional
array in table format as shown below.
Exercise 2: Write a function SumArray that takes an integer two-dimensional array of size
4x4 as an argument, and prints the sum of all the numbers in the array. Write a main function
that prompts the user to enter 16 numbers into a 4x4 integer array, and then call the function
SumArray and pass the array as argument.
Exercise 3: Write a function Smallest that takes a float two-dimensional array of size 3x4 as
an argument, and returns the smallest positive number in the array. Write a main function
that prompts the user to enter 12 float numbers into a two dimensional array of size 3x4, and
then call the function Smallest and pass the array as argument. Print the smallest positive
number in the main function.
//sample output
Enter 16 numbers into the array:
7 8 9 6
4 5 6 2
1 8 7 6
5 6 8 4
The sum of all numbers in the array is: 92
//sample output
Enter 15 numbers into a single dimensional array: 5 8 9 6 5 8 7 4 8 9 6 6 8 1 2
Two dimensional array elements are:
5 8 9 6 5
8 7 4 8 9
6 6 8 1 2
//sample output
Enter 12 numbers into the array:
7.5 8.2 9.5 6.0
4.5 -5.1 -2.5 2.7
1.5 8.0 7.3 -6.0
The smallest positive number in the array is: 1.5
P a g e | 7
Exercise 4: Write a function check that takes two arguments; an integer two-dimensional
array of size 3x3, and an integer X. The function should search for X in the array, and returns
whether X is found in the array. Write a main function that prompts the user to enter 9
numbers into a 2D array of size 3x3, and to enter an integer X. Then call the function check
and pass the array and X as arguments. In the main, print whether X is found in the array, or
not found.
//sample output
Enter 9 numbers into the array:
5 2 6
3 4 2
4 1 3
Enter a number to search for in the array: 4
4 is found in the array CS159: Programming Application for Engineering
2
Objectives
To understand the basic concepts and uses of arrays
To be able to define C arrays
To be able to pass arrays and array elements to functions
To understand the sequential search algorithm
To write programs that search arrays
Chapter 8
Arrays
3
8-1 Concepts
Imagine we have a problem that requires us to read, process, and print a
large number of integers. We must also keep the integers in memory for
the duration of the program.
To process large amounts of data we need a powerful data structure, the
array.
An array is a collection of elements of the same data type.
Since an array is a sequenced collection, we can refer to the elements in
the array as the first element, the second element, and so forth until we
get to the last element.
4
FIGURE 8-2 Ten Variables
5
FIGURE 8-3 Process 10 variables
6
FIGURE 8-4 An Array of Scores
7
FIGURE 8-5 Loop for 10 Scores
8
8-2 Using Arrays in C
In this section, we first show how to declare and define arrays. Then we
present several typical applications using arrays including reading values
into arrays, accessing and exchanging elements in arrays, and printing
arrays.
Declaration and Definition
Accessing Elements in Arrays
Storing Values in Arrays
Index Range Checking
Topics discussed in this section:
9
FIGURE 8-6 The Scores Array
10
FIGURE 8-7 Declaring and Defining Arrays
11
Only fixed-length arrays can be initialized when they are defined. Variable length arrays must be initialized by
inputting or assigning the values.
Note
12
FIGURE 8-8 Initializing Arrays
13
One array cannot be copied to another using assignment.
Note
Exercise 1
Write a C program that:
Declare a constant value SIZE= 10
Declare two arrays A and B with 10 elements
Read 10 elements in an array A of integer.
Copy the elements of array A into another array B.
Change the values in the array B to 100 if they are greater than 100.
Change the values in the array B to 10 if they are less or equal to 0.
Print the values of the original array A and the new array B as below:
14
Exercise 1 - Solution
Write a C program that:
Declare a constant value SIZE= 10
#define SIZE 10
Declare two arrays A and B with 10 elements
int A[SIZE], B[SIZE];
Read 10 elements in an array A of integer.
printf("Input %d elements in the array :n", SIZE);
for (i = 0;i
B[i] = 100;
else if (B[i] <= 0)
B[i] = 10;
}
16
Exercise 1 - - Solution
Print the values of the original array A and the new array B as below:
printf("nOrigin array A :n");
for (i = 0; i
#include
#define SIZE 12
int main()
{
int A[SIZE], i, linecount=0;
printf(“Input %d elements in the array :n”, SIZE);
for (i = 0;i
#include
#define SIZE 10
int f1(int a[]);
int main()
{
int A[SIZE] = {5,2,1,2,3,6,1,8,9,6,}, i, r;
for (i = 0; i < SIZE; i++)
printf("%4d", A[i]);
printf("n");
r = f1(A);
for (i = 0; i < SIZE; i++)
printf("%4d", A[i]);
printf(nSum = %d n",r);
return 0;
}
int f1(int a[])
{
int i,s = 2;
for (i = 0;i < SIZE; i = i + 2)
{
s = s + a[i];
a[i] = 0;
}
return(s + 10);
}
Exercise 3: Find the output of the following code
33
#include
#include
#define SIZE 10
int f1(int a[]);
int main()
{
int A[SIZE] = {5,2,1,2,3,6,1,8,9,6,}, i, r;
for (i = 0; i < SIZE; i++)
printf("%4d", A[i]);
printf("n");
r = f1(A);
for (i = 0; i < SIZE; i++)
printf("%4d", A[i]);
printf(nSum = %d n",r);
return 0;
}
int f1(int a[])
{
int i,s = 2;
for (i = 0;i < SIZE; i = i + 2)
{
s = s + a[i];
a[i] = 0;
}
return(s + 10);
}
34
Chapter 8 Problems
16, 28, 29
Computer Science: A Structured Programming Approach Using
C
35
8-6 Searching
Another common operation in computer science is searching, which is the
process used to find the location of a target among a list of objects. In the
case of an array, searching means that given a value, we want to find the
location (index) of the first element in the array that contains that value.
Sequential Search
Topics discussed in this section:
Computer Science: A Structured Programming Approach Using
C
36
FIGURE 8-27 Search Concept
Computer Science: A Structured Programming Approach Using
C
37
FIGURE 8-28 Locating Data in Unordered List
Computer Science: A Structured Programming Approach Using
C
38
FIGURE 8-29 Unsuccessful Search in Unordered List
Sequential Search
Compares each element of an array with a search key
Just as likely that the value will be found in the first element as the last
On average, program must compare the search key with half the elements of the array
To determine that value is not in array, program must compare the search key to every
element in the array
Works well for small or unsorted arrays
39
EXERCICE 4
40
Define a function linearSearch for searching a target within an array. The function
should return the position of the target if it is found in the array, otherwise the
function should return -1.
Function prototype: int linearSearch(int a[], int size, int target);
Write a main function to ask user to enter 10 values and find the position of the
target 15 in the array.
Expected output:
Exercise 4 Solution (Sequential Search)
int linearSearch(int a[], int size, int
target)
{
// Return the position of target if it is
found in the array
// Return -1 if the target is not found in the
array
int i;
for (i = 0;i
#include
#define SIZE 10
int linearSearch(int a[], int size, int target);
int main()
{
int t[SIZE], search, i, a;
int target = 15;
printf(“Enter %d integer(s)n”, SIZE);
for (i = 0; i < SIZE; i++)
scanf("%d", &t[i]);
a = linearSearch(t, SIZE, target);
printf("position = %d n", a);
if (a != -1)
printf("%d is found in the array at position %dn", target,a);
else
printf("%d is not found in the array.nn", target);
system("pause");
return 0;
}
42
Chapter 8 Problem
36
Computer Science: A Structured Programming Approach Using
C
43
8-7 Two-Dimensional Arrays
The arrays we have discussed so far are known as one-dimensional arrays
because the data are organized linearly in only one direction. Many
applications require that data be stored in more than one dimension. One
common example is a table, which is an array that consists of rows and
columns.
Declaration
Passing A Two-Dimensional Array
Topics discussed in this section:
Computer Science: A Structured Programming Approach Using
C
44
FIGURE 8-34 Two-dimensional Array
Computer Science: A Structured Programming Approach Using
C
45
FIGURE 8-35 Array Of Arrays
Computer Science: A Structured Programming Approach Using
C
46
FIGURE 8-36 Memory Layout
Computer Science: A Structured Programming Approach Using
C
47
FIGURE 8-37 Passing a Row
Exercise 5
Write a program that sums the elements of a given 2D array.
Computer Science: A Structured
Programming Approach Using C
48
Exercise 6
How will you calculate the average of integers in this 2-D array?
Computer Science: A Structured
Programming Approach Using C
49
Computer Science: A Structured Programming Approach Using
C
50
PROGRAM 8-16 Convert Table to One-dimensional Array
Computer Science: A Structured Programming Approach Using
C
51
PROGRAM 8-16 Convert Table to One-dimensional Array
Square Matrix
no. of rows = no. of columns
(e.g. 6 x 6 Matrix)
Computer Science: A Structured Programming Approach Using
C
52
FIGURE 8-39 Example of Filled Matrix
Computer Science: A Structured Programming Approach Using
C
53
PROGRAM 8-17 Fill Matrix
Computer Science: A Structured Programming Approach Using
C
54
PROGRAM 8-17 Fill Matrix
Exercise
Write a function sum2D that takes a 2D array of integers and its
number of rows as arguments, and returns their sum.
Write a function sumRows that takes a 2D array of integers, an empty
array, and its size, as arguments, and returns the sum of each row in
the empty array.
Computer Science: A Structured
Programming Approach Using C
55