Python by DR Sab
Python by DR Sab
Applied Sciences
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
7. Within Lab task must be submitted on below mentioned path till Tuesday, 08-Dec-2020
(13:30PM)
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.
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.
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
// 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];
Output
Enter 5 integers: 1
-3
34
0
3
Displaying integers: 1
-3
34
0
3
#include <stdio.h>
int main()
{
int marks[10], i, n, sum = 0, average;
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
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];
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.
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;
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] = {
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}
};
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
Home Task 1
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?
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
123
456
789
68
10 12