MCT-242L CP1 2022 LM16 33
MCT-242L CP1 2022 LM16 33
Now, consider the following function, which takes an array as an argument along with another
argument and based on the passed arguments, it returns the average of the numbers passed
through the array as follows:
Example 16.1: Passing Array to a Function
/* Example_16_1.c: Passing Array to a Function
----------------------------------------------------------------
This program demonstrates the idea of passing array to a function
in C language. User-defined function getAverage takes and array
as argument and returns the average of its elements.
----------------------------------------------------------------
Written by Shujat Ali (engrshujatali@gmail.com) on 11-Nov-2021.
IDE: Visual Studio Code 1.60.0
C Compiler: GCC (Rev. 5, Built by MSYS2 Project) 10.3.0 */
#include <stdio.h>
// function declaration
double getAverage(int arr[], int arrsize);
int main()
{
int balance[5] = {10, 2, 3, 17, 50};
double avgValue;
return 0;
}
// function definition
double getAverage(int arr[], int arrSize)
{
int i;
double avg, sum;
// End of program
Program Output Average value is 16.40
As you can see, the length of the array doesn't matter as far as the function is concerned because
C performs no bounds checking for formal parameters.
Create a Visual Studio Code Workspace, Lab_16 and c files (Task_16_1.c to Task_16_5.c) for
individual tasks and add them to Lab_16 workspace.
Determinant of A
det(A) = 24.00
Answer:
#include <stdio.h>
int determinant1(int matrix[2][2])
{
int determinant = 0;
determinant = (matrix[0][0] * matrix[1][1]) - (matrix[0][1] * matrix[1][0]);
printf("The determinant of this matrix is %d\n", determinant);
return determinant;
}
int main()
{
int matrix[2][2];
printf("Enter a 2x2 matrix:\n");
// taking inputs for the matrix
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
printf("Enter element at position [%d] [%d]", i + 1, j + 1);
scanf("%d", &matrix[i][j]);
}
}
// displaying the matrix
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
determinant1(matrix);
return 0;
}
Determinant of A
det(A) = 49.00
Answer:
#include <stdio.h>
int determinantof3x3(int matrix[3][3])
{
int determinant;
determinant = (matrix[0][0] * ((matrix[1][1] * matrix[2][2]) - (matrix[1][2] * matrix[2][1]))) -
(matrix[0][1] * ((matrix[1][0] * matrix[2][2]) - (matrix[1][2] * matrix[2][0]))) + (matrix[0][2] *
((matrix[1][0] * matrix[2][1]) - (matrix[1][1] * matrix[2][0])));
printf("The determinant of this matrix is %d", determinant);
return determinant;
}
int main()
{
int matrix[3][3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("Enter element at position [%d] [%d]", i + 1, j + 1);
scanf("%d", &matrix[i][j]);
}
}
// displaying the matrix
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
determinantof3x3(matrix);
}
Transpose of A
| 2.00 2.00 1.00|
At = |-3.00 0.00 4.00|
| 1.00 -1.00 5.00|
Answer:
#include <stdio.h>
int transposeof3x3(int matrix[3][3])
{
printf("The transpose of this matrix is:");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d\t", matrix[j][i]);
}
printf("\n");
}
}
int main()
{
int matrix[3][3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("Enter element at position [%d] [%d]", i + 1, j + 1);
scanf("%d", &matrix[i][j]);
}
}
// displaying the matrix
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
transposeof3x3(matrix);
}
Answer:
#include <stdio.h>
void displayboard(char grid[3][3])
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%c ", grid[i][j]);
}
printf("\n");
}
}
int checkwin(char grid[3][3], char player)
{
for (int i = 0; i < 3; i++)
{
if ((grid[i][0] == player && grid[i][1] == player && grid[i][2] == player) || (grid[0][i] == player
&& grid[1][i] == player && grid[2][i] == player))
{
return 1;
}
if ((grid[0][0] == player && grid[1][1] == player && grid[2][2] == player) || (grid[0][2] ==
player && grid[1][1] == player && grid[2][0] == player))
{
return 1;
}
}
}
int main()
{
char grid[3][3] = {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}};
int slot;
char currentPlayer = 'X';
int turns = 0;
while (turns < 9)
{
displayboard(grid);
printf("Player %c, enter your move (0-8): ", currentPlayer);
scanf("%d", &slot);
int row = slot / 3;
int column = slot % 3;
if (grid[row][column] == ' ')
{
grid[row][column] = currentPlayer;
if (checkwin(grid, currentPlayer) == 1)
{
displayboard(grid);
printf("player %c has won", currentPlayer);
return 0;
}
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
turns++;
}
else
{
printf("Slot already occupied. Try again.\n");
}
}
displayboard(grid);
printf("It's a draw!\n");
return 0;
}
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
---------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | |X| | | |
---------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | |X| |O| |
---------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | |X|X|O| |
---------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | |O|X|X|O| |
---------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | |X| | | |
| | |O|X|X|O| |
---------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | |O|X| | | |
| | |O|X|X|O| |
---------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | |X| | | | |
| | |O|X| | | |
| | |O|X|X|O| |
---------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | |X| | | | |
| | |O|X|O| | |
| | |O|X|X|O| |
---------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | |X|X| | | |
| | |O|X|O| | |
| | |O|X|X|O| |
---------------
| | | | | | | |
| | | | | | | |
| | | |O| | | |
| | |X|X| | | |
| | |O|X|O| | |
| | |O|X|X|O| |
---------------
| | | | | | | |
| | | | | | | |
| | |X|O| | | |
| | |X|X| | | |
| | |O|X|O| | |
| | |O|X|X|O| |
---------------
| | | | | | | |
| | | | | | | |
| | |X|O| | | |
| | |X|X| | | |
| | |O|X|O| | |
| |O|O|X|X|O| |
---------------
| | | | | | | |
| | | | | | | |
| | |X|O| | | |
| | |X|X| | | |
| |X|O|X|O| | |
| |O|O|X|X|O| |
---------------
| | | | | | | |
| | | | | | | |
| | |X|O| | | |
| |O|X|X| | | |
| |X|O|X|O| | |
| |O|O|X|X|O| |
---------------
Player-1 please enter slot number (0-6) >> 1
|0|1|2|3|4|5|6|
| | | | | | | |
| | | | | | | |
| |X|X|O| | | |
| |O|X|X| | | |
| |X|O|X|O| | |
| |O|O|X|X|O| |
---------------
Game Over!
Player-1 won the game!
Answer:
#include <stdio.h>
int main()
{
char grid[6][7] = {
{' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' '}};
int slot;
char currentPlayer = 'X';
int turns = 0;
while (turns < 42)
{
displayBoard(grid);
if (checkWin(grid, currentPlayer))
{
displayBoard(grid);
printf("Player %c wins!\n", currentPlayer);
return 0;
}
turns++;
}
return 0;
}
Students are advised to fill the manual and submit it before the upcoming lab. Kindly rename the file as
‘MCT-242L_CP1_2022_LM16_XX’, where XX is your roll number. After completing the manual, turn it in Google
Classroom.