Course code: EEE 2402
Course title: Structured Programming Language Laboratory
Submitted By :
Group: 2
1. Junayet Antor ( 0212320018 )
2.Nafisul Islam Shifa (0212230061 )
3.Abu Jubair ( 0212320028 )
Submitted To :
Md. Nure Alam Dipu
Lecturer, Department of Electrical and Electronic Engineering
Lab Assignment
Question :1
Explanation: Here we want to identify the determinant of a 2x2 matrix. We urge to get the
inputs from the user, for that we use a nested loop to get the input from the user. By using the
scanf command under the nested loop, we save the values in separate elements like
matrix[0][0], matrix[0][1]. For the calculation of the determinant, we simply take the help of
simple multiplication and subtraction. At last, we show the result by printf command.
Code:
#include<stdio.h>
int main()
{
int i,j,d,matrix[2][2];
printf("enter values:");
for (i=0;i<2;i++)
{
for (j=0;j<2;j++)
{
scanf("%d",&matrix[i][j]);
}
}
d=(matrix[0][0]*matrix[1][1])-(matrix[1][0]*matrix[0][1]);
printf("DITERMINANT OF THE MATRIX:%d",d);
getch();}
Result:
Lab Assignment
Question :2
Explanation: In this code we have tried to create the Pascal’s triangle.
In order to do so we have used the condition given by the question. Firstly we have declared a
void type function called ”pascal” and we will give an integer as input.
In the main function: Firstly, we have declared an integer type variable “n” which will take the
nth number of matrix. Then we have called the function “pascal”.
In the pascal function: In the pascal function we have two integers to control loop. Here “i” will
control the row and the “j” will control the column. Assume we have put the value of n= 4. So
now there are 4 row and column. Here we have an outer loop which will be controlled by “i”
and an internal loop which will be controlled by “j”. now inside the internal loop we have “if”
statement which was given in the question which is j == 0 || i == j which means, the first
column will have a value 1 and every i=j will have value 1. Now for any other value of “i and j”
we will use the else condition.
One of the major problem in this code was the \n. we couldn’t understand where to put the
new line statement. Then we saw some examples in the web where we found the photo below
which helped us understand that the \n will come inside the internal loop.
Code:
#include <stdio.h>
void pascal(int n);
int main() {
int n;
printf("Enter the nth matrix:");
scanf("%d", &n);
pascal(n);
return 0;
}
void pascal(int n) {
int i,j;
int a[n][n];
for (i = 0; i < n; i++){
for (j = 0; j <= i; j++){
if (j == 0 || i == j) {
a[i][j] = 1;}
else {
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];}
printf("%d ", a[i][j]);}
printf("\n");}}
Result:
Lab Assignment
Question :3
Practice Problems
Question :1
Explanation: In this code we have an integer type array of size 4, a variable “i” whose value
is 4 and another variable “j”. Then we have a while loop where the condition is “i” that means
the loop will run until the value of i is zero.
Now when the loop runs we have j=arr[i]+i and then we decrement statement for “i” and lastly
we have printed the value of j. But in this code j=arr[i]+I is wrong since the size of the array is 4
which means it has arr[0] to arr[3]. Since there is an error the only possible explanation for the
result is that, the loop runs till i=0 but it doesn’t show output for any of the value. But for the
last value of “i” which is 1 we get arr[1] whose value is 4 and +1 will give the result, j=5. And this
value is the only thing that is printing.
Result:
Practice Problems
Question :2
Explanation: In this code we can understand sum the values of an array.
Here we have an integer type array of size 6, and two variables “i” and “sum” both of which has
initial value of zero. Then we have an “while” loop that will run until any element of the array is
zero. And inside the while loop we have sum=sum +arr[i] that will calculate the sum, and lastly
we are incrementing the value of I by 1 each loop.
Now the result of the sum is 12. It is because the value of arr[3] is zero, this will stop the loop.
So the loop will work till arr[2], that is 4+2+6 =12.
Result:
Practice Problems
Question :3
Explanation: In this code we have 1D, 2D and 3D array and we are printing the size of the
array.
Here, all the arrays are integer type. The 1D array size is 10, 2D array size is 3x4=12 and 3D
array size is 2x3x5=30.
Now, we know that each element of integer type is 4 byte, so the size of the array will be 4xsize
of array. This will result in, for 1D, 4x10=10. For 2D, 12x4=48 and lastly for 3D, 30x4=120.
Result:
Practice Problems
Question :4
Explanation: In this code, we will learn how to change row to column and column to row of
an 2D array.
Here, we have 2 integer variables , i and j, and a 2D array,arr, where it has 3 row and 4 column.
So the size of the array is 12.
Now, we have a nested “for” loop where the outer loop (i) controls the row and the internal
loop (j) controls the column. And inside the internal loop we have a printf statement that will
print the value of arr[j][i] and after each time the internal loop is complete there will be a new
line.
So whats happening?
Here, for i=0, and j=0 we will print the value of arr[0][0] which is 1 and since the external loop
will only start after the internal loop is done with the loop. So the value of j will increment to 1
and the code will print the value of arr[1][0] , this is the value of array of row 1 column 0 which
is 5. And in the next loop it will print arr[2][0] which is 9. By now the internal loop will be
finished and get out of the loop and create a new line and then the external loop will increment
its value to 1 and the internal loop will start again. And this time the value of the column of the
array will be 1.
This is how row and column will swap.
Result:
Example
Question :1
Explanation: Here, the size of the array is 50. For getting elements' values from the user,
we use a loop. The loop control variable is initialized to 0, and the loop will run until i < 5. For i =
0, the value that the user has given is saved in arr[0] by the scanf command under the loop.
That's how we take input from the user by using a simple loop. For printing the elements, we
use a loop as usual. In the printf command, we print arr[i], and as the printf command is under
the loop, the values of i will change, and the exact values of the array elements will be printed.
Result:
Example
Question :2
Explanation: In this code we will learn how to find the largest and smallest element from an
array.
In this code, we have a two integer type variable “i” and “n” and two float type variable “large”
and ”small” and we also have a float type array of size 50.
Firstly, we take input from the user of how many elements the user is going to input. It should
not be greater than 50. Then, we take the values from the user and assign them using for loop.
In this case there are 5 elements. We have put random values and the values are assigned from
a[0] to a[4]. Now to find the largest and smallest number we assign the 1st value of the array
a[0] to both largest and smallest. Now we will use loop to find the largest and smallest.
The for loop will run from i=1 to i=4. In the loop we have “if” and “else if” condition. In the if
condition, it will check if any other value in the array is larger than a[0]. If there are any values
then the new value of large will be the value that made the condition true. In this case a[0] is 12
and a[1] is 33 which is greater than a[0], so the new value of large is a[1] and then it will check
again if any other value in the array satisfy the condition.
The condition in the else if condition works in a similar principle. Here the condition will check If
any value in the array is less than a[0]. If there are then the value of “small” will change it to
that value.
Then finally we will print the value of the largest and smallest.
Result:
Example
Question :3
Explanation: This is an array of integer data type whose size is 10. We put 10 items as the
array elements. Our objective is to find the location of the item that has been set into this
array.
For that, we use a loop where the loop control variable "i" is initialized to "0" and the loop will
run till "i<SIZE". Then we use a conditional operation under the loop that checks whether the
condition is true or not.
For the value of "i" where the condition is true—in other words, "item==array[i]"—the position
of that item will be "i+1" as arrays start counting from "0". After that, the loop will break down.
However, if the user inputs a number that is not in the array, it will show that the item is not
found.
Result:
Example
Question :5
Explanation: In this code we can learn how we can sort values of an array. Here, we have
two integer variables “i” and” j” and an array of size 10 where the values are not sorted.
Firstly, we have printed the array, with the help of “for” loop, as it is to see the before and after
result. Then we have a for loop that will control how many times the loop will run. Since, there
are 10 elements in the array so the loop will run 10 time starting from 0 to 9. And then we have
an internal loop that will control the arr[0]. Inside the internal for loop we have an” if”
condition and the main mechanism of this is that it will swap the values if arr[0] is greater than
its any other values in that array.
When i=0 and j=0, we have if arr[0] is greater than arr[1], which is true because, arr[0]=23 and
arr[1]=12 so the statements within the if condition will run and the values will swap, now the
value of arr[1] is 23 and arr[0] is 12. Now, the value of j will increase to 1, now arr[1]=23 will be
compared with arr[2]=56. Since, arr[2] is 56 the condition will be false and now swap will occur
and then the value of j will increase again to 2 and do the same thing. So mainly each loop will
check for greater values and if it finds one it will just swap the values. This loop will repeat 10
times. Here the largest value in the array is 98. So, in the first loop the value 98 will go furthest
right.
When the internal loop is done, the outer loop variable “i” will increase by 1 and then the
internal loop will run again but this time the internal loop will run for only 8 times because we
already have the highest value in the array in the furthest right so we don’t need to check. So
each loop of “i” we will have a sorted value from right to left and the amount of times the
internal loop will run will decrease too.
This is how we get the sorted value of array.
Result: