Skip to content

Commit

Permalink
12 | 5 | sharafat
Browse files Browse the repository at this point in the history
  • Loading branch information
SharafatKarim committed Aug 18, 2023
1 parent 21e50d8 commit 1128035
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 1 deletion.
13 changes: 12 additions & 1 deletion solutions/sharafat/12/1.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
#include <stdio.h>

void print_array(int *array, int size)
{
for (int i = size - 1; i >= 0; i--)
{
printf("%d ", *(array + i));
}
printf("\n");
}

int main()
{

int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
print_array(array, 10);
return 0;
}
29 changes: 29 additions & 0 deletions solutions/sharafat/12/2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <stdio.h>
#include <math.h>

void calculate_roots(float a, float b, float c, float *x1, float *x2)
{
float discriminant = b * b - 4 * a * c;
if (discriminant < 0)
{
*x1 = *x2 = 0;
}
else if (discriminant == 0)
{
*x1 = *x2 = -b / (2 * a);
}
else
{
*x1 = (-b + sqrt(discriminant)) / (2 * a);
*x2 = (-b - sqrt(discriminant)) / (2 * a);
}
}

int main()
{
float a, b, c, x1, x2;
printf("Enter a, b, c: ");
scanf("%f %f %f", &a, &b, &c);
calculate_roots(a, b, c, &x1, &x2);
printf("x1 = %f, x2 = %f\n", x1, x2);
}
35 changes: 35 additions & 0 deletions solutions/sharafat/12/3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <stdio.h>

void fill_array(int *array, int size, int value)
{
int temp;
int i;
for (i = 0; i < size; i++)
{
if ( array[i] > value )
{
temp = array[i];
array[i] = value;
break;
}
}
for (i = i + 1; i < size; i++)
{
int temp2 = array[i];
array[i] = temp;
temp = temp2;
}

}

int main()
{
int array[10] = {1, 2, 3, 5, 6, 7, 8};
int remaining = 4;

fill_array(array, 10, remaining);
for (int i = 0; i < 10; i++)
{
printf("%d ", array[i]);
}
}
56 changes: 56 additions & 0 deletions solutions/sharafat/12/4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <stdio.h>

void matrix_addition(int *matrix_one, int *matrix_two, int row, int col, int *matrix_sum)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
*(matrix_sum + i * col + j ) = *(matrix_one + i * col + j) + *(matrix_two + i * col + j);
}
}
return;
}

int main()
{
int row, col;
printf("Enter row and column: ");
scanf("%d %d", &row, &col);

int matrix_one[row][col];
int matrix_two[row][col];

printf("Row: %d, Col: %d\n", row, col);

printf("Enter elements of the first matrix:\n");
for (int i = 0 ; i < row; i++)
{
for (int j = 0; j < col; j++)
{
scanf("%d", &matrix_one[i][j]);
}
}

printf("Enter elements of the second matrix:\n");
for (int i = 0 ; i < row; i++)
{
for (int j = 0; j < col; j++)
{
scanf("%d", &matrix_two[i][j]);
}
}

int matrix_sum[row][col];
matrix_addition((int *)matrix_one, (int *)matrix_two, row, col, (int *)matrix_sum);

printf("Sum of the two matrices:\n");
for (int i = 0 ; i < row; i++)
{
for (int j = 0; j < col; j++)
{
printf("%d ", matrix_sum[i][j]);
}
printf("\n");
}
}
38 changes: 38 additions & 0 deletions solutions/sharafat/12/5.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <stdio.h>

void remove_character(char *string, char what_to_remove)
{
int i = 0;
while (string[i] != '\0')
{
if (string[i] == what_to_remove)
{
int j = i;
while (string[j] != '\0')
{
string[j] = string[j + 1];
j++;
}
}
else
{
i++;
}
}
}

int main()
{
char string[100];
char what_to_remove;

printf("Enter a string: ");
scanf("%s", string);

printf("Enter a character to remove: ");
scanf(" %c", &what_to_remove);

remove_character(string, what_to_remove);

printf("Result: %s\n", string);
}

0 comments on commit 1128035

Please sign in to comment.