0% found this document useful (0 votes)
43 views

Python by DR Sab

Here are the programs to solve the within lab tasks: Task 1: #include <stdio.h> int main() { int arr[10], i; printf("Input 10 elements in the Array:\n"); for(i=0; i<10; i++) { printf("element - %d : ", i); scanf("%d", &arr[i]); } printf("Elements in array are: "); for(i=0; i<10; i++) { printf("%d ", arr[i]); } return 0; } Task 2: #include <stdio.h> int main()

Uploaded by

Dukky
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Python by DR Sab

Here are the programs to solve the within lab tasks: Task 1: #include <stdio.h> int main() { int arr[10], i; printf("Input 10 elements in the Array:\n"); for(i=0; i<10; i++) { printf("element - %d : ", i); scanf("%d", &arr[i]); } printf("Elements in array are: "); for(i=0; i<10; i++) { printf("%d ", arr[i]); } return 0; } Task 2: #include <stdio.h> int main()

Uploaded by

Dukky
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Pakistan Institute of Engineering and

Applied Sciences

Computing Fundamentals & Programming


Fall 2020
Laboratory Exercise-04

Date: DEC 08, 2020

Computing Fundamentals & Programming Lab 4


Instructions:

Any One Found


1. Using Mobile Without Permission Will Be Marked As Absent And 50% Marks Of This
Lab Will Be Deducted.

2. Copying Assignment / Using internet During Lab tasks and Marking Proxy will Lead you
to “F” Grade in the Lab. Be very Careful.

3. Use appropriate naming convention for variable name. e.g to calculate the sum of number
variable name can be sum, sum_of_number. Random variable names are not allowed.

4. Lab Tasks must also be submitted in report format too. Sample format is available on
below path. \\172.30.10.24\FacultyShare\Muhammad Yousaf Hamza
Dr\Public\1_CFP_Zero_Sem_2020_22\2_Lab_Assignments

5. Submission Deadline of Home Tasks of Lab 04 is Tuesday , 15-Dec-2020 (11:00 AM)

6. Assignment Submission Path: \\172.30.10.2\Assignments\Applied Sciences


Dept\DPAM\Dr. Muhammad Yousaf Hamza\0_Zero_Lab4

7. Within Lab task must be submitted on below mentioned path till Tuesday, 08-Dec-2020
(13:30PM)

8. Assignment Submission Path: \\172.30.10.2\Assignments\Applied Sciences


Dept\DPAM\Dr. Muhammad Yousaf Hamza\0_Zero_Lab4

9. Please follow following file names format for file submission:


DegreeName_Full_Name_TwoDigitSerialNumber_LabNo_WithinLab_DateOfSubmission
DegreeName_Full_Name_TwoDigitSerialNumber_LabNo_HomeTasks_DateOfSubmissio
n
Example: Process_01_Abdul Aziz_LabNo1_WithinLab_Nov24_2020

Computing Fundamentals & Programming Lab 4


Topics Covered
1. Arrays
2. Single Dimensional Array
3. declaration of arrays
4. initialization of arrays
5. printing of arrays
6. how to read arrays using scanf
7. Two Dimensional (2D) Array

Arrays

An array is a variable that can store multiple values. For example, if you want to store 100
integers, you can create an array for it.

How to declare an array?

dataType arrayName[arraySize];

For example,

float mark[5];

Here, we declared an array, mark, of floating-point type. And its size is 5. Meaning, it can hold
5 floating-point values.
It's important to note that the size and type of an array cannot be changed once it is declared.

Access Array Elements


You can access elements of an array by indices.
Suppose you declared an array mark as above. The first element is mark[0], the second
element is mark[1] and so on.

Computing Fundamentals & Programming Lab 4


Few keynotes:
 Arrays have 0 as the first index, not 1. In this example, mark[0] is the first element.
 If the size of an array is n, to access the last element, the n-1 index is used. In this
example, mark[4]
 Suppose the starting address of mark[0] is 2120d. Then, the address of the mark[1] will
be 2124d. Similarly, the address of mark[2] will be 2128d and so on.
This is because the size of a float is 4 bytes.

How to initialize an array?


It is possible to initialize an array during declaration. For example,

int mark[5] = {19, 10, 8, 17, 9};

You can also initialize an array like this.

int mark[] = {19, 10, 8, 17, 9};

Here, we haven't specified the size. However, the compiler knows its size is 5 as we are
initializing it with 5 elements.

Here,

mark[0] is equal to 19

mark[1] is equal to 10

mark[2] is equal to 8

mark[3] is equal to 17

mark[4] is equal to 9

Change Value of Array elements

int mark[5] = {19, 10, 8, 17, 9}

// make the value of the third element to -1


mark[2] = -1;

// make the value of the fifth element to 0


mark[4] = 0;

Input and Output Array Elements


Here's how you can take input from the user and store it in an array element.

// take input and store it in the 3rd element


scanf("%d", &mark[2]);

Computing Fundamentals & Programming Lab 4


// take input and store it in the ith element
scanf("%d", &mark[i-1]);

Here's how you can print an individual element of an array.

// print the first element of the array


printf("%d", mark[0]);

// print the third element of the array


printf("%d", mark[2]);

// print ith element of the array


printf("%d", mark[i-1]);

Self-Acivity-Task-01: Array Input/Output

// Program to take 5 values from the user and store them in an array
// Print the elements stored in the array
#include <stdio.h>

int main() {
int values[5];

printf("Enter 5 integers: ");

// taking input and storing it in an array


for(int i = 0; i < 5; ++i) {
scanf("%d", &values[i]);
}

printf("Displaying integers: ");

// printing elements of an array


for(int i = 0; i < 5; ++i) {
printf("%d\n", values[i]);
}
getchar();
return 0;
}

Output

Enter 5 integers: 1
-3
34
0
3
Displaying integers: 1
-3
34
0
3

Computing Fundamentals & Programming Lab 4


Here, we have used a for loop to take 5 inputs from the user and store them in an array. Then,
using another for loop, these elements are displayed on the screen.

Self-Acivity-Task-02: Calculate Average

// Program to find the average of n numbers using arrays

#include <stdio.h>
int main()
{
int marks[10], i, n, sum = 0, average;

printf("Enter number of elements: ");


scanf("%d", &n);

for(i=0; i<n; ++i)


{
printf("Enter number%d: ",i+1);
scanf("%d", &marks[i]);

// adding integers entered by the user to the sum variable


sum += marks[i];
}

average = sum/n;
printf("Average = %d", average);

return 0;
}

Output

Enter n: 5
Enter number1: 45
Enter number2: 35
Enter number3: 38
Enter number4: 31
Enter number5: 49
Average = 39

Here, we have computed the average of n numbers entered by the user.


Two Dimensional Array (Matrix)

An array of arrays is known as 2D array. The two dimensional (2D) array in C programming is
also known as matrix. A matrix can be represented as a table of rows and columns. Before we
discuss more about two Dimensional array let’s have a look at the following C program.

Syntax:
type arrName[rows][cols];

Example
int score[4][5];

Computing Fundamentals & Programming Lab 4


In the above example an array named score of type int is created. It has 4 rows and 5 columns
i.e., total 4x5 = 20 elements. So, the above code will instruct the computer to allocate memory
to store 20 elements of type int and the score array is represented as shown below.

Array indexing starts from 0 so, in the above image there are 4 rows having index 0, 1, 2 and 3.
And there are 5 columns having index 0, 1, 2, 3 and 4.

Assigning value to a 2D Array


We can assign value to a 2D array by targeting the cell using the row-index and col-index. For
example, if we want to assign 10 to element at row-index 0 and column-index 0 then we will
write the following code.
score[0][0] = 10;

Similarly, if we want to assign let’s say 50 to an element of an array at row-index 1 and column-
index 3 then we will write the following code.
score[1][3] = 50;

Creating and initializing 2D array


There are a couple of ways we can create and initialize a 2D array in C.

In the following code we are creating a 2D array bonus of type int having 2 rows and 3 columns.

int bonus[2][3] = {
1, 2, 3,
4, 5, 6
};

In the above code 1, 2, 3 is for the first row and 4, 5, 6 is for the second row of the array bonus.
We can achieve the same result by separating the rows with curly brackets { } as show below.
int bonus[2][3] = {
{1, 2, 3},
{4, 5, 6}
};

We can also skip elements and they will be auto filled with 0s.
In the following example we are creating an array bonus of type int having 2 rows and 3
columns but this time we are initializing only few elements.
int bonus[2][3] = {

Computing Fundamentals & Programming Lab 4


{1, 2},
{3}
};

The above code will create the bonus array having 2 rows and 3 columns and we will get the
following initialization.
int bonus[2][3] = {
{1, 2, 0},
{3, 0, 0}
};

If we want to initialize all elements of a row to let’s say 0 then we can write the following.
int bonus[2][3] = {
{0},
{0}
};

So, the above code will give us the following result.


int bonus[2][3] = {
{0, 0, 0},
{0, 0, 0}
};

Self-Acivity-Task-03

#include<stdio.h>
int main(){
/* 2D array declaration*/
int disp[2][3];
/*Counter variables for the loop*/
int i, j;
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("Enter value for disp[%d][%d]:", i, j);
scanf("%d", &disp[i][j]);
}
}
//Displaying array elements
printf("Two Dimensional array elements:\n");
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("%d ", disp[i][j]);
if(j==2){
printf("\n");
}
}
}
getchar();
return 0;
}

Output:
Enter value for disp[0][0]:1
Enter value for disp[0][1]:2
Enter value for disp[0][2]:3
Enter value for disp[1][0]:4
Enter value for disp[1][1]:5
Enter value for disp[1][2]:6
Two Dimensional array elements:
123
456

Computing Fundamentals & Programming Lab 4


Within Lab Task 1
Write a program in C to store elements in an array and print it.
Sample Output:
Input 10 elements in the Array:
element - 0 : 1
element - 1 : 1
element - 2 : 2
.......
Expected Output :
Elements in array are: 1 1 2 3 4 5 6 7 8 9

Within Lab Task 2


Write a program in C to read n number of values in an array and display it in reverse
order.
Sample Output :
Input the number of elements to store in the array :3
Input 3 number of elements in the array :
element - 0 : 2
element - 1 : 5
element - 2 : 7
Expected Output :
The values store into the array are :
257
The values store into the array in reverse are :
752

Home Tasks Section

Home Task 1

Write a program in C to print all unique elements in an array.


Sample Output :
Print all unique elements of an array:
------------------------------------------
Input the number of elements to be stored in the array: 4
Input 4 elements in the array :
element - 0 : 3
element - 1 : 2
element - 2 : 2
element - 3 : 5
Expected Output :
The unique elements found in the array are:
35

Computing Fundamentals & Programming Lab 4


Home Task 2

Write a program in C to merge two arrays of same size sorted in descending order.
Test Data :
Input the number of elements to be stored in the first array :3
Input 3 elements in the array :
element - 0 : 1
element - 1 : 2
element - 2 : 3
Input the number of elements to be stored in the second array :3
Input 3 elements in the array :
element - 0 : 1
element - 1 : 2
element - 2 : 3
Expected Output :
The merged array in descending order is :
332211

Home Task 3
Write a program in C to find the missing number from a given array. There are no
duplicates in list.
Expected Output :
The given array is : 1 3 4 2 5 6 9 8
The missing number is : 7

Home Task 4

Write a program in C to check whether a matrix provided by user is identity matrix or not?

Home Task 5

Write a C program to check whether a matrix provided by user is scalar matrix or not?

Home Task 6

Write a C program to check whether a matrix provided by user is diagonal matrix or not?

Computing Fundamentals & Programming Lab 4


Non-Submission Important Practice Tasks

Important Practice Task 1

Write a program in C to find the smallest missing element from a sorted array.
Expected Output :
The given array is : 0 1 3 4 5 6 7 9
The missing smallest element is: 2

Important Practice Task 2


Write a program in C to find two elements whose sum is closest to zero.
Expected Output :
The given array is : 38 44 63 -51 -35 19 84 -69 4 -46
The Pair of elements whose sum is minimum are:
[44, -46]

Important Practice Task 3


Write a program in C to count the frequency of each element of an array.
Test Data :
Input the number of elements to be stored in the array :3
Input 3 elements in the array :
element - 0 : 25
element - 1 : 12
element - 2 : 43
Expected Output :
The frequency of all elements of an array :
25 occurs 1 times
12 occurs 1 times
43 occurs 1 times

Important Practice Task 4

Write a program in C to find the maximum and minimum element in an array.


Test Data :
Input the number of elements to be stored in the array :3
Input 3 elements in the array :
element - 0 : 45
element - 1 : 25
element - 2 : 21
Expected Output :
Maximum element is : 45
Minimum element is : 21

Computing Fundamentals & Programming Lab 4


Important Practice Task 5

Write a program in C to insert New value in the array (sorted list).


Test Data :
Input the size of array : 3
Input 3 elements in the array in ascending order:
element - 0 : 5
element - 1 : 7
element - 2 : 9
Input the value to be inserted : 8
Expected Output :
The exist array list is :
579
After Insert the list is :
5789

Important Practice Task 6

Write a program in C to delete an element at desired position from an array..


Test Data :
Input the size of array : 5
Input 5 elements in the array in ascending order:
element - 0 : 1
element - 1 : 2
element - 2 : 3
element - 3 : 4
element - 4 : 5
Input the position where to delete: 3
Expected Output :
The new list is : 1 2 4 5

Important Practice Task 7


Write a program in C to find the second smallest element in an array.
Test Data :
Input the size of array : 5
Input 5 elements in the array (value must be <9999) :
element - 0 : 0
element - 1 : 9
element - 2 : 4
element - 3 : 6
element - 4 : 5
Expected Output :
The Second smallest element in the array is : 4

Computing Fundamentals & Programming Lab 4


Important Practice Task 8
Write a program in C to find the second largest element in an array.
Test Data :
Input the size of array : 5
Input 5 elements in the array (value must be <9999) :
element - 0 : 0
element - 1 : 9
element - 2 : 4
element - 3 : 6
element - 4 : 5
Expected Output :
The Second largest element in the array is : 6

Important Practice Task 9


.
Write a program in C for a 2D array of size 3x3 and print the matrix.
Test Data :
Input elements in the matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [0],[2] : 3
element - [1],[0] : 4
element - [1],[1] : 5
element - [1],[2] : 6
element - [2],[0] : 7
element - [2],[1] : 8
element - [2],[2] : 9
Expected Output :
The matrix is :

123
456
789

Important Practice Task 10


Write a program in C for addition of two Matrices of same size.
Test Data :
Input the size of the square matrix (less than 5): 2
Input elements in the first matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [1],[0] : 3
element - [1],[1] : 4
Input elements in the second matrix :
element - [0],[0] : 5
element - [0],[1] : 6
element - [1],[0] : 7
element - [1],[1] : 8
Expected Output :
The First matrix is :
12
34
The Second matrix is :
56
78 .
The Addition of two matrix is :

68
10 12

Computing Fundamentals & Programming Lab 4


Important Practice Task 11
Write a program in C for subtraction of two Matrices.
Test Data :
Input the size of the square matrix (less than 5): 2
Input elements in the first matrix :
element - [0],[0] : 5
element - [0],[1] : 6
element - [1],[0] : 7
element - [1],[1] : 8
Input elements in the second matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [1],[0] : 3
element - [1],[1] : 4
Expected Output :
The First matrix is :
56
78
The Second matrix is :
12
34
The Subtraction of two matrix is :
44
44

Important Practice Task 12


Write a program in C to find a pair with given sum in the array.
Expected Output :
The given array : 6 8 4 -5 7 9
The given sum : 15
Pair of elements can make the given sum by the value of index 0 and 5

Important Practice Task 13

Write a C program to check whether a matrix provided by user is upper-diagonal matrix


or not?

Important Practice Task 14


Write a C program to check whether a matrix provided by user is lower diagonal matrix or
not?

Important Practice Task 15


Write a program in C to find determinant of a matrix provided by user.

Computing Fundamentals & Programming Lab 4

You might also like